0

I have an ESP-01 that I want to measure pool temperature with. It will send the data over MQTT to the broker.

I have connected RST to GPIO16 so I can use the deep sleep functionality. Currently I am testing sending a "hello world" to the MQTT broker, and everything works fine when booting up the first time, but it seems like the ESP never wakes up correctly after the first deep sleep.

My script looks like this:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Set ADC mode
ADC_MODE(ADC_VCC);

// WiFi settings
const char* ssid = "Network";
const char* password = "Password";
IPAddress ip(192, 168, 0, 100);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

// MQTT settings
const char* mqtt_server = "192.168.0.110";
const int mqtt_port = 1883;
const char* mqtt_user = "User";
const char* mqtt_pass = "Password";
const char* mqtt_client_name = "poolsensor";
const char* mqtt_topic = "pool";

// Data wire plugged into D1 on the ESP8266 ESP-01 - GPIO 5
#define ONE_WIRE_BUS 5

// Setup OneWire instance to communicate with any device
OneWire oneWire(ONE_WIRE_BUS);

// Pass our OneWire reference to Dallas Temperature
DallasTemperature DS18B20(&oneWire);

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(10);

  // Annuonce connection
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  // Setup WiFi config and connect
  WiFi.mode(WIFI_STA);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're connected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass)) {
      // Success
      Serial.println("connected");
    } else {
      // Fail
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");

      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);

  // Start WiFi setup
  setup_wifi();

  // Set MQTT server
  client.setServer(mqtt_server, mqtt_port);

  // Set resolution to 0.25
  DS18B20.setResolution(10);

  // Start sensor
  DS18B20.begin();
}

void loop() {
  if (!client.connected()) {
    reconnect();
  } else {
    // Get temperature
    /*
    DS18B20.requestTemperatures();
    float temp = DS18B20.getTempCByIndex(0);

    if ((temp > -20) && (temp < 60)) {
      client.publish("pool/temperature", String(temp).c_str(), true);
    }
    */

    client.publish("pool/temperature", "hello world");
    Serial.println("published temperature");

    delay(1000);

    // Sleep
    ESP.deepSleep(60 * 1000000);

    delay(3000);
  }
}

Everything looks good in the serial monitor when compiling and uploading code, as the code runs the first time:

Connecting to Network
......
Connected, IP address: 192.168.0.100
Attempting MQTT connection...connected
published temperature

And I can see in my MQTT debugger that it receives hello world. After around 60 seconds, the following is shown in the serial monitor (lots of gibberish):

{l�l��|��d�|�d�b<����s�c$�#��ng�lgn���cxp��${$sd�s��'�c�ncp��

This is the last message I ever receive from the ESP, so I assume it never deep sleeps again and runs over the code again.

Anyone experienced the same, or see a bug in my code?

9
  • you have io 0 LOW?
    – Juraj
    Feb 19, 2019 at 10:50
  • 1
    How did you connect anything to GPIO16 on an ESP01? Other than very carefully of course - and D1??? that's wemos/nodemcu garbage (plus the ESP01 doesn't break out gpio 5 either Feb 19, 2019 at 10:53
  • if you set your serial monitor to 74880 baud, then you may be able to read the gibberish Feb 19, 2019 at 11:06
  • @JaromandaX Actually the temp. sensor is not connected yet, so I commented out the code. I ran a thin copper wire from RST to the correct pin on the chip. Baud 74880 worked, and it reads the following: ets Jan 8 2013,rst cause:5, boot mode:(1,6) ets_main.c but only once, and never more. @Juraj no, should I use pinMode(0, LOW); ?
    – rebellion
    Feb 19, 2019 at 11:32
  • 2
    @dandavis, he answered that " I ran a thin copper wire from RST to the correct pin on the chip"
    – Juraj
    Feb 20, 2019 at 6:52

3 Answers 3

2

Pretty sure you will need gpio 16 connected to reset in order for this to ever work, it’s tricky but not impossible. Have a read here:

https://randomnerdtutorials.com/esp8266-deep-sleep-with-arduino-ide/

2
  • 1
    I did that. Small copper wire from RST to GPIO16 on the chip.
    – rebellion
    Feb 20, 2019 at 9:19
  • Sorry, missed that one. From your code above it should be waking as expected. As others have said pulling GPIO 0 high is required for normal boot, GPIO 2 is not required but it is good practice to ensure stability. What you are most likely seeing and what I have seen in the past, if you have GPIO 0 grounded, i.e. to program with the arduino IDE, immediately after programming is complete, the chip will reboot normally and run with GPIO 0 grounded, but only once. If you leave GPIO 0 grounded and reboot again (or the chip wakes from sleep) it will not boot. Take GPIO 0 high and it should work.
    – user54057
    Feb 21, 2019 at 22:18
1

You left io 0 floating. With io 0 not HIGH at boot, the esp8266 goes into flashing mode.

The wake-up from deep-sleep is boot startup. So your code doesn't start if io 0 is not pulled high. Add a pull-up for io 0.

If you set Serial Monitor to 74880 baud, you will see the boot-log readable. It will print 1 as first value in "boot mode". This value is set from state of io 15, 2 and 0. 15 should be LOW and io 2 and io 0 HIGH for normal boot. 1 indicates that only 2 is HIGH.

9
  • Thanks for your reply. I'm not quite sure if I understand. No guides I've read says I have to pull 0 high. I use a programmer for programming, but I still have the same initial issue when I disconnect the programmer and just connect VCC and GND to a power source.
    – rebellion
    Feb 19, 2019 at 14:37
  • every good esp-01 tutorial connects EN and io 0 to VCC. I would use a pull-up resistor
    – Juraj
    Feb 19, 2019 at 15:15
  • do you understand that io 0 state at boot determines if the board boots to application or to bootloader?
    – Juraj
    Feb 19, 2019 at 15:20
  • Of course, but previously I never have to do this. I've used ESP-01 in several projects, but never with deep sleep, so this is new to me.
    – rebellion
    Feb 19, 2019 at 17:01
  • after upload it start without reboot
    – Juraj
    Feb 19, 2019 at 17:25
1

I had the same problem and i found out, my CH_PD pin and RST pin were shorted together, when i was connecting wire from RST to GPIO_16. On the chip, the CH_PD pin is next to GPIO_16, so when you short them, the RST pin is always high, so esp cannot reboot anymore without external grounding. I fixed it and now deep sleep is working again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.