ESP8266-01 problem with low energy consumption and mqtt

Hi everyone :slight_smile:
I have a raspberry pi 3 with hass.io. To control some heating automation I built a sensor with an esp8266-01 and a DS18B20 sensor. The esp8266 publishes the data using mqtt and raspberry is the broker.

After some work on the code it now runs nicely and sends the reading to raspberry every 10 minutes. What I would like to obtain is a lower consumption as the sensor runs on two AA batteries.

I tried to put the esp to sleep but with very little success, I then tried a loop to switch the wifi off for ten minutes and then restart it, connect and send the reading.

I managed to have it going through the loop a couple of times but it then crashes.
The error I get in the serial monitor of the Arduino IDE looks like:

ets Jan 8, 2013,rst cause:4, boot mode:(1, 7)
wdt reset

I am honestly clueless about this…

This is one of the last versions.

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


// Data wire is conntec to the Arduino digital pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
//DallasTemperature sensors(&oneWire);
DallasTemperature DS18B20(&oneWire);


#define wifi_ssid "mywlan"
#define wifi_password "my_pwd"
#define mqtt_server "raspberry_ip"
#define mqtt_user "my_user"
#define mqtt_password "my_mqtt_pwd"
#define temperature_topic "sensor/temperature"

WiFiClient espClient;
PubSubClient client(espClient);

void setup(){
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  DS18B20.begin();
  
}


void setup_wifi() {
  delay(10);
  Serial.print("Connecting to WLAN");
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  }
  Serial.print("Connected to WLAN");
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  DS18B20.requestTemperatures();
  float temp = DS18B20.getTempCByIndex(0);
  Serial.print("New temperature:");
  Serial.println(String(temp).c_str());
  client.publish(temperature_topic, String(temp).c_str(), true);
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  delay(60000);
  WiFi.mode(WIFI_STA);
  setup_wifi();

}

I would be grateful for any suggestion, both in order to make this code run or ideas to put esp to sleep (given that many snippets of code for esp8266 seem not to work for esp8266-01. :smiley:

Little contribution
For those using DS18B20. I read many threads about the fact that the sensor reads β€˜85’. I had the same problem and (without changing wiring or other portions of the code), adding the line

DS18B20.requestTemperatures();

before

float temp = DS18B20.getTempCByIndex(0);

seemed to solve the problem

1 Like