#define DEBUG 1 #include //#include #include #include #define BUZZER_PIN D2 const char* ssid = "FrankHilde"; const char* password = "HildeFrank"; const char* mqtt_server = "192.168.2.153"; const char* mqtt_user = "domoticz"; const char* mqtt_pwd = "MQTT&Domoticz"; WiFiClient espClient; PubSubClient client(espClient); #include RCSwitch mySwitch = RCSwitch(); long lastMsg = 0; char msg[100]; int value = 0; #define idx_deurbel 906 #define idx_deurbelstil 917 #define idx_overkapping 2268 char buffer[100] = {0}; //global veriables //Page main const char* datumtijd; const char* weer_text; int main_btn_staandelamp = 0; int main_btn_tuinverlichting = 0; int main_btn_hanglamp = 0; int main_btn_haardverlichting = 0; int main_btn_rooster = 1; int main_btn_deurbelstil = 0; int alarm_sound = 0; int main_pic_weer; float main_temp_in = 0; float main_hum_in = 0; float main_baro = 0; float main_temp_out = 0; float main_hum_out = 0; float main_temp_min = 0; float main_temp_max = 0; float main_rain = 0; float main_rain_hour = 0; void setup_wifi() { delay(10); // We start by connecting to a WiFi network #ifdef DEBUG Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); #endif WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); #ifdef DEBUG Serial.print("."); #endif } randomSeed(micros()); #ifdef DEBUG Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); #endif } void reconnect() { // Loop until we're reconnected while (!client.connected()) { #ifdef DEBUG Serial.print("Attempting MQTT connection..."); #endif // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect // if (client.connect(clientId.c_str())) { if (client.connect(clientId.c_str(),mqtt_user,mqtt_pwd)) { #ifdef DEBUG Serial.println("connected"); #endif // Once connected, publish an announcement... client.publish("outTopic", "hello world"); // ... and resubscribe // client.subscribe("domoticz/out/example/display"); client.subscribe("domoticz/out/display/Overkapping"); } else { #ifdef DEBUG Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); #endif // Wait 5 seconds before retrying delay(5000); } } } void setup() { pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, 0); pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output #ifdef DEBUG Serial.begin(115200); #endif setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(mqtt_callback); // Transmitter is connected to Arduino Pin #10 mySwitch.enableTransmit(D1); // Optional set protocol (default is 1, will work for most outlets) mySwitch.setProtocol(1); // Optional set pulse length. mySwitch.setPulseLength(395); //Orgineel op 390 !! // Optional set number of transmission repetitions. mySwitch.setRepeatTransmit(2); Serial.println(F("RF Init done...")); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); if(alarm_sound == 1) { tone(BUZZER_PIN, 1000, 50); delay(1000); } long now = millis(); if (now - lastMsg > 20000) { lastMsg = now; ++value; } } //MQTT receive void mqtt_callback(char* topic, byte* payload, unsigned int length) { int svalue1, loc, StepperTarget; String sub, devicename; StaticJsonDocument<512> doc; #ifdef DEBUG Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); #endif // Deserialize the JSON document DeserializationError error = deserializeJson(doc, payload); // Test if parsing succeeds. if (error) { #ifdef DEBUG Serial.print(F("deserializeJson() failed: ")); Serial.println(error.c_str()); #endif return; } // Get the root object in the document JsonObject root = doc.as(); int idx = root["idx"]; const char* idx_name = root["name"]; const char* idx_svalue1 = root["svalue1"]; const char* idx_svalue2 = root["svalue2"]; const char* idx_svalue4 = root["svalue4"]; int idx_nvalue = root["nvalue"]; #ifdef DEBUG Serial.println(idx); Serial.println(idx_name); Serial.println(idx_svalue1); Serial.println(idx_nvalue); #endif if (idx == idx_deurbel) { if (idx_nvalue == 1) { #ifdef DEBUG Serial.println("Show doorbell"); #endif //delay(200); for (int i = 0; i < 10; i++) { tone(BUZZER_PIN, 1000, 200); delay(500); } } } if (idx == idx_overkapping) { if (idx_nvalue == 1) { #ifdef DEBUG Serial.println("Overkapping LED --> On !!"); #endif mySwitch.send(14944537, 24); //ON } else { #ifdef DEBUG Serial.println("Overkapping LED --> Off !!"); #endif mySwitch.send(14944539, 24); //OFF } } }