When I run my code with manual authentication, it is able to connect to Blynk, however immediately after it enters INIT => ERROR and forces a restart. When I run my code on dynamic authentication, it will get stuck on WAIT_CONFIG and cannot cast as an AP. I am running my code on an ESP32 on a TrigBoard.
// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "TMPL9i2N_Ai_"
#define BLYNK_DEVICE_NAME "Test Device TrigBoard Manual"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
char auth[] = "***";
char ssid[] = "***";
char pass[] = "***";
// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
#include "BlynkEdgent.h"
#include <Ultrasonic.h>
// Deep Sleep functionality
// sleep time in seconds
long DEEP_SLEEP_TIME = 3600; //1 hour
#define MAX_SLEEPNUM 2 //number of sleeps
RTC_DATA_ATTR unsigned int count = 0;
RTC_DATA_ATTR unsigned int first = 0;
//firmware specific globals
//GPIO 5 => D1
Ultrasonic ultrasonic(33);
float batteryThreshold = 3.5;
bool send = true;
int InitVal = 0;
double Height = 0.0; //controlled by V6
double threshold = 0; //controlled by V7
int updateMode = 0; //controlled by V1
void setup()
{
Serial.begin(74880);
delay(100);
//check sleep count before connection
Serial.println("Woke up! Testing Sleep...");
// check if it is first time start up
if(count <MAX_SLEEPNUM){
count++;
Serial.println(count);
goToSleep();
}
else{
count = 0;
}
Serial.println("=== Initiating Blynk Connection ===");
Blynk.begin(auth, ssid, pass);
//BlynkEdgent.begin();
while(!Blynk.connected()){}
}
/*
* Activates once Blynk is connected
* The function is blocking and will not let other operations continue like saving credentials to memory
*/
BLYNK_CONNECTED(){
//maintains widget values after device reset or shutoff
Serial.println("=== Blynk Connected ===");
Blynk.syncVirtual(V6,V7,V1,V9);
}
//OTA switch
BLYNK_WRITE(V1){
updateMode = param.asInt();
if(updateMode == 0){
Blynk.syncVirtual(V9);
}
Serial.println("Im in V1");
}
//Tank Height
BLYNK_WRITE(V6){
Height = param.asInt();
Serial.println("Im in V6");
}
//Tank Fill Percentage
BLYNK_WRITE(V7){
InitVal = param.asInt();
threshold = Height - (((InitVal/25.5)/10.0)*Height);
Blynk.virtualWrite(V8, threshold);
Serial.println("Im in V7");
}
//Disconnect Button
//Takes reading after all the checks
BLYNK_WRITE(V9){
if(param.asInt()){
Blynk.disconnect();
}
else{
Serial.println("Im in V9");
timedSend();
}
}
/*
* Initiates deep sleep based on defined sleep time
*/
void goToSleep(){
Serial.println("Going to Sleep");
esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME*1000000);
esp_deep_sleep_start();
delay(10);
}
/*
* Initiates ultrasonic reading.
* Uses virtual pin values to decide if barrel is low and to send a notification.
*/
void timedSend(){
Serial.println("Checking Height...");
delay(20);
long distcm = ultrasonic.MeasureInCentimeters();
Serial.println(distcm);
//send current reading to app
Blynk.virtualWrite(V5, distcm);
//has the threshold not been set?
if(threshold == 0){
Blynk.logEvent("advnotif", String("Please Enter a tank height and notification percentage"));
}
//is the reading above the threshold?
else if(distcm >= threshold){
if(send){
Blynk.logEvent("advnotif", String("Barrel is low level"));
//prevents from sending multiple of the same notification...
send = false;
}
}
//is the reading below the threshold?
else if(distcm < threshold){
send = true;
}
//check if OTA is active
Blynk.syncVirtual(V1);
Serial.println(updateMode);
if(!updateMode){
Blynk.disconnect();
goToSleep();
delay(10);
}
else{
Serial.println("=== OTA mode active ===");
//reset the sleep numbers
count = 0;
}
}
void loop() {
BlynkEdgent.run();
}