Hi all,
Just recently I have problem connecting to Cloud Blynk Server (I suspect after recent update on the server, but this is just my thought)
This is my sketch that several days ago has no issue with Cloud Server and I haven’t change any wiring on my NodeMCU:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <DHT.h>
#include <NTPtimeESP.h>
// -- NodeMCU PIN to ESP8266 ESP-12 Pinout
#define LDRPIN A0 // ADC Pin for LDR Sensor
#define D1 5 // LED 1 (Yellow)
#define D2 4 // LED 4 (White)
#define D3 0 // LED 2 (Green)
#define SystemLED 2 // GPIO-2 --> D4
#define D5 14 // LED 3 (Red)
#define DHTPIN 12 // GPIO-12 --> D6
#define D7 13
#define D8 15
#define D9 3
#define D10 1
// -- Blynk Virtual PIN definition
#define VDisplayTemp_Pin V10
#define VDisplayHumid_Pin V11
#define VDisplayLDR_Pin V12
#define VDisplayUptime_Pin V20
#define VDisplayNetConnect_Pin V21
#define VDisplayNetConnectUp_Pin V22
#define VDisplayDate_Time V23
#define VTimeInput1 V28
#define VTimeInput2 V29
#define VTimeInput3 V30
#define VTimeInput4 V27
#define VButton1_Pin V31
#define VButton2_Pin V32 // with Timer
#define VButton3_Pin V33
#define VButton5_Pin V35
#define VButtonTimer1_Pin V36
#define VButtonTimer3_Pin V37
#define VButtonTimer5_Pin V38
#define VButtonTimer2_Pin V39
#define VLEDblink_Pin V3
#define VLEDDHT11_Pin V4
//#define VLED1_Pin V41
//#define VLED2_Pin V42
//#define VLED3_Pin V43
//#define VLED5_Pin V45
const char auth[] = "xxxxxxxxxxxxxxxxxxxx"; //insert here your token generated by Blynk
const char ssid[] = "Camar-24";
const char pass[] = "xxxxxxxxxx";
const String MyDayOfWeek[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
const String MyMonth[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int startTimeInSecs1;
int startTimeInSecs2;
int stopTimeInSecs2;
int startTimeInSecs3;
int stopTimeInSecs3;
int startTimeInSecs4;
int stopTimeInSecs4;
int NetworkDisconectedTimes = 0; // Reset every start-up
int NetConnectUp; // Reset every network re-conected
boolean AutoLamp1_Set = false;
boolean AutoLamp3_Set = false;
boolean AutoLamp5_Set = false;
boolean AutoLamp2_Set = false;
strDateTime dateTime;
SimpleTimer timer;
DHT dht(DHTPIN, DHT11);
NTPtime MyNTP("id.pool.ntp.org");
WidgetLED VLED_SystemBlink(VLEDblink_Pin);
WidgetLED VLED_SensorDHT11(VLEDDHT11_Pin);
//WidgetLED VLED1(VLED1_Pin);
//WidgetLED VLED2(VLED2_Pin);
//WidgetLED VLED3(VLED3_Pin);
//WidgetLED VLED5(VLED5_Pin);
/********************************************************************************
* This function will run every time Blynk connection is established
********************************************************************************/
BLYNK_CONNECTED() {
Blynk.syncAll();
Blynk.syncVirtual(VButton1_Pin);
Blynk.syncVirtual(VButton3_Pin);
Blynk.syncVirtual(VButton5_Pin);
Blynk.syncVirtual(VButton2_Pin);
Blynk.syncVirtual(VTimeInput1);
Blynk.syncVirtual(VTimeInput2);
Blynk.syncVirtual(VTimeInput3);
Blynk.syncVirtual(VTimeInput4);
NetworkDisconectedTimes++;
Blynk.virtualWrite(VDisplayNetConnect_Pin, NetworkDisconectedTimes); // -- Disconected time counter on V21
NetConnectUp = 0; // -- Reset Network Connection Uptime upon Network Disconected
// -- Send email when reconnected not in startup
if (NetworkDisconectedTimes > 1) {
String MyDateTime = String(hour()) + MyPrintDigits(minute())+" - ";
MyDateTime += MyDayOfWeek[weekday()-1]+", "+String(day())+"-"+MyMonth[month()-1]+"-"+String(year());
Blynk.email("noersaleh@gmail.com", "Noer Home Automation just reconected: "+String(NetworkDisconectedTimes), "Reconected Time:"+MyDateTime);
}
}
/********************************************************************************
* Define all button related to Lamp ON-OFF Toggle
********************************************************************************/
BLYNK_WRITE(VButton1_Pin) //-- Lamp 1
{
digitalWrite(D1, param.asInt());
// if (digitalRead(D1)==1) {VLED1.on();} else {VLED1.off();}
#ifdef DEBUG
Serial.print("Virtual Button 1 ++> Pressed: ");
Serial.print(param.asInt());
Serial.print(" - ");
Serial.println(digitalRead(D1));
#endif
}
BLYNK_WRITE(VButton2_Pin) //-- Lamp 2
{
digitalWrite(D2, param.asInt());
// if (digitalRead(D2)==1) {VLED2.on();} else {VLED2.off();}
#ifdef DEBUG
Serial.print("Virtual Button 2 ++> Pressed: ");
Serial.print(param.asInt());
Serial.print(" - ");
Serial.println(digitalRead(D2));
#endif
}
BLYNK_WRITE(VButton3_Pin) //-- Lamp 3
{
digitalWrite(D3, param.asInt());
// if (digitalRead(D3)==1) {VLED3.on();} else {VLED3.off();}
#ifdef DEBUG
Serial.print("Virtual Button 3 ++> Pressed: ");
Serial.print(param.asInt());
Serial.print(" - ");
Serial.println(digitalRead(D3));
#endif
}
BLYNK_WRITE(VButton5_Pin) //-- Lamp 4
{
digitalWrite(D5, param.asInt());
// if (digitalRead(D5)==1) {VLED5.on();} else {VLED5.off();}
#ifdef DEBUG
Serial.print("Virtual Button 5 ++> Pressed: ");
Serial.print(param.asInt());
Serial.print(" - ");
Serial.println(digitalRead(D5));
#endif
}
/********************************************************************************
* Define all button related to Auto / Manual Lamp ON-OFF
********************************************************************************/
BLYNK_WRITE(VButtonTimer1_Pin) //-- Button Widget for Lamp 1 Auto (Timer Lamp)
{
AutoLamp1_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonTimer3_Pin) //-- Button Widget for Lamp 3 Auto (Timer or LDR)
{
AutoLamp3_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonTimer5_Pin) //-- Button Widget for Lamp 5 Auto (Timer or LDR)
{
AutoLamp5_Set = (param.asInt()==1);
}
BLYNK_WRITE(VButtonTimer2_Pin) //-- Button Widget for Lamp 5 Auto (Timer or LDR)
{
AutoLamp2_Set = (param.asInt()==1);
}
// --- This function will convert UTC (from TimeInput) to GMT+7
int UTCtoGMT7(int TimeInSecs)
{
TimeInSecs += 25200; // --- From UTC convert to GMT+7
if (TimeInSecs >= 86400) { // 86400 = 24hour x 3600
TimeInSecs -= 86400;
}
return TimeInSecs;
}
/********************************************************************************
* Define Timer Time from Mobile Apps (Using TimeInput)
********************************************************************************/
BLYNK_WRITE(VTimeInput1) {
startTimeInSecs1 = UTCtoGMT7(param[0].asInt()); // UTC to GMT+7
#ifdef DEBUG
Serial.print("SET>> 1. Now : ");
Serial.print(hour()*3600+minute()*60);
Serial.print(" --> Start: ");
Serial.print(startTimeInSecs1);
Serial.println(" - " + ClockFormat(startTimeInSecs1, false));
#endif
}
BLYNK_WRITE(VTimeInput2) {
startTimeInSecs2 = UTCtoGMT7(param[0].asInt()); // UTC to GMT+7
stopTimeInSecs2 = UTCtoGMT7(param[1].asInt()); // UTC to GMT+7
#ifdef DEBUG
Serial.print("SET>> 2. Now : ");
Serial.print(hour()*3600+minute()*60);
Serial.print(" --> Start: ");
Serial.print(startTimeInSecs2);
Serial.print(" - " + ClockFormat(startTimeInSecs2, false));
Serial.print(" - Stop : ");
Serial.print(stopTimeInSecs2);
Serial.println(" - " + ClockFormat(stopTimeInSecs2, false));
#endif
}
BLYNK_WRITE(VTimeInput3) {
startTimeInSecs3 = UTCtoGMT7(param[0].asInt()); // UTC to GMT+7
stopTimeInSecs3 = UTCtoGMT7(param[1].asInt()); // UTC to GMT+7
#ifdef DEBUG
Serial.print("SET>> 3. Now : ");
Serial.print(hour()*3600+minute()*60);
Serial.print(" --> Start: ");
Serial.print(startTimeInSecs3);
Serial.print(" - ");
Serial.print(ClockFormat(startTimeInSecs3, false));
Serial.print(" - Stop : ");
Serial.print(stopTimeInSecs3);
Serial.print(" - ");
Serial.println(ClockFormat(stopTimeInSecs3, false));
#endif
}
BLYNK_WRITE(VTimeInput4) {
startTimeInSecs4 = UTCtoGMT7(param[0].asInt()); // UTC to GMT+7
stopTimeInSecs4 = UTCtoGMT7(param[1].asInt()); // UTC to GMT+7
#ifdef DEBUG
Serial.print("SET>> 4. Now : ");
Serial.print(hour()*3600+minute()*60);
Serial.print(" --> Start: ");
Serial.print(startTimeInSecs4);
Serial.print(" - " + ClockFormat(startTimeInSecs4, false));
Serial.print(" - Stop : ");
Serial.print(stopTimeInSecs4);
Serial.println(" - " + ClockFormat(stopTimeInSecs4, false));
#endif
}
// --- This function will sync current time with time from the "Network Time Protocol" Server
void SyncTimetoNTP()
{
// first parameter: first parameter Time zone in floating point; second parameter: 0 no daylight saving, 1 for European summer time;
dateTime = MyNTP.getNTPtime(7.0, 0);
/*
byte actualhour = dateTime.hour;
byte actualminute = dateTime.minute;
byte actualsecond = dateTime.second;
int actualyear = dateTime.year;
byte actualmonth = dateTime.month;
byte actualday = dateTime.day;
byte actualdayofWeek = dateTime.dayofWeek;
*/
setTime(dateTime.hour, dateTime.minute, dateTime.second, dateTime.day, dateTime.month, dateTime.year); //(hours, minutes, seconds, days, months, years)
#ifdef DEBUG
Serial.println("========================");
Serial.println("= Sync to NTP Server: =");
Serial.println("========================");
MyNTP.printDateTime(dateTime);
Serial.println("========================");
#endif
}
/********************************************************************************
* Display Clock to Mobile Apps and Check & execute Timer setup
********************************************************************************/
void DisplayClock_CheckAlarm()
{
int MyNowSecond = hour()*3600+minute()*60+second();
String MyTime = String(hour()) + MyPrintDigits(minute());
String MyDate = MyDayOfWeek[weekday()-1]+", "+String(day())+"-"+MyMonth[month()-1]+"-"+String(year());
Blynk.virtualWrite(VDisplayDate_Time, MyDate+" "+MyTime);
// -- Check for ALAMRM
if (AutoLamp2_Set) {
if ((MyNowSecond >= startTimeInSecs1) && (MyNowSecond <= (startTimeInSecs1+20))) {
Blynk.virtualWrite(VButton2_Pin, HIGH);
digitalWrite(D2, HIGH);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" ALARM -- ALARM -- ALARM");
Serial.println("===========================");
#endif
} else {
Blynk.virtualWrite(VButton2_Pin, LOW);
digitalWrite(D2, LOW);
}
// if (digitalRead(D2)==1) {VLED2.on();} else {VLED2.off();}
#ifdef DEBUG
Serial.print("1. Now: ");
Serial.print(now());
Serial.print(" ("+String(MyNowSecond)+" vs. ");
Serial.print(String(startTimeInSecs1));
Serial.print(") --> Date: ");
Serial.print(MyDate);
Serial.print(" -- ");
Serial.println(MyTime+MyPrintDigits(second()));
#endif
} // end if (AutoLamp_Set)
// -- Check for Timer LAMP 1
if (AutoLamp1_Set) {
if ((MyNowSecond >= startTimeInSecs2) && (MyNowSecond <= (startTimeInSecs2+20))) {
Blynk.virtualWrite(VButton1_Pin, HIGH);
digitalWrite(D1, HIGH);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 1 ON");
Serial.println("===========================");
#endif
}
if ((MyNowSecond >= stopTimeInSecs2) && (MyNowSecond <= (stopTimeInSecs2+20))) {
Blynk.virtualWrite(VButton1_Pin, LOW);
digitalWrite(D1, LOW);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 1 OFF");
Serial.println("===========================");
#endif
}
// if (digitalRead(D1)==1) {VLED1.on();} else {VLED1.off();}
#ifdef DEBUG
Serial.print("2. Now: ");
Serial.print(now());
Serial.print(" ("+String(MyNowSecond)+" vs. ");
Serial.print(String(startTimeInSecs2));
Serial.print("-");
Serial.print(String(stopTimeInSecs2));
Serial.print(") --> Date: ");
Serial.print(MyDate);
Serial.print(" -- ");
Serial.println(MyTime+MyPrintDigits(second()));
#endif
} // end if (AutoLamp_Set)
// -- Check for Timer LAMP 2
if (AutoLamp3_Set) {
if ((MyNowSecond >= startTimeInSecs3) && (MyNowSecond <= (startTimeInSecs3+20))) {
Blynk.virtualWrite(VButton3_Pin, HIGH);
digitalWrite(D3, HIGH);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 2 ON");
Serial.println("===========================");
#endif
}
if ((MyNowSecond >= stopTimeInSecs3) && (MyNowSecond <= (stopTimeInSecs3+20))) {
Blynk.virtualWrite(VButton3_Pin, LOW);
digitalWrite(D3, LOW);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 2 OFF");
Serial.println("===========================");
#endif
}
// if (digitalRead(D3)==1) {VLED3.on();} else {VLED3.off();}
#ifdef DEBUG
Serial.print("3. Now: ");
Serial.print(now());
Serial.print(" ("+String(MyNowSecond)+" vs. ");
Serial.print(String(startTimeInSecs3));
Serial.print("-");
Serial.print(String(stopTimeInSecs3));
Serial.print(") --> Date: ");
Serial.print(MyDate);
Serial.print(" -- ");
Serial.println(MyTime+MyPrintDigits(second()));
#endif
} // end if (AutoLamp_Set)
// -- Check for Timer LAMP 3
if (AutoLamp5_Set) {
if ((MyNowSecond >= startTimeInSecs4) && (MyNowSecond <= (startTimeInSecs4+20))) {
Blynk.virtualWrite(VButton5_Pin, HIGH);
digitalWrite(D5, HIGH);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 3 ON");
Serial.println("===========================");
#endif
}
if ((MyNowSecond >= stopTimeInSecs4) && (MyNowSecond <= (stopTimeInSecs4+20))) {
Blynk.virtualWrite(VButton5_Pin, LOW);
digitalWrite(D5, LOW);
#ifdef DEBUG
Serial.println();
Serial.println("===========================");
Serial.println(" LAMP 3 OFF");
Serial.println("===========================");
#endif
}
// if (digitalRead(D5)==1) {VLED5.on();} else {VLED5.off();}
#ifdef DEBUG
Serial.print("4. Now: ");
Serial.print(now());
Serial.print(" ("+String(MyNowSecond)+" vs. ");
Serial.print(String(startTimeInSecs4));
Serial.print("-");
Serial.print(String(stopTimeInSecs4));
Serial.print(") --> Date: ");
Serial.print(MyDate);
Serial.print(" -- ");
Serial.println(MyTime+MyPrintDigits(second()));
#endif
} // end if (AutoLamp_Set)
} // End: DisplayClock_CheckAlarm()
/********************************************************************************
* Display Network connection time
********************************************************************************/
void sendNetConnectTime()
{
// -- Blink LED that in Sync
if (VLED_SystemBlink.getValue()) {
VLED_SystemBlink.off();
digitalWrite(SystemLED, LOW);
} else {
VLED_SystemBlink.on();
digitalWrite(SystemLED, HIGH);
}
NetConnectUp++;
} // End: sendNetConnectTime()
/********************************************************************************
* Display System Up time
********************************************************************************/
void sendUptime()
{
// ------------- Converting miliseconds to Clock and daisplay on Virtual Pin
String MyClock = ClockFormat((millis() / 1000), false);
Blynk.virtualWrite(VDisplayUptime_Pin, MyClock); //virtual pin 20 as UpTime
Blynk.virtualWrite(VDisplayNetConnectUp_Pin, ClockFormat(NetConnectUp, false));
#ifdef DEBUG
Serial.print("Uptime (s): ");
Serial.println(MyClock);
#endif
} // End: sendUptime()
/********************************************************************************
* Display DHT11 (temp and humidity) and LDR sensor
********************************************************************************/
void sendDHT11_LDR()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(t)) {Blynk.virtualWrite(VDisplayTemp_Pin, t); VLED_SensorDHT11.on();} // V10 -- LED off --> DHT Offline
if (!isnan(h)) {Blynk.virtualWrite(VDisplayHumid_Pin, h);} else {VLED_SensorDHT11.off();} //V11
int LDR_value = analogRead(LDRPIN);
LDR_value = map(LDR_value, 0, 1023, 0, 100); // -- convert value of 0-1023 to 0-100
Blynk.virtualWrite(VDisplayLDR_Pin, LDR_value);
#ifdef DEBUG
Serial.print("D1 D3 D5 D2 Status:" );
Serial.print(digitalRead(D1));
Serial.print(" ");
Serial.print(digitalRead(D3));
Serial.print(" ");
Serial.print(digitalRead(D5));
Serial.print(" ");
Serial.print(digitalRead(D2));
Serial.print(" == Temp:" );
Serial.print(t);
Serial.print(" - Humidity:" );
Serial.print(h);
Serial.print(" - Light:" );
Serial.println(LDR_value);
#endif
} // End: sendDHT11_LDR()
// --- Utility function for digital clock display: prints preceding colon and leading 0
String MyPrintDigits(int digits)
{
String new_digits = ":";
if (digits < 10) {
new_digits += "0";
}
new_digits += String(digits);
return new_digits;
}
// --- Utility function to convert seconds to clock format
String ClockFormat(int InputClock, boolean DisplaySecond)
{
int my_h,my_m,my_s;
my_h = InputClock / 3600; // Hour ==>> total_seconds div 3600
String MyClock = String(my_h);
my_m = InputClock%3600/60; // Minute ==>> total_seconds mod 3600 div 60
MyClock += MyPrintDigits(my_m);
if (DisplaySecond)
{
my_s = InputClock%3600%60; // Second ==>> total_seconds mod 3600 mod 60
MyClock += MyPrintDigits(my_s);
}
return MyClock;
} // End: ClockFormat()
/****************************************************
* SETUP: Initialize for the first Time
*****************************************************/
void setup()
{
#ifdef DEBUG
Serial.begin(9600); // See the connection status in Serial Monitor
#endif
Blynk.begin(auth, ssid, pass);
// while (!Blynk.connect()) {
// // Wait until connected
// }
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(SystemLED, OUTPUT);
dht.begin();
SyncTimetoNTP();
timer.setInterval(1000L, sendNetConnectTime); // every second
timer.setInterval(5000L, sendDHT11_LDR); // every 5 seconds
timer.setInterval(60000L, sendUptime); // every minutes
timer.setInterval(20000L, DisplayClock_CheckAlarm); // every 20 second
timer.setInterval(3600000L, SyncTimetoNTP); // every hour sync with NTP Server
Blynk.virtualWrite(VDisplayUptime_Pin, "0:00"); // Display initial UpTime
Blynk.virtualWrite(VDisplayNetConnectUp_Pin, "0:00"); // Display initial last Network Connect time
Blynk.email("noersaleh@gmail.com", "Noer Home Automation just restarted ... ", "RESTARTED ...");
} // End: setup()
/****************************************************
* LOOP: all the time
*****************************************************/
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
} // End: loop()
This is the shown on Serial:
1384, room 16
tail 8
chksum �[21758] Connecting to Camar-24
[25761] Connected to WiFi
[25761] IP: 192.168.1.109
[25761] Blynk v0.3.8 on NodeMCU
========================
= Sync to NTP Server: =
========================
11-9-2016 [1] - 19:58:51
========================
[33718] Connecting to blynk-cloud.com:8442
D1 D3 D5 D2 Status:0 0 0 0 == Temp:nan - Humidity:nan - Light:1
[42126] Ready (ping: 271ms).
[47258] Login timeout
D1 D3 D5 D2 Status:0 0 0 0 == Temp:nan - Humidity:nan - Light:1
[47529] Connecting to blynk-cloud.com:8442
D1 D3 D5 D2 Status:0 0 0 0 == Temp:nan - Humidity:nan - Light:1
[55376] Ready (ping: 272ms).
[60616] Login timeout
D1 D3 D5 D2 Status:0 0 0 0 == Temp:nan - Humidity:nan - Light:1
[60887] Connecting to blynk-cloud.com:8442
As comparison I tried to upload following sketch, and is working perfectly…
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Camar-24";
char pass[] = "xxxxxxxxxxx";
#define SystemLED 2 // GPIO-2 --> D4
#define VLEDblink_Pin V3
WidgetLED VLED_SystemBlink(VLEDblink_Pin);
SimpleTimer timer;
void sendNetConnectTime()
{
// -- Blink LED that in Sync
if (VLED_SystemBlink.getValue()) {
VLED_SystemBlink.off();
digitalWrite(SystemLED, LOW);
} else {
VLED_SystemBlink.on();
digitalWrite(SystemLED, HIGH);
}
} // End: sendNetConnectTime()
void setup()
{
Serial.begin(9600);
pinMode(SystemLED, OUTPUT);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendNetConnectTime); // every second
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
}
The serial shown:
1384, room 16
tail 8
chksum �[20440] Connecting to Camar-24
[24443] Connected to WiFi
[24443] IP: 192.168.1.109
[24443] Blynk v0.3.8 on NodeMCU
[24443] Connecting to blynk-cloud.com:8442
[32362] Ready (ping: 1ms).
TIA