I have a Blynk legacy project I’m finally trying to get working again. I had a lot of success at first and had everything working except the temp/humidity DHT22 sensor. (Was getting a failed to read from DHT sensor). As I was flashing the board again at some point it would not connect to Blynk. Looking at the serial monitor it seems to get to the Blynk.begin line and hangs (after it says connecting to SSID). The I get a soft WDT reset, Exception (4) and the exception text.
I’m trying to figure out if I have a loop somewhere, if for some reason it is taking too long to connect to wifi like with a watchdog error? Or maybe I screwed up the board.
(Sparkfun ESP8266 Thing)
Would appreciate any advice, thanks in advance.
// pin 12 - power status, pin 13 temp data, pin 0 - garage mag sensor, pin 4 - open/close garage
// NOTE PIN 12 NOT USED FOR GARAGE - NO EXTERNAL POWER CHECK
#define BLYNK_TEMPLATE_ID "XXXXXXXX"
#define BLYNK_TEMPLATE_NAME "XXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXX"
#define BLYNK_PRINT Serial
#define DHTTYPE DHT22
#define DHTPIN 13
#define humidity_topic "sensor/humidity" //maybe don't need?
#define temperature_topic "sensor/temperature" //maybe dont need?
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
DHT dht(DHTPIN, DHTTYPE, 13); // ESP8266 Temp sensor
unsigned long startMillis; //Garge door time open
unsigned long currentMillis; //set current time to variable
unsigned long period = 1800000; //3,600,000 milliseconds in one hour, 7200000 - 2 hours, 120000 - 2 minutes, 1 min = 60,000ms, 30 min = 1,800,000
unsigned int Gtempnotify = 0; //notify flag for garage temp low
unsigned int GtempnotifyHIGH = 0; //notify flag for garage temp high
//int priorState = digitalRead(12); //setup variable for PowerSensorStatus (notify flag)
BlynkTimer timer;
// 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.
// ========{ Timer for garage door status and temp/humidity }==================
void myTimerEvent()
{
currentMillis = millis(); //get the current "time" moved here from loop...
// You can send any value at any time.
// Please don't send more that 10 values per second.
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
int h = dht.readHumidity(); //changed from fload to int
// Read temperature as Celsius (the default)
int t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
//Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
int hif = dht.computeHeatIndex(f, h); //changed from float to int
// Compute heat index in Celsius (isFahreheit = false)
int hic = dht.computeHeatIndex(t, h, false); //changed from fload to int
/* check working
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F"); */
Serial.println(); //blank line
Serial.print("Temperature: ");
Serial.print(f);
Serial.println(); //blank line
Blynk.virtualWrite(V2, f);
Blynk.virtualWrite(V3, h);
Blynk.virtualWrite(V4, hif);
if (f<20 && Gtempnotify == 0) { //check temp and notify flag
Gtempnotify = 1;
Blynk.logEvent("notify",String("Cold in garage : ") + f + String("°F"));
}
else if(f>22){
Gtempnotify = 0;
}
if (f>122 && GtempnotifyHIGH == 0) {
GtempnotifyHIGH = 1;
Blynk.logEvent("notify",String("Hot in garage : ") + f + String("°F"));
}
else if(f<120) {
GtempnotifyHIGH = 0;
}
}
// ========{ End Timer }==================
// ========{ Garage Magnetic SW Monitoring Input }==================
void garageMagSensor ()
{
//Serial.println(digitalRead(0)); TESTING only
WidgetLCD lcd5(V5); //Garage door
int garageSensorSW = digitalRead(12); // GPIO5
int GarageOpenMinutes = (currentMillis - startMillis) / 1000 /60;
if (garageSensorSW == LOW)
{
int DoorStatus = 1;
lcd5.print(0, 0, "GARAGE IS CLOSED"); // LCD print, column 1, row 0.
startMillis = currentMillis; //reset timer for garage open to current time
period = 1800000; // Reset the time garage door is open before push notify
lcd5.print(0, 1, " "); //prints spaces on second line to clear it out
}
else
{
int DoorStatus = 0;
lcd5.print(0, 0, "GARAGE IS OPEN "); // LCD print, column 1, row 0
lcd5.print(0, 1, (String(GarageOpenMinutes) + String(" minutes"))); //used string to combine var and text
}
if (currentMillis - startMillis >= period) //test if period has elapsed and push notify
{
Blynk.logEvent("notify",String("Garage door is open for: ") + GarageOpenMinutes + String(" minutes"));
//String EmailString; LEGACY
//EmailString = String(GarageOpenMinutes) + " minutes"; LEGACY
//Blynk.email("XXXXXXX", "Garage Open for",EmailString); // send email LEGACY
period = period * 2; //delay next notification * 2 so I don't keep getting notified as quickly while remains open
Serial.print("Garage Door is open for: ");
int GarageOpenMinutes = (currentMillis - startMillis) /1000 /60;
Serial.print(GarageOpenMinutes);
Serial.println(" minutes");
//startMillis = millis(); //reset notify variable time
}
else
{ }
}
//======== { End Garage magnetic sensor } ============
// ========{ WiFi Setup }==================
void setup_wifi ()
{
char ssid[] = "DontPanic";
char pass[] = "XXXXXXX";
Serial.print("Connecting to ");
Serial.println(ssid);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Blynk.begin(auth, ssid, pass); blynk legacy
Blynk.logEvent("notify","Garage Thing Started");
Serial.println("Garage Thing startup");
WidgetLCD lcd(V0);
IPAddress myip;
myip = WiFi.localIP();
String fullip = String(myip[0]) + "." + myip[1] + "." + myip[2] + "." + myip[3];
lcd.print(0, 0, "IP Address:");
lcd.print(0, 1, fullip); //Show IP on app V0
}
// ========{ End WiFi Setup }==================
//============ { Setup } =======================
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
setup_wifi();
// Setup a function to be called every x seconds
timer.setInterval(15000L, myTimerEvent); //temp, humidity, heat index
timer.setInterval(5000L, garageMagSensor); //check garage door status
// timer.setInterval(5000L, PowerSensorStatus); //check to see if power is on
//timer.setInterval(500L, StillConnected); //check to see if still connected in app
pinMode(12,INPUT_PULLUP); //Garage Door mag sensor INPUT_PULLUP HAD to use pullup as pull up resistor to work
//pinMode(12,INPUT); //PowerStatus checks for voltage 3.3v
pinMode(4, OUTPUT) ; //button to close/open garage
}
//============ { End Setup } =======================
//============== { Loop } ==========================
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
timer.run(); // Initiates BlynkTimer
}
// ============== { End Loop } ====================