From c2442ca3ee244f70b949c33ba15252ba702daa56 Mon Sep 17 00:00:00 2001 From: lexa Date: Tue, 14 Jun 2022 15:09:39 +0300 Subject: [PATCH] Door sens adapted --- ESP_Kor/.vscode/extensions.json | 17 +-- MainDoor/.vscode/extensions.json | 17 +-- MainDoor/platformio.ini | 6 +- MainDoor/src/leds.cpp | 1 + MainDoor/src/main.cpp | 157 ++++++++++++++++++---------- MainDoorMyS/.vscode/extensions.json | 17 +-- 6 files changed, 137 insertions(+), 78 deletions(-) diff --git a/ESP_Kor/.vscode/extensions.json b/ESP_Kor/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/ESP_Kor/.vscode/extensions.json +++ b/ESP_Kor/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/MainDoor/.vscode/extensions.json b/MainDoor/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/MainDoor/.vscode/extensions.json +++ b/MainDoor/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/MainDoor/platformio.ini b/MainDoor/platformio.ini index 1f698da..0d19d09 100644 --- a/MainDoor/platformio.ini +++ b/MainDoor/platformio.ini @@ -16,4 +16,8 @@ board_build.f_cpu = 26000000L board_build.ldscript = eagle.flash.1m.ld ;board_build.flash_mode = dout upload_protocol = espota -upload_port = 192.168.1.133 +upload_port = 192.168.1.25 +lib_deps = + # RECOMMENDED + # Accept new functionality in a backwards compatible manner and patches + ottowinter/AsyncMqttClient-esphome @ ^0.8.6 \ No newline at end of file diff --git a/MainDoor/src/leds.cpp b/MainDoor/src/leds.cpp index bb3739b..ad70390 100644 --- a/MainDoor/src/leds.cpp +++ b/MainDoor/src/leds.cpp @@ -6,6 +6,7 @@ leds::leds(int pinLED, int onms, bool inverse) inv = inverse; onMS = onms; state = false; + pinMode(pinLED, OUTPUT); } void leds::start() diff --git a/MainDoor/src/main.cpp b/MainDoor/src/main.cpp index e49de0d..58d126f 100644 --- a/MainDoor/src/main.cpp +++ b/MainDoor/src/main.cpp @@ -9,7 +9,8 @@ const char* ssid = "wf-home"; const char* password = "0ndthnrf"; -const char* mqtt_server = "192.168.1.250"; +const char* mqtt_server = "192.168.1.111"; +#define TOPIC "home/kor/" AsyncMqttClient mqttClient; Ticker mqttReconnectTimer; @@ -18,15 +19,17 @@ WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; Ticker wifiReconnectTimer; -#define L_TOP (2) -#define L_INT (3) -#define L_DOWN (0) -#define L_SMALL (1) +#define L_TOP (0) +#define L_INT (1) +#define L_DOWN (3) +#define L_SMALL (2) #define DOOR (4) -#define L_WIFI (2) //On board blue -#define L_WIFI2 (14) //Green -#define L_MQTT (13) //Blue -#define L_MSG (12) //Red +#define L_WIFI (2) //On board blue and Red +#define IRQ_PIN (14) +//#define L_WIFI2 (14) //Green +#define L_MQTT (13) //Yelow +#define L_MSG (12) //Green +leds msg = leds(L_MSG, 300, true); unsigned long cRun = 0; unsigned long l_run; @@ -37,10 +40,12 @@ uint8_t tP, iP, dP, sP, door, doorP; // bool doorSent, lockSent; uint16_t nSec = 0; leds r_led(L_MSG); -uint8_t pcfreg, opcfreg; -PCF8574 pcf(0x3F); +uint8_t pcfreg;//, opcfreg; +PCF8574 pcf(0x20); bool l_m = false; +void trim(char *s); + void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); WiFi.begin(ssid, password); @@ -56,14 +61,14 @@ void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.print("IP: "); Serial.println(WiFi.localIP()); digitalWrite(L_WIFI, HIGH); - digitalWrite(L_WIFI2, LOW); + //digitalWrite(L_WIFI2, HIGH); connectToMqtt(); } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi."); digitalWrite(L_WIFI, LOW); - digitalWrite(L_WIFI2, HIGH); + //digitalWrite(L_WIFI2, LOW); mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi wifiReconnectTimer.once(2, connectToWifi); } @@ -73,18 +78,18 @@ void onMqttConnect(bool sessionPresent) { Serial.print("Session present: "); Serial.println(sessionPresent); //uint16_t packetIdSub = - //mqttClient.subscribe("/home/kor/doortimeout_set", 1); - mqttClient.publish("/home/kor/locker_top", 1, false, tP ? "0" : "1"); - mqttClient.publish("/home/kor/locker_int", 1, false, iP ? "0" : "1"); - mqttClient.publish("/home/kor/locker_down", 1, false, dP ? "0" : "1"); - mqttClient.publish("/home/kor/locker_small", 1, false, sP ? "0" : "1"); - mqttClient.publish("/home/kor/door", 1, false, doorP ? "0" : "1"); - //mqttClient.publish("/home/kor/doorlocker", 1, false, sP || dP || iP || tP ? "1" : "0"); + //mqttClient.subscribe(TOPIC"doortimeout_set", 1); + mqttClient.publish(TOPIC"locker_top", 1, false, tP ? "0" : "1"); + mqttClient.publish(TOPIC"locker_int", 1, false, iP ? "0" : "1"); + mqttClient.publish(TOPIC"locker_down", 1, false, dP ? "0" : "1"); + mqttClient.publish(TOPIC"locker_small", 1, false, sP ? "0" : "1"); + mqttClient.publish(TOPIC"door", 1, false, doorP ? "1" : "0"); + //mqttClient.publish(TOPIC"doorlocker", 1, false, sP || dP || iP || tP ? "1" : "0"); /*char v[7]; itoa(timeAlm, v, 10); - mqttClient.publish("/home/kor/doortimeout", 1, false, v);*/ + mqttClient.publish(TOPIC"doortimeout", 1, false, v);*/ //digitalWrite(L_WIFI2, LOW); - digitalWrite(L_MQTT, LOW); + digitalWrite(L_MQTT, HIGH); } void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { @@ -93,7 +98,7 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { if (WiFi.isConnected()) { mqttReconnectTimer.once(2, connectToMqtt); } - digitalWrite(L_MQTT, HIGH); + digitalWrite(L_MQTT, LOW); //if(WiFi.isConnected()) // digitalWrite(L_WIFI2, HIGH); } @@ -113,13 +118,13 @@ void onMqttUnsubscribe(uint16_t packetId) { } void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { -/* if(strcmp(topic, "/home/kor/doortimeout_set") == 0){ +/* if(strcmp(topic, TOPIC"doortimeout_set") == 0){ char v[7]; timeAlm = atoi(payload); EEPROM.put(0, timeAlm); EEPROM.commit(); itoa(timeAlm, v, 10); - mqttClient.publish("/home/kor/doortimeout", 0, false, v); + mqttClient.publish(TOPIC"doortimeout", 0, false, v); Serial.print("New Timeout: "); Serial.println(timeAlm); }*/ @@ -129,6 +134,7 @@ void onMqttPublish(uint16_t packetId) { Serial.println("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); + msg.start(); } void setup() { @@ -172,7 +178,7 @@ void setup() { mqttClient.setServer(mqtt_server, 1883); mqttClient.setClientId("Door"); - Wire.begin(); + Wire.begin(SCL, SDA); // Serial.println("Begin Scan"); // for (byte i = 8; i < 120; i++) // { @@ -188,10 +194,11 @@ void setup() { // } // end of good response // } // end of for loop // Serial.println("End Scan"); - Wire.beginTransmission(0x3F); + //Wire.beginTransmission(0x3F); + Wire.beginTransmission(0x20); if(Wire.endTransmission() == 0) Serial.println("PCF Found"); else Serial.println("PCF Not Found"); - pcf.begin(); + pcf.begin(SCL, SDA); // pinMode(L_TOP, INPUT_PULLUP); // pinMode(L_INT, INPUT_PULLUP); @@ -199,14 +206,19 @@ void setup() { // pinMode(L_SMALL, INPUT_PULLUP); // pinMode(DOOR, INPUT_PULLUP); pinMode(L_WIFI, OUTPUT); - pinMode(L_WIFI2, OUTPUT); + //pinMode(L_WIFI2, OUTPUT); pinMode(L_MQTT, OUTPUT); - pinMode(L_MSG, OUTPUT); - - digitalWrite(L_MQTT, HIGH); - digitalWrite(L_WIFI, HIGH); + //pinMode(L_MSG, OUTPUT); + digitalWrite(L_MQTT, LOW); + digitalWrite(L_WIFI, LOW); + //digitalWrite(L_MSG, HIGH); + Serial.printf("PCF Err=%d\n", pcf.lastError()); uint8_t pcfreg = pcf.read8(); + if(pcf.lastError() != 0) + Serial.printf("Read PCF Err=%d\n", pcf.lastError()); + else + Serial.println("Read PCF OK"); tP = pcfreg & 0x1; iP = (pcfreg >> L_INT) & 0x1; dP = (pcfreg >> L_DOWN) & 0x1; @@ -231,6 +243,7 @@ void loop() { // uint8_t sC = digitalRead(L_SMALL); uint8_t tC, iC, dC, sC; char v[10]; + msg.tick(); //door = digitalRead(DOOR); if(l_m && (l_run + 100) < millis()){ analogWrite(L_MSG, 0); @@ -248,60 +261,63 @@ void loop() { dC = (pcfreg >> L_DOWN) & 0x1; sC = (pcfreg >> L_SMALL) & 0x1; door = (pcfreg >> DOOR) & 0x1; - if(opcfreg != pcfreg){ - itoa(pcfreg, v, 2); - mqttClient.publish("/home/kor/pcf", 1, false, v); - opcfreg = pcfreg; - } + // if(opcfreg != pcfreg){ + // itoa(pcfreg, v, 2); + // mqttClient.publish(TOPIC"pcf", 1, false, v); + // opcfreg = pcfreg; + // } if(tC != tP){ Serial.println("Top Changed"); if(mqttClient.connected()){ - mqttClient.publish("/home/kor/locker_top", 1, false, tC ? "0" : "1"); + mqttClient.publish(TOPIC"locker_top", 1, false, tC ? "0" : "1"); } tP = tC; } if(iC != iP){ if(mqttClient.connected()){ - mqttClient.publish("/home/kor/locker_int", 1, false, iC ? "0" : "1"); + mqttClient.publish(TOPIC"locker_int", 1, false, iC ? "0" : "1"); } iP = iC; } if(dC != dP){ if(mqttClient.connected()){ - mqttClient.publish("/home/kor/locker_down", 1, false, dC ? "0" : "1"); + mqttClient.publish(TOPIC"locker_down", 1, false, dC ? "0" : "1"); } dP = dC; } if(sC != sP){ if(mqttClient.connected()){ - mqttClient.publish("/home/kor/locker_small", 1, false, sC ? "0" : "1"); + mqttClient.publish(TOPIC"locker_small", 1, false, sC ? "0" : "1"); } sP = sC; } if(door != doorP){ if(mqttClient.connected()){ - mqttClient.publish("/home/kor/door", 1, false, door ? "0" : "1"); + mqttClient.publish(TOPIC"door", 1, false, door ? "1" : "0"); } doorP = door; } nSec++; // if(nSec % 10 == 0){ - // char v[11]; - // itoa(pcfreg, v, 2); - // if(mqttClient.connected()) - // mqttClient.publish("/home/test/pcfreg", 0, false, v); + // Serial.println(pcfreg, 2); + // // char v[11]; + // // itoa(pcfreg, v, 2); + // // if(mqttClient.connected()) + // // mqttClient.publish("/home/test/pcfreg", 0, false, v); // } if(nSec > 99){ if(mqttClient.connected()){ char v[11]; - ultoa(millis(), v, 10); - analogWrite(L_MSG, 1); - l_run = millis(); - l_m = true; + dtostrf(millis() / 60000.0, 7, 1, v); + trim(v); + //ultoa(millis(), v, 10); + //analogWrite(L_MSG, 1); + //l_run = millis(); + //l_m = true; //r_led.start(); - mqttClient.publish("/home/kor/doormillis", 1, false, v); + mqttClient.publish(TOPIC"doormin", 1, false, v); Serial.print("Millis: "); Serial.println(millis()); } @@ -313,12 +329,12 @@ void loop() { if(mqttClient.connected()){ if (door == 0){ //Door to long time open r_led.start(); - mqttClient.publish("/home/kor/doorlong", 1, false, "1"); + mqttClient.publish(TOPIC"doorlong", 1, false, "1"); doorSent = true; } else{ //Not closed locker r_led.start(); - mqttClient.publish("/home/kor/doorlocker", 1, false, "0"); + mqttClient.publish(TOPIC"doorlocker", 1, false, "0"); lockSent = true; } } @@ -330,14 +346,43 @@ void loop() { doorSent = false; if(mqttClient.connected()) r_led.start(); - mqttClient.publish("/home/kor/doorlong", 1, false, "0"); + mqttClient.publish(TOPIC"doorlong", 1, false, "0"); } if(lockSent == true){ lockSent = false; if(mqttClient.connected()) r_led.start(); - mqttClient.publish("/home/kor/doorlocker", 1, false, "1"); + mqttClient.publish(TOPIC"doorlocker", 1, false, "1"); } }*/ } } + +void trim(char *s) +{ + // удаляем пробелы и табы с начала строки: + int i=0,j; + while((s[i]==' ')||(s[i]=='\t')) + { + i++; + } + if(i>0) + { + for(j=0; j < strlen(s); j++) + { + s[j]=s[j+i]; + } + s[j]='\0'; + } + + // удаляем пробелы и табы с конца строки: + i=strlen(s)-1; + while((s[i]==' ')||(s[i]=='\t')) + { + i--; + } + if(i < (strlen(s)-1)) + { + s[i+1]='\0'; + } +} \ No newline at end of file diff --git a/MainDoorMyS/.vscode/extensions.json b/MainDoorMyS/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/MainDoorMyS/.vscode/extensions.json +++ b/MainDoorMyS/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +}