Compare commits

...

6 Commits

Author SHA1 Message Date
Patrizio Trinchini 49cad936b9 Update access point 3 weeks ago
Patrizio Trinchini f433a2c191 Ota Conig 3 weeks ago
ptrinchini f6e61ffb7c OTA Update 3 weeks ago
ptrinchini 08e71b96e2 Changed input and output pin 2 months ago
ptrinchini 54bee65185 Internal pullup 2 months ago
ptrinchini b779613fc0 Fix libraries 2 months ago
  1. 3
      .vscode/extensions.json
  2. 19
      platformio.ini
  3. 8
      src/credentials.h
  4. 76
      src/main.cpp

3
.vscode/extensions.json

@ -3,5 +3,8 @@
// for the documentation about the extensions.json format // for the documentation about the extensions.json format
"recommendations": [ "recommendations": [
"platformio.platformio-ide" "platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
] ]
} }

19
platformio.ini

@ -8,10 +8,23 @@
; Please visit documentation for the other options and examples ; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:nodemcuv2] [platformio]
platform = espressif8266 default_envs = com, ota
board = nodemcuv2
[env:com]
platform = espressif32
board = esp32dev
framework = arduino framework = arduino
upload_protocol = esptool upload_protocol = esptool
lib_deps = lib_deps =
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
[env:ota]
platform = espressif32
board = esp32dev
framework = arduino
upload_protocol = espota
upload_port = 192.168.1.235
upload_flags = --auth=P@ssw0rd
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 "P@ssw0rd"

76
src/main.cpp

@ -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";

Loading…
Cancel
Save