diff --git a/KuhLight_ESP/src/main.cpp b/KuhLight_ESP/src/main.cpp index 87ba59a..8d706d7 100644 --- a/KuhLight_ESP/src/main.cpp +++ b/KuhLight_ESP/src/main.cpp @@ -12,6 +12,7 @@ const char* mqtt_server = "192.168.1.250"; #define LED_WRK (14) #define SENS (4) #define LAMP (5) +#define LED_MS (300) AsyncMqttClient mqttClient; Ticker mqttReconnectTimer; @@ -24,7 +25,8 @@ unsigned long cRun = 0; unsigned long lastSense; uint16_t nSec = 0; //uint8_t lamp, lampP; -uint8_t light; +uint8_t lightk; +unsigned long led_ms; bool switchLight(uint8_t nLamp, int state, bool pub); void connectToWifi(); @@ -88,11 +90,12 @@ void setup() { Serial.println("Connect to WiFi"); connectToWifi(); + led_ms = millis(); lastSense = millis(); } void loop() { - static bool led_wrk = false; + //static bool led_wrk = false; ArduinoOTA.handle(); if(digitalRead(SENS) == 0){ @@ -104,23 +107,30 @@ void loop() { if(cRun + 999 < millis()){ cRun = millis(); nSec++; - led_wrk = !led_wrk; - digitalWrite(LED_WRK, led_wrk); + //led_wrk = !led_wrk; + //digitalWrite(LED_WRK, led_wrk); if(nSec > 59){ if(mqttClient.connected()){ char v[11]; ultoa(millis(), v, 10); + digitalWrite(LED_WRK, LOW); + led_ms = millis(); mqttClient.publish("/home/kuh/ltblmillis", 0, false, v); } nSec = 0; } } + if(led_ms + LED_MS <= millis()) digitalWrite(LED_WRK, HIGH); } bool switchLight(uint8_t nLamp, int state, bool pub) { digitalWrite(nLamp, state); - if (pub) mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "true" : "false"); + if (pub){ + digitalWrite(LED_WRK, LOW); + led_ms = millis(); + mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "true" : "false"); + } return state; } @@ -144,7 +154,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - mqttClient.subscribe("/home/kuh/lighttbl", 1); + mqttClient.publish("/home/kuh/lighttbl_set", 1, false, digitalRead(LAMP) == 1 ? "true" : "false"); + mqttClient.subscribe("/home/kuh/lighttbl_set", 1); mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "true" : "false"); digitalWrite(LED_MQ, HIGH); } @@ -157,8 +168,11 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { } void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - if(strcmp(topic, "/home/kuh/lighttbl") == 0){ - if (strcmp("true", payload) == 0) switchLight(LAMP, 1, false);//lStat2 = true; - else switchLight(LAMP, 0, false);//lStat2 = false; + char pl[6]; + strncpy(pl, payload, len); + pl[len] = '\0'; + if(strcmp(topic, "/home/kuh/lighttbl_set") == 0){ + if (strcmp("true", pl) == 0) switchLight(LAMP, 1, true);//lStat2 = true; + else switchLight(LAMP, 0, true);//lStat2 = false; } } diff --git a/MidRoomNLight/src/main.cpp b/MidRoomNLight/src/main.cpp index c92a233..b247384 100644 --- a/MidRoomNLight/src/main.cpp +++ b/MidRoomNLight/src/main.cpp @@ -9,6 +9,8 @@ // Enable and select radio type attached #define MY_RADIO_RF24 #define MY_RF24_CHANNEL (105) +#define MY_RF24_PA_LEVEL RF24_PA_MAX +#define MY_REPEATER_FEATURE #include #define RELAY_PIN 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)