Vidéo
Les Schémas
Le Code
//------------------------------------------------------
// https://gammatroniques.fr/
// Code Interface Web ESP32-8266
// 03/2022 - by GammaTroniques
//------------------------------------------------------
//https://github.com/me-no-dev/arduino-esp32fs-plugin/releases
//https://github.com/esp8266/arduino-esp8266fs-plugin/releases
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>// https://github.com/me-no-dev/AsyncTCP
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <SPIFFS.h>
#include <ESPAsyncWebServer.h>//https://github.com/me-no-dev/ESPAsyncWebServer
#include <FS.h>
#include <SD.h>
#include <SPI.h>
#include <Servo.h> //https://github.com/RoboticsBrno/ServoESP32
AsyncWebServer server(80);
const char* ssid = "ESP-WIFI";
const char* password = "motDePasse";
Servo myservo;
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
const char ledPin = 2;
const char servoPin = 26;
String ledState;
// Replaces placeholder in html file
String editHTML(const String& var) {
Serial.println(var);
if (var == "ETAT-LED") {
if (digitalRead(ledPin)) {
ledState = "ON";
}
else {
ledState = "OFF";
}
Serial.print(ledState);
return ledState;
}
return String();
}
void initSD()
{
if (!SD.begin())
{
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE)
{
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC)
Serial.println("MMC");
else if (cardType == CARD_SD)
Serial.println("SDSC");
else if (cardType == CARD_SDHC)
Serial.println("SDHC");
else
Serial.println("UNKNOWN");
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}
void setup() {
Serial.begin(115200);
initSD();
pinMode(ledPin, OUTPUT);
myservo.attach(servoPin);
//--------------- Mode connection Wifi -----------------
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
//-------------- Mode Point d'acces Wifi ------------------
// WiFi.softAP(ssid, password);
// IPAddress IP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(IP);
//---------------------Server WEB --------------------------------
//-----------------Page Principale: simple texte
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(200, "text/html", "<h1>Hello, world</h1><h2>Esp32</h2>");
});
//-----------------Page Principale: fichier html dans la memoire de l'ESP
/*// Initialize SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/index.html", String(), false, editHTML);
});
server.serveStatic("/", SPIFFS, "/");*/
//-----------------Page Principale: fichier html sur une carte SD
/*// Initialize the SD card
initSD();
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SD, "/index.html", String(), false, editHTML);
});
server.serveStatic("/", SD, "/");*/
// Send a GET request to <IP>/get?message=<message>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest * request) {
String ledParam = "led"; // <IP>/get?led=<state>
String ledstate;
String servoParam = "servo"; // <IP>/get?servo=<pos>
String servoPos;
if (request->hasParam(ledParam)) {
ledstate = request->getParam(ledParam)->value(); //Get the value
digitalWrite(ledPin, ledstate.toInt());
Serial.println("Changing LED to " + ledstate);
request->send(200, "text/plain", "OK");
}
else if (request->hasParam(servoParam)) {
servoPos = request->getParam(servoParam)->value();//Get the value
myservo.write(servoPos.toInt());
Serial.println("Changing Servo to " + servoPos);
request->send(200, "text/plain", "OK");
}
else {
request->send(200, "text/plain", "ERROR");
}
});
server.onNotFound(notFound);
server.begin();
}
void loop() {
}