So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By MajorBoredom
#88238 Hello All,

I've been developing a sensor unit to take environmental readings and publish them to a Google Sheet.
It had been working, then I had an issue with the Arduino IDE and reinstalled it and the libraries.

Now the Google Sheets connection part throws an exception (28) and the board resets.

(On a side note is there any harm in letting a board reset this way repeatedly? Could it corrupt something and render the board useless?)

Board: NodeMCU 1.0 (ESP-12 module)
SSL: All / Basic ciphers
Frequency: 80 / 160MHz

Thanks for any guidance.

Code (simplified down to isolate error):
Code: Select all#define DEBUG

//wifi
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"

const char* ssid = "#####";
const char* password = "#####";
const char *ssidAP = "####";
const char *passwordAP = "####";

//gscript
const char* target = "script.google.com";
const char* GScriptId = "####################";
const int httpsPort = 443;

String url = String("/macros/s/") + GScriptId + "/exec?value=Temperature"; 
String url2 = String("/macros/s/") + GScriptId + "/exec";

String payload_base =  "{\"command\": \"appendRow\", \"sheet_name\": \"DataSheet\", \"values\": ";
String payload = "";

HTTPSRedirect* client = nullptr;
bool error = false;

void setup() {
  Serial.begin(115200);
  while(!Serial);
  delay(3000);
  Serial.flush();
  Serial.println(F("------------    Google Sheet Upload Test    --------------"));
 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  delay(1000);

}

void loop() {
  for (int n=1; n<11; n++) {
    delay(1000);
    Serial.printf("%d seconds \n", n);
  }
  storeValues();
}

float temperature = 0;
float humidity = 0;
float pressure = 0;
float battery = 0;

void storeValues() {
    Serial.println(F("Sending..."));
    delete client;
    client = new HTTPSRedirect(httpsPort);
    client->setInsecure();
    client->setPrintResponseBody(true);
    client->setContentTypeHeader("application/json");
    Serial.print(F("Connecting to "));
    Serial.println(target + url2);
    bool flag = false;
    int errorcount = 0;
    if (!flag) {
      for (int i = 0; i < 5; i++) {
        int retval = client->connect(target, httpsPort);
        if (retval == 1) {
            flag = true;
            break;
        } else {
            Serial.print(F("..."));
        }
      }
    }
    if (flag) {
        payload = payload_base + "\"," + temperature + "," + humidity + "," + pressure + "," + battery + "\"}";
        Serial.println(payload);
        if (client->POST(url2, target, payload, false)) {
            errorcount = 0;
            Serial.println(F("Sent!"));
        } else {
            errorcount++;
            if (errorcount > 3) {
                //Serial.println(F("Could not connect to host. Halting storage of values."));
                Serial.print(F("Stopping"));
                error = true;
                delete client;
                client = nullptr;
            } else {
                //Serial.print(F("Attempt "));
                //Serial.print(errorcount);
                //Serial.println(F(". Error connecting to host....Retrying")); 
                Serial.print(F("Fail"));
            }
        }
    }
}


Serial output (with DEBUG):
Code: Select allDK:2.2.2-dev(38a443e)/Core:2.7.2-7-g5d3af165=20702007/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-30-g92add50/BearSSL:5c771be
wifi evt: 2
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 5
cnt

connected with VM9347349_EXT, channel 11
dhcp client start...
wifi evt: 0
ip:192.168.0.103,mask:255.255.255.0,gw:192.168.0.1
wifi evt: 3
------------    Google Sheet Upload Test    --------------
....scandone
.Sending...
Connecting to script.google.com/macros/s/AKfycbyz8lBtcwLavg7U7RsSO5UuBGGsz6RVSGMTVlFEFbYQVH-pF2M/exec
[hostByName] request IP for: script.google.com
[hostByName] Host: script.google.com IP: 172.217.20.142
:ref 1
BSSL:_connectSSL: start connection
Fatal exception 28(LoadProhibitedCause):
epc1=0x40209bf0, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00300030, depc=0x00000000

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (28):
epc1=0x40209bf0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00300030 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffc40 end: 3fffffc0 offset: 0190
3ffffdd0:  3ffffef0 3ffee88c 3fff01c4 4020452d 
3ffffde0:  3ffe0000 00000000 0000000a 40100c21 
3ffffdf0:  00000000 00000000 3ffeee68 4021b88c 
3ffffe00:  00000c30 3ffee99c 3fff1de8 40100bd2 
3ffffe10:  00000014 3fff1cac 000000ff 00000000 
3ffffe20:  00000006 3ffeedf0 00000020 40100f40 
3ffffe30:  40100b0c 00000008 00302064 3fff1cac 
3ffffe40:  3ffefb04 3ffeee68 00000000 4021b8d4 
3ffffe50:  3ffeee68 00000000 00000000 4010037a 
3ffffe60:  00000000 402138dc 0000422f 4021b8fa 
3ffffe70:  3ffeee68 00000000 00000000 4021c48f 
3ffffe80:  004b65af 0000005a 00000008 402179de 
3ffffe90:  3ffeee68 00000000 00000001 402137e1 
3ffffea0:  401054d1 004f864e d916872b 00000000 
3ffffeb0:  00f42400 6bc97d31 004f8600 00000000 
3ffffec0:  40105755 004f864e 3ffee974 00000000 
3ffffed0:  3ffee220 3ffee974 00000001 3ffee86c 
3ffffee0:  00000019 3ffee974 00000001 40207840 
3ffffef0:  00000000 00000001 3ffee86c 40208086 
3fffff00:  00000000 3fff01c4 3ffefb7c 40202b74 
3fffff10:  3ffee86c 00000000 00000d50 000001bb 
3fffff20:  3fff01c4 3ffe86f3 3ffee88c 000001bb 
3fffff30:  3fff01c4 3ffe86f3 00000001 40204763 
3fffff40:  4020a290 8e14d9ac 4020a290 8e14d9ac 
3fffff50:  3ffefdac 3ffee83c 3ffee88c 3ffe84e0 
3fffff60:  00000005 3ffee820 3ffee88c 40201183 
3fffff70:  3ffefa00 0057005f 80002710 40207840 
3fffff80:  40208091 00002710 3ffee918 3ffee918 
3fffff90:  3fffdad0 00000000 3ffee8d8 4020124b 
3fffffa0:  feefeffe 00000000 3ffee8d8 40207958 
3fffffb0:  feefeffe feefeffe 3ffe8504 40101231 
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3584, room 16
tail 0
chksum 0xb0
csum 0xb0
v5d3af165
~ld


Decoded Exception:
Code: Select allException 28: LoadProhibited: A load referenced a page mapped with an attribute that does not permit loads
PC: 0x40209c38: BearSSL::PrivateKey::isRSA() const at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WiFi\src\BearSSLHelpers.cpp line 734
EXCVADDR: 0x00300030

Decoding stack results
0x40204505: BearSSL::WiFiClientSecure::_connectSSL(char const*) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WiFi\src\WiFiClientSecureBearSSL.cpp line 1093
0x40100bd2: check_poison_neighbors(uint16_t) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\umm_malloc/umm_local.c line 59
0x40100f40: umm_malloc(size_t) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\umm_malloc\umm_malloc.cpp line 552
0x40100b0c: get_poisoned(void*, size_t) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\umm_malloc/umm_poison.c line 117
0x4021b768: ip4_output_if_opt at core/ipv4/ip4.c line 1577
0x4010037a: malloc(size_t) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\heap.cpp line 228
0x40213768: tcpip_tcp_timer at core/timeouts.c line 145
0x4021b78e: ip4_output_if at core/ipv4/ip4.c line 1550
0x4021c323: ip_chksum_pseudo at core/inet_chksum.c line 395
0x4021786a: tcp_output at core/tcp_out.c line 1361
0x4021366d: sys_timeout_abs at core/timeouts.c line 189
0x40207888: __esp_yield() at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_main.cpp line 119
0x402080d9: __delay(unsigned long) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_wiring.cpp line 57
0x402080ce: __delay(unsigned long) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_wiring.cpp line 54
0x40202b90: WiFiClient::connect(IPAddress, unsigned short) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WiFi\src/include/ClientContext.h line 144
0x402046e7: BearSSL::WiFiClientSecure::connect(char const*, unsigned short) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\libraries\ESP8266WiFi\src\WiFiClientSecureBearSSL.cpp line 228
0x40201183: storeValues() at C:\Users\ben_s\Documents\Code\Arduino\ESP_ENVIRONMENT/ESP_ENVIRONMENT.ino line 69
0x402080d9: __delay(unsigned long) at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_wiring.cpp line 57
0x40201261: loop() at C:\Users\ben_s\Documents\Code\Arduino\ESP_ENVIRONMENT/ESP_ENVIRONMENT.ino line 48
0x402079a0: loop_wrapper() at C:\Users\ben_s\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3\cores\esp8266\core_esp8266_main.cpp line 197
User avatar
By MajorBoredom
#88557 After some investigation it seems that the issue is with the HTTPRedirect library.
When using WiFiClientSecure and creating my own post request as below it works and does not throw the error.

Code: Select allWiFiClientSecure client;

void storeValues() {
    //Serial.println(F("Sending..."));
    client.stop();
    client.setInsecure();
    //Serial.print(F("Connecting to "));
    //Serial.println(target + url2);
    bool flag = false;
    int errorcount = 0;
    if (!flag) {
      for (int i = 0; i < 5; i++) {
        int retval = client.connect(target, httpsPort);
        if (retval == 1) {
            flag = true;
            break;
        } else {
            //Serial.print(F("..."));
        }
      }
    }
    if (flag) {
        payload = payload_base + "\"," + temperature + "," + humidity + "," + pressure + "," + battery + "\"}";
        //Serial.println(payload);
        unsigned int len = payload.length() + 1;
        client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                          "Host: " + target + "\r\n" +
                          "User-Agent: ESP8266\r\n" +
                          "Connection: close\r\n" +
                          "Content-Type: application/json\r\n" +
                          "Content-Length: " + len + "\r\n" +
                          "\r\n" + payload + "\r\n\r\n");
        while(client.available()) {
          String line = client.readStringUntil('\r');
          //Serial.println(line);
          delay(10);               
        }
        client.stop();
    }
}


The failing with this method is that now the response back from the Google Sheets script cannot be read as there is a redirect (or two) to follow (which is why the HTTPRedirects library is needed).

Even though the response contains only the re-direct headers, you still need to read it otherwise the connection stays open and uses up all the heap.