Browse Source

OTA Update

esp32ota
ptrinchini 3 weeks ago
parent
commit
f6e61ffb7c
  1. 13
      platformio.ini
  2. 8
      src/credentials.h
  3. 40
      src/main.cpp

13
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
[env:ota]
platform = espressif32
board = esp32dev
framework = arduino
upload_protocol = espota
upload_port = 192.168.1.235
lib_deps =
knolleary/PubSubClient@^2.8

8
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"

40
src/main.cpp

@ -1,18 +1,14 @@
#include <Arduino.h>
#include <WiFi.h>
#include <ArduinoOTA.h>
#include <WebServer.h>
#include <PubSubClient.h>
#define WLAN_SSID "TP-LINK-OUT"
#define WLAN_PASS "6JM54UG4EX"
#include "credentials.h"
#define MQTT_CLID "ValveControl"
#define MQTT_HOST "192.168.1.146"
#define MQTT_PORT 1883
#define MQTT_USER "admin"
#define MQTT_PASS "22pa0799"
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;
@ -40,6 +36,11 @@ 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 = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>Linear Actuator</title>\n";
@ -218,7 +222,7 @@ String SendHTML(uint8_t ValveOpen) {
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>Linear Actuator</h1>\n";
if(ValveOpen) {
if(ValveState == HIGH) {
ptr +="<p>Valve is OPEN</p><a class=\"button button-close\" href=\"/close\">CLOSE</a>\n";
} else {
ptr +="<p>Valve is CLOSED</p><a class=\"button button-open\" href=\"/open\">OPEN</a>\n";

Loading…
Cancel
Save