BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to Add a delay between each device to join local server?

#1

Hello,

I am running 7 nodemcu (1Timer, 3Physical button sync 3Led dimmer)
Each have there different token but all the devices are running under same project.

I tried to mimic the power cut off by turning of the power supply to the nodemcu, local server and wifi router.

Now when i turn back on all the devices(just like would happen after the power comes back), the problem starts.
None of the device[Nodemcu’s] want to connect to the server. But the blynk app is online and the local server is running and in the app the devices are offline. And i have provided DHCP(specified ip address to all the nodemcu’s and server as well.)
Now if i want to get the devices online i manually need to reset all the Nodemcu’s and they come online and works just fine.

The problem is when the power comes back all the devices try to rush into the router or server and gets stuck there. What is the way to prevent this from happening ? Is there a way to add delay between devices , so that one after the other joins the server thus reducing the incoming traffic to the server or router ?

And for testing purpose i have connected all the nodemcu’s to the same Lab bench power supply !! Is there any problem with that ??[Hopefully not]. And all connections are solid, no loose wires.

Codes running
1)Physical Sync example sketch
2)Timer (Automatic scheduler. ESP-01 with 4 Time Input Widgets) with minor changes…
3) Normal PWM led dimmer sketch.

Can anyone point out the mistake ??

0 Likes

#2

I suspect that the try to join the network (and fail) before the router is ready, not because they are all trying at the same time.

You will need to add code to try getting onto the network if it fails.
Since it is not yet on the network using a delay will probably be fine, or use timers to keep trying until it is connected.

0 Likes

#3

I’m not 100% convinced of your problem or your proposed solution, but you can try this …

const int inter_device_delay = 2000;  // milliseconds

int power_on_delay(char c) {
   if (c >= '0' && c <= '9')
      return (c % 16) * inter_device_delay;
   else if (c >= 'a' && c <= 'f')
      return (c - 'a' + 10) % 16 * inter_device_delay;
   else if (c >= 'A' && c <= 'F')
      return (c - 'A' + 10) % 16 * inter_device_delay;
   else
      return -1;
}

void setup(void) {           
   delay(power_on_delay(auth[0]));

I’ve tied the power-on delay to the most-significant nibble (actually, it’s a character) of your auth token. The delay is power_on_delay(X) where ‘X’ is the first character of your auth token,

power_on_delay(0):     0 ms
power_on_delay(1):  2000 ms
power_on_delay(2):  4000 ms
power_on_delay(3):  6000 ms
power_on_delay(4):  8000 ms
power_on_delay(5): 10000 ms
power_on_delay(6): 12000 ms
power_on_delay(7): 14000 ms
power_on_delay(8): 16000 ms
power_on_delay(9): 18000 ms
power_on_delay(a): 20000 ms
power_on_delay(b): 22000 ms
power_on_delay(c): 24000 ms
power_on_delay(d): 26000 ms
power_on_delay(e): 28000 ms
power_on_delay(f): 30000 ms

This won’t guarantee the devices power-up sequentially. A few devices may power-up at the same time. That’s a function of your auth tokens. There are essentially sixteen time slots in which devices can power-up. You only need seven. However, this improves the chances of your devices powering up one at a time. You can always increase or decrease the inter-device delay. You can choose any character of your auth token.

Just an idea …

Joe

0 Likes

#4

Actually i tried keeping the server and router running and only powering off the nodemcu and powering back . Even then it was not connecting. And also in turn there was so much of wifi traffic around that it also interfered with my home router and disconnected from my laptop. Basically turning on all the devices at the same time will work as a wifi jammer.

When I manually reset the nodemcu with the on-board reset switch then it would connect and stay on and work very well.
The only problem is when the power cycles this setup will be a total mess. Nothing will come online or anything will work as intended to !!!

What can be done to try to reconnect untill the connection is established?

0 Likes

#5

Ummm !!! :roll_eyes: … i am feeling there is a better way of getting this right. Your idea will work probably. But incase we have more devices then we cannot manually alter the code for each and every device.

Yes you have answered to my problem because i asked to set a delay. Thank you for that.

But are you finding any other way to prevent every nodemcu rushing towards the server?
And atleast keep trying to connect until connection is established?

0 Likes

#6

You don’t have to modify the code. There are 16 time slots. If you have 32 devices, on average, you’ll have two devices per time slot. If two devices can’t connect at roughly the same time, you have other problems.

Again, I’m not convinced your problem is multiple devices trying to connect at the same time.

That’s a given. You’ll always retry the connection, even with one and only one device.

0 Likes

#7

The problem is all the device booting at the same time. Because when i power on the Nodemcu’s one by one … everything works well. The problem arises only when all the devices try to boot at the same time.
Is it something to do with the router ? Should i buy any new router ? The one am using now is Dlink the only RJ11 type input. Not like the one we now use with RJ45 fiber net.
Or should I switch to RPi3 as server instead of RPi zero w ? But the ram and cpu usage are hardly 5 to 10 percent at max and the ram will be 65mb out of 117mb . So there should be no problem on the processing end my be.

What do you feel the problem is ?
And what is the code that i need to inject so the module keep trying to connect to serve ? Right now blynk.connect is used .

0 Likes

#8

Can you include your code in,

image

or at least your setup() route.

I gave you a dozen lines of code. Can you at least try them. They assume your auth token is stored in,

char auth[] = "xxx";

If not, you’ll need to change the references to “auth” accordingly. If you share your code, I can help you.

0 Likes

#9
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
BlynkTimer timer;
int ReCnctFlag;


#define server "192.168.1.x" 
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxx"; 
char pass[] = "xxxxxxxxxx";
int val;
int encoder0PinA = 5;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

int curBrightness = 0;
bool isFirstConnect = true;

const int PinDT=5;    // DATA signal
const int PinCLK=4;    // CLOCK signal


BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncAll();
    Blynk.syncVirtual(V6, V5, V3); // V6 Brightness  V5 Switch
    isFirstConnect = false;
  }


   int value = millis() / 1000;
  Blynk.virtualWrite(V33, value);
}



BLYNK_WRITE(V6) {

  curBrightness = param.asInt();
  Blynk.virtualWrite(V6, curBrightness);
  analogWrite(D5, curBrightness);
}

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode(D5, OUTPUT);
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
  delay(10);
  
  Blynk.begin(auth, ssid, pass, IPAddress(192,1xx,1,x), 8080);
  Serial.begin (115200);
  while (Blynk.connect() == false) {  }   // Wait until connected
  timer.setInterval(60 * 1000, reconnectBlynk); 
}


void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
     BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop() {

  
    n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
      if(curBrightness - 200 >= 0)curBrightness -= 100;
      Blynk.virtualWrite(V6, curBrightness);
      analogWrite(D5, curBrightness);
    } else {
      encoder0Pos++;
      if(curBrightness + 100 <= 1023) curBrightness += 100;
      Blynk.virtualWrite(V6, curBrightness);
      analogWrite(D5, curBrightness);
    }
  }
  encoder0PinALast = n;
  

  if(Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
}

This is the LED Dimmer Sketch that is used

And this is the Automatic Scheduler Code (Automatic scheduler. ESP-01 with 4 Time Input Widgets) the code is too lengthy so i am putting the link.

And this the Sync Physical Button example from Blynk sketch builder

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxx";
char ssid[] = "xxxxxx";
char pass[] = "xxxxx";

const int ledPin = 7;
const int btnPin = 8;

BlynkTimer timer;
void checkPhysicalButton();

int ledState = LOW;
int btnState = HIGH;

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V2);
 }

BLYNK_WRITE(V2) {
  ledState = param.asInt();
  digitalWrite(ledPin, ledState);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin) == LOW) {
     if (btnState != LOW) {

    
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
      Blynk.virtualWrite(V2, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  Blynk.begin(auth, ssid, pass, IPAddress(192,1xx,1,xxx), 8080);

  pinMode(ledPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(ledPin, ledState);

   timer.setInterval(100L, checkPhysicalButton);
}

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

This is the code thats currently running on my Nodemcu’s.
I would like to repeat once again>
The problem occurs only during the devices being booted at the same time just like it would happen during a power Re-cycle.
If i turn on the devices one by one there is no problem.

Tried out scenerios

  1. Powered of Server, Router, Nodemcu. Powered them back >> Only 1 or 2 device will connect

  2. Powered of Both server and router powered them back keeping nodemcu’s on>> nodemcu will not sync back automatically and remains offline. (fewer times 1 or 3 devices come online random nodemcu’s everytime).

3)Server and router are up and running then connecting the nodemcu’s one by one>> Works as it should. No problem.

I guess i have covered all the tried out ways by me. I have tried to explain as much as i can effectively. Sorry for my bad English…

0 Likes

#10

Let’s try this for starters …

void setup(void) {
   Serial.begin(9600);

   WiFi.begin(ssid, pass);
   Serial.print("Wi-fi Connecting ");
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
   Serial.println("\nWi-fi Connected");
   Blynk.config(auth, IPAddress(192,1xx,1,xxx), 8080);
   Serial.print("Blynk Connecting ");
   while (!Blynk.connect()) {
      delay(500);
      Serial.print(".");
   }
   Serial.println("\nBlynk Connected"); 

   pinMode(ledPin, OUTPUT);
   pinMode(btnPin, INPUT_PULLUP);
   digitalWrite(ledPin, ledState);

   timer.setInterval(100L, checkPhysicalButton);
}
0 Likes

#11

should i add the lines you have provided to all the nodemcu’s or anyone ?
i just to be sure what needs to be done. Because doing right things in a wrong manner will not resolve the problem, and again leads to confusions.
So to make sure i am asking the above question. May be silly though !!!

0 Likes

#12

Can we try running this sketch (below) on all seven NodeMCU’s (each with a unique auth token)? That way we can test the connection on power-up without having to worry about differences between the sketches.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";

void setup(void) {
   Serial.begin(9600);

   WiFi.begin(ssid, pass);
   Serial.print("Wi-fi Connecting ");
   while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
   }
   Serial.println("\nWi-fi Connected");
   Blynk.config(auth, IPAddress(192,xxx,xxx,xxx), 8080);
   Serial.print("Blynk Connecting ");
   while (!Blynk.connect()) {
      delay(500);
      Serial.print(".");
   }
   Serial.println("\nBlynk Connected"); 
}

void loop() {}
0 Likes

#13

Yep sure thing !! Here it is Midnight 2:30 . So i will be trying this sketch in the morning and update the result i get . :slightly_smiling_face:

But a small doubt here… There is no blynk.begin or blynk.run any blynk related commands so that nodemcu will connect and i can check them on the Blynk app(online status)…

Will this code join the Blynk server ?
And if not, should login to my router page and see whether the devices have connected or not ?

0 Likes

#14

I doubt that… I have 7-9+ devices (depending on what I am working on) all running and re-connecting at same time when I reboot the router.

0 Likes

#15

Can you tell me you are using local server or cloud ? If local server ,which version and on Rpi 3 or Rpi zero w ?
Which Router ?
And can you please give the lines of code that is responsible for reconnecting as soon as the connection drops ?

0 Likes

#16

Either Cloud or Local will have same ability to connect multiple devices/projects at same time. I have Local Server

Doesn’t really matter the RPi, even the Zero with a USB WiFi adapter would work. I am running an RPi3, but I was running on an old netbook running Linux mint just as easily.

Also doesn’t matter the router, I have migrated across three different ones in last two years.

0 Likes

#17

Pasted the code you have provided in the link above to all the nodemcu’s with diff auth tokens and powered it up and out of 7 nodemcu’s 4 connected and rest did not even come to life. and also jammed the wifi signal. So later i connected 3 nodemcu’s to one power supply and 4 to the other one. And powered up and now everything connects with a delay(can say this by seeing the led blinking on each nodemcu at diff intervals…even though powered at the same time.)

I guess there is some noise introduced in the circuit and as the vcc and gnd is common for all the nodemcu’s, i guess this problem was arising !!! But the end result will have all the nodemcu their own power supply !! But what do you think the problem was ? Was it purely code related of even power problem ?

0 Likes

#18

An ESP9266 can draw a significant amount of current for continuous WiFi… not enough current = poor signal, brownouts, etc.

0 Likes

#19

Yes i agree … But the lab bench power supply is capable of giving an constant out put of 1-13v 5A(max 10A) . But the total consumption of all the nodemcu’s is not crossing 200mA to 220mA…

But as per my understanding there must a sudden surge on the power rails, and due to this some of them will malfunction !!!
Now i have two power supply one for 3 nodemcu and another for 4 nodemcu. And now everything seems to work fine.

I have left the devices to work over night… And tested all kinds of scenarios like power cycle and either switching off only the router or the sever or both… And as soon as the router or server comes online… the devices will connect…There is a bit of delay when there is no wifi connectivity !! But that’s manage able…

0 Likes

#20

Hello !
I am currently using the reconnect bit code from the link @Gunner provided. And it works well.
But only after i purchased a new router. Before it was not working. Not sure what was the problem.
And now the devices once in a while goes offline and connects back. I am not sure what is causing the connection issue. The codes that am using are given in the earlier threads… Did anyone find any mistake going on in the code ?
Can anyone tell me how to get rid of this connection issue ???

0 Likes