202 lines
6.1 KiB
C++
202 lines
6.1 KiB
C++
//#include <ArduinoJson.h>
|
|
#include <EEPROM.h>
|
|
#include <ArduinoJson.h>
|
|
#include <WiFiUdp.h>
|
|
#include <ArduinoOTA.h>
|
|
|
|
// Time to sleep (in seconds):
|
|
const int sleepTimeS = 10;
|
|
|
|
// =======================
|
|
// WIFI
|
|
// =======================
|
|
#include <ESP8266WebServer.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266HTTPClient.h>
|
|
// Your WiFi credentials.
|
|
// Set password to "" for open networks.
|
|
const char* ssid = "Livebox-37cc";
|
|
const char* pass = "8A6060920A8A86896F770F2C47";
|
|
|
|
IPAddress gateway(192, 168, 1, 1);
|
|
IPAddress subnet(255, 255, 0, 0);
|
|
IPAddress DNS(192, 168, 1, 1);
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
const char* host = "192.168.1.3";
|
|
const int port = 81;
|
|
const char* apiEndpoint = "/json.htm?type=command¶m=getSunRiseSet";
|
|
unsigned long lastUpdateTime = 0;
|
|
const unsigned long updateInterval = 5 * 60 * 1000; // 5 minutes en millisecondes
|
|
|
|
String currentTime = "";
|
|
String sunriseTime = "";
|
|
String sunsetTime = "";
|
|
String error = "";
|
|
|
|
String date = "";
|
|
String heure = "";
|
|
|
|
#define PIN_1 0
|
|
#define PIN_2 2
|
|
|
|
void setup() {
|
|
// initialize digital pin LED_BUILTIN as an output.
|
|
Serial.begin(115200);
|
|
pinMode(PIN_1, OUTPUT);
|
|
pinMode(PIN_2, OUTPUT);
|
|
delay(10);
|
|
|
|
// Connectez-vous au réseau WiFi
|
|
WiFi.begin(ssid, pass);
|
|
Serial.println("Connexion au WiFi en cours.");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connecté au réseau WiFi");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
// Définissez les gestionnaires pour les différentes URL
|
|
server.on("/", HTTP_GET, handleRoot);
|
|
server.on("/allumer", HTTP_GET, handleAllumer);
|
|
server.on("/eteindre", HTTP_GET, handleEteindre);
|
|
// server.on("/stop", HTTP_GET, handleStop);
|
|
// server.on("/reset", HTTP_GET, handleReset);
|
|
// server.on("/setFinCourse", HTTP_GET, handleFinCourse);
|
|
// server.on("/add", HTTP_GET, handleAdd);
|
|
// server.on("/sub", HTTP_GET, handleSub);
|
|
// server.on("/data", HTTP_GET, handleData);
|
|
|
|
// Démarrer le serveur
|
|
server.begin();
|
|
Serial.println("Serveur Web démarré");
|
|
|
|
///////////////////////////////////
|
|
// UPDATE OTA
|
|
///////////////////////////////////
|
|
// Port defaults to 8266
|
|
ArduinoOTA.setPort(8266);
|
|
|
|
// Hostname defaults to esp8266-[ChipID]
|
|
//ArduinoOTA.setHostname("ESP8266_VELUX_NORD_1");
|
|
|
|
// No authentication by default
|
|
// ArduinoOTA.setPassword("admin");
|
|
|
|
// Password can be set with it's md5 value as well
|
|
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
|
|
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
Serial.println("Start");
|
|
});
|
|
ArduinoOTA.onEnd([]() {
|
|
Serial.println("\nEnd");
|
|
});
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
|
|
});
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
|
Serial.printf("Error[%u]: ", error);
|
|
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
|
else if (error == OTA_END_ERROR) Serial.println("End Failed");
|
|
});
|
|
ArduinoOTA.begin();
|
|
|
|
handleEteindre();
|
|
}
|
|
|
|
void loop() {
|
|
|
|
// getJson();
|
|
ArduinoOTA.handle();
|
|
|
|
// Gérez les requêtes du serveur
|
|
server.handleClient();
|
|
|
|
if (millis() - lastUpdateTime >= updateInterval) {
|
|
// Mettre à jour le temps de la dernière mise à jour
|
|
lastUpdateTime = millis();
|
|
}
|
|
|
|
}
|
|
|
|
void handleRoot() {
|
|
// Générer la page HTML avec CSS
|
|
String html = "<html><head>";
|
|
|
|
html += "<meta charset='UTF-8'>";
|
|
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
|
|
|
|
html += "<link type='text/css' rel='stylesheet' href='http://192.168.1.3:81/velux/velux.css'>";
|
|
html += "<script src='http://192.168.1.3:81/velux/jquery.min.js'></script>";
|
|
html += "<script src='http://192.168.1.3:81/velux/velux.js'></script>";
|
|
|
|
|
|
// html += "<style>";
|
|
// html += "body { font-family: Arial, sans-serif; text-align: center; }";
|
|
// html += "h1 { color: #333; }";
|
|
// html += "form { margin: 10% auto; max-width: 100%; }";
|
|
// html += "input { padding: 1em; margin: 1em; }";
|
|
// html += "#buttons {list-style: none;}";
|
|
// html += "#button { display: inline-block; list-style-type: none;}";
|
|
// html += "@media (max-width: 600px) {li {display: block; margin: 5px 0;list-style-type: none;}}";
|
|
// html += "</style>";
|
|
|
|
html += "</head><body>";
|
|
html += "<h1>Controle des batteries</h1>";
|
|
html += "<h2>" + String(WiFi.localIP().toString()) + "</h2>";
|
|
html += "<ul class='buttons'>";
|
|
|
|
html += "<li class='button' onclick='showNotification()'><form action='/allumer' method='get'>";
|
|
// html += "<input type='hidden' id='time' name='time' value='" + String(to_store.max_time - to_store.theorique_position) + "'>";
|
|
|
|
html += "<input type='submit' value='Allumer'>";
|
|
html += "</form></li>";
|
|
|
|
html += "<li class='button' onclick='showNotification()'><form action='/eteindre' method='get'>";
|
|
html += "<input type='submit' value='Arrêter'>";
|
|
html += "</form></li>";
|
|
|
|
html += "</ul>";
|
|
|
|
if (error != "") {
|
|
html += "<p>" + error + "</p>";
|
|
}
|
|
|
|
html += "<div id='notification' class='notification'></div>";
|
|
html += "</body></html>";
|
|
|
|
// Envoyer la page HTML au client
|
|
server.send(200, "text/html", html);
|
|
}
|
|
|
|
void handleAllumer() {
|
|
Serial.println("avance");
|
|
digitalWrite(PIN_2, HIGH);
|
|
delay(200);
|
|
digitalWrite(PIN_1, HIGH);
|
|
|
|
// Rediriger vers la page principale après le traitement
|
|
server.sendHeader("Location", "/");
|
|
server.send(302, "text/plain", "Redirection vers la page principale");
|
|
}
|
|
|
|
void handleEteindre() {
|
|
digitalWrite(PIN_2, LOW);
|
|
delay(200);
|
|
digitalWrite(PIN_1, LOW); // turn the LED on (HIGH is the voltage level)
|
|
delay(200);
|
|
// Vous pouvez ajouter ici le code pour traiter la vitesse comme vous le souhaitez
|
|
|
|
// Rediriger vers la page principale après le traitement
|
|
server.sendHeader("Location", "/");
|
|
server.send(302, "text/plain", "Redirection vers la page principale");
|
|
}
|