#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
-
Powered of Server, Router, Nodemcu. Powered them back >> Only 1 or 2 device will connect
-
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…