From f6e61ffb7c8b4c135460a3c5fbfa0383a701f863 Mon Sep 17 00:00:00 2001 From: ptrinchini Date: Tue, 2 Jun 2026 12:05:57 +0200 Subject: [PATCH] OTA Update --- platformio.ini | 15 +++++++++++++-- src/credentials.h | 8 ++++++++ src/main.cpp | 48 +++++++++++++++++++++++++---------------------- 3 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 src/credentials.h diff --git a/platformio.ini b/platformio.ini index 8782fc7..b8005cf 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,11 +8,22 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[platformio] +default_envs = com, ota -[env:esp32dev] +[env:com] platform = espressif32 board = esp32dev framework = arduino upload_protocol = esptool lib_deps = - knolleary/PubSubClient@^2.8 \ No newline at end of file + knolleary/PubSubClient@^2.8 + +[env:ota] +platform = espressif32 +board = esp32dev +framework = arduino +upload_protocol = espota +upload_port = 192.168.1.235 +lib_deps = + knolleary/PubSubClient@^2.8 diff --git a/src/credentials.h b/src/credentials.h new file mode 100644 index 0000000..4cc5bda --- /dev/null +++ b/src/credentials.h @@ -0,0 +1,8 @@ +#define WLAN_SSID "TP-LINK-OUT" +#define WLAN_PASS "6JM54UG4EX" + +#define MQTT_USER "admin" +#define MQTT_PASS "22pa0799" + +#define OTA_USER "admin" +#define OTA_PASS "Ut@meri$22" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 34e63e4..47b3f8b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,14 @@ #include #include +#include #include - #include +#include "credentials.h" -#define WLAN_SSID "TP-LINK-OUT" -#define WLAN_PASS "6JM54UG4EX" - -#define MQTT_CLID "ValveControl" -#define MQTT_HOST "192.168.1.146" -#define MQTT_PORT 1883 -#define MQTT_USER "admin" -#define MQTT_PASS "22pa0799" +#define MQTT_CLID "ValveControl" +#define MQTT_HOST "192.168.1.146" +#define MQTT_PORT 1883 void openValve(); void closeValve(); @@ -24,9 +20,9 @@ void OnClose(); void OnRestart(); void OnNotFound(); -String SendHTML(uint8_t ValveOpen); +String SendHTML(uint8_t ValveState); -bool ValveOpen = false; +bool ValveState = LOW; int ButtonOpen = 16; int ButtonClose = 19; @@ -39,7 +35,12 @@ WebServer server(80); WiFiClient espClient; PubSubClient client(espClient); - + +void setup_ota() { + ArduinoOTA.setPassword(OTA_PASS); + ArduinoOTA.begin(); +} + void setup_wifi() { Serial.println(); Serial.print("Connecting to "); @@ -53,6 +54,8 @@ void setup_wifi() { Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); + Serial.print("Starting OTA..."); + } void onMessage(char* topic, byte* payload, unsigned int length) { @@ -118,12 +121,13 @@ void setup() { pinMode(MotorPin2, OUTPUT); setup_wifi(); + setup_ota(); setup_mqtt(); setup_http(); - } void loop() { + ArduinoOTA.handle(); if (!client.connected()) { reconnect(); @@ -139,14 +143,14 @@ void loop() { buttonCloseState = !buttonOpenState; digitalWrite(MotorPin1, LOW); digitalWrite(MotorPin2, HIGH); - ValveOpen = true; + ValveState = HIGH; } if(buttonCloseState == LOW) { buttonOpenState = !buttonCloseState; digitalWrite(MotorPin1, HIGH); digitalWrite(MotorPin2, LOW); - ValveOpen = false; + ValveState = LOW; } if(buttonOpenState == HIGH && buttonCloseState == HIGH) { @@ -156,17 +160,17 @@ void loop() { } void OnConnect() { - server.send(200, "text/html", SendHTML(ValveOpen)); + server.send(200, "text/html", SendHTML(ValveState)); } void OnOpen() { openValve(); - server.send(200, "text/html", SendHTML(ValveOpen)); + server.send(200, "text/html", SendHTML(ValveState)); } void OnClose() { closeValve(); - server.send(200, "text/html", SendHTML(ValveOpen)); + server.send(200, "text/html", SendHTML(ValveState)); } void OnRestart() { @@ -185,7 +189,7 @@ void openValve() { digitalWrite(MotorPin2, HIGH); delay(duration); stopValve(); - ValveOpen = HIGH; + ValveState = HIGH; } void closeValve() { @@ -194,7 +198,7 @@ void closeValve() { digitalWrite(MotorPin2, LOW); delay(duration); stopValve(); - ValveOpen = false; + ValveState = LOW; } void stopValve() { @@ -202,7 +206,7 @@ void stopValve() { digitalWrite(MotorPin2, LOW); } -String SendHTML(uint8_t ValveOpen) { +String SendHTML(uint8_t ValveState) { String ptr = " \n"; ptr +="\n"; ptr +="Linear Actuator\n"; @@ -218,7 +222,7 @@ String SendHTML(uint8_t ValveOpen) { ptr +="\n"; ptr +="\n"; ptr +="

Linear Actuator

\n"; - if(ValveOpen) { + if(ValveState == HIGH) { ptr +="

Valve is OPEN

CLOSE\n"; } else { ptr +="

Valve is CLOSED

OPEN\n";