Nodemcu Data logger stops suddenly all at a time

Using Nodemcu 1.0 with DHT22 sensors (multiples) with arduino sketch and buildup temperature, humidity data logger to Google Spreadsheets. Working fine enough with Pushing box Api. Surprisingly all 4 such modules stops one by one since April 9th, 20.
No data is coming to spread sheet, but built in LCD displays the data as usual.
Individually all local setup checked with hardware and power supply, found absolutely in order. Local Wifi (Used CDMA sims/ 4G) IS working. Checked the sketch, test Pushing box Api key and hard data pushing to script etc.
Can you suggest what could be the reason of failure at a time by all four. Is it by any chance related to covid 19 pandemic and server issue or change in internet setting etc. What could be other alternate choice.

Seeking an alternate views… i revised the Sketch with

  1. HTTPSrdirect, having uploaded the data logger directly to spreadsheet
  2. Due to interruption of proper internet connectivity data logging stops and whole system is hag out.
  3. i tried with serial software Restart… but is not acting when it failed to connect the internet. Although proper wifi is there

anybody can help, what to do

/*

  • ESP822 temprature logging to Google Sheet
  • CircuitDigest(www.circuitdigest.com)
    */
    #include <Wire.h>
    #include <ESP8266WiFi.h>
    #include “HTTPSRedirect.h”
    #include “DebugMacros.h”
    #include <DHT.h>

#define DHT1PIN 2 // D4 // what digital pin we’re connected to
#define DHT2PIN 12 //D6
#define DHTTYPE DHT11 // select dht type as DHT 11 or DHT22
DHT dht1(DHT1PIN, DHT11,15);
DHT dht2(DHT2PIN, DHT11,15);

float h1 = 0.0;
float t1 = 0.0;
float h2 = 0.0;
float t2 = 0.0;

int cnt = 10;
String sheetHumid1 = “”;
String sheetTemp1 = “”;
String sheetHumid2 = “”;
String sheetTemp2 = “”;

//String timestamp = “”;

//const char* ssid = “office use only”; //replace with our wifi ssid
//const char* password = “office@4321”; //replace with your wifi password
//const char* ssid = “JioFi3_1215B2”; //replace with our wifi ssid
//const char* password = “vh2cj27kk6”;
const char* ssid = “vivo 1901”;
const char* password = “subha2702”;
//const char* ssid = “JioFi2_D2266A”;
//const char* password = “5k3qmhtkhg”;

const char* host = “script.google.com”;
const char *GScriptId = “AKfycbwtSjL_5EUJxQZ1dDKkCfamNl8MTOfsz7DEiSfulKItN60ltOY”; // Replace with your own google script id
const int httpsPort = 443; //the https port is same

// echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
const char* fingerprint = “F7:D5:03:00:68:BB:08:D5:AF:C3:EB:AA:5E:7E:A8:9B:07:BF:78:B8”;
//const char* fingerprint = “”;
//const uint8_t fingerprint[20] = {};

String url = String("/macros/s/") + GScriptId + “/exec?value=Temperature1”; // Write Teperature to Google Spreadsheet at cell A1
// Fetch Google Calendar events for 1 week ahead
String url2 = String("/macros/s/") + GScriptId + “/exec?cal”; // Write to Cell A continuosly

//replace with sheet name not with spreadsheet file name taken from google
String payload_base = "{“command”: “appendRow”,
“sheet_name”: “TempSheet”,
“values”: ";
String payload = “”;

HTTPSRedirect* client = nullptr;

// used to store the values of free stack and heap before the HTTPSRedirect object is instantiated
// so that they can be written to Google sheets upon instantiation

void setup() {
delay(30000);

dht1.begin(); //initialise DHT11
dht2.begin();
pinMode(0, OUTPUT); // Rood

Serial.begin(115200);
Serial.println();
Serial.print("Connecting to wifi: ");
Serial.println(ssid);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

// Use HTTPSRedirect class to create a new TLS connection
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
client->setPrintResponseBody(true);
client->setContentTypeHeader(“application/json”);
Serial.print("Connecting to ");
Serial.println(host); //try to connect with “script.google.com

// Try to connect for a maximum of 5 times then exit
bool flag = false;
for (int i = 0; i < 5; i++) {
int retval = client->connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println(“Connection failed. Retrying…”);
}

if (!flag) {
Serial.print("Could not connect to server: ");
Serial.println(host);
Serial.println(“Exiting…”);
return;
}
// Finish setup() function in 1s since it will fire watchdog timer and will reset the chip.
//So avoid too many requests in setup()

Serial.println("\nWrite into cell ‘A1’");
Serial.println("------>");
// fetch spreadsheet data
client->GET(url, host);

Serial.println("\nGET: Fetch Google Calendar Data:");
Serial.println("------>");
// fetch spreadsheet data
client->GET(url2, host);

Serial.println("\nStart Sending Sensor Data to Google Spreadsheet");

// delete HTTPSRedirect object
delete client;
client = nullptr;
Serial.println("");
}
void ReadDHT11 () {
h1 = dht1.readHumidity ();
t1 = dht1.readTemperature (true);
h2 = dht2.readHumidity ();
t2 = dht2.readTemperature (true);
}
void loop()
{
Serial.println(cnt);
if(cnt == 0){
Serial.println(“Resetting ESP”);
ESP.restart();
}
cnt–;
ReadDHT11 ();
if (h1 == 0 && t1 == 0 && h2 == 0 && t2 == 0)
delay (10000);

float h1 = dht1.readHumidity(); // Reading temperature or humidity takes about 250 milliseconds!
float t1 = dht1.readTemperature(true); // Read temperature as Celsius (the default)
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature(true);

if (isnan(h1) || isnan(t1) || isnan(h2) || isnan(t2)) { // Check if any reads failed and exit early (to try again).
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
Serial.print(“Humidity1: “); Serial.print(h1);
sheetHumid1 = String(h1) + String(”%”); //convert integer humidity to string humidity
Serial.print(“Humidity2: “); Serial.print(h2);
sheetHumid2 = String(h2) + String(”%”);

Serial.print("% Temperature1: "); Serial.print(t1); Serial.println("° F “);
sheetTemp1 = String(t1) + String(“° F”);
Serial.print(”% Temperature2: "); Serial.print(t2); Serial.println("° F ");
sheetTemp2 = String(t2) + String(“° F”);

static int error_count = 0;
static int connect_count = 0;
const unsigned int MAX_CONNECT = 20;
static bool flag = false;

// payload = payload_base + “”" + timestamp+"," + sheetTemp + “,” + sheetHumid + “”}";
payload = payload_base + “”" + sheetTemp1 + “,” + sheetHumid1 + “,” + sheetTemp2 + “,” + sheetHumid2 + “”}";
if (!flag) {
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader(“application/json”);
}

if (client != nullptr) {
if (!client->connected()) {
client->connect(host, httpsPort);
client->POST(url2, host, payload, false);
Serial.print("Sent : "); Serial.println(“Temp and Humid”);
}
}
else {
DPRINTLN(“Error creating client object!”);
error_count = 5;
}

if (connect_count > MAX_CONNECT) {
connect_count = 0;
flag = false;
delete client;
return;
}

// Serial.println(“GET Data from cell ‘A1’:”);
// if (client->GET(url3, host)) {
// ++connect_count;
// }
// else {
// ++error_count;
// DPRINT("Error-count while connecting: ");
// DPRINTLN(error_count);
// }

Serial.println(“POST or SEND Sensor data to Google Spreadsheet:”);
if (client->POST(url2, host, payload)) {
;
}
else {
++error_count;
DPRINT("Error-count while connecting: ");
DPRINTLN(error_count);
}

if (error_count > 10) {
Serial.println(“Halting processor…”);
delete client;
client = nullptr;
Serial.printf(“Final free heap: %u\n”, ESP.getFreeHeap());
Serial.printf(“Final stack: %u\n”, ESP.getFreeContStack());
Serial.flush();
ESP.deepSleep(0);

}

delay (6000);
}

Your topic was Moved to it’s current location / section as it is more suitable.

Please use code tags for sketches and error messages ( </> )

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Thanks for your valued advice. i am new to this forum.

When ever there is lack of signal or no internet available, systems holds back, not retrieved automatically.
Manual hard reset is only solution to get back the same.

it was working very fine for last 1 year with API PushingBox and google Spreadsheet data logger combination. But may be with some reason all are stopped at a time.

hence New Code is done as below.

I really need to know, where i am missing? plz help.

My Present code

#include <Wire.h>
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include "DebugMacros.h"
#include <DHT.h>

#define DHT1PIN 2 // D4                                                           // what digital pin we're connected to
#define DHT2PIN 12 //D6
#define DHTTYPE DHT11                                                       // select dht type as DHT 11 or DHT22
DHT dht1(DHT1PIN, DHT11,15);
DHT dht2(DHT2PIN, DHT11,15);

float h1 = 0.0;
float t1 = 0.0;
float h2 = 0.0;
float t2 = 0.0;


String sheetHumid1 = "";
String sheetTemp1 = "";
String sheetHumid2 = "";
String sheetTemp2 = "";

//String timestamp = "";

//const char* ssid = "office use only";                //replace with our wifi ssid
//const char* password = "office@4321";         //replace with your wifi password
//const char* ssid = "JioFi3_1215B2";                //replace with our wifi ssid
//const char* password = "vh2cj27kk6";
const char* ssid = "vivo 1901";
const char* password = "subha2702";
//const char* ssid = "JioFi2_D2266A";
//const char* password =  "5k3qmhtkhg";

const char* host = "script.google.com";
const char *GScriptId = "AKfycbwtSjL_5EUJxQZ1dDKkCfamNl8MTOfsz7DEiSfulKItN60ltOY"; // Replace with your own google script id
const int httpsPort = 443; //the https port is same

// echo | openssl s_client -connect script.google.com:443 |& openssl x509 -fingerprint -noout
const char* fingerprint = "F7:D5:03:00:68:BB:08:D5:AF:C3:EB:AA:5E:7E:A8:9B:07:BF:78:B8";
//const char* fingerprint = "";
//const uint8_t fingerprint[20] = {};

String url = String("/macros/s/") + GScriptId + "/exec?value=Temperature1";  // Write Teperature to Google Spreadsheet at cell A1
// Fetch Google Calendar events for 1 week ahead
String url2 = String("/macros/s/") + GScriptId + "/exec?cal";  // Write to Cell A continuosly

//replace with sheet name not with spreadsheet file name taken from google
String payload_base =  "{\"command\": \"appendRow\", \
                    \"sheet_name\": \"TempSheet\", \
                       \"values\": ";
String payload = "";

HTTPSRedirect* client = nullptr;

// used to store the values of free stack and heap before the HTTPSRedirect object is instantiated
// so that they can be written to Google sheets upon instantiation

void setup() {
  delay(30000);
 
  dht1.begin();     //initialise DHT11
  dht2.begin();
  pinMode(0, OUTPUT); // Rood
 
 
  Serial.begin(115200);
  Serial.println();
  Serial.print("Connecting to wifi: ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Use HTTPSRedirect class to create a new TLS connection
  client = new HTTPSRedirect(httpsPort);
  client->setInsecure();
  client->setPrintResponseBody(true);
  client->setContentTypeHeader("application/json");
  Serial.print("Connecting to ");
  Serial.println(host);          //try to connect with "script.google.com"

  // Try to connect for a maximum of 5 times then exit
  bool flag = false;
  for (int i = 0; i < 5; i++) {
    int retval = client->connect(host, httpsPort);
    if (retval == 1) {
      flag = true;
      break;
    }
    else
      Serial.println("Connection failed. Retrying...");
  }

  if (!flag) {
    Serial.print("Could not connect to server: ");
    Serial.println(host);
    Serial.println("Exiting...");
    return;
  }
// Finish setup() function in 1s since it will fire watchdog timer and will reset the chip.
//So avoid too many requests in setup()

  Serial.println("\nWrite into cell 'A1'");
  Serial.println("------>");
  // fetch spreadsheet data
  client->GET(url, host);
 
  Serial.println("\nGET: Fetch Google Calendar Data:");
  Serial.println("------>");
  // fetch spreadsheet data
  client->GET(url2, host);

 Serial.println("\nStart Sending Sensor Data to Google Spreadsheet");

 
  // delete HTTPSRedirect object
  delete client;
  client = nullptr;
  Serial.println("");
}
void ReadDHT11 () {
  h1 = dht1.readHumidity ();
  t1 = dht1.readTemperature (true);
  h2 = dht2.readHumidity ();
  t2 = dht2.readTemperature (true);
}
void loop(){
  
 ReadDHT11 ();
 if (h1 == 0 && t1 == 0 && h2 == 0 && t2 == 0)
 delay (10000);
 
 float h1 = dht1.readHumidity();                                              // Reading temperature or humidity takes about 250 milliseconds!
 float t1 = dht1.readTemperature(true);                                           // Read temperature as Celsius (the default)
 float h2 = dht2.readHumidity();
 float t2 = dht2.readTemperature(true);
 
  if (isnan(h1) || isnan(t1) || isnan(h2) || isnan(t2)) {                                                // Check if any reads failed and exit early (to try again).
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print("Humidity1: ");  Serial.print(h1);
  sheetHumid1 = String(h1) + String("%");                                         //convert integer humidity to string humidity
  Serial.print("Humidity2: ");  Serial.print(h2);
  sheetHumid2 = String(h2) + String("%");
 
  Serial.print("%  Temperature1: ");  Serial.print(t1);  Serial.println("° F ");
  sheetTemp1 = String(t1) + String("° F");
  Serial.print("%  Temperature2: ");  Serial.print(t2);  Serial.println("° F ");
  sheetTemp2 = String(t2) + String("° F");
 
 
  static int error_count = 0;
  static int connect_count = 0;
  const unsigned int MAX_CONNECT = 20;
  static bool flag = false;

 // payload = payload_base + "\"" + timestamp+"," + sheetTemp + "," + sheetHumid + "\"}";
    payload = payload_base + "\"" + sheetTemp1 + "," + sheetHumid1 + "," + sheetTemp2 + "," + sheetHumid2 + "\"}";
  if (!flag) {
    client = new HTTPSRedirect(httpsPort);
    client->setInsecure();
    flag = true;
    client->setPrintResponseBody(true);
    client->setContentTypeHeader("application/json");
  }

  if (client != nullptr) {
    if (!client->connected()) {
      client->connect(host, httpsPort);
      client->POST(url2, host, payload, false);
      Serial.print("Sent : ");  Serial.println("Temp and Humid");
    }
  }
  else {
    DPRINTLN("Error creating client object!");
    error_count = 5;
  }

  if (connect_count > MAX_CONNECT) {
    connect_count = 0;
    flag = false;
    delete client;
    return;
  }

//  Serial.println("GET Data from cell 'A1':");
//  if (client->GET(url3, host)) {
//    ++connect_count;
//  }
//  else {
//    ++error_count;
//    DPRINT("Error-count while connecting: ");
//    DPRINTLN(error_count);
//  }

  Serial.println("POST or SEND Sensor data to Google Spreadsheet:");
  if (client->POST(url2, host, payload)) {
    ;
  }
  else {
    ++error_count;
    DPRINT("Error-count while connecting: ");
    DPRINTLN(error_count);
  }

  if (error_count > 10) {
    Serial.println("Halting processor...");
    delete client;
    client = nullptr;
    Serial.printf("Final free heap: %u\n", ESP.getFreeHeap());
    Serial.printf("Final stack: %u\n", ESP.getFreeContStack());
    Serial.flush();
    ESP.deepSleep(0);

 
    }     
 
  delay (6000);
}