|
|
@ -1,16 +1,17 @@ |
|
|
#include <Arduino.h> |
|
|
#include <Arduino.h> |
|
|
#include <ESP8266WiFi.h> |
|
|
|
|
|
#include <ESP8266WebServer.h> |
|
|
#include <WiFi.h> |
|
|
|
|
|
#include <ArduinoOTA.h> |
|
|
|
|
|
#include <WebServer.h> |
|
|
#include <PubSubClient.h> |
|
|
#include <PubSubClient.h> |
|
|
|
|
|
#include "credentials.h" |
|
|
|
|
|
|
|
|
#define WLAN_SSID "TP-LINK-RPT0" |
|
|
#define MQTT_CLID "ValveControl" |
|
|
#define WLAN_PASS "6JM54UG4EY" |
|
|
#define MQTT_HOST "192.168.1.146" |
|
|
|
|
|
#define MQTT_PORT 1883 |
|
|
|
|
|
|
|
|
#define MQTT_CLID "ValveControl" |
|
|
#define OPENED HIGH |
|
|
#define MQTT_HOST "192.168.1.133" |
|
|
#define CLOSED LOW |
|
|
#define MQTT_PORT 1883 |
|
|
|
|
|
#define MQTT_USER "admin" |
|
|
|
|
|
#define MQTT_PASS "22pa0799" |
|
|
|
|
|
|
|
|
|
|
|
void openValve(); |
|
|
void openValve(); |
|
|
void closeValve(); |
|
|
void closeValve(); |
|
|
@ -22,22 +23,26 @@ void OnClose(); |
|
|
void OnRestart(); |
|
|
void OnRestart(); |
|
|
void OnNotFound(); |
|
|
void OnNotFound(); |
|
|
|
|
|
|
|
|
String SendHTML(uint8_t ValveOpen); |
|
|
String SendHTML(uint8_t ValveState); |
|
|
|
|
|
|
|
|
bool ValveOpen = false; |
|
|
bool ValveState = LOW; |
|
|
|
|
|
|
|
|
int ButtonOpen = 14; |
|
|
int ButtonOpen = 16; |
|
|
int ButtonClose = 12; |
|
|
int ButtonClose = 19; |
|
|
|
|
|
|
|
|
int MotorPin1 = 0; |
|
|
int MotorPin1 = 25; // estende il braccio
|
|
|
int MotorPin2 = 2; |
|
|
int MotorPin2 = 26; // ritrae il braccio
|
|
|
int duration = 10000; |
|
|
|
|
|
|
|
|
|
|
|
ESP8266WebServer server(80); |
|
|
WebServer server(80); |
|
|
|
|
|
|
|
|
WiFiClient espClient; |
|
|
WiFiClient espClient; |
|
|
PubSubClient client(espClient); |
|
|
PubSubClient client(espClient); |
|
|
|
|
|
|
|
|
|
|
|
void setup_ota() { |
|
|
|
|
|
ArduinoOTA.setPassword(OTA_PASS); |
|
|
|
|
|
ArduinoOTA.begin(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void setup_wifi() { |
|
|
void setup_wifi() { |
|
|
Serial.println(); |
|
|
Serial.println(); |
|
|
Serial.print("Connecting to "); |
|
|
Serial.print("Connecting to "); |
|
|
@ -49,8 +54,10 @@ void setup_wifi() { |
|
|
} |
|
|
} |
|
|
Serial.println(); |
|
|
Serial.println(); |
|
|
Serial.println("WiFi connected"); |
|
|
Serial.println("WiFi connected"); |
|
|
Serial.println("IP address: "); |
|
|
Serial.print("IP address: "); |
|
|
Serial.println(WiFi.localIP()); |
|
|
Serial.println(WiFi.localIP()); |
|
|
|
|
|
Serial.println("Starting OTA..."); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void onMessage(char* topic, byte* payload, unsigned int length) { |
|
|
void onMessage(char* topic, byte* payload, unsigned int length) { |
|
|
@ -104,13 +111,13 @@ void setup_http() { |
|
|
|
|
|
|
|
|
void setup() { |
|
|
void setup() { |
|
|
|
|
|
|
|
|
Serial.begin(115200); |
|
|
Serial.begin(9600); |
|
|
while(!Serial) { |
|
|
while(!Serial) { |
|
|
delay(100); |
|
|
delay(100); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pinMode(ButtonOpen, INPUT); |
|
|
pinMode(ButtonOpen, INPUT_PULLUP); |
|
|
pinMode(ButtonClose, INPUT); |
|
|
pinMode(ButtonClose, INPUT_PULLUP); |
|
|
|
|
|
|
|
|
pinMode(MotorPin1, OUTPUT); |
|
|
pinMode(MotorPin1, OUTPUT); |
|
|
pinMode(MotorPin2, OUTPUT); |
|
|
pinMode(MotorPin2, OUTPUT); |
|
|
@ -118,10 +125,11 @@ void setup() { |
|
|
setup_wifi(); |
|
|
setup_wifi(); |
|
|
setup_mqtt(); |
|
|
setup_mqtt(); |
|
|
setup_http(); |
|
|
setup_http(); |
|
|
|
|
|
setup_ota(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
|
|
|
ArduinoOTA.handle(); |
|
|
|
|
|
|
|
|
if (!client.connected()) { |
|
|
if (!client.connected()) { |
|
|
reconnect(); |
|
|
reconnect(); |
|
|
@ -137,14 +145,16 @@ void loop() { |
|
|
buttonCloseState = !buttonOpenState; |
|
|
buttonCloseState = !buttonOpenState; |
|
|
digitalWrite(MotorPin1, LOW); |
|
|
digitalWrite(MotorPin1, LOW); |
|
|
digitalWrite(MotorPin2, HIGH); |
|
|
digitalWrite(MotorPin2, HIGH); |
|
|
ValveOpen = true; |
|
|
|
|
|
|
|
|
ValveState = OPENED; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(buttonCloseState == LOW) { |
|
|
if(buttonCloseState == LOW) { |
|
|
buttonOpenState = !buttonCloseState; |
|
|
buttonOpenState = !buttonCloseState; |
|
|
digitalWrite(MotorPin1, HIGH); |
|
|
digitalWrite(MotorPin1, HIGH); |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
ValveOpen = false; |
|
|
|
|
|
|
|
|
ValveState = CLOSED; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(buttonOpenState == HIGH && buttonCloseState == HIGH) { |
|
|
if(buttonOpenState == HIGH && buttonCloseState == HIGH) { |
|
|
@ -154,17 +164,17 @@ void loop() { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void OnConnect() { |
|
|
void OnConnect() { |
|
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
|
|
server.send(200, "text/html", SendHTML(ValveState)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void OnOpen() { |
|
|
void OnOpen() { |
|
|
openValve(); |
|
|
openValve(); |
|
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
|
|
server.send(200, "text/html", SendHTML(ValveState)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void OnClose() { |
|
|
void OnClose() { |
|
|
closeValve(); |
|
|
closeValve(); |
|
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
|
|
server.send(200, "text/html", SendHTML(ValveState)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void OnRestart() { |
|
|
void OnRestart() { |
|
|
@ -181,18 +191,14 @@ void openValve() { |
|
|
Serial.println("Opening valve..."); |
|
|
Serial.println("Opening valve..."); |
|
|
digitalWrite(MotorPin1, LOW); |
|
|
digitalWrite(MotorPin1, LOW); |
|
|
digitalWrite(MotorPin2, HIGH); |
|
|
digitalWrite(MotorPin2, HIGH); |
|
|
delay(duration); |
|
|
ValveState = OPENED; |
|
|
stopValve(); |
|
|
|
|
|
ValveOpen = HIGH; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void closeValve() { |
|
|
void closeValve() { |
|
|
Serial.println("Closing valve..."); |
|
|
Serial.println("Closing valve..."); |
|
|
digitalWrite(MotorPin1, HIGH); |
|
|
digitalWrite(MotorPin1, HIGH); |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
delay(duration); |
|
|
ValveState = CLOSED; |
|
|
stopValve(); |
|
|
|
|
|
ValveOpen = false; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void stopValve() { |
|
|
void stopValve() { |
|
|
@ -200,7 +206,7 @@ void stopValve() { |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
digitalWrite(MotorPin2, LOW); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String SendHTML(uint8_t ValveOpen) { |
|
|
String SendHTML(uint8_t ValveState) { |
|
|
String ptr = "<!DOCTYPE html> <html>\n"; |
|
|
String ptr = "<!DOCTYPE html> <html>\n"; |
|
|
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; |
|
|
ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"; |
|
|
ptr +="<title>Linear Actuator</title>\n"; |
|
|
ptr +="<title>Linear Actuator</title>\n"; |
|
|
@ -216,7 +222,7 @@ String SendHTML(uint8_t ValveOpen) { |
|
|
ptr +="</head>\n"; |
|
|
ptr +="</head>\n"; |
|
|
ptr +="<body>\n"; |
|
|
ptr +="<body>\n"; |
|
|
ptr +="<h1>Linear Actuator</h1>\n"; |
|
|
ptr +="<h1>Linear Actuator</h1>\n"; |
|
|
if(ValveOpen) { |
|
|
if(ValveState == OPENED) { |
|
|
ptr +="<p>Valve is OPEN</p><a class=\"button button-close\" href=\"/close\">CLOSE</a>\n"; |
|
|
ptr +="<p>Valve is OPEN</p><a class=\"button button-close\" href=\"/close\">CLOSE</a>\n"; |
|
|
} else { |
|
|
} else { |
|
|
ptr +="<p>Valve is CLOSED</p><a class=\"button button-open\" href=\"/open\">OPEN</a>\n"; |
|
|
ptr +="<p>Valve is CLOSED</p><a class=\"button button-open\" href=\"/open\">OPEN</a>\n"; |
|
|
|