|
|
|
@ -1,18 +1,14 @@ |
|
|
|
#include <Arduino.h> |
|
|
|
|
|
|
|
#include <WiFi.h> |
|
|
|
#include <ArduinoOTA.h> |
|
|
|
#include <WebServer.h> |
|
|
|
|
|
|
|
#include <PubSubClient.h> |
|
|
|
#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 = "<!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"; |
|
|
|
|