diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad0de1a --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Controllo Canale Irriguo + diff --git a/platformio.ini b/platformio.ini index ea23b77..e4f2df1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -8,7 +8,10 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:uno] -platform = atmelavr -board = uno +[env:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 framework = arduino +upload_protocol = esptool +lib_deps = + knolleary/PubSubClient@^2.8 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 58b344c..419080e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,227 @@ #include +#include +#include +#include + +#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() { - // 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() { - // put your main code here, to run repeatedly: -} \ No newline at end of file + + 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 = " \n"; + ptr +="\n"; + ptr +="Linear Actuator\n"; + ptr +="\n"; + ptr +="\n"; + ptr +="\n"; + ptr +="

Linear Actuator

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

Valve is OPEN

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

Valve is CLOSED

OPEN\n"; + } + ptr +="\n"; + ptr +="\n"; + return ptr; +}