Hello arduino community,
Today I ran into a problem with my Wemos D1 Mini connected to a DHT22. What I'm trying to do is, reading the data from the sensor and sending it to mqtt. After that the Wemos should sleep for 5 minutes and then start from the beginning. But after the waking up from deep sleep I only get
ets_main.c
And it's not doing anything else. What does this mean?
Code:
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <DHT.h>
const char* ssid = "******";
const char* password = "**********";
const char* mqtt_server = "********";
const char* mqtt_user = "***********";
const char* mqtt_password = "***********";
#define DHTTYPE DHT22
WiFiClient espClient;
PubSubClient client(espClient);
#define DHTPIN D1
DHT dht(DHTPIN, DHTTYPE);
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while(!client.connected()) {
Serial.println("Attempting MQTT connection...");
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 setup() {
Serial.begin(76800);
delay(1000);
setup_wifi();
ArduinoOTA.setHostname("ESP_PhilippsZimmer");
ArduinoOTA.begin();
client.setServer(mqtt_server, 1883);
dht.begin();
}
void loop() {
ArduinoOTA.handle();
if(!client.connected()) {
reconnect();
}
if(!client.loop()) {
client.connect("ESP8266Client", mqtt_user, mqtt_password);
}
Serial.println("Trying to read sensor data");
float t = dht.readTemperature();
float h = dht.readHumidity();
if(isnan(t) || isnan(h)) {
Serial.println("Failed to read from sensor!");
t = 0.0;
h = 0.0;
}
float hic = dht.computeHeatIndex(t, h, false);
static char temperatureTemp[7];
dtostrf(t, 6, 2, temperatureTemp);
static char humidityTemp[7];
dtostrf(h, 6, 2, humidityTemp);
client.publish("home/philippsroom/temperature", temperatureTemp);
client.publish("home/philippsroom/humidity", humidityTemp);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
delay(2000);
Serial.println("ESP Going into deep sleep");
ESP.deepSleep(10 * 1000000);
delay(100);
}
Don't wonder for testing purpose I only use 10 seconds deep sleep.
Thanks in advance
Philipp