Niveau APPRENTISSAGE : ► Fort
Prérequis :
Matériel :
- 1 x NodeMCU Microcontroller Board
- 1 x Led
- 1 x Résistance 220KΩ
- 1 x 4-Channel 5V Power Relay Module
- Fils de connexion
- 1 x Breadboard
Vidéo de démonstration :
Schéma de câblage :
Code :
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxx"; // "Votre Nom_du_routeur"
const char* password = "xxxxxx"; // "Votre Mot_de_passe_du_routeur"
int ledPinVert = 16;
int PinRelay1 = 5;
int PinRelay2 = 4;
int PinRelay3 = 14;
int PinRelay4 = 12;
int valueRelay1 = HIGH;
int valueRelay2 = HIGH;
int valueRelay3 = HIGH;
int valueRelay4 = HIGH;
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(ledPinVert, OUTPUT);
digitalWrite(ledPinVert, LOW);
pinMode(PinRelay1, OUTPUT);
digitalWrite(PinRelay1, HIGH);
pinMode(PinRelay2, OUTPUT);
digitalWrite(PinRelay2, HIGH);
pinMode(PinRelay3, OUTPUT);
digitalWrite(PinRelay3, HIGH);
pinMode(PinRelay4, OUTPUT);
digitalWrite(PinRelay4, HIGH);
// Connect to WiFi network
Serial.println();
Serial.print("Connexion Wi-Fi à ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
digitalWrite( ledPinVert , LOW);
}
Serial.println("");
Serial.println("Wi-Fi Connecté avec Succès !");
digitalWrite( ledPinVert , HIGH);
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Utiliser cette URL pour vous connecter : ");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop()
{
// Vérifie si un client s'est connecté.
WiFiClient client = server.available();
if (!client)
{
return;
}
// Attends que le client envoie des données.
while (!client.available())
{
delay(1);
}
// Lire la première ligne de la requête
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
if (request.indexOf("/RELAY1=ON") != -1)
{
Serial.println("RELAY1=ON");
digitalWrite(PinRelay1, LOW);
valueRelay1 = LOW;
}
if (request.indexOf("/RELAY1=OFF") != -1)
{
Serial.println("RELAY1=OFF");
digitalWrite(PinRelay1, HIGH);
valueRelay1 = HIGH;
}
if (request.indexOf("/RELAY2=ON") != -1)
{
Serial.println("RELAY2=ON");
digitalWrite(PinRelay2, LOW);
valueRelay2 = LOW;
}
if (request.indexOf("/RELAY2=OFF") != -1)
{
Serial.println("RELAY2=OFF");
digitalWrite(PinRelay2, HIGH);
valueRelay2 = HIGH;
}
if (request.indexOf("/RELAY3=ON") != -1)
{
Serial.println("RELAY3=ON");
digitalWrite(PinRelay3, LOW);
valueRelay3 = LOW;
}
if (request.indexOf("/RELAY3=OFF") != -1)
{
Serial.println("RELAY3=OFF");
digitalWrite(PinRelay3, HIGH);
valueRelay3 = HIGH;
}
if (request.indexOf("/RELAY4=ON") != -1)
{
Serial.println("RELAY4=ON");
digitalWrite(PinRelay4, LOW);
valueRelay4 = LOW;
}
if (request.indexOf("/RELAY4=OFF") != -1)
{
Serial.println("RELAY4=OFF");
digitalWrite(PinRelay4, HIGH);
valueRelay4 = HIGH;
}
// Renvoyez la réponse
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>ESP8266 Led Controm</title>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\">");
client.println("</head>");
client.println("<body>");
client.println("<div class=\"p-2 rounded\">");
client.println("<div class=\"row g-2 p-3\">");
//Relay1
client.println("<div class=\"p-2 col-12 col-sm-6 col-md-4 col-lg-3 col-xl-3 col-xxl-2\"> ");
client.println(" <div class=\"p-3 border bg-light text-center\">");
client.println("<a href=\"/RELAY1=ON\" class=\"btn btn-success\" role=\"button\">ON</a>");
client.println("<a href=\"/RELAY1=OFF\" class=\"btn btn-danger\" role=\"button\">OFF</a> Relay 1 :");
if (valueRelay1 == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("</div>");
client.println("</div>");
//Relay2
client.println("<div class=\"p-2 col-12 col-sm-6 col-md-4 col-lg-3 col-xl-3 col-xxl-2\"> ");
client.println(" <div class=\"p-3 border bg-light text-center\">");
client.println("<a href=\"/RELAY2=ON\" class=\"btn btn-success\" role=\"button\">ON</a>");
client.println("<a href=\"/RELAY2=OFF\" class=\"btn btn-danger\" role=\"button\">OFF</a> Relay 2 :");
if (valueRelay2 == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("</div>");
client.println("</div>");
//Relay3
client.println("<div class=\"p-2 col-12 col-sm-6 col-md-4 col-lg-3 col-xl-3 col-xxl-2\"> ");
client.println(" <div class=\"p-3 border bg-light text-center\">");
client.println("<a href=\"/RELAY3=ON\" class=\"btn btn-success\" role=\"button\">ON</a>");
client.println("<a href=\"/RELAY3=OFF\" class=\"btn btn-danger\" role=\"button\">OFF</a> Relay 3 :");
if (valueRelay3 == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("</div>");
client.println("</div>");
//Relay4
client.println("<div class=\"p-2 col-12 col-sm-6 col-md-4 col-lg-3 col-xl-3 col-xxl-2\"> ");
client.println(" <div class=\"p-3 border bg-light text-center\">");
client.println("<a href=\"/RELAY4=ON\" class=\"btn btn-success\" role=\"button\">ON</a>");
client.println("<a href=\"/RELAY4=OFF\" class=\"btn btn-danger\" role=\"button\">OFF</a> Relay 4 :");
if (valueRelay4 == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("</div>");
client.println("</div>");
//
client.println("</div>");
client.println("</div>");
client.println("</body>");
client.println("<script src=\"https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js\" integrity=\"sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM\" crossorigin=\"anonymous\"></script>");
client.println("</html>");
delay(1);
Serial.println("");
}
+ Infos sur le langage utilisé :
1. Fonctions
2. Variables
3. Stucture
Nombre de vues: 1 187
Excellent montage. Après plusieurs essais, çà fonctionne. Quand le téléversement est fait un reset de la carte s’impose. Cependant après un certain temps on ne peut plus accéder aux boutons depuis l’ordinateur ou le téléphone. Pourtant la carte apparait bien dans les éléments sur le réseau.