Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <DHT.h>
- #include <ArduinoJson.h>
- #define MQTT_VERSION MQTT_VERSION_3_1_1
- #define DHTPIN 13
- #define DHTTYPE DHT11
- const char* WIFI_SSID = "Drueck mich!";
- const char* WIFI_PASSWORD = "Key01@BM";
- const PROGMEM char* MQTT_CLIENT_ID = "bedroom_dht11";
- const PROGMEM char* MQTT_SERVER_IP = "";
- const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
- const PROGMEM char* MQTT_USER = "";
- const PROGMEM char* MQTT_PASSWORD = "";
- const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/bedroom/sensor_dht";
- const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30;
- DHT dht(DHTPIN, DHTTYPE);
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- void publishData(float p_temperature, float p_humidity) {
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& root = jsonBuffer.createObject();
- root["temperature"] = (String)p_temperature;
- root["humidity"] = (String)p_humidity;
- root.prettyPrintTo(Serial);
- Serial.println("");
- char data[200];
- root.printTo(data, root.measureLength() + 1);
- client.publish(MQTT_SENSOR_TOPIC, data, true);
- }
- // function called when a MQTT message arrived
- void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("INFO: Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
- Serial.println("INFO: connected");
- } else {
- Serial.print("ERROR: failed, rc=");
- Serial.print(client.state());
- Serial.println("DEBUG: try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- // init the serial
- Serial.begin(9600);
- dht.begin();
- // init the WiFi connection
- Serial.println();
- Serial.println();
- Serial.print("INFO: Connecting to ");
- WiFi.mode(WIFI_STA);
- Serial.println(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("INFO: WiFi connected");
- Serial.println("INFO: IP address: ");
- Serial.println(WiFi.localIP());
- // init the MQTT connection
- client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println("ERROR: Failed to read from DHT sensor!");
- return;
- } else {
- //Serial.println(h);
- //Serial.println(t);
- publishData(t, h);
- }
- Serial.println("INFO: Closing the MQTT connection");
- client.disconnect();
- Serial.println("INFO: Closing the Wifi connection");
- WiFi.disconnect();
- ESP.deepSleep(SLEEPING_TIME_IN_SECONDS, WAKE_RF_DEFAULT);
- delay(500); // wait for deep sleep to happen
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement