From 5c5ef929fa4c52efdd9b846215824665c01c0953 Mon Sep 17 00:00:00 2001 From: lexa Date: Sun, 20 Mar 2022 17:25:10 +0300 Subject: [PATCH] Add WebSerial --- ESP_BigRoom/include/main.h | 5 +- ESP_BigRoom/platformio.ini | 3 +- ESP_BigRoom/src/main.cpp | 102 ++++++++++++++++++++++++++----------- 3 files changed, 76 insertions(+), 34 deletions(-) diff --git a/ESP_BigRoom/include/main.h b/ESP_BigRoom/include/main.h index f648e20..d8d0ace 100644 --- a/ESP_BigRoom/include/main.h +++ b/ESP_BigRoom/include/main.h @@ -19,6 +19,7 @@ extern "C" { #include #include #include +#include #include #define BAUDRATE 9600 @@ -27,9 +28,9 @@ extern "C" { #define MOVE_SENS 39 //VN -#define LED_WHITE 25 //25 +#define LED_WHITE 26 //25 #define PWM_WHITE 0 -#define LED_BLUE 26 //26 +#define LED_BLUE 25 //26 #define PWM_BLUE 1 #define POWER 27 //27 #define TXD2 17 diff --git a/ESP_BigRoom/platformio.ini b/ESP_BigRoom/platformio.ini index 155443d..1a667b0 100644 --- a/ESP_BigRoom/platformio.ini +++ b/ESP_BigRoom/platformio.ini @@ -25,8 +25,9 @@ lib_deps = ottowinter/AsyncMqttClient-esphome @ ^0.8.6 ottowinter/ESPAsyncWebServer-esphome @ ^2.1.0 ayushsharma82/AsyncElegantOTA @ ^2.2.6 + ayushsharma82/WebSerial @ ^1.3.0 ;upload_protocol = espota -;upload_port = 192.168.1.31 +;upload_port = 192.168.1.138 [env:esp12] platform = espressif8266 diff --git a/ESP_BigRoom/src/main.cpp b/ESP_BigRoom/src/main.cpp index 915aafd..e205b3d 100644 --- a/ESP_BigRoom/src/main.cpp +++ b/ESP_BigRoom/src/main.cpp @@ -8,8 +8,10 @@ AsyncMqttClient mqttClient; TimerHandle_t mqttReconnectTimer; TimerHandle_t wifiReconnectTimer; -const char* ssid = "wf-home"; -const char* password = "0ndthnrf"; +//const char* ssid = "wf-home"; +//const char* password = "0ndthnrf"; +const char* ssid = "mt-24"; +const char* password = "727479-o"; const char* mqtt_server = "192.168.1.111"; @@ -73,6 +75,7 @@ void setup() { pinMode(LED_R, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(LED_B, OUTPUT); + pinMode(POWER, OUTPUT); ledcSetup(PWM_WHITE, 5000, 8); ledcSetup(PWM_BLUE, 5000, 8); ledcAttachPin(LED_WHITE, PWM_WHITE); @@ -83,8 +86,9 @@ void setup() { digitalWrite(LED_G, HIGH); digitalWrite(LED_B, HIGH); Serial2.begin(9600); - myMHZ19.begin(Serial2); // *Important, Pass your Stream reference here - myMHZ19.autoCalibration(false); + myMHZ19.begin(Serial2); + myMHZ19.setRange(); // *Important, Pass your Stream reference here + myMHZ19.autoCalibration(true); char myVersion[4]; myMHZ19.getVersion(myVersion); @@ -171,8 +175,7 @@ void setup() { }); AsyncElegantOTA.begin(&server); // Start ElegantOTA - server.begin(); - Serial.println("HTTP server started"); + WebSerial.begin(&server); connectToWifi(); } @@ -180,6 +183,7 @@ void setup() { void loop() { static uint32_t cRun = millis(); static uint8_t sec = 0; + static uint16_t prevLLevel = 0; char s[7]; //ArduinoOTA.handle(); @@ -201,6 +205,7 @@ void loop() { statLamp |= 0x01; bLamp = true; Serial.println(F("BLamp On")); + WebSerial.println(F("Blue Lamp On")); } else if((LightLev > (spLight + dbLight)) || (move == 0)){ ledcWrite(PWM_BLUE, 0); @@ -208,10 +213,11 @@ void loop() { bLamp = false; } if(lightWhite && !wLamp){ - ledcWrite(PWM_WHITE, (levelBlue) * 2.55); + ledcWrite(PWM_WHITE, (levelWhite) * 2.55); statLamp |= 0x02; wLamp = true; Serial.println(F("WLamp On")); + WebSerial.println(F("White Lamp On")); } else if (!lightWhite){ ledcWrite(PWM_WHITE, 0); @@ -220,9 +226,13 @@ void loop() { } if(statLamp > 0) digitalWrite(POWER, HIGH); else digitalWrite(POWER, LOW); - if((cRun + 1000) <= millis()){ //Once per second cRun = millis(); + if(abs(LightLev - prevLLevel) > 100){ + itoa(LightLev, s, 10); + mqttClient.publish(TOPIC"lightlev", 1, false, s); + } + prevLLevel = LightLev; if(curDelay > -1){ Serial.println("ADC: " + String(LightLev) + " MV:" + String(move) + " TMv:" + curDelay + " SL:" + String(statLamp) + " PS:" + digitalRead(POWER)); Serial.println("SPL: " + String(spLight) + " DBL:" + String(dbLight)+ " BL:" + String(bLamp)); @@ -279,6 +289,8 @@ void loop() { bled.start(); dtostrf(cRun / 60000.0, 6, 1, s); mqttClient.publish(TOPIC"mins", 1, false, s); + itoa(WiFi.RSSI(), s, 10); + mqttClient.publish(TOPIC"RSSI", 1, false, s); itoa(LightLev, s, 10); mqttClient.publish(TOPIC"lightlev", 1, false, s); } @@ -304,12 +316,15 @@ void WiFiEvent(WiFiEvent_t event) { Serial.println("IP address: "); Serial.println(WiFi.localIP()); digitalWrite(LED_R, HIGH); + server.begin(); + Serial.println("HTTP server started"); connectToMqtt(); break; case SYSTEM_EVENT_STA_DISCONNECTED: Serial.println("WiFi lost connection"); xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi xTimerStart(wifiReconnectTimer, 0); + server.end(); digitalWrite(LED_R, LOW); break; default: @@ -325,12 +340,6 @@ void onMqttConnect(bool sessionPresent) { // Serial.print("Subscribing Lamp1, packetId: "); // Serial.println(packetIdSub); //packetIdSub = - mqttClient.subscribe(TOPIC"lampblevel", 1); - mqttClient.subscribe(TOPIC"lampwlevel", 1); - mqttClient.subscribe(TOPIC"lampw", 1); - mqttClient.subscribe(TOPIC"moveperiod", 1); - mqttClient.subscribe(TOPIC"spLevel", 1); - mqttClient.subscribe(TOPIC"dbLevel", 1); char s[7]; itoa(periodMotion, s, 10); @@ -344,6 +353,15 @@ void onMqttConnect(bool sessionPresent) { itoa(dbLight, s, 10); mqttClient.publish(TOPIC"dbLevel", 1, false, s); mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0"); + mqttClient.publish(TOPIC"lampw_set", 1, false, lightWhite ? "1" : "0"); + + mqttClient.subscribe(TOPIC"lampblevel", 1); + mqttClient.subscribe(TOPIC"lampwlevel", 1); + //mqttClient.subscribe(TOPIC"lampw", 1); + mqttClient.subscribe(TOPIC"lampw_set", 1); + mqttClient.subscribe(TOPIC"moveperiod", 1); + mqttClient.subscribe(TOPIC"spLevel", 1); + mqttClient.subscribe(TOPIC"dbLevel", 1); Serial.print(F("PeriodMove:"));Serial.println(periodMotion); Serial.print(F("SPLight:"));Serial.println(spLight); @@ -382,42 +400,64 @@ void onMqttUnsubscribe(uint16_t packetId) { void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { bool w = false; + char pl[20]; + strncpy(pl, payload, len); + pl[len] = 0; + Serial.printf("New message - topic %s, payload %s\n", topic, pl); + WebSerial.print("New message - topic ");WebSerial.print(topic);WebSerial.print(", payload ");WebSerial.println(pl); if(strcmp(topic, TOPIC"lampblevel") == 0){ - levelBlue = atoi(payload); - EEPROM.put(5, levelBlue); + levelBlue = atoi(pl); + EEPROM.writeByte(5, levelBlue); + Serial.printf("New LevelBlue: %d\n", levelBlue); + WebSerial.print("New LevelBlue: ");WebSerial.println(levelBlue); w = true; + if(bLamp) + ledcWrite(PWM_BLUE, (levelBlue) * 2.55); } if(strcmp(topic, TOPIC"lampwlevel") == 0){ - levelWhite = atoi(payload); - EEPROM.put(6, levelWhite); + levelWhite = atoi(pl); + Serial.printf("New LevelWite: %d\n", levelWhite); + WebSerial.print("New LevelWhite: ");WebSerial.println(levelWhite); + EEPROM.writeByte(6, levelWhite); w = true; + if(lightWhite) + ledcWrite(PWM_WHITE, (levelWhite) * 2.55); } - if(strcmp(topic, TOPIC"lampw") == 0){ - lightWhite = atoi(payload); + if(strcmp(topic, TOPIC"lampw_set") == 0){ + lightWhite = atoi(pl); + Serial.printf("New LampWhite: %d\n", lightWhite); + WebSerial.print("New LampWhite: ");WebSerial.println(lightWhite); + mqttClient.publish(TOPIC"lampw", 1, false, lightWhite ? "1" : "0"); //EEPROM.put(0, levelBlue); //w = true; } if(strcmp(topic, TOPIC"moveperiod") == 0){ - periodMotion = atoi(payload); - EEPROM.put(0, periodMotion); + periodMotion = atoi(pl); + Serial.printf("New MovePeriod: %d\n", periodMotion); + WebSerial.print("New MovePeriod: ");WebSerial.println(periodMotion); + EEPROM.writeByte(0, periodMotion); w = true; } - if(strcmp(topic, TOPIC"splevel") == 0){ - spLight = atoi(payload); - EEPROM.put(1, spLight); + if(strcmp(topic, TOPIC"spLevel") == 0){ + spLight = atoi(pl); + Serial.printf("New SPLevel: %d\n", spLight); + WebSerial.print("New SPLevel: ");WebSerial.println(spLight); + EEPROM.writeShort(1, spLight); w = true; } - if(strcmp(topic, TOPIC"dblevel") == 0){ - dbLight = atoi(payload); - EEPROM.put(3, dbLight); + if(strcmp(topic, TOPIC"dbLevel") == 0){ + dbLight = atoi(pl); + Serial.printf("New DBLevel: %d\n", dbLight); + WebSerial.print("New DBLevel: ");WebSerial.println(dbLight); + EEPROM.writeShort(3, dbLight); w = true; } if(w) EEPROM.commit(); } void onMqttPublish(uint16_t packetId) { - Serial.println("Publish acknowledged."); - Serial.print(" packetId: "); - Serial.println(packetId); + // Serial.println("Publish acknowledged."); + // Serial.print(" packetId: "); + // Serial.println(packetId); //g_led.start(); }