3 changed files with 229 additions and 6 deletions
@ -0,0 +1,2 @@ |
|||||
|
# Controllo Canale Irriguo |
||||
|
|
||||
@ -1,9 +1,227 @@ |
|||||
#include <Arduino.h> |
#include <Arduino.h> |
||||
|
#include <ESP8266WiFi.h> |
||||
|
#include <ESP8266WebServer.h> |
||||
|
#include <PubSubClient.h> |
||||
|
|
||||
|
#define WLAN_SSID "TP-LINK-RPT0" |
||||
|
#define WLAN_PASS "6JM54UG4EY" |
||||
|
|
||||
|
#define MQTT_CLID "ValveControl" |
||||
|
#define MQTT_HOST "192.168.1.133" |
||||
|
#define MQTT_PORT 1883 |
||||
|
#define MQTT_USER "admin" |
||||
|
#define MQTT_PASS "22pa0799" |
||||
|
|
||||
|
void openValve(); |
||||
|
void closeValve(); |
||||
|
void stopValve(); |
||||
|
|
||||
|
void OnConnect(); |
||||
|
void OnOpen(); |
||||
|
void OnClose(); |
||||
|
void OnRestart(); |
||||
|
void OnNotFound(); |
||||
|
|
||||
|
String SendHTML(uint8_t ValveOpen); |
||||
|
|
||||
|
bool ValveOpen = false; |
||||
|
|
||||
|
int ButtonOpen = 14; |
||||
|
int ButtonClose = 12; |
||||
|
|
||||
|
int MotorPin1 = 0; |
||||
|
int MotorPin2 = 2; |
||||
|
int duration = 10000; |
||||
|
|
||||
|
ESP8266WebServer server(80); |
||||
|
|
||||
|
WiFiClient espClient; |
||||
|
PubSubClient client(espClient); |
||||
|
|
||||
|
void setup_wifi() { |
||||
|
Serial.println(); |
||||
|
Serial.print("Connecting to "); |
||||
|
Serial.println(WLAN_SSID); |
||||
|
WiFi.begin(WLAN_SSID, WLAN_PASS); |
||||
|
while (WiFi.status() != WL_CONNECTED) { |
||||
|
delay(500); |
||||
|
Serial.print("."); |
||||
|
} |
||||
|
Serial.println(); |
||||
|
Serial.println("WiFi connected"); |
||||
|
Serial.println("IP address: "); |
||||
|
Serial.println(WiFi.localIP()); |
||||
|
} |
||||
|
|
||||
|
void onMessage(char* topic, byte* payload, unsigned int length) { |
||||
|
Serial.print("Message arrived on topic "); |
||||
|
Serial.print(topic); |
||||
|
Serial.print(": "); |
||||
|
for (unsigned int i=0; i < length; i++) { |
||||
|
Serial.print((char)payload[i]); |
||||
|
} |
||||
|
Serial.println(); |
||||
|
const char * command = (char *)payload; |
||||
|
if (!strncmp(command, "open", length)) { |
||||
|
openValve(); |
||||
|
} else if (!strncmp(command, "close", length)) { |
||||
|
closeValve(); |
||||
|
} else { |
||||
|
Serial.println("Unnown command"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void reconnect() { |
||||
|
while (!client.connected()) { |
||||
|
Serial.println("Attempting MQTT connection..."); |
||||
|
if (client.connect(MQTT_CLID, MQTT_USER, MQTT_PASS)) { |
||||
|
Serial.println("MQTT connected"); |
||||
|
client.subscribe("/valve/command"); |
||||
|
} else { |
||||
|
Serial.print("Connection failed, rc="); |
||||
|
Serial.print(client.state()); |
||||
|
Serial.println(" try again in 5 seconds"); |
||||
|
delay(5000); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void setup_mqtt() { |
||||
|
client.setServer(MQTT_HOST, MQTT_PORT); |
||||
|
client.setCallback(onMessage); |
||||
|
} |
||||
|
|
||||
|
void setup_http() { |
||||
|
Serial.println("Starting HTTP server..."); |
||||
|
server.on("/", OnConnect); |
||||
|
server.on("/open", OnOpen); |
||||
|
server.on("/close", OnClose); |
||||
|
server.on("/restart", OnRestart); |
||||
|
server.onNotFound(OnNotFound); |
||||
|
server.begin(); |
||||
|
Serial.println("HTTP server started"); |
||||
|
} |
||||
|
|
||||
void setup() { |
void setup() { |
||||
// put your setup code here, to run once:
|
|
||||
|
Serial.begin(115200); |
||||
|
while(!Serial) { |
||||
|
delay(100); |
||||
|
} |
||||
|
|
||||
|
pinMode(ButtonOpen, INPUT); |
||||
|
pinMode(ButtonClose, INPUT); |
||||
|
|
||||
|
pinMode(MotorPin1, OUTPUT); |
||||
|
pinMode(MotorPin2, OUTPUT); |
||||
|
|
||||
|
setup_wifi(); |
||||
|
setup_mqtt(); |
||||
|
setup_http(); |
||||
|
|
||||
} |
} |
||||
|
|
||||
void loop() { |
void loop() { |
||||
// put your main code here, to run repeatedly:
|
|
||||
|
if (!client.connected()) { |
||||
|
reconnect(); |
||||
|
} |
||||
|
client.loop(); |
||||
|
|
||||
|
server.handleClient(); |
||||
|
|
||||
|
int buttonOpenState = digitalRead(ButtonOpen); |
||||
|
int buttonCloseState = digitalRead(ButtonClose); |
||||
|
|
||||
|
if(buttonOpenState == LOW) { |
||||
|
buttonCloseState = !buttonOpenState; |
||||
|
digitalWrite(MotorPin1, LOW); |
||||
|
digitalWrite(MotorPin2, HIGH); |
||||
|
ValveOpen = true; |
||||
|
} |
||||
|
|
||||
|
if(buttonCloseState == LOW) { |
||||
|
buttonOpenState = !buttonCloseState; |
||||
|
digitalWrite(MotorPin1, HIGH); |
||||
|
digitalWrite(MotorPin2, LOW); |
||||
|
ValveOpen = false; |
||||
|
} |
||||
|
|
||||
|
if(buttonOpenState == HIGH && buttonCloseState == HIGH) { |
||||
|
stopValve(); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
void OnConnect() { |
||||
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
||||
|
} |
||||
|
|
||||
|
void OnOpen() { |
||||
|
openValve(); |
||||
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
||||
|
} |
||||
|
|
||||
|
void OnClose() { |
||||
|
closeValve(); |
||||
|
server.send(200, "text/html", SendHTML(ValveOpen)); |
||||
|
} |
||||
|
|
||||
|
void OnRestart() { |
||||
|
server.send(200, "text/plain","Restarting..."); |
||||
|
delay(1000); |
||||
|
ESP.restart(); |
||||
|
} |
||||
|
|
||||
|
void OnNotFound(){ |
||||
|
server.send(404, "text/plain", "Not found"); |
||||
|
} |
||||
|
|
||||
|
void openValve() { |
||||
|
Serial.println("Opening valve..."); |
||||
|
digitalWrite(MotorPin1, LOW); |
||||
|
digitalWrite(MotorPin2, HIGH); |
||||
|
delay(duration); |
||||
|
stopValve(); |
||||
|
ValveOpen = HIGH; |
||||
|
} |
||||
|
|
||||
|
void closeValve() { |
||||
|
Serial.println("Closing valve..."); |
||||
|
digitalWrite(MotorPin1, HIGH); |
||||
|
digitalWrite(MotorPin2, LOW); |
||||
|
delay(duration); |
||||
|
stopValve(); |
||||
|
ValveOpen = false; |
||||
|
} |
||||
|
|
||||
|
void stopValve() { |
||||
|
digitalWrite(MotorPin1, LOW); |
||||
|
digitalWrite(MotorPin2, LOW); |
||||
|
} |
||||
|
|
||||
|
String SendHTML(uint8_t ValveOpen) { |
||||
|
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"; |
||||
|
ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"; |
||||
|
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n"; |
||||
|
ptr +=".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n"; |
||||
|
ptr +=".button-open {background-color: #1abc9c;}\n"; |
||||
|
ptr +=".button-open:active {background-color: #16a085;}\n"; |
||||
|
ptr +=".button-close {background-color: #34495e;}\n"; |
||||
|
ptr +=".button-close:active {background-color: #2c3e50;}\n"; |
||||
|
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n"; |
||||
|
ptr +="</style>\n"; |
||||
|
ptr +="</head>\n"; |
||||
|
ptr +="<body>\n"; |
||||
|
ptr +="<h1>Linear Actuator</h1>\n"; |
||||
|
if(ValveOpen) { |
||||
|
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"; |
||||
|
} |
||||
|
ptr +="</body>\n"; |
||||
|
ptr +="</html>\n"; |
||||
|
return ptr; |
||||
} |
} |
||||
Loading…
Reference in new issue