1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| *********************************************************************************
***********Maquette d'essai ESP32 en serveur Web - librairie WebServer************
***********Controle de 2 leds une en On/Off l'autre en clignotement**************
***********accès en local par réseau WiFi et sur Internet via la Box**************
***********utilisation d'un domaine sur no-IP*************************************
**************************************version 1.08 Michel MORICE juillet 2023****/
#include <Arduino.h>
#include <FS.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <WebServer.h>
#include "maBox.h"
#define LED_P01 26
#define LED_P02 25
#define BTN_PIN 22
#define FORMAT_SPIFFS_IF_FAILED true
#define HTTP_PORT 80 //ou 8080
const uint8_t DEBOUNCE_DELAY = 50;
const char* mon_ssid = SECRET_SSID;
const char* mon_pass = SECRET_PASS;
//***************définition des leds*******************************
struct Del{
uint8_t pin; bool etat;
void update(){
digitalWrite(pin, etat? HIGH : LOW);
}
};
//**************définition des boutons*****************************
struct Bouton{
uint8_t pin; bool lastReading; uint32_t lastDebounceTime; uint16_t state;
//Méthodes pour déterminer l'état du bouton
bool pressed() {return state == 1;}
bool released() {return state == 0xFFFF;}
bool held(uint16_t count=0) {return state >1 + count && state < 0xFFFF;}
//Méthode pour lire l'état du bouton
void read(){
bool lecture = digitalRead(pin);
if(lecture != lastReading){
lastDebounceTime = millis();
}
if(millis() - lastDebounceTime > DEBOUNCE_DELAY){
bool pressed = lecture == LOW;
if(pressed){
if(state < 0xFFFE) state++;
else if(state == 0xFFFE) state = 2;
}
else if(state){ state = state == 0xFFFF? 0 : 0xFFFF;}
}
lastReading = lecture;
}
};
//****************Page HTML***********************
const char index_html[] PROGMEM = R"=====(
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<link rel="icon" href="data:,">
<title>Commande Leds</title>
</head>
<body>
<h1>Etat des leds</h1>
<h2 id="etatLed1"></h2>
<h2 id="etatLed2"></h2>
<h1>Boutons de commande</h1>
<button onclick="appServeur('/led1',actionLed1)">Basculer Led1</button>
<button onclick="appServeur('/led2',actionLed2)">Activation Led2</button>
<script>
function appServeur(url,cFonction){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
cFonction(this);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function actionLed1(xhttp){
document.getElementById("etatLed1").innerHTML = "LED1 " + xhttp.responseText;
}
function actionLed2(xhttp){
document.getElementById("etatLed2").innerHTML = "LED2 " + xhttp.responseText;
}
</script>
</body>
</html>
)=====";
//***************variables globales***********************
Del led2 = {LED_P02, false };
bool cligneOn = false;
Del led1 = {LED_P01, false};
Bouton btn = {BTN_PIN, HIGH, 0, 0};
//****************instantiations*********************
WebServer serveur(HTTP_PORT);
//**************SPIFFS initialisation*******************
void initSPIFFS(){
Serial.println("init SPIFFS");
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("cannot mount SPIFFS");
while(1){
led2.etat = millis() % 200 < 50;
led2.update();
}
}
}
//****************WIFI initialisation********************
void connexion_BOX(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info){
Serial.println("connexion reussie");
}
void obtention_IP(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info){
Serial.printf("%s\n", WiFi.localIP().toString().c_str());
}
void deconnexion_BOX(WiFiEvent_t wifi_event, WiFiEventInfo_t wifi_info){
WiFi.begin(mon_ssid,mon_pass);
}
//**************WebServer init***************************
void handleRoot() {
String tempo(reinterpret_cast<const __FlashStringHelper*> (index_html));
serveur.send(200,"text/html", tempo);
}
void comLed1(){
led1.etat = !led1.etat;
String msg = led1.etat? "allumée" : "éteinte";
serveur.send(200,"text/plain", msg);
}
void comLed2(){
cligneOn = !cligneOn;
String msg = cligneOn? "cligne" : "éteinte";
serveur.send(200,"text/plain", msg);
}
void initWebServer() {
serveur.on("/", handleRoot);
serveur.on("/index.html",handleRoot);
serveur.on("/led1", comLed1);
serveur.on("/led2", comLed2);
serveur.begin();
}
void initWiFi(){
WiFi.mode(WIFI_STA);
Serial.println("init WIFI");
WiFi.onEvent(connexion_BOX,ARDUINO_EVENT_WIFI_STA_CONNECTED); //attend une connexion wifi
WiFi.onEvent(obtention_IP, ARDUINO_EVENT_WIFI_STA_GOT_IP); //attend l'obtention de l'adresse IP
WiFi.onEvent(deconnexion_BOX, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); //si une déconnexion survient
WiFi.begin(mon_ssid, mon_pass);
}
//*****************paramétrage initial***********************
void setup() {
Serial.begin(9600);
Serial.println("Start");
pinMode(led2.pin,OUTPUT);
pinMode(led1.pin,OUTPUT);
pinMode(btn.pin,INPUT_PULLUP);
initWiFi();
delay(1000);
initSPIFFS();
delay(1000);
initWebServer();
}
//*****************boucle principale*****************************
void loop() {
serveur.handleClient();
btn.read();
if (btn.pressed()) led1.etat = !led1.etat;
led2.etat = cligneOn? millis() % 1000 < 50 : false;
led1.update();
led2.update();
} |
Partager