My program will use an array defined as a "structure."
I was able to fill it in in a test mode, but when I try to put a value to any element the program will compile, but the operation finds an "Exception 3" and resets.
I've deleted most of the code for simplicity.
The part that gives the trouble is in the "loop" where I have reduced things to two "for" loops - One to define the strings CDC[j] and CDT[j] and a second one to fill in some values. (In the final version real values will be filled in. For test purposes I have simply used the index j .
By now I've tried many variations on this, and seem to have located the problem - but I don't understand why it is a problem, nor what I'm doing wrong.
Any help will be appreciated.
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include "DebugMacros.h"
float x ;
float h;
float t;
String sheetHumid = "";
String sheetTemp = "";
String sheetXXX = "";
String sheetYYY = "";
String sheetZZZ = "";
String datastring = "";
const char* ssid = "A47FB0"; //replace with our wifi ssid
const char* password = "96880104"; //replace with your wifi password
const char* host = "script.google.com";
//const char *GScriptId = "1wF7PdOpxmfXFZWarJS7847TOcYhkSF9iVGZkqhVQMmU"; // Replace with your own google script id
// this worked: const char *GScriptId = "AKfycbzPAOKrYAOrmFpmyCqZ9d8u984JmOpZkS70c_iiZA2F3UDZqwyK"; // Replace with your own google script id
// AKfycbzPAOKrYAOrmFpmyCqZ9d8u984JmOpZkS70c_iiZA2F3UDZqwyK
const char *GScriptId = "AKfycbyb1uS4foQKRAhde3MVJJ_GZIqv-zNrTejmUngZVzk6EYfw8CI"; // Replace with your own google script id
//https://script.google.com/macros/s/AKfycbyb1uS4foQKRAhde3MVJJ_GZIqv-zNrTejmUngZVzk6EYfw8CI/exec
// AKfycbyb1uS4foQKRAhde3MVJJ_GZIqv-zNrTejmUngZVzk6EYfw8CI
//https://script.google.com/macros/s/AKfycbzPAOKrYAOrmFpmyCqZ9d8u984JmOpZkS70c_iiZA2F3UDZqwyK/exec
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 = "";
//const uint8_t fingerprint[20] = {};
String url = String("/macros/s/") + GScriptId + "/exec?value=Steri."; // 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
// String url2 = String("/macros/s/") + GScriptId + "/exec?value=Temp=xx=-re"; // Write to Cell A continuosly
// the above change made NO DIFFERENCE!!??
// What writes "Humidity" into B1 ?? This is done in the webapp.
//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
////////////////////////////
// Before setup:
// ng enum State : byte { 0, 1, 2 };
//enum State : byte { WU, ST, CD };
//----- Here we set up variables for making up the payload to be used later in the loop ---
int j ;
struct Celldata {
uint8_t temp; // this will be the actual temperature minus 150 degrees, and will ALWAYS be between 0 and 255.
byte cyc;
byte extra;
} // End of struct Celldata
Celldata[15]; // for now, try with only 15 slots. Eventually 60.
// String adder = ""; //
// -------------- end of payload variables setup
//
void setup() {
delay(100);
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 1 second 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;
} // End of setup
void loop() {
delay(1000);
sheetXXX = "mcb XXX mcb";
sheetYYY = "mcb YYY mcb";
sheetZZZ = "mcb ZZX mcb";
static int error_count = 0;
static int connect_count = 0;
const unsigned int MAX_CONNECT = 20;
static bool flag = false;
for ( int j = 0 ; j <=7 ; j++) { // Celldata was defined for 15 cells for now
// ----------------------------
String CDT[j];
String CDC[j];
// this causes error 3 - then resets CDT[j] = j; // mcb 11:34 pm "sss"; - This causes error 3.
/// ----------------------
Serial.print(" ");
Serial.print(j);
Serial.print(" = ");
CDC[j] = j; // >> Error Exception 3
// Serial.println(CDC[j]);
} // end of "for" loop - puitting something into each cerll.
Serial.println(); // to end line from above.
for ( int j = 0 ; j <=5 ; j++) { // Celldata was defined for 15 cells for now
// Serial.println("In For loop");
Serial.print("Data so far. j= ");
Serial.print(j);
Serial.print(" CDC= ");
// CDC[j] = j;
//Serial.print(CDC[j]); // error CDC not defined in this scope
Serial.print(" ");
Serial.println();
} // end of for
Serial.println("End of 2d for ");
payload = payload_base + "\"" + sheetTemp + "," + sheetXXX + "\"}";
if (!flag) {
client = new HTTPSRedirect(httpsPort);
client->setInsecure();
flag = true;
client->setPrintResponseBody(true);
client->setContentTypeHeader("application/json");
} // End of if ??
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 {
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;
}
if (error_count > 3) {
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(3000); // keep delay of minimum 2 seconds as dht allow reading after 2 seconds interval and also for google sheet
} // loopend - I think!