#include <Arduino.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <WiFiClientSecure.h>
#include <DNSServer.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#define debug_en 1
//#define threshold 1000
#define PRINTF_BUF 50
#define StartWordCount 32
#define reconnect_try 1 // How many times the wifi will try to reconenct
#define dooropen_thresshold 3 // not required as of now.
#define batt_sw 22
#define batt_sense_pin 32
#define BATT_VOLT_DIV_R1 10 //kohm
#define BATT_VOLT_DIV_R2 10 //kohm
#define BATT_VOLT_OFFSET 2.25906
#define BATT_AVG_COUNT 60
char auth[] = "##############"; //Sourav Token
//char auth[] = "##########"; //
int reconnect_count = 0;
boolean doorOPEN = false; // to figure out who woke up the board
boolean firealarm = false; // to figure out who woke up the board
boolean con_status = false; // to figure out connection establishment
WiFiManager wifiManager; // for provisioning
char door_State = 0;
int fire_State = 0;
unsigned long previousMillis = 0;
const long interval = 6000; // milli seconds, wait after detecting door is opened.
float voltage = 0; // Variable to keep track of LiPo voltage
void start_wifi_blynk(void);
void release_power(void);
void checkdoor(void);
void check_door_state(void);
void setup()
{
setCpuFrequencyMhz(60);
Serial.begin(115200);
start_wifi_blynk(); //Starting the WiFi Facility
Blynk.run();
Serial.println("Applciation Started . . . ");
}
void loop(){
}
void start_wifi_blynk(void)
{
wifiManager.setDebugOutput(false);
wifiManager.setTimeout(60); //1min, if can't get connected just go back to sleep
// WiFiManager wifiManager;
//wifiManager.startConfigPortal("dumadum-dum-dum");
Serial.println("connected...yeey :)");
Serial.println("WiFi Process started");
if (!wifiManager.autoConnect("dumadum-dum-dum"))
{ // SSID you connect to from browser
#if debug_en
Serial.println("failed to connect WiFiand hit timeout");
#endif
}
#if debug_en
Serial.println(""); // WiFi info
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
Blynk.config(auth);
//Blynk.run();
while (!(con_status = Blynk.connect()))
{
#if debug_en
Serial.println("connecting to Blynk Server");
Serial.println("Reconnecting try -");
#endif
reconnect_count++;
#if debug_en
Serial.println(reconnect_count);
#endif
if (reconnect_count == reconnect_try)
{
#if debug_en
Serial.println("Failed to connect Internet. Power is turing OFF");
Serial.println("Releasing Power");
#endif
}
}
#if debug_en
if (con_status)
{
Serial.println(" Blynk Connected!");
Blynk.notify("I am on...");
//Blynk.email("#########@gmail.com", "ESP32 Alert", "Connected");
}
#endif
}