Created
February 1, 2025 00:02
-
-
Save TheyCallMeLinux/924bf3b7c6c40254d120c3e3b236ea3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <WiFi.h> | |
#include <HTTPClient.h> | |
#include "secrets.h" | |
#define SCREEN_WIDTH 128 // Largeur de l'écran OLED, en pixels | |
#define SCREEN_HEIGHT 32 // Hauteur de l'écran OLED, en pixels | |
#define OLED_RESET -1 // Pin de réinitialisation (non nécessaire pour I2C) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
A | |
#define BUTTON_PIN 15 | |
unsigned long lastDebounceTime = 0; // Pour éviter les déclenchements multiples | |
unsigned long debounceDelay = 50; // Délai de débounce | |
// Fonction pour afficher du texte centré | |
void displayCenteredText(const char* line1, const char* line2, int textSize) { | |
display.clearDisplay(); | |
display.setTextSize(textSize); | |
display.setTextColor(WHITE); // Assurer que le texte est visible | |
// Première ligne | |
int16_t x1, y1; | |
uint16_t width, height; | |
display.getTextBounds(line1, 0, 0, &x1, &y1, &width, &height); | |
int xPos = (SCREEN_WIDTH - width) / 2; | |
int yPos = (SCREEN_HEIGHT - height) / 2 - 4; // Ajuster pour l'espacement | |
display.setCursor(xPos, yPos); | |
display.print(line1); | |
// Deuxième ligne (un peu plus bas) | |
display.getTextBounds(line2, 0, 0, &x1, &y1, &width, &height); | |
yPos += height + 2; // Ajouter un peu d'espace entre les lignes | |
display.setCursor(xPos, yPos); | |
display.print(line2); | |
display.display(); // S'assurer que l'écran est mis à jour | |
} | |
// Vérifie si les paramètres de connexion sont valides | |
bool checkConnectionParams() { | |
if (serverURL == NULL || strlen(serverURL) == 0) { | |
displayCenteredText("Erreur serveur", "Invalide", 1); | |
delay(2000); | |
return false; | |
} | |
if (apiKey == NULL || strlen(apiKey) == 0) { | |
displayCenteredText("Erreur", "API", 1); | |
delay(2000); | |
return false; | |
} | |
return true; | |
} | |
void setup() { | |
Serial.begin(115200); | |
Wire.begin(21, 22); // SDA = GPIO21, SCL = GPIO22 | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
// Initialisation de l'écran OLED | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { | |
Serial.println(F("Échec de l'initialisation de l'écran SSD1306")); | |
while (true); | |
} | |
// Message de bienvenue | |
displayCenteredText("Bienvenue", " ", 2); | |
delay(3000); | |
// Connexion au WiFi | |
displayCenteredText("Connexion WiFi...", " ", 1); | |
WiFi.begin(ssid, password); | |
Serial.print("Connexion au WiFi..."); | |
unsigned long startAttemptTime = millis(); | |
while (WiFi.status() != WL_CONNECTED) { | |
if (millis() - startAttemptTime >= 10000) { | |
Serial.println("Échec de la connexion WiFi"); | |
displayCenteredText("WiFi Echoue!", " ", 1); | |
delay(5000); | |
return; | |
} | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println("\nConnecté!"); | |
displayCenteredText("WiFi Connecte!", " ", 1); | |
delay(2000); | |
// Message prêt | |
displayCenteredText("Pret", " ", 2); | |
delay(5000); | |
} | |
void displayProgressBar(int progress) { | |
display.clearDisplay(); | |
// Texte d'en-tête | |
display.setTextSize(1); | |
display.setCursor((SCREEN_WIDTH - 100) / 2, 5); | |
display.print("Redemarrage"); | |
// Dessiner la barre de progression | |
int barWidth = 100; | |
int barHeight = 8; | |
int barX = (SCREEN_WIDTH - barWidth) / 2; | |
int barY = 15; // Un peu plus bas pour créer plus d'espace | |
display.drawRect(barX, barY, barWidth, barHeight, WHITE); | |
// Remplir la barre | |
int fillWidth = (progress * barWidth) / 100; | |
display.fillRect(barX, barY, fillWidth, barHeight, WHITE); | |
// Afficher le pourcentage sous la barre | |
display.setCursor((SCREEN_WIDTH - 12) / 2, barY + barHeight + 2); // Position ajustée | |
display.print(progress); | |
display.print("%"); | |
display.display(); | |
} | |
void loop() { | |
// Vérifier si le bouton est constamment HAUT (ce qui signifie qu'il est déconnecté) | |
bool buttonAlwaysHigh = true; | |
for (int i = 0; i < 10; i++) { // Lire l'état du bouton plusieurs fois | |
if (digitalRead(BUTTON_PIN) == LOW) { | |
buttonAlwaysHigh = false; | |
break; // Bouton détecté, on sort de la boucle | |
} | |
delay(10); | |
} | |
if (buttonAlwaysHigh) { | |
displayCenteredText("Erreur", "Bouton absent!", 1); | |
delay(2000); | |
return; | |
} | |
// Vérifier si le bouton est pressé normalement | |
if (digitalRead(BUTTON_PIN) == LOW && (millis() - lastDebounceTime) > debounceDelay) { | |
lastDebounceTime = millis(); | |
Serial.println("Bouton appuyé ! Redémarrage du serveur..."); | |
// Vérifier les paramètres de connexion avant de tenter la requête | |
if (!checkConnectionParams()) { | |
return; // Si les paramètres sont incorrects, ne pas continuer | |
} | |
WiFiClient client; | |
HTTPClient http; | |
http.begin(client, serverURL); | |
http.addHeader("Content-Type", "application/json"); | |
http.addHeader("X-API-Key", apiKey); | |
unsigned long requestStartTime = millis(); | |
int httpResponseCode = http.POST("{}"); | |
unsigned long requestDuration = millis() - requestStartTime; | |
Serial.println("Code de réponse: " + String(httpResponseCode)); | |
// Si le serveur ne répond pas dans les 2 secondes, afficher l'échec immédiatement | |
if (requestDuration >= 2000) { | |
displayCenteredText("Echec! :(", " ", 1); | |
delay(2000); | |
} else { | |
// Animer la barre de progression sur 30 secondes | |
unsigned long startTime = millis(); | |
while (millis() - startTime < 30000) { | |
int progress = ((millis() - startTime) * 100) / 30000; | |
displayProgressBar(progress); | |
delay(500); | |
} | |
// Message final | |
if (httpResponseCode == 200) { | |
displayCenteredText("Redemarrage", "Termine! :)", 1); | |
} else { | |
displayCenteredText("Echec! :(", " ", 1); | |
} | |
delay(5000); | |
} | |
http.end(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment