Created
December 20, 2017 18:13
-
-
Save labajo/77e534cd5d11397a2eb7d499ab767bbc to your computer and use it in GitHub Desktop.
MqttSensor with deepsleep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include "DHTesp.h" | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* mqtt_server = ""; | |
const int dhtPin = 16; | |
RTC_DATA_ATTR int bootCount = 0; | |
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ | |
#define TIME_TO_SLEEP 70 /* Time ESP32 will go to sleep (in seconds) */ | |
#define TEMP_TOPIC "parameters/esp32" | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
DHTesp dht; | |
char tmp[10]; | |
char hum[10]; | |
char message[50]; | |
void mqttconnect() { | |
/* Loop until reconnected */ | |
while (!client.connected()) { | |
Serial.print("MQTT connecting ..."); | |
/* client ID */ | |
String clientId = "ESP32Client"; | |
/* connect now */ | |
if (client.connect(clientId.c_str())) { | |
Serial.println("connected"); | |
} else { | |
Serial.print("failed, status code ="); | |
Serial.print(client.state()); | |
Serial.println("try again in 5 seconds"); | |
delay(5000); | |
} | |
} | |
} | |
void print_wakeup_reason(){ | |
esp_sleep_wakeup_cause_t wakeup_reason; | |
wakeup_reason = esp_sleep_get_wakeup_cause(); | |
switch(wakeup_reason) | |
{ | |
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; | |
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; | |
case 3 : Serial.println("Wakeup caused by timer"); break; | |
case 4 : Serial.println("Wakeup caused by touchpad"); break; | |
case 5 : Serial.println("Wakeup caused by ULP program"); break; | |
default : Serial.println("Wakeup was not caused by deep sleep"); break; | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(1000); | |
++bootCount; | |
Serial.println("Boot number: " + String(bootCount)); | |
print_wakeup_reason(); | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | |
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + | |
" Seconds"); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Enabling DHT22"); | |
dht.setup(dhtPin); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, 1883); | |
if (!client.connected()) { | |
mqttconnect(); | |
} | |
float humidity = dht.getHumidity(); | |
float temperature = dht.getTemperature(); | |
if (!isnan(temperature)) { | |
snprintf (tmp, 10, "%lf", temperature); | |
snprintf (hum, 10, "%lf", humidity); | |
String data = "{\"temp\": "; | |
data.concat(tmp); | |
data.concat(", \"hum\": "); | |
data.concat(hum); | |
data.concat("}"); | |
/* publish the message */ | |
data.toCharArray(message, 50); | |
Serial.print("Publishing "); | |
Serial.println(data); | |
client.publish(TEMP_TOPIC, message); | |
} | |
Serial.println("Going to sleep now"); | |
esp_deep_sleep_start(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment