I've programmed an DOIT ESP32 devkit V1 board to turn on and off an LED. The board is controlled by Blynk app via Wifi. Today Wifi in my apartment got disconnected and I wasn't able to control the board after it was connected again, I had to connect the board to my computer and to re-upload the program to the board in order to make it respond to my app again. I think the problem was that it disconnected from the Wifi, and didn't try to re-establish the connection. How can I make it re-connect to the Wifi in such cases?
Here's my code:
Code: Select all
#define BLYNK_PRINT Serial
#define RELAY_PIN 32
#define TEMP_PIN V5
#define MSG_PIN V6
// Initialize Telegram BOT
#define BOTtoken "1200001400:AAF6RKXn_FjMHjWh4p1CqrCe7Tmc9P3AFQA" // your Bot Token (Get from Botfather)
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Global variables
bool was_overheated;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
String chat_id = "1225422883";
int botRequestDelay = 1000;
// Sends log every 15 minutes.
// int botRequestDelay = 900000;
unsigned long lastTimeBotRan;
char ledState;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "...";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "...";
char pass[] = "...";
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
BlynkTimer timer;
void createLOG(){
// Send LOG
bot.sendMessage(chat_id, "Temperature: ", "");
String celsius = String((temprature_sens_read() - 32) / 1.8) + " C";
bot.sendMessage(chat_id, celsius, "");
bot.sendMessage(chat_id, "Light State: ", "");
if (digitalRead(RELAY_PIN)){
bot.sendMessage(chat_id, "LED is ON", "");
}
else{
bot.sendMessage(chat_id, "LED is OFF", "");
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
pinMode(RELAY_PIN, HIGH);
was_overheated = false;
Serial.begin(115200);
WiFi.begin(ssid, pass);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Blynk.begin("...", ssid, pass);
timer.setInterval(1000L, myTimerEvent);
}
void myTimerEvent()
{
// Conver raw temp in F to Celsius degrees
Blynk.virtualWrite(TEMP_PIN, (temprature_sens_read() - 32) / 1.8);
if(((temprature_sens_read() - 32) / 1.8) <= 85){
Blynk.virtualWrite(MSG_PIN, "NO MESSAGES");
if(was_overheated == true){
digitalWrite(RELAY_PIN, HIGH);
was_overheated = false;
}
}
if((((temprature_sens_read() - 32) / 1.8) > 85) && (was_overheated == false) ){
was_overheated = true;
digitalWrite(RELAY_PIN, LOW);
Blynk.virtualWrite(MSG_PIN, "OVERHEATED!!");
}
}
void loop(){
Blynk.run();
timer.run();
if (millis() > lastTimeBotRan + botRequestDelay) {
createLOG();
lastTimeBotRan = millis();
}
}