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 = "asdf";
- const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
- const PROGMEM char* MQTT_USER = "asdf";
- const PROGMEM char* MQTT_PASSWORD = "asdf";
- const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/bedroom/sensor_dht";
- const PROGMEM char* MQTT_LIGHT_STATE_TOPIC[] = { "/house/livingroom/light_left/status", "/house/livingroom/light_right/status", "/house/livingroom/light_center/status" };
- const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC[] = { "/house/livingroom/light_left/switch", "/house/livingroom/light_right/switch", "/house/livingroom/light_center/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();
- 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]);
- }
- Serial.println(p_topic);// handle message topic
- for(int i=0; i<3; i++) {
- if (String(MQTT_LIGHT_COMMAND_TOPIC[i]).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;
- mySwitch.switchOn(housecode, socketcodes[i]);
- Serial.println("INFO: Turn light on...");
- client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_ON, true);
- }
- }
- else if (payload.equals(String(LIGHT_OFF))) {
- if (m_light_state != false) {
- m_light_state = false;
- mySwitch.switchOff(housecode, socketcodes[i]);
- Serial.println("INFO: Turn light off...");
- client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_OFF, true);
- }
- }
- }
- }
- }
- 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();
- for(int i=0; i<3; i++) {
- client.subscribe(MQTT_LIGHT_COMMAND_TOPIC[i]);
- }
- } 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);
- delay(1000);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- unsigned long now = millis();
- if(now - last_millis >= wait) {
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement