Hello,
ceci n’est pas un tuto mais plutôt une demande d’aide pour fiabiliser mon système =)
J’explique :
j’ai un bouton de porte de garage, ainsi qu’une mini télécommande pour ouvrir mon portail, et j’ai tenté de les domotiser en simulant un appui à l’aide d’un Servo connecté à un NodeMCU, le tout piloté en wifi par jeedom.
Voici le code utilisé pour la télécommande de portail :
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h> // Include the WebServer library
#include <Servo.h>
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
void handleRoot(); // function prototypes for HTTP handlers
void handleNotFound();
const char* ssid = "XXX";
const char* password = "XXX";
Servo servo;
int SERVO_PIN = 2; // GPIO2 of nodemcu
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
wifiMulti.addAP(ssid, password); // add Wi-Fi networks you want to connect to
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
server.on("/", HTTP_GET, handleRoot); // Call the 'handleRoot' function when a client requests URI "/"
server.on("/servoGrand", HTTP_POST, handleServoGrand); // Call the 'handleServo' function when a POST request is made to URI "/servo"
server.on("/servoPetit", HTTP_POST, handleServoPetit); // Call the 'handleServo' function when a POST request is made to URI "/servo"
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
server.begin(); // Actually start the server
Serial.println("HTTP server started");
servo.attach(SERVO_PIN); // attach servo
servo.write(15); // place servo in base position (15 degree)
}
void loop() {
server.handleClient(); // Listen for HTTP requests from clients
}
void handleRoot() {
// with a button to toggle the servo
server.send(200, "text/html", "<form action=\"/servoGrand\" method=\"POST\"><input type=\"submit\" value=\"Ouverture Grand\"></form> <form action=\"/servoPetit\" method=\"POST\"><input type=\"submit\" value=\"Ouverture Petit\"></form>");
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}
void handleServoGrand(){
servo.write(30); // Change the state of the servo (move to 15°)
delay(500); // little pause
servo.write(15); // Go back to previous position
server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}
void handleServoPetit(){
servo.write(0); // Change the state of the servo (move to -15°)
delay(500); // little pause
servo.write(15); // Go back to previous position
server.sendHeader("Location","/"); // Add a header to respond with a new location for the browser to go to the home page again
server.send(303); // Send it back to the browser with an HTTP status 303 (See Other) to redirect
}
Je créé une page web avec 2 boutons (ouverture grand et ouverture pieton du portail), et côté jeedom j’ai créé un script qui contient 2 commandes de type script (script php pour ouverture petit qui appelle la page /servoPetit et idem pour l’ouverture grand).
Dans l’ensemble ma solution fonctionne, mais j’ai plusieurs soucis :
- de temps en temps, le servo merdouille et se met à tourner en boucle (du coup ca appuie en continue sur les boutons => PAS BIEN
- j’ai peur pour la sécurité (pas de protection sur la page web du nodeMCU)
Je voulais donc savoir si quelqu’un avait une idée pour fiabiliser tout ça, voire même une méthode différente (je sais qu’il existe le plugin ESPeasy, mais je sais pas si je peux faire ce genre de scénarios).
Merci d’avance pour vos conseils!
Sylvain.