-->
Page 1 of 2

SOLVED - Not Stack overflow but readvdd33 shouldnt be used

PostPosted: Sun Sep 06, 2015 11:44 am
by gordonendersby
Hi chaps,

Ive just updated my arduino ide support for the ESP8266 to the latest stable version.

But a previously working program using the sleep timers is now playing up.
Im using an ESP8266-12 on a breadboard exactly as I did when it was working under the previous Stable version.
Usually I use the board definition for the Nodemcu for the esp8266-12.
I have also tried the generic definition but it still fails.
I notice that the board definitions now mentions spiffs does this reduce the memory available?

Im getting what seems to be a stack overflow after it connects to the wifi router.
This will happen after a couple of sleep and restarts
The ESP then reboots and can no longer connect to the wifi router.
I get this in the serial monitor:
Code: Select allctx: cont
sp: 3ffea830 end: 3ffeaa90 offset: 01b0

>>>stack>>>
3ffea9e0:  40208d19 3ffe9a44 3ffeaad0 42020000 
3ffea9f0:  41ae6666 3ffe9778 3ffe9a44 3ffe9728 
3ffeaa00:  3ffe9728 00000000 00000000 3ffe9728 
3ffeaa10:  3ffe9728 4020234c 3ffe9a44 3ffe9778 
3ffeaa20:  42020000 3ffe9778 3ffe9a44 40202347 
3ffeaa30:  00000000 00000000 00000000 00000000 
3ffeaa40:  00000000 00000000 00000000 00000000 
3ffeaa50:  00000000 00000000 00000000 00000000 
3ffeaa60:  00000000 00000000 00000000 3ffeaabc 
3ffeaa70:  3fffdc20 00000000 3ffeaab4 4020183f 
3ffeaa80:  00000000 00000000 3ffe9a70 40100378 
<<<stack<<<
i�߈�
JZ�! !)���Zn�����DnZ��*��Xj(ȽC����H   �ܿHn:�� �O��d


Ive added a count into the loop waiting for the connection so that the ESP restarts after 20 iterations of the loop. As previously it would have connected by then. Hoping that a restart would clear whatever is causing the problem.
But it hasnt helped.

Heres my code:
Code: Select all 
#include <DHT.h>
#include <ESP8266WiFi.h>
extern "C" {
  #include "user_interface.h"
  uint16 readvdd33(void);
}
 
// replace with your channel's thingspeak API key,
String apiKey = "N33F4IRBI8EI8K8J";
const char* ssid = "ThisOldHouse";
const char* password = "thingstodoinalandrover";
 
const char* server = "api.thingspeak.com";
#define DHTPIN 2 // what pin we're connected to

//const unsigned long SLEEP_INFTERVAL = 10 * 60 * 1000 * 1000; // 10 minutes
const unsigned long SLEEP_INFTERVAL = 30 * 1000 * 1000; // 30 seconds
 
DHT dht(DHTPIN, DHT22,15);
WiFiClient client;
   
 
void setup() {               
  Serial.begin(57600);
  delay(10);
  dht.begin();
 
  WiFi.begin(ssid, password);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
   
  WiFi.begin(ssid, password);
  int c = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    c++;
    if (c > 20){ESP.restart();}
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // ################################ get and send reading

   
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float vdd = readvdd33() / 1000.0;
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

   Serial.println();
   Serial.print("Temperature: ");
   Serial.print(t);
   Serial.print("c  - Humidity: ");
   Serial.print(h);
   Serial.print("%  - Voltage: ");
   Serial.print(vdd);
   Serial.println("v ");

   Serial.println();
   Serial.println("Send to Thingspeak");   
   Serial.println();

  while (!client.connect(server, 80)) {
    Serial.println("connection failed, retrying...");
  }

  String postStr = apiKey;
         postStr +="&field1=";
         postStr += String(t);
         postStr +="&field2=";
         postStr += String(h);
         postStr +="&field3=";
         postStr += String(vdd);
         
         postStr += "\r\n\r\n";

   client.print("POST /update HTTP/1.1\n");
   client.print("Host: api.thingspeak.com\n");
   client.print("Connection: close\n");
   client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
   client.print("Content-Type: application/x-www-form-urlencoded\n");
   client.print("Content-Length: ");
   client.print(postStr.length());
   client.print("\n\n");
   client.print(postStr);
         
 

   // wait for response
    while(!!!client.available()) {
      delay(10);
    }
 
    // Read all the lines of the reply from server and print them to Serial
    Serial.println("Response: ");
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    Serial.println();
    Serial.println("closing connection");
 
  client.stop();

  Serial.print("up time: ");
  Serial.print(millis());

  Serial.print(", deep sleep for ");
  Serial.print(SLEEP_INFTERVAL / 1000000);
  Serial.println(" secs...");
  system_deep_sleep_set_option(0);
  system_deep_sleep(SLEEP_INFTERVAL - micros());
 
}
 
 
void loop() {


}


Ill leave this posted here in case anyone else is having problems and can point me to a solution or tell me what the message in the serial monitor means.
If I dont get much response Ill raise it on the ESP8266 Arduino GitHub project.

Thanks

Gordon

Re: Stack overflow?

PostPosted: Mon Sep 07, 2015 6:41 am
by gordonendersby
Ive raised this on github
https://github.com/esp8266/Arduino/issues/756
All ready getting some help.

Gordon

Re: Stack overflow?

PostPosted: Mon Sep 07, 2015 9:52 am
by gordonendersby
All sorted.
It was all down to using readvdd33 which doesnt do what we expect.
One problem it causes is it can cause the watchdog timer to trip.
This is what i was seeing, not a stack overflow.

The real problem is there are loads of examples using readvdd33 that shouldnt.
We should be using ESP.getVcc()
As documented here https://github.com/esp8266/Arduino/blob/esp8266/hardware/esp8266com/esp8266/doc/reference.md

The developers on the project page sorted me out.
Heres the explanation from https://github.com/esp8266/Arduino/issues/756

Code: Select allt for the record, this wasn't a stack overflow :)
When you get something like

ctx: cont
sp: 3ffea830 end: 3ffeaa90 offset: 01b0
without Exception: ... lines above it, this means that software watchdog timer has been triggered.
Stack overflows usually cause some Exception: ... lines to be generated.

Now regarding readvdd33...
readvdd33 is a function which is internal to the SDK, it never was part of public API neither in Espressif SDK nor in Arduino package. Why it happened to work in the previous version but not in the current one, i have no idea. Espressif SDK provides a public function, system_get_vdd33, which calls readvdd33 internally but also ensures that WiFi hardware is an a proper state before this call. In arduino environment we have this ESP.getVcc() function which basically wraps system_get_vdd33 call. You also need to add ADC_MODE(ADC_VCC) to your sketch as @chaeplin pointed out (this is also mentioned in the documentation).

The bottom line is, don't use readvdd33 in your sketches.

Re: SOLVED - Not Stack overflow but readvdd33 shouldnt be us

PostPosted: Mon Sep 07, 2015 1:46 pm
by AcmeUK
Nice find, that explains why my readvdd33 code is doing a stack dump intermittently!

Any chance of a simple code example for us noobos?