My project just suddenly stopped working. Thinking I might have a bad ESP-32, I loaded a new one with my code. It still would not communicate. However the original processor and the new one, both functioned correctly with a different sketch loaded. I can’t seem to figure this one out. It used to work flawlessly.
#define BLYNK_TEMPLATE_ID "TMPL2c----------"
#define BLYNK_TEMPLATE_NAME "TEMPHUMECAR1"
// 09 IMPALA
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <BlynkSimpleEsp32.h>
#include <Update.h>
#include <HTTPClient.h>
#define BLYNK_AUTH_TOKEN "----------------------------------------"
#define BLYNK_PRINT Serial
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define USE_ESP32_DEV_MODULE
#define DHTTYPE DHT11
#define DHTPIN 27 //Sensor Green
#define BattIn 32
#define Starter 25
DHT dht(DHTPIN, DHTTYPE);
WiFiMulti wifiMulti;
char auth[] = BLYNK_AUTH_TOKEN;
String ssid = "yourNetwork";
float Mintemp = 110;
float Maxtemp = 0;
int Hyst = 200; //rain sensor hysteresis setting
int humidHyst = 5; // Humidity sensor Hysteresis
int ct; // counter variable
int ct1;
int ctAvg1;
float Vcc1;
float Vout1;
float Vin1;
float CorrectionFactor1=1.06;
float Vtot1;
float BATTIN;
float BATT1;
float BATTAVG1;
float Vavg1;
int Smoother=10;
int ALRM1;
int R1 = 67400; //volt div resistor
int R2 = 16530; //volt div resistor
int Div = 4095;
float humeCal = 1.0; // Humidity sensor calibration offset multiplier
float tempF = 0;
float tempCal = -9.5;
float tempSet = 0;
float hif;
// float humid =65;
// float tempSet = 50; //set temprature threshold in F
int ReBoot = 0;
int Booting = 0;
int START = 0;
int iled=0;
int dur=750; // delay in Ms
int connectTimeoutMs = 10000;
unsigned long cntNow = 0; // VARIABLES FOR Millis()
unsigned long cntInit = millis();
unsigned long cntInterv = 900000; // 15 min reboot
float TEMP = 0;
float HUMIDITY = 0;
float INDEX;
BLYNK_WRITE(V4)
{
tempCal = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V8)
{
START = param.asInt(); // Get value as integer
}
BLYNK_CONNECTED() { // when device is conneceted to Blynk Cloud...
Blynk.syncAll(); // request the values for all datastreams that has "sync" setting enabled
}
// This function creates the timer object. It's part of Blynk library
BlynkTimer timer;
void myTimer()
{
// This function describes what will happen with each timer tick
// e.g. writing sensor value to datastream V5
Vcc1 = analogRead(BattIn);
Vout1 = (Vcc1 * 3.3) / Div;
//R2 = R2+CorrectionFactor1;
Vin1 = Vout1 / (R2/float(R1+R2));
Vin1 = Vin1 * CorrectionFactor1;
BATT1 = Vin1;
if (ct1 < Smoother) {
Vtot1 = Vtot1+Vin1;
//BATT1 = Vtot1/ct1+1;
ct1=ct1+1;
}
else if (ct1==Smoother) {
ct1=0;
BATT1 = Vtot1/Smoother-1;
Vtot1=0;
Blynk.virtualWrite(V5, ALRM1);
}
Blynk.virtualWrite(V0, TEMP);
Blynk.virtualWrite(V1, HUMIDITY);
Blynk.virtualWrite(V3, WiFi.SSID()+" "+WiFi.RSSI());
Blynk.virtualWrite(V2, INDEX);
Blynk.virtualWrite(V7, BATT1);
// Blynk.virtualWrite(V9, iled);
Blynk.virtualWrite(V6, Booting);
}
String overTheAirURL = "";
BLYNK_WRITE(InternalPinOTA) {
overTheAirURL = param.asString();
HTTPClient http;
http.begin(overTheAirURL);
int httpCode = http.GET();
if (httpCode != HTTP_CODE_OK) {return;}
int contentLength = http.getSize();
if (contentLength <= 0) {return; }
bool canBegin = Update.begin(contentLength);
if (!canBegin) { return;}
Client& client = http.getStream();
int written = Update.writeStream(client);
if (written != contentLength) {return;}
if (!Update.end()) {return;}
if (!Update.isFinished()) {return;}
reboot();
}
void reboot()
{
#if defined(ARDUINO_ARCH_MEGAAVR)
wdt_enable(WDT_PERIOD_8CLK_gc);
#elif defined(__AVR__)
wdt_enable(WDTO_15MS);
#elif defined(__arm__)
NVIC_SystemReset();
#elif defined(ESP8266) || defined(ESP32)
{Booting = 1;
Blynk.virtualWrite(V6, Booting);
delay(1000);
Booting = 0;
Blynk.virtualWrite(V6, Booting);
delay(1000);
ESP.restart();}
#else
#error "MCU reset procedure not implemented"
#endif
for (;;) {}
}
void setup() { // open setup
pinMode(DHTPIN,INPUT); // DHT11 Data In
pinMode(32,INPUT_PULLDOWN); // Battery sense
pinMode(Starter, OUTPUT); //KeyFob Lock Button Press
digitalWrite (Starter, HIGH);
Serial.begin(115200);
delay(10);
wifiMulti.addAP("");
wifiMulti.addAP("");
wifiMulti.addAP("");
wifiMulti.addAP("");
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("Connected to : ");
Serial.println(WiFi.SSID());
ssid = WiFi.SSID();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else
{ Serial.println("");
Serial.print("Connected to : ");
Serial.println(ssid);}
if (ssid == "Truck Hotspot") {
timer.setInterval(30000L, myTimer);
} else {
// timer.setInterval(500L, myTimer);
}
Blynk.config(auth);
Blynk.connect(0);
dht.begin();
} //close setup
void impala() {
if(START == 1){
digitalWrite (Starter, LOW);
iled=1;
Blynk.virtualWrite(V9, iled);
delay(dur);
digitalWrite (Starter, HIGH);
iled=0;
Blynk.virtualWrite(V9, iled);
delay(dur);
digitalWrite (Starter, LOW);
iled=1;
Blynk.virtualWrite(V9, iled);
delay(dur);
digitalWrite (Starter, HIGH);
iled=0;
Blynk.virtualWrite(V9, iled);
delay(dur);
digitalWrite (Starter, LOW);
iled=1;
Blynk.virtualWrite(V9, iled);
delay(dur);
digitalWrite (Starter, HIGH);
iled=0;
Blynk.virtualWrite(V9, iled);
}
}
void update() {
// open test humidity/temp
int HUMIDITY = dht.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
float TEMP = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(HUMIDITY) || isnan(TEMP)) { // open check
Serial.println(F("Failed to read from DHT sensor!"));
HUMIDITY=0;
TEMP=0;
return;
}
// close check
}
void loop() { //open MAIN loop !!!!!!!!!!!!!!!!!!!!
Blynk.run();
timer.run();
//if the connection to the stongest hotspot is lost, it will connect to the next network on the list
if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {
Serial.print("WiFi connected: ");
Serial.print(WiFi.SSID());
Serial.print(" ");
Serial.println(WiFi.RSSI());
Serial.println("");
Serial.print("Vcc1 = ");
Serial.println(Vcc1);
Serial.print("Vout1 = ");
Serial.println(Vout1);
Serial.print("Vin1 = ");
Serial.println(Vin1);
Serial.print("BATT1 = ");
Serial.println(BATT1);
Serial.println();
delay(1000);
}
else {
Serial.println("WiFi not connected!");
}
TEMP = dht.readTemperature(true)+tempCal;
HUMIDITY = dht.readHumidity();
INDEX = dht.computeHeatIndex(TEMP,HUMIDITY);
if (BATT1 < 10.50) {
delay(2000);
if (BATT1 < 10.50) ALRM1 = 1;}
if (BATT1 > 10.70) ALRM1 = 0;
update();
impala();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Temp = ");
Serial.println(TEMP);
Serial.print("Humidity = ");
Serial.println(HUMIDITY);
Serial.println("");
Serial.print("Heat Index = ");
Serial.println(INDEX);
//delay(2000);
// if (millis() - cntInit >= cntInterv) reboot();
unsigned long currentMillis = millis();
}// close loop