I have an old program I made that I’m trying to get to work with Blynk 2.0. For some strange reason I can only get it to go online when I power it on early in the morning. Any other time I power on the Thing it doesn’t connect. I’ve confirmed it is not my wifi, so it must be my programing or a library? (I’ve tried using a second board, powering off and on my router, and do not experience any other issues with other devices in my house).
Once it is connected to the internet it works fine, it seems to only be during a power outage or if I have to reboot it that it cannot connect (unless I leave it off and turn it on early morning)
Here is the Serial output:
state: 3 → 5 (10)
add 0
aid 23
cnt
state: 5 → 2 (2c0)
rm 0
wifi evt: 1
STA disconnect: 2
reconnect
state: 2 → 0 (0)
scandone
state: 0 → 2 (b0)
state: 2 → 3 (0)
state: 3 → 5 (10)
add 0
aid 24
cnt
connected with DontPanic, channel 9
dhcp client start…
wifi evt: 0
pm open,type:2 0
–
Yes, my code is clunky - I’m not an expert but it seems to work ok for what I need
// 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 "XXXXX"
#define BLYNK_TEMPLATE_NAME "XXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXX hidden"
#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 <Adafruit_Sensor.h>
//#include <DHT_U.h>
#include <DHT.h>
//DHT dht(DHTPIN, DHTTYPE, 13); // ESP8266 Temp sensor
DHT dht(DHTPIN, DHTTYPE);
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)
float h = dht.readHumidity(); //changed from fload to int
// Read temperature as Celsius (the default)
float 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(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h); //changed from float to int
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false); //changed from fload to int
/*
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"); */
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;
//EmailString = String(GarageOpenMinutes) + " minutes";
//Blynk.email("XXXXXXXX@gmail.com", "Garage Open for",EmailString); // send email
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[] = "password here I hid this";
Serial.print("Connecting to ");
Serial.println(ssid);
//Blynk.config(BLYNK_AUTH_TOKEN, "64.225.16.22", 8080);
//Blynk.connect();
//Blynk.run();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
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 } ====================