Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By mrtc
#70886 I am trying to run an ESP8266 to send a http rest post when a momentary push button is pressed.

I want to run the device from a battery & so need to use the deepsleep function to obtain longevity (the device will be used once or twice per day & potentially away from a power supply for months).

My understanding is that the ESP can decipher a timed wake out of deep sleep from a manual reset (by linking the rst pin to gnd) as itemised on page 5 (2/4) of this document: https://espressif.com/sites/default/files/documentation/esp8266_reset_causes_and_common_fatal_exception_causes_en.pdf.

I have a simple built a simple circuit to hand this:

simpleespreset.png


I have correspondingly written the following code:

Code: Select all#include <ESP8266WiFi.h>
extern "C" {
#include <user_interface.h>
}

const char* ssid     = "[MY SSID]";    //  This is the name or your wireless network

const char* password = "53¢r£t";      //  This is your wireless network password

int connectionCount;

void setup() {
 
  rst_info *resetInfo;
  resetInfo = ESP.getResetInfoPtr();
 
  Serial.begin(115200);

  Serial.println(" ");
 
  delay(100);
  Serial.println("I'm awake!");
  Serial.println("Reset Reason is: ");
  Serial.println(resetInfo->reason);

  // Intermittent wake
  if (resetInfo->reason == 5) {
Serial.println("Intermittent Wake");
Serial.println("I've woken up because I can only stay asleep for 35 minutes.  I'm preparing to go back to sleep now ...");
  };

  // Push button wake
  if (resetInfo->reason == 6) {
    Serial.println("Push Button Pressed");
    Serial.println("You woke me.  I'm connecting to the WIFI to do stuff!");
    connectToWifi();
    Serial.println("WIFI related stuff will happen here in the future");
  };

  Serial.println("Do nothing for 10 seconds");  // This makes it easier to re-flash the chip later.
  delay(10000);
  Serial.println("Off To Sleep");
  delay(100);
  ESP.deepSleep(2100000000, WAKE_RF_DEFAULT); //35 minutes (2100 seconds)
  Serial.println("Should be asleep but not!");

}

void loop() {
  // put your main code here, to run repeatedly:
}

// use the ssid and password to connect to the customer's WiFi
void connectToWifi() {
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // Disable AP_SSID publication in Client mode
  WiFi.mode(WIFI_STA);

  // Starts WiFi Connection
  WiFi.begin(ssid, password);

  connectionCount = 30;
  Serial.println("");

  Serial.println("Connection timeout in: ");

  // loop to check if connected to the wi fi network
  while (WiFi.status() != WL_CONNECTED && connectionCount > 0) {
    delay(450);
    digitalWrite(LED_WIFI_GOOD, HIGH);
    digitalWrite(BUZZER, HIGH);
    delay(50);
    digitalWrite(LED_WIFI_GOOD, LOW);
    digitalWrite(BUZZER, LOW);
    connectionCount--;
    Serial.print(connectionCount);
    Serial.print(" Current WiFi Status = ");
    Serial.print(WiFi.status());
    Serial.println(", ");
  }
  // we're connected to the wi fi network
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.println("Setup Complete");
  }
  else { // else we never connected to the network, inform the user of the error using the BUZZER and LED
    Serial.println("");
    Serial.println("Not Connected To WiFi");
  }
}


Using ESP.getResetInfoPtr(); I am expecting to see a code of 5 when the device intermittently wakes itself up (I understand that it can only stay asleep for 35 minutes) & a code of 6 when I press the button which initiates a hard reset.

    I am finding that I get a code of 5 each time the device manually wakes from sleep so that works fine.
    I am finding that when the device is not in deep sleep & I reset the device manually I get a code of 6 so that works fine
    HERES MY ISSUE: I find that when the device is in deep sleep & I reset the device, I get a response of 5, not 6 which is what I would expect.

Any thoughts on how to fix this?
You do not have the required permissions to view the files attached to this post.
User avatar
By jarnoldbrown
#70889 As I understand it, GPIO16 is connected to /RST, so the the wake from deep sleep is activating the /RST. I don't see how the chip could possibly tell the difference. It is possible to differentiate between a power up and a /RST, but not a /RST via a button press from a /RST via GPIO16.
User avatar
By jarnoldbrown
#70891 Ignore my last post. I will modify my reply.
There is some code in the operating system that probably writes a value in RTC memory when deep sleep is invoked. This allows the device to "know" that it has woken up from deep sleep. However, since the mechanism for waking up from deep sleep is simply the fact that the GPIO16 goes low and is connected to the reset pin, there is no way to know whether the reset pin was driven low by a reset button press or by GPIO16 going low. There may be some way or doing this with extra hardware, but I suspect it would not be too simple, as you will need to find some way of producing a single pulse from a low level, because the chip will not start up until the reset pin is high, so simply reading a level on another GPIO pin won't help. If you diode AND the GPIO signal and the reset button signal, you might be able to use a D type latch circuit with a negative edge clock. So imagine GPIO16 goes to the negative edge clock of a D type latch, and the reset button signal goes to the D input, then the Q output would only be set if the reset was cased by GPIO16. Maybe. You'd probably ahve to use another IO pin to clear the D type latch. I struggled with this for a project I did, and in the end I went an entirely different route. It was getting too complicated.
User avatar
By mrtc
#70903 From what I can see, the ESP must write a flag into RTC when it goes to sleep. Thats the only way that I can see that it works & makes sense why I am getting the same reading after deep sleep is initiated & not before.

For my use case I don't need it to wake on a schedule (it can stay asleep indefinitely), with a manual reset making the Wifi connection & API call.

So I've rewritten the deep sleep command to now read ESP.deepSleep(0);