//ESP32 MQTT + Deep Sleep + Power Reset //WiFi and MQTT #include "WiFi.h" //1 #include "PubSubClient.h" const char* password = "****************"; //2 const char* ssid = "*************"; #define mqtt_broker "192.168.2.103" //3 WiFiClient espClient; //4 PubSubClient mqttClient(espClient); //Example Values: //5 String val1= "Hello"; String val2= "World"; String val3= "Hey"; String val4= "ESP32"; char Buffer[5]; //Deep Sleep #include //6 #define DS3231RTC_I2C_ADDRESS 0x68 #define I2C_SDA 21 #define I2C_SCL 22 //EEPROM #include //7 #define EEPROM_BYTES 2 #define WIFIRESETADDRESS 0 #define MQTTRESETADDRESS 1 //Restart #define RestartPin 27 //8 void setup(){ Wire.begin(I2C_SDA, I2C_SCL); //9 clearAlarm1(); EEPROM.begin(EEPROM_BYTES); //10 Serial.begin(115200); esp_sleep_enable_ext0_wakeup(GPIO_NUM_13,0); WiFiConnect(); //11 MQTTConnect(); Serial.println("Transmitting data."); val1.toCharArray(Buffer, 6); //12 mqttClient.publish("ESP32/Test1", Buffer); //13 delay(500); //14 val2.toCharArray(Buffer, 6); mqttClient.publish("ESP32/Test2", Buffer); delay(500); val3.toCharArray(Buffer, 4); mqttClient.publish("ESP32/Test3", Buffer); delay(500); val4.toCharArray(Buffer, 6); mqttClient.publish("ESP32/Test4", Buffer); Serial.println("Going into sleep mode"); esp_deep_sleep_start(); //15 } void loop(){ //Main loop is not going to be called. } void clearAlarm1(){ Wire.beginTransmission(DS3231RTC_I2C_ADDRESS); Wire.write(0x0F); Wire.write(B00000000); Wire.endTransmission(); } void WiFiConnect(){ //16 byte WiFiResets = EEPROM.read(WIFIRESETADDRESS); //17 unsigned long Timer = millis()+1000; //18 Serial.print("Connecting with WiFi"); WiFi.begin(ssid, password); //19 unsigned long Timeout = millis()+30000; //20 while(WiFi.status() !=WL_CONNECTED){ //21 if(millis()>Timer){ //22 Timer= millis()+1000; Serial.print("."); } if(millis()>Timeout){ //23 if(WiFiResets ==2){ //24 EEPROM.write(WIFIRESETADDRESS, 0); EEPROM.commit(); Serial.println(); Serial.println("Could not connect to MQTT Broker 3 times. Going into deep sleep."); esp_deep_sleep_start(); } else{ //25 WiFiResets++; EEPROM.write(WIFIRESETADDRESS, WiFiResets); EEPROM.commit(); Serial.println(); Serial.println("Could not connect to MQTT Broker. Restarting MCU."); digitalWrite(RestartPin, HIGH); } } } EEPROM.write(WIFIRESETADDRESS, 0); //26 EEPROM.commit(); Serial.println(); Serial.println("Successfully connected to WiFi"); } void MQTTConnect(){ //27 byte MQTTResets = EEPROM.read(MQTTRESETADDRESS); unsigned long Timer = millis()+1000; Serial.print("Connecting with MQTT Broker"); mqttClient.setServer(mqtt_broker, 1883); //28 mqttClient.connect("ESP32Client"); //29 unsigned long Timeout = millis()+30000; while(!mqttClient.connected()){ //30 if(millis()>Timer){ Timer= millis()+1000; Serial.print("."); mqttClient.connect("ESP32Client"); } if(millis()>Timeout){ if(MQTTResets ==2){ EEPROM.write(MQTTRESETADDRESS, 0); EEPROM.commit(); Serial.println(); Serial.println("Could not connect to MQTT Broker 3 times. Going into deep sleep."); esp_deep_sleep_start(); } else{ MQTTResets++; EEPROM.write(MQTTRESETADDRESS, MQTTResets); EEPROM.commit(); Serial.println(); Serial.println("Could not connect to MQTT Broker. Restarting MCU."); digitalWrite(RestartPin, HIGH); } } } EEPROM.write(MQTTRESETADDRESS, 0); EEPROM.commit(); Serial.println(); Serial.println("Successfully connected to MQTT Broker"); }