NRF24 to blynk

The difference is that bkynk_timer is non-blocking, which means that other functions can continue to run between timer cycles.
When you throw a delay()in to your code nothing else can run during that delay period, which will cause the Blynk connection to drop because the device isn’t keeping “heartbeat” communications going with the server.

Yes, one Blynk function per vPin is normal (but there are some cases where multiple vins can be merged - like in the case of Joystick or zeRGBa)… and the state or value of that Widget is transferred via the ‘param’

thank you so much, I got it
I have successfully connected my arduino to the blynk app that can send temp, humidity and soil humidity to blynk and I can control Relay from blynk app. now I have started connect arduino to NRF24 building master and slave which slave will do sending data and controlling relay and master is connected to the bynk to control slaves.
this is my master code:

#define BLYNK_PRINT Serial
#define BLYNK_NO_BUILTIN
#define BLYNK_NO_INFO
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

int Value;
int LED = 5;
int DataMgs[0];
RF24 radio(8, 9);
byte addresses[][6] = {"0"};


char auth[] = "XXXXXXXXXXXXXXXXX";

unsigned long lastRunTime = 0;
unsigned long currentTime = 0;
int h;
int t;
int s;


struct package
{
  int t ;
  int h ;
  int s ;
};
typedef struct package Package;
Package data;

void setup()
{
  Blynk.begin(auth);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  radio.begin();
 radio.setChannel(115); 
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.openReadingPipe(1, addresses[0]);
  radio.startListening();

}

void loop()
{
   Blynk.run();
   currentTime = millis();
     if ((currentTime - lastRunTime >= 5000) ){     //timer low-cost  
    lastRunTime = currentTime;
   if (!Blynk.connected()) {
    digitalWrite(LED, LOW);
   } else {
    digitalWrite(LED, HIGH);
   }
 if ( radio.available()) 
  {
    while (radio.available())
    {
      radio.read( &data, sizeof(data) );
     
    Blynk.virtualWrite(V5, data.h);  
    Blynk.virtualWrite(V6, data.t);
    Blynk.virtualWrite(V0, data.s);
    }
  }
}
}

  BLYNK_WRITE(V1) {
             radio.stopListening();

      radio.openWritingPipe(addresses[0]); 
      if (param.asInt() == 1) {
   DataMgs[0] = 1;
   radio.write(DataMgs, 1);
  } else { 
     DataMgs[0] = 1;
   radio.write(DataMgs, 0);
  }
}

and the slave:

#include <DHT.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"

int relay = 4;
int DataMgs[0];

RF24 radio(8, 9);
byte addresses[][6] = {"0"};

unsigned long lastRunTime = 0;
unsigned long currentTime = 0;

struct package
{
int h;
int t;
int s;
};

typedef struct package Package;
Package data;


#define DHTPIN 7
#define DHTTYPE DHT11  
DHT dht(DHTPIN, DHTTYPE);


void readSensor()
{
  data.h = dht.readHumidity();
  data.t = dht.readTemperature();
  data.s = analogRead(A0);
}


void setup()
{
  dht.begin();
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  radio.begin();
  radio.setChannel(115); 
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(addresses[0]); 
 
 
}


void loop()
{

   if ((currentTime - lastRunTime >= 5000) ){     //timer low-cost  
    lastRunTime = currentTime;
    readSensor();
    radio.write(&data, sizeof(data));
    
}
  radio.startListening();
  if (radio.available())
   {
   bool done = false;
   while (!done)
  { 
   done = radio.read(DataMgs, 1);
    if (DataMgs[0] ==1)
    {
      digitalWrite(relay, HIGH);
    }  if (DataMgs[0] ==0)
    {
      digitalWrite(relay, LOW);
    }
}
   }
}

but I couldn’t receive any data and control relay! :expressionless:

Well that part is NOT Blynk related… you might be on your own there, at least in this Blynk forum.

maybe someone has done it before and could help me in this case; bcs I can send and receive data from two NRF’s but can’t share it to the blynk…

Try cleaning up your Blynk sketch… that void loop() is still messy… there is a good chance that you could lose Blynk connection while awaiting your RF data transfer.

Checkout those links and Help Center documents I mentioned earlier… the key is to keep the main loop clear and use timers to periodically poll sensor input and App output as required, not in a loop that can run hundreds of times a second.

Being very dependent on reading the RF network in a while loop, using NRF chips is at best unreliable in my opinion. I suggest using Wemos or similar chips. They cost almost the same and are much more stable. I haven’t used the NRF’s in a long time and probably never will again. They also interfere with the Wifi networks seeings as they are also on the 2.4 Ghz band.

No, just forget and it and use regular Wifi ESP-like chips, that would be my advice. Especially with Blynk. The combination with Blynk and NRF makes a bad headache.

If you use the NRF standalone or with and Arduino (without Blynk) it’s all find and dandy (I got some simple node0 node1 information exchange sketches if you really want to try).

thank you for reply
my project is about irrigation system iot , then I’m not able to connect all the stations to the wifi bcs they are far away…
bcs of that I’m gonna use NRFs to build a mesh network and a gateway which is near the router to connect internet via wifi or ethernet. any other suggestion to build a fixed network? have you ever tried ‘mysensors’ library? I think it’s more stable.
and I will be grateful if you share node0 node1 sketch with me.
thanks.

Just make a simple WiFi mesh network. In the past I used a product called openmesh to create a large WiFi network through a 1km long, and wooded, RV park. Nice thing about it is at it was dirt simple to then just pass any other networked signal across it, In my case IP cameras as well and WiFi hotspots.

No need for mixing wireless standards.

can I do it with esp8266?
I think I have to buy lots of that bcs I’m doing in 10km area.

Probably… haven’t looked into it, but they would NOT be running Blynk, but only acting as the mesh nodes for other independent clients (i.e. Blynk based MCUs).

The hardware I was referring to used higher gain antennas then a simple ESP, with options for external ones for much langer ranges between nodes.

Do you need hotspot coverage throughout the 10k, or just from one end to the other? You can easily use directional (yagi or dish) antennas to push a single long range link. Then run your Blynk based ESPs at either end

thats a good product but it’s not in my country and due to some law I can’t buy from amazon;
anyway, it’s a hotspot coverage for 20 nodes.
why blynk couldn’t run with this?

That’s not what Blynk is designed for… it will connect OVER a mesh (by connecting to mesh node hotspots), but it is not designed to be PART of a mesh (unless the developers are holding out on us :stuck_out_tongue_winking_eye: ). Even if you could hack the two different internal programs together, it would severely compromise the function of both and be completely unnecessary.

Ýou would also need a lot of NRF’s…

1 Like

Well the software can reportedly run on 3rd party hardware… look into other mesh options as well.

but nrfs have more range which I have is about 900m
could u please send me a link or something to find out how to have nrf information exchange between 2 nodes?

Lol , I’m trying to use blynk as a controller of the mesh like a boss :sunglasses:

yah… don’t make a 10k long train… make a shorter train on a 10k track with multiple stations :stuck_out_tongue:

Sure thing, here are the code’s for the NRF nodes:

It sends the millis() and a certain value to the other NRF. As you can see they are handled in the loop because they need to constantly pull info from the NRF network.

There is one case I think of that this will work fine and that is by using an ESP32 (which has two cores). You could run the Blynk loop on one core and the NRF loop on the second core. Three LEDs with Dirty Delay (ESP32) this topic has some pointers for that case :slight_smile:

Based on the RF24 lib (excuse the comments, they are in Dutch)

Node 0

// ONTVANGER NODE, RF24 SENSOR NETWERK
// PROGRAMMEERVOORBEELD BITS & PARTS, WWW.BITSANDPARTS.EU

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

// we definieeren de structuur van het te ontvangen bericht (het envelopje met daarin de gegevens)
struct sensor_bericht
{
  unsigned long ms;        // de huidige tijd in milliseconden
  unsigned long sensor_waarde;  // de uitgelezen sensor-waarde
};

RF24 radio(9,10);       // nRF24L01(+) transceiver aangesloten met pins 9 en 10
RF24Network network(radio);   // netwerk definiëren

const uint16_t deze_node = 0;   // adres van onze (ontvanger) node
const uint16_t andere_node = 1; // adres van de andere (sensor) node

//*******************************************************************************
// SETUP
//*******************************************************************************
void setup()
{
  Serial.begin(57600);
 
  SPI.begin();
  radio.begin();
  radio.setDataRate(RF24_1MBPS);
  radio.setPayloadSize(24);
  network.begin(90, deze_node);
  
  Serial.println("Sensor netwerk ontvanger start...");
  Serial.print("Datarate: ");
  Serial.println(radio.getDataRate());
  Serial.print("Power level: ");
  Serial.println(radio.getPALevel());
  Serial.print("Payload size: ");
  Serial.println(radio.getPayloadSize());
}

//*******************************************************************************
// HOOFD LOOP
//*******************************************************************************
void loop()
{
  // houdt het netwerk aan de gang
  network.update();

  // is er data beschikbaar op het netwerk voor ons?
  while ( network.available() )
  {
    // zo ja, pak de data en print het
  
  // we geven aan dat 'header' de structuur heeft van 'RF24NetworkHeader' (voorgedefinieerd in de RF24network library)
    RF24NetworkHeader header;
  
  // we geven aan dat 'bericht' de structuur heeft van 'sensor_bericht'
    sensor_bericht bericht; 

  // we lezen het bericht van het netwerk
    network.read(header,&bericht,sizeof(bericht));
    
  // de ontvangen gegevens die in het sensor bericht zaten printen  
    Serial.print(" Sensor waarde: ");
    Serial.print(bericht.sensor_waarde);
    
    Serial.print(" Tijd: ");
    Serial.println(bericht.ms); 
    
  }
}

Node 1

// ZENDER (SENSOR) NODE, RF24 SENSOR NETWERK
// PROGRAMMEERVOORBEELD BITS & PARTS, WWW.BITSANDPARTS.EU

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

// we definieeren de structuur van het te sturen bericht (het envelopje met daarin de gegevens)
struct sensor_bericht
{
  unsigned long ms;        // de huidige tijd in milliseconden
  unsigned long sensor_waarde;  // de uitgelezen sensor-waarde
};

RF24 radio(9,10);       // nRF24L01(+) transceiver aangesloten met pins 9 en 10
RF24Network network(radio);   // netwerk definiëren

const uint16_t deze_node = 1;   // adres van onze (sensor) node
const uint16_t andere_node = 0; // adres van de andere (ontvanger) node

const unsigned long interval = 2000; //om de hoeveel tijd we de sensorwaarde naar de ontvanger node sturen (2 seconden)

unsigned long tijd_laatste_bericht;  // wanneer we voor het laatste een bericht hebben gestuurd

//*******************************************************************************
// SETUP
//*******************************************************************************
void setup(void)
{
  Serial.begin(57600);
 
  SPI.begin();
  radio.begin();
  radio.setDataRate(RF24_1MBPS);
  radio.setPayloadSize(24);
  network.begin(90, deze_node);
  
  Serial.println("Sensor netwerk zender (sensor node) start..."); 
  Serial.print("Datarate: ");
  Serial.println(radio.getDataRate());
  Serial.print("Power level: ");
  Serial.println(radio.getPALevel());
  Serial.print("Payload size: ");
  Serial.println(radio.getPayloadSize());
}

//*******************************************************************************
// HOOFD LOOP
//*******************************************************************************
void loop(void)
{
  // houdt het netwerk aan de gang
  network.update();

  // als het tijd is om een bericht te sturen dan gaan we sturen
  unsigned long tijd_nu = millis();
  
  if( tijd_nu - tijd_laatste_bericht >= interval )
  {
  Serial.print("Verzending gegevens...");
  
  // we geven aan dat 'header' de structuur heeft van 'RF24NetworkHeader' (voorgedefinieerd in de RF24network library)
    RF24NetworkHeader header(/*naar node*/ andere_node);
  
  // de sensor waarde uitlezen van analoge input A0
  unsigned long sensor_waarde = analogRead( A0 ); 

  unsigned long mv = (sensor_waarde ) * (5000/1024); 
  unsigned long cel = (mv - 500)/10;

  // nog even laten zien welke waarde we versturen
    Serial.print("sensor_waarde:");
    Serial.print(cel);
    Serial.print("...");
    
  // we geven aan dat 'bericht' de structuur heeft van 'sensor_bericht'
  // en zetten er gelijk de gegevens in
    sensor_bericht bericht = { millis(), cel }; 

  // we sturen het bericht het netwerk op
  bool ok = network.write(header,&bericht,sizeof(bericht));
    
  if( ok )
  {
    Serial.println("gelukt.");
  } else
  {
    Serial.println("mislukt.");
  }

  // onthouden wanneer we het laatste bericht hebben verzonden
    tijd_laatste_bericht = millis();
  }
}

damn
thank you so much @Lichtsignaal and @Gunner :blush:
I have found that “mysensor” library is stable and easy to use for NRF24 + gateway
but in case of using with blynk app needs to translate data; I think someone in your topic has done it before.
I don’t have time to test it, I have to present my project in a week…
anyway I love blynk