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. 74
      src/main.cpp

3
.vscode/extensions.json

@ -3,5 +3,8 @@
// for the documentation about the extensions.json format
"recommendations": [
"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
; https://docs.platformio.org/page/projectconf.html
[env:nodemcuv2]
platform = espressif8266
board = nodemcuv2
[platformio]
default_envs = com, ota
[env:com]
platform = espressif32
board = esp32dev
framework = arduino
upload_protocol = esptool
lib_deps =
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"

74
src/main.cpp

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

Loading…
Cancel
Save