Hello, I decide to share my source code, where time to time nodemcu hangs /freeze. It is simple program, but as I wrote in previous WDT does not work for me. Any help is welcome.
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <Wire.h>
#include “HTTPSRedirect.h”
#include “DHT.h”
#include “BMP085.h”
#define DHTPIN 3
#define DHTTYPE 22
#define BACK_BTN 5
#define MENU_BTN 4
#define OK_BTN 12
#define UP_BTN 2
#define DOWN_BTN 14
#define OSWATCH_RESET_TIME 120
#define SECONDS 1000000
#define SLEEPTIME 100
const int lowTemperature = 5;
const int highTemperature = 25;
const float stepTemperature = 0.5;
const float adc_coef = 6.27;
const float adc_div = 1024;
const float adc_off = 0.2;
const int httpsPort = 443;
const int doneInt = 1;
const char* ssid = “WIFI”;
const char* password = “PASS”;
const char* host = “http://script.google.com”;
const char* googleRedirHost = “http://script.googleusercontent.com”;
const char gScriptId = “googleFileID”;
const char fingerprint = “google FinderPrint”;
String carryUrl = String(“/macros/s/”) + gScriptId + “/exec?value=”;
String queryUrl = String(“/macros/s/”) + gScriptId + “/exec?read”;
static unsigned long last_loop;
int doneSettings = doneInt;
int sleepInterval = 900;
int stateThermostat = 0;
int actualRow = 0;
float setTemperature = 23.5;
float actTemperature = 23.5;
BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
Ticker tickerOSWatch;
void setup() {
setDigitalState(0);
last_loop = millis();
tickerOSWatch.attach_ms(((OSWATCH_RESET_TIME / 3) * 1000), osWatch);
dht.begin();
bmp.begin();
Serial.begin(9600);
Serial.println(“”);
Serial.print("Pripojuji k: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(“.”);
}
Serial.println(" WiFi pripojeno");
Serial.print(“IP adresa: “);
Serial.println(WiFi.localIP());
Serial.println(”-----------------------”);
}
void loop() {
String response = “”;
String parameter = formatParameter();
last_loop = millis();
HTTPSRedirect client(httpsPort);
Serial.print("Pripojuji se k serveru (queryUrl): ");
Serial.println(host);
for (int i=0; i<5; i++){
int retval = client.connect(host, httpsPort);
if (retval == 1) {
break;
}else
Serial.println(“Pripojení selhalo.”);
ESP.deepSleep(sleepInterval * SECONDS, WAKE_NO_RFCAL); // WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED
delay(2000);
//ESP.reset();
return;
}
response = client.printRedir(queryUrl, host, googleRedirHost);
formatResponse(response, 14);
if (doneSettings == 0){
if (stateThermostat == 1){
if (actTemperature <= setTemperature){
//wakeUp();
setMenu();
startHeating(false);
setTemporaryChange(setTemperature-0.01);
stateThermostat = 0;
doneSettings = doneInt;
}else if (actTemperature > setTemperature){
//wakeUp();
setMenu();
startHeating(true);
stateThermostat = 1;
doneSettings = doneInt;
}
}else if (stateThermostat == 0){
//wakeUp();
setMenu();
stopHeating();
stateThermostat = 0;
doneSettings = doneInt;
}
}
for (int i=0; i<5; i++){
int retval = client.connect(host, httpsPort);
if (retval == 1) {
break;
}else
Serial.println(“Pripojení selhalo.”);
ESP.deepSleep(sleepInterval * SECONDS, WAKE_NO_RFCAL); // WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED
delay(2000);
//ESP.reset();
return;
}
client.printRedir(carryUrl + parameter, host, googleRedirHost);
Serial.println("Response: " + response);
Serial.println("Parameter: " + parameter);
ESP.deepSleep(sleepInterval * SECONDS, WAKE_NO_RFCAL); // WAKE_RF_DEFAULT, WAKE_RFCAL, WAKE_NO_RFCAL, WAKE_RF_DISABLED
delay(2000);
}
String formatParameter(){
float t_dht = dht.readTemperature();
float h_dht = dht.readHumidity();
float t_bmp = bmp.readTemperature();
float p_bmp = bmp.readPressure()/100;
float v_adc = ((analogRead(A0)*adc_coef)/adc_div)+adc_off;
float t_avg = (t_dht + t_bmp)/2;
actTemperature = t_avg;
String temperature = String(t_avg);
String humidity = String(h_dht);
String pressure = String(p_bmp);
String voltage = String(v_adc);
String done = String(doneInt);
temperature.replace(“.”,“,”);
humidity.replace(“.”,“,”);
pressure.replace(“.”,“,”);
voltage.replace(“.”,“,”);
return “t” + temperature + “h” + humidity + “p” + pressure + “v” + voltage + “d” + done;
}
void formatResponse(String response, int startIndex){
String t_resp = “t”;
String s_resp = “s”;
String r_resp = “r”;
String i_resp = “i”;
String d_resp = “d”;
String tempString = “”;
String resp_t = “”;
String resp_s = “”;
String resp_r = “”;
String resp_i = “”;
String resp_d = “”;
tempString = response.substring(startIndex-1);
resp_t = tempString.substring((tempString.indexOf(t_resp)+1), tempString.indexOf(s_resp));
setTemperature = resp_t.toFloat();
resp_s = tempString.substring((tempString.indexOf(s_resp)+1), tempString.indexOf(r_resp));
stateThermostat = resp_s.toInt();
resp_r = tempString.substring((tempString.indexOf(r_resp)+1), tempString.indexOf(i_resp));
actualRow = resp_r.toInt();
resp_i = tempString.substring((tempString.indexOf(i_resp)+1), tempString.indexOf(d_resp));
sleepInterval = resp_i.toInt();
resp_d = tempString.substring((tempString.indexOf(d_resp)+1));
doneSettings = resp_d.toInt();
}
void wakeUp(){
pressButton(BACK_BTN, 0);
}
void setMainScreen(){
pressButton(MENU_BTN, 1);
}
void setMainScreenWithConfirm(boolean confirm){
pressButton(MENU_BTN, 0);
pressButton(DOWN_BTN, 0);
pressButton(OK_BTN, 0);
}
void setMenu(){
pressButton(MENU_BTN, 0);
}
void startHeating(boolean onlyStart){
pressButton(OK_BTN, 4);
if (onlyStart == true) {
pressButton(MENU_BTN, 0);
}
}
void stopHeating(){
pressButton(OK_BTN, 2);
pressButton(DOWN_BTN, 0);
pressButton(OK_BTN, 0);
pressButton(MENU_BTN, 0);
}
void setTemporaryChange(float temperature){
int steps = 0;
steps = nrOfPress(temperature);
pressButton(DOWN_BTN, 2);
pressButton(OK_BTN, 0);
pressButton(DOWN_BTN, ((((highTemperature-lowTemperature) / stepTemperature))3)-1);
pressButton(UP_BTN, ((3steps)-1));
pressButton(OK_BTN, 0);
pressButton(MENU_BTN, 0);
}
int nrOfPress(float reqTemperature){
int steps;
steps = (reqTemperature - lowTemperature)/stepTemperature;
return steps;
}
void holdButton(int button, int duration){
digitalWrite(button, HIGH);
delay((duration*1000));
digitalWrite(button, LOW);
}
boolean pressButton(int button, int nrOfPress) {
boolean done = false;
for (int i = 0; i <= nrOfPress; i++){
digitalWrite(button, HIGH);
delay(150);
digitalWrite(button, LOW);
delay(150);
}
done = true;
return done;
}
void ICACHE_RAM_ATTR osWatch(void) {
unsigned long t = millis();
unsigned long last_run = abs(t - last_loop);
if(last_run >= (OSWATCH_RESET_TIME * 1000)) {
ESP.restart();
//ESP.reset();
}
}
void setDigitalState(byte tristate){
switch (tristate){
case 0:
pinMode(BACK_BTN, OUTPUT);
digitalWrite(BACK_BTN, LOW);
delay(5);
pinMode(MENU_BTN, OUTPUT);
digitalWrite(MENU_BTN, LOW);
delay(5);
pinMode(OK_BTN, OUTPUT);
digitalWrite(OK_BTN, LOW);
delay(5);
pinMode(UP_BTN, OUTPUT);
digitalWrite(UP_BTN, LOW);
delay(5);
pinMode(DOWN_BTN, OUTPUT);
digitalWrite(DOWN_BTN, LOW);
delay(5);
break;
case 1:
pinMode(BACK_BTN, OUTPUT);
digitalWrite(BACK_BTN, HIGH);
delay(5);
pinMode(MENU_BTN, OUTPUT);
digitalWrite(MENU_BTN, HIGH);
delay(5);
pinMode(OK_BTN, OUTPUT);
digitalWrite(OK_BTN, HIGH);
delay(5);
pinMode(UP_BTN, OUTPUT);
digitalWrite(UP_BTN, HIGH);
delay(5);
pinMode(DOWN_BTN, OUTPUT);
digitalWrite(DOWN_BTN, HIGH);
delay(5);
break;
case 2:
pinMode(BACK_BTN, INPUT);
delay(5);
pinMode(MENU_BTN, INPUT);
delay(5);
pinMode(OK_BTN, INPUT);
delay(5);
pinMode(UP_BTN, INPUT);
delay(5);
pinMode(DOWN_BTN, INPUT);
delay(5);
break;
}
}