Advertisement
sergiocntr

Untitled

Jun 19th, 2024
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3.  
  4. const char* ssid = "your_SSID";
  5. const char* password = "your_PASSWORD";
  6.  
  7. ESP8266WebServer server(80);
  8.  
  9. int valoreVariabile = 42;
  10.  
  11. // Definisci il contenuto HTML come una stringa
  12. const char* htmlTemplate = R"(
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16.    <meta charset="UTF-8">
  17.    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18.    <title>ESP8266 Web Server</title>
  19. </head>
  20. <body>
  21.    <h1>Valore della variabile: %VALORE_VARIABILE%</h1>
  22. </body>
  23. </html>
  24. )";
  25.  
  26. void setup() {
  27.   Serial.begin(115200);
  28.   WiFi.begin(ssid, password);
  29.  
  30.   // Attendi connessione WiFi
  31.   while (WiFi.status() != WL_CONNECTED) {
  32.     delay(1000);
  33.     Serial.println("Connecting to WiFi...");
  34.   }
  35.  
  36.   Serial.println("Connected to WiFi!");
  37.  
  38.   // Stampa l'indirizzo IP
  39.   Serial.print("Indirizzo IP del server: ");
  40.   Serial.println(WiFi.localIP());
  41.  
  42.   // Imposta le route
  43.   server.on("/", handleRoot);
  44.  
  45.   server.begin();
  46.   Serial.println("HTTP server started");
  47. }
  48.  
  49. void loop() {
  50.   server.handleClient();
  51. }
  52.  
  53. void handleRoot() {
  54.   String htmlContent = String(htmlTemplate);
  55.   htmlContent.replace("%VALORE_VARIABILE%", String(valoreVariabile));
  56.   server.send(200, "text/html", htmlContent);
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement