I have searched extensively on the forum and I think I am following best practices for coding too, but my ESP8266 keeps reconnecting to the server.
Am I sending too many requests to get the connection terminated?
Here is my code
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXX";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXX";
char pass[] = "XXX";
#define DHTPIN 6 // What digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21 // DHT 21, AM2301
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer , timer1;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
Serial.println(h);
Serial.println(t);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V6, t);
Blynk.virtualWrite(V8, h);
}
void sendReport()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
String report = "";
report +="&field1=";
report += String(t);
report +="&field2=";
report += String(h);
Blynk.virtualWrite(V0, report);
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "cloud.blynk.cc", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
dht.begin();
// Setup a function to be called every second
timer.setInterval(2000L, sendSensor);
timer1.setInterval(20000L, sendReport);
}
void loop()
{
Blynk.run();
timer.run();
timer1.run();
}
here is my serial monitor dump
[20447] Connected to WiFi
[20447] IP: 192.168.2.212
[20447]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU
[20527] Connecting to blynk-cloud.com:80
[21083] Ready (ping: 74ms).
⸮uV⸮⸮⸮-S⸮⸮[65] Connecting to XXX
[2567] Connected to WiFi
[2567] IP: 192.168.2.212
[2567]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU
[2644] Connecting to blynk-cloud.com:80
[2874] Ready (ping: 73ms).
⸮bJ⸮z⸮⸮[61] Connecting to XXX
[562] Connected to WiFi
[562] IP: 192.168.2.212
[563]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU
[636] Connecting to blynk-cloud.com:80
[1207] Ready (ping: 76ms).
⸮;⸮⸮⸮[63] Connecting to XXX
[565] Connected to WiFi
[565] IP: 192.168.2.212
[566]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on NodeMCU
[639] Connecting to blynk-cloud.com:80
[5640] Connecting to blynk-cloud.com:80
[5792] Ready (ping: 72ms).