Use an ESP32 board to send DHT to MQTT and then deep sleep
使用ESP32板將DHT發送到MQTT然後深度睡眠
·
以下是我如何發布從DHT11傳感器收集的濕度和溫度值的範例。我使用ESP32(Lolin32)板並將值發佈到MQTT開源消息代理(Eclipse Mosquitto)。最後,我深深地睡在板上以節省能源。
我使用以下庫:
- Adafruit的DHT11 / DHT22 Arduinot程式庫[ >> ]
https://github.com/adafruit/DHT-sensor-library
- 由Nick O'Leary負責MQTT的Arduino客戶端(PubSubClient)[ >> ]
https://pubsubclient.knolleary.net/
- 用於ESP32 WiFi芯片的Arduino核心[ >> ]
https://github.com/espressif/arduino-esp32
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
/* Project ESP32, DHT, MQTT and Deepsleep */
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h" // Library for DHT sensors
//#define wifi_ssid "My_APs_ssid" //wifi ssid
//#define wifi_password "my_password" //wifi password
#define wifi_ssid "74170287" //wifi ssid
#define wifi_password "24063173" //wifi password
//#define mqtt_server "mqtt_broker_IP" // server name or IP
//#define mqtt_user "username" // username
//#define mqtt_password "password" // password
#define mqtt_server "broker.mqtt-dashboard.com" // server name or IP
#define mqtt_user "alex9ufo" // username
#define mqtt_password "alex9981" // password
#define temperature_topic "alex9ufo/outTopic/temp1" //Topic temperature
#define humidity_topic "alex9ufo/outTopic/humid1" //Topic humidity
#define debug_topic "debug" //Topic for debugging
/* definitions for deepsleep */
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
//#define TIME_TO_SLEEP 900 /* Time ESP32 will go to sleep for 15 minutes (in seconds) */
//#define TIME_TO_SLEEP_ERROR 3600 /* Time to sleep in case of error (1 hour) */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep for 5 seconds (in seconds) */
#define TIME_TO_SLEEP_ERROR 3 /* Time to sleep in case of error (3 sec) */
bool debug = true; //Display log message if True
#define DHTPIN 23 // DHT Pin
// Uncomment depending on your sensor type:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Create objects
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
setup_wifi(); //Connect to Wifi network
client.setServer(mqtt_server, 1883); // Configure MQTT connection, change port if needed.
if (!client.connected()) {
reconnect();
}
dht.begin();
}
//Setup connection to wifi
void setup_wifi() {
delay(20);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi is OK ");
Serial.print("=> ESP32 new IP address is: ");
Serial.print(WiFi.localIP());
Serial.println("");
}
//Reconnect to wifi if connection is lost
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT broker ...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
Serial.println("OK");
} else {
Serial.print("[Error] Not connected: ");
Serial.print(client.state());
Serial.println("Wait 5 seconds before retry.");
delay(5000);
}
}
}
void loop() {
// Read temperature in Celcius
float t = dht.readTemperature();
// Read humidity
float h = dht.readHumidity();
// Nothing to send. Warn on MQTT debug_topic and then go to sleep for longer period.
if ( isnan(t) || isnan(h)) {
Serial.println("[ERROR] Please check the DHT sensor !");
client.publish(debug_topic, "[ERROR] Please check the DHT sensor !", true); // Publish humidity on mosohomes/humid1
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
Serial.println("Going to sleep now because of ERROR");
esp_deep_sleep_start();
return;
}
if ( debug ) {
Serial.print("Temperature : ");
Serial.print(t);
Serial.print(" | Humidity : ");
Serial.println(h);
}
// Publish values to MQTT topics
client.publish(temperature_topic, String(t).c_str(), true); // Publish temperature on mosohomes/temp1
if ( debug ) {
Serial.println("Temperature sent to MQTT.");
}
delay(100); //some delay is needed for the mqtt server to accept the message
client.publish(humidity_topic, String(h).c_str(), true); // Publish humidity on mosohomes/humid1
if ( debug ) {
Serial.println("Humidity sent to MQTT.");
}
delay(900); //Delay for show message & publish humidity
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
Serial.println("Going to sleep as normal now.");
esp_deep_sleep_start();
}
#define wifi_ssid
"My_APs_ssid" //wifi
ssid
#define wifi_password
"my_password" //wifi password
#define wifi_ssid "PTS-2F"
#define wifi_password
""
#define wifi_ssid "74170287"
#define wifi_password
"24063173"
#define wifi_ssid " alex9ufo "
#define wifi_password
"
alex9981"
#define mqtt_server "mqtt_broker_IP" //
server name or IP
#define mqtt_user "username" //
username
#define mqtt_password
"password" // password
#define mqtt_server "broker.mqtt-dashboard.com" // server name or IP
#define mqtt_user "alex9ufo" // username
#define mqtt_password
"alex9981" //
password
https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=en
沒有留言:
張貼留言