How to constantly listen for data via RF link without blocking Blynk.run()?

I’ve created a project which uses two Arduino Pro Minis. One takes inputs from various sensors and sends the data to the other over RF link. The receiver Arduino is connected to the web using an Adafruit FONA cellular module. It is connected to the Blynk servers and forwards to data to a Blynk mobile app on my phone.

I’ve tried both the NRF24L01 2.4 GHz module and the RFM69 433 MHz module for the RF link. They both work ok but the device will frequently disconnect from the Blynk server. radio.available() or rf69.available() is used to listen out for data being sent from the sensor. I have to place this inside of void loop() within a function I created so that the radio is always listening and I’m wondering if it is blocking Blynk.run() ? If so, how else can I ensure the radio is always listening for data without blocking Blynk.run()?

When data is available via the RF link, it is stores in buf[]. The data contained in the different elements of buf[] is then read and uploaded to the corresponding Blynk widgets. I had this code working perfectly for a while but since coming back to it I keep having this with it disconnecting from the server. I’m using Blynk library 1.0.0 but have tried going back to older versions too. I even tried defining port 8080 and defining the IP address of the European server. None of these solved the issue so I believe the radio is the issue. I’m using Blynk for iOS.

#define TINY_GSM_MODEM_SIM800
#define RFM69_INT     3  
#define RFM69_CS      5  
#define RFM69_RST     2 
#define FONA_RX 8
#define FONA_TX 9
#define FONA_RST 4
#define RF69_FREQ 433.0

#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#include <BlynkSimpleSIM800.h>
#include <TinyGsmClient.h>
#include <SPI.h> //RF
#include <RH_RF69.h> //RF

RH_RF69 rf69(RFM69_CS, RFM69_INT);

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);

SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
TinyGsm modem(fonaSS);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

char auth[] = "********************************";
char apn[]  = "hologram";
char user[] = "";
char pass[] = "";

uint16_t hum;
uint16_t temp;
uint16_t cut = 0b0011111111111111;
float h;
float t;
uint8_t moisture;
uint8_t buf[25];

void setup() {

pinMode(RFM69_RST, OUTPUT);
Serial.begin(115200);

digitalWrite(RFM69_RST, LOW);

digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);

rf69.setTxPower(20, true);

uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

rf69.setEncryptionKey(key);

fonaSerial->begin(4800);

Blynk.begin(auth, modem, apn, user, pass);
}

void humidityFun()
{
hum = buf[0];
hum = (hum << 8) | buf[1];
hum = hum & cut;
h = (hum/(pow(2,14)-2))*100;
Blynk.virtualWrite(V0, h);
}

void temperatureFun()
{
temp = buf[2];
temp = (temp << 6) | buf[3];
t = (temp/(pow(2,14)-2))*165-40;
Blynk.virtualWrite(V1, t);
}

void moistureFun()
{
moisture = buf[4];
Blynk.virtualWrite(V2, moisture);
}

void radioListen()
{
   if (rf69.available()) 
   {
      uint8_t len = sizeof(buf); 

      if (rf69.recv(buf, &len)) 
      {
          if (!len) return;
          buf[len] = 0;
      }

      humidityFun();
      temperatureFun();
      moistureFun();
   }
}

void loop() 
{
Blynk.run();
radioListen();
}

Hello

Did you try to clean the loop with
https://docs.blynk.io/en/blynk.edgent/api/blynk-timer

#include <SimpleTimer.h>

// the timer object
SimpleTimer timer;

void setup() {
    timer.setInterval(10000, repeatMe);  //10sec
}

void repeatMe() {
//////////////////////listen RF link
}

void loop() {
    Blynk.run();
    timer.run();
}

@Alx-I Thanks for the idea. I’ve tried that already though and had the same issue. I played around with how often the function was called too but it either missed the data or kept disconnecting from the server.

And with a more sofisticated code.
Listen RF => store data => if data have changed : disconnect RF / connect Blynk => send data to blynk => disconnect blynk / connect RF => …

@Alx-I The issue is, sometimes it struggles to reconnect to the server once it’s been interrupted by the radio. Or at least it takes a while. I’d like to eventually implement a feature which allows a motor (connected to the hardware) to be controlled from the app as well. This means it needs to be connected to the server as much as possible.

@Alx-I please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.