I have a ESP8266 D1 Mini and want it to go into deep sleep overnight. I have the D0 and RST pin conned through a jumper wire. But when my code runs the deepsleep only lasts a second then reboots. I have a MQTT line that makes it go to sleep.
My code is below, any help is appreciated.
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <time.h>
#include "DHT.h"
#include <PubSubClient.h>
int pinCS = D6;
int numberOfHorizontalDisplays = 4;
int numberOfVerticalDisplays = 1;
char time_value[20];
const char* mqttServer = "192.168.0.50";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
//bool timer_newMessageAvailable = false;
bool timer_done = false;
bool timer_clear = false;
bool timer_sleep = false;
String PayloadString = "";
int Seconds = 0;
int Minutes = 0;
String stSeconds = "";
String stMinutes = "";
WiFiClient espClient;
PubSubClient client(espClient);
// LED Matrix Pin -> ESP8266 Pin
// Vcc -> 3v (3V on NodeMCU 3V3 on WEMOS)
// Gnd -> Gnd (G on NodeMCU)
// DIN -> D7 (Same Pin for WEMOS)
// CS -> D4 (Same Pin for WEMOS)
// CLK -> D5 (Same Pin for WEMOS)
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
int wait = 70; // In milliseconds
int spacer = 1;
int width = 5 + spacer; // The font width is 5 pixels
int m;
#define DHTPIN 0 // D3
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
String t, h;
void setup() {
Serial.begin(115200);
dht.begin();
//INSERT YOUR SSID AND PASSWORD HERE
WiFi.begin("", "");
matrix.setIntensity(0); // Use a value between 0 and 15 for brightness
matrix.setRotation(0, 1); // The first display is position upside down
matrix.setRotation(1, 1); // The first display is position upside down
matrix.setRotation(2, 1); // The first display is position upside down
matrix.setRotation(3, 1); // The first display is position upside down
matrix.fillScreen(LOW);
matrix.write();
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");
matrix.drawChar(2, 0, 'W', HIGH, LOW, 1); // H
matrix.drawChar(8, 0, 'I', HIGH, LOW, 1); // HH
matrix.drawChar(14, 0, '-', HIGH, LOW, 1); // HH:
matrix.drawChar(20, 0, 'F', HIGH, LOW, 1); // HH:M
matrix.drawChar(26, 0, 'I', HIGH, LOW, 1); // HH:MM
matrix.write(); // Send bitmap to display
delay(250);
matrix.fillScreen(LOW);
matrix.write();
delay(250);
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("wled/status", "starting");
delay(1000);
client.publish("wled/status", "up");
client.subscribe("wled/timer");
client.subscribe("wled/timerdone");
//CHANGE THE POOL WITH YOUR CITY. SEARCH AT https://www.ntppool.org/zone/@
configTime(0 * 3600, 0, "it.pool.ntp.org", "time.nist.gov");
setenv("TZ", "GMT+6BST", 1);
}
void loop() {
//Serial.println("Test");
if (!client.connected()) { // MQTT reconnect
reconnect();
}
client.loop();
//m = map(analogRead(0), 0, 1024, 0, 12);
//matrix.setIntensity(m);
//matrix.fillScreen(LOW);
// Serial.println(Seconds);
while (Seconds > 0) {
stMinutes=Minutes;
if (Minutes>9) {stMinutes=stMinutes;} else {stMinutes="0" + stMinutes;}
stSeconds = Seconds-(Minutes*60);
if ((Seconds-(Minutes*60))>9) {stSeconds=stSeconds;} else {stSeconds="0" + stSeconds;}
String time_value = stMinutes + ":" + stSeconds;
//String time_value = String(Seconds);
matrix.drawChar(2, 0, time_value[0], HIGH, LOW, 1); // H
matrix.drawChar(8, 0, time_value[1], HIGH, LOW, 1); // HH
matrix.drawChar(14, 0, time_value[2], HIGH, LOW, 1); // HH:
matrix.drawChar(20, 0, time_value[3], HIGH, LOW, 1); // HH:M
matrix.drawChar(26, 0, time_value[4], HIGH, LOW, 1); // HH:MM
matrix.write(); // Send bitmap to display
Seconds--;
Minutes = (Seconds/60);
client.loop();
delay(1000);
}
if (timer_done) {
matrix.drawChar(2, 0, 'D', HIGH, LOW, 1); // H
matrix.drawChar(8, 0, 'O', HIGH, LOW, 1); // HH
matrix.drawChar(14, 0, 'N', HIGH, LOW, 1); // HH:
matrix.drawChar(20, 0, 'E', HIGH, LOW, 1); // HH:M
matrix.drawChar(26, 0, '!', HIGH, LOW, 1); // HH:MM
matrix.write(); // Send bitmap to display
delay(6000);
timer_done=false;
}
if (timer_clear) {
matrix.fillScreen(LOW);
matrix.write();
//matrix.clear();
delay(1);
timer_clear=false;
}
if (timer_sleep) {
//sleep();
client.publish("wled/status", "sleep");
delay(1000);
timer_sleep=false;
//Serial.println("going to sleep");
ESP.deepSleep(10000);
//WiFi.disconnect();
//WiFi.forceSleepBegin();
delay(10000);
}
//ESP.deepSleep(5000)
delay(10);
}
void sleep(){
client.publish("wled/status", "sleep");
delay(1000);
timer_sleep=false;
Serial.println("going to sleep");
ESP.deepSleep(10000);
delay(10000);
}
void display_message(String message) {
for ( int i = 0 ; i < width * message.length() + matrix.width() - spacer; i++ ) {
//matrix.fillScreen(LOW);
int letter = i / width;
int x = (matrix.width() - 1) - i % width;
int y = (matrix.height() - 8) / 2; // center the text vertically
while ( x + width - spacer >= 0 && letter >= 0 ) {
if ( letter < message.length() ) {
matrix.drawChar(x, y, message[letter], HIGH, LOW, 1); // HIGH LOW means foreground ON, background off, reverse to invert the image
}
letter--;
x -= width;
}
matrix.write(); // Send bitmap to display
delay(wait / 2);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
PayloadString="";
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
PayloadString = PayloadString + (char)payload;
Serial.print((char)payload);
}
//Serial.print(payload);
Serial.println();
Serial.println("-----------------------");
if (strcmp(topic, "wled/timer") == 0) {
//timer_newMessageAvailable = true;
Serial.println(PayloadString);
Seconds = PayloadString.toInt();
Minutes = Seconds/60;
//Serial.print("Minutes:");
//Serial.println(Minutes);
}
if (strcmp(topic, "wled/timerdone") == 0) {
if (PayloadString == "Done") {
timer_done = true;
}
if (PayloadString == "clear") {
timer_clear = true;
}
if (PayloadString == "sleep") {
timer_sleep = true;
}
}
}
void reconnect() {
while (!client.connected()) {
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
// Attempt to connect
if (client.connect(clientId.c_str(), mqttUser, mqttPassword)) {
Serial.println("subscribe objects");
client.publish("wled/status", "up");
client.subscribe("wled/timerdone");
client.subscribe("wled/timer");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}