241 lines
6.8 KiB
C++
241 lines
6.8 KiB
C++
/*
|
|
ESP32 WiFi STA Mode
|
|
http:://www.electronicwings.com
|
|
*/
|
|
|
|
#include <WiFi.h>
|
|
#include <NTPClient.h>
|
|
#include <WiFiUdp.h>
|
|
#include "esp_system.h"
|
|
|
|
// For example purpose
|
|
String formattedDate;
|
|
|
|
// Define NTP Client to get time
|
|
WiFiUDP ntpUDP;
|
|
NTPClient timeClient(ntpUDP);
|
|
|
|
// Timer conf
|
|
const int wdtTimeout = 30; //time in ms to trigger the watchdog
|
|
hw_timer_t *timer = NULL;
|
|
|
|
// Wifi conf
|
|
const char* ssid = "Wokwi-GUEST"; /*Enter Your SSID*/
|
|
const char* password = ""; /*Enter Your Password*/
|
|
WiFiServer server(80); /* Instance of WiFiServer with port number 80 */
|
|
String request;
|
|
|
|
bool L1 = false;
|
|
bool L2 = false;
|
|
bool L3 = false;
|
|
bool L4 = false;
|
|
bool L5 = false;
|
|
bool L6 = false;
|
|
bool L7 = false;
|
|
bool L8 = false;
|
|
|
|
String programme = "000000000000000000";
|
|
|
|
WiFiClient client;
|
|
|
|
void callback() {
|
|
|
|
Serial.print("Callback... ");
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
Serial.print("Connecting to: ");
|
|
Serial.println(ssid);
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
Serial.print(".");
|
|
delay(100);
|
|
}
|
|
Serial.print("\n");
|
|
Serial.print("Connected to Wi-Fi ");
|
|
Serial.println(WiFi.SSID());
|
|
delay(1000);
|
|
server.begin(); /* Start the HTTP web Server */
|
|
Serial.print("Connect to IP Address: ");
|
|
Serial.print("http://");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
timeClient.begin();
|
|
// Set offset time in seconds to adjust for your timezone, for example:
|
|
timeClient.setTimeOffset(7200);
|
|
timeClient.update();
|
|
|
|
timer = timerBegin(0, 80, true); //timer 0, div 80
|
|
timerAttachInterrupt(timer, &callback, true); //attach callback
|
|
timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
|
|
timerAlarmEnable(timer); //enable interrupt
|
|
|
|
|
|
}
|
|
|
|
void html() {
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println("Connection: close");
|
|
client.println();
|
|
|
|
client.println("<!DOCTYPE HTML>");
|
|
client.println("<html>");
|
|
|
|
client.println("<head>");
|
|
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
|
client.println("<link rel=\"icon\" href=\"data:,\">");
|
|
client.println("<style>");
|
|
client.println("html { font-family: Roboto; display: inline-block; margin: 0px auto; text-align: center;}");
|
|
client.println(".button {float:left;background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;");
|
|
client.println("text-decoration: none; font-size: 25px; margin: 2px; cursor: pointer;}");
|
|
client.println(".button_ON {background-color: white; color: black; border: 2px solid #4CAF50;}");
|
|
client.println(".button_OFF {background-color: white; color: black; border: 2px solid #f44336;}");
|
|
client.println("</style>");
|
|
client.println("</head>");
|
|
client.println("<body>");
|
|
client.println("<h2>Programmation actuelle</h2>");
|
|
client.println("<h3>"+programme+"</h3>");
|
|
client.print("<form action=\"/PROG\n\" method=\"post\">");
|
|
client.print("<label>Modifier Programmation : <input type=\"text\" name=\"progo\" id=\"toto\" value=\"" + programme + "\"/></label>");
|
|
client.print("<input type=\"submit\" /></form>");
|
|
client.println("<h2>Prises Programmées</h2>");
|
|
|
|
|
|
if (L1) {
|
|
client.print("<p><a href=\"/L1\n\"><button class=\"button button_ON\">1 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L1\n\"><button class=\"button button_OFF\">1 OFF</button></a></p>");
|
|
}
|
|
|
|
if (L2) {
|
|
client.print("<p><a href=\"/L2\n\"><button class=\"button button_ON\">2 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L2\n\"><button class=\"button button_OFF\">2 OFF</button></a></p>");
|
|
}
|
|
if (L3) {
|
|
client.print("<p><a href=\"/L3\n\"><button class=\"button button_ON\">3 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L3\n\"><button class=\"button button_OFF\">3 OFF</button></a></p>");
|
|
}
|
|
|
|
if (L4) {
|
|
client.print("<p><a href=\"/L4\n\"><button class=\"button button_ON\">4 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L4\n\"><button class=\"button button_OFF\">4 OFF</button></a></p>");
|
|
}
|
|
if (L5) {
|
|
client.print("<p><a href=\"/L5\n\"><button class=\"button button_ON\">5 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L5\n\"><button class=\"button button_OFF\">5 OFF</button></a></p>");
|
|
}
|
|
|
|
if (L6) {
|
|
client.print("<p><a href=\"/L6\n\"><button class=\"button button_ON\">6 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L6\n\"><button class=\"button button_OFF\">6 OFF</button></a></p>");
|
|
}
|
|
if (L7) {
|
|
client.print("<p><a href=\"/L7\n\"><button class=\"button button_ON\">7 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L7\n\"><button class=\"button button_OFF\">7 OFF</button></a></p>");
|
|
}
|
|
|
|
if (L8) {
|
|
client.print("<p><a href=\"/L8\n\"><button class=\"button button_ON\">8 ON</button></a></p>");
|
|
} else {
|
|
client.print("<p><a href=\"/L8\n\"><button class=\"button button_OFF\">8 OFF</button></a></p>");
|
|
}
|
|
|
|
client.println("</body>");
|
|
client.println("</html>");
|
|
}
|
|
|
|
void loop() {
|
|
|
|
|
|
formattedDate = timeClient.getFormattedTime();
|
|
Serial.println(formattedDate);
|
|
|
|
timerWrite(timer, 0); //reset timer (feed watchdog)
|
|
long loopTime = millis();
|
|
|
|
|
|
|
|
/*
|
|
// Extract date
|
|
int splitT = formattedDate.indexOf("T");
|
|
dayStamp = formattedDate.substring(0, splitT);
|
|
Serial.print("DATE: ");
|
|
Serial.println(dayStamp);
|
|
// Extract time
|
|
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
|
|
Serial.print("HOUR: ");
|
|
Serial.println(timeStamp);
|
|
*/
|
|
delay(1000);
|
|
/*
|
|
client = server.available();
|
|
if (!client) {
|
|
return;
|
|
}
|
|
|
|
while (client.connected()) {
|
|
if (client.available()) {
|
|
char c = client.read();
|
|
|
|
|
|
request += c;
|
|
|
|
if (c == '\n') {
|
|
String postres = client.readString();
|
|
int indp = postres.indexOf("progo=")+6;
|
|
Serial.println(postres);
|
|
Serial.println(request);
|
|
|
|
|
|
if (request.indexOf("POST /PROG") != -1) {
|
|
Serial.println("go");
|
|
programme =postres.substring(indp);
|
|
}
|
|
if (request.indexOf("GET /L1") != -1) {
|
|
L1 = !L1;
|
|
}
|
|
if (request.indexOf("GET /L2") != -1) {
|
|
L2 = !L2;
|
|
}
|
|
if (request.indexOf("GET /L3") != -1) {
|
|
L3 = !L3;
|
|
}
|
|
if (request.indexOf("GET /L4") != -1) {
|
|
L4 = !L4;
|
|
}
|
|
|
|
if (request.indexOf("GET /L5") != -1) {
|
|
L5 = !L5;
|
|
}
|
|
if (request.indexOf("GET /L6") != -1) {
|
|
L6 = !L6;
|
|
}
|
|
if (request.indexOf("GET /L7") != -1) {
|
|
L7 = !L7;
|
|
}
|
|
if (request.indexOf("GET /L8") != -1) {
|
|
L8 = !L8;
|
|
}
|
|
|
|
html();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
delay(1);
|
|
request = "";
|
|
client.stop();
|
|
*/
|
|
}
|