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>
- #include <RCSwitch.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 = "beutler.nl";
- 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 char* MQTT_LIGHT_STATE_TOPIC = "/house/livingroom/light_left/status";
- const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC = "/house/livingroom/light_left/switch";
- const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30;
- const char* LIGHT_ON = "ON";
- const char* LIGHT_OFF = "OFF";
- boolean m_light_state = false;
- const char* housecode = "11010";
- const char* socketcodes[] = {"00010", "00001", "10000"};
- const char* socketnames[] = {"Steckdose_Wohnzimmer_Links", "Steckdose_Wohnzimmer_rechts", "Steckdose_Wohnzimmer_Eingang"};
- const long wait = 20000;
- unsigned long last_millis = 0;
- DHT dht(DHTPIN, DHTTYPE);
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- RCSwitch mySwitch = RCSwitch();
- // function called to publish the state of the light (on/off)
- void publishLightState() {
- if (m_light_state) {
- client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
- } else {
- client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
- }
- }
- // function called to turn on/off the light
- void setLightState() {
- if (m_light_state) {
- mySwitch.switchOn(housecode, socketcodes[0]);
- Serial.println("INFO: Turn light on...");
- } else {
- mySwitch.switchOff(housecode, socketcodes[0]);
- Serial.println("INFO: Turn light off...");
- }
- }
- 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);
- }
- void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
- String payload;
- for (uint8_t i = 0; i < p_length; i++) {
- payload.concat((char)p_payload[i]);
- }
- // handle message topic
- if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
- // test if the payload is equal to "ON" or "OFF"
- if (payload.equals(String(LIGHT_ON))) {
- if (m_light_state != true) {
- m_light_state = true;
- setLightState();
- publishLightState();
- }
- } else if (payload.equals(String(LIGHT_OFF))) {
- if (m_light_state != false) {
- m_light_state = false;
- setLightState();
- publishLightState();
- }
- }
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.println("INFO: Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
- Serial.println("INFO: connected");
- publishLightState();
- client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
- } 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(115200);
- mySwitch.enableTransmit(2);
- dht.begin();
- setLightState();
- // 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();
- unsigned long now = millis();
- if(now - last_millis >= wait) {
- delay(500);
- last_millis = now;
- 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 * 1000000, WAKE_RF_DEFAULT);
- delay(500); // wait for deep sleep to happen*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement