Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Questo sketch dimostra come impostare un semplice server del tipo HTTP.
  3.     Il server imposterà un pin in base alla richiesta.
  4.       http://server_ip/gpio/0 imposterà il pin LOW
  5.       http://server_ip/gpio/1 imposterà il pin HIGH
  6.     server_ip è l'indirizzo IP del modulo ESP8266 module
  7.      e sarà stampato sulla seriale quando il modulo è connesso.
  8. */
  9.  
  10. // includo la libreria
  11. #include <ESP8266WiFi.h>
  12.  
  13. // Imposto SSID e Password WiFi al quale aggangiarmi
  14. const char* ssid = "Nome del tuo WiFi";                    
  15. const char* password = "Password del tuo wifi";  
  16.  
  17. // Creo un'istanza del server e specifica la porta da ascoltare
  18. WiFiServer server(80);
  19.  
  20. void setup() {
  21.   Serial.begin(9600);
  22.   delay(10);
  23.   // Connetto al WiFi
  24.   WiFi.mode(WIFI_STA);
  25.   WiFi.begin(ssid, password);
  26.  
  27.   while (WiFi.status() != WL_CONNECTED) {
  28.     delay(500);
  29.   }
  30.  
  31.   // Inizio il server
  32.   server.begin();
  33. }
  34.  
  35. void loop() {
  36.   //Controllo se un client si è connesso
  37.   WiFiClient client = server.available();
  38.   if (!client) {
  39.     return;
  40.   }
  41.  
  42.   // Aspetto che il client invii qualche dato
  43.   while (!client.available()) {
  44.     delay(1);
  45.   }
  46.  
  47.  
  48.   // Leggo la prima linea della richiesta
  49.   String req = client.readStringUntil('\r');
  50.   client.flush();
  51.  
  52.   // Abbino la richiesta
  53.   int val;
  54.   if (req.indexOf("/gpio/0") != -1) {
  55.     val = 0;
  56.   } else if (req.indexOf("/gpio/1") != -1) {
  57.     val = 1;
  58.   } else {
  59.     client.stop();
  60.     return;
  61.   }
  62.  
  63.   // Imposto il pin scelto secondo la richiesta
  64.   if (val == 1){
  65.     Serial.write("\xa0\x01\x01\xa2"); // CHIUDE RELAY
  66.   } else if (val == 0){
  67.     Serial.write("\xa0\x01"); // APRE RELAY
  68.     Serial.write(0x00); // null terminates a string so it has to be sent on its own
  69.     Serial.write(0xa1);
  70.   }
  71.   client.flush();
  72.  
  73.   // Preparo la risposta
  74.   String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<head><title>Chiudi/Apri Rele</title> <link rel='shortcut icon' type='image/x-icon' href='http://i44.servimg.com/u/f44/16/84/89/65/23570310.png' /></head>\r\n<body>Il rele è </body>";
  75.   s += (val) ? "Chiuso" : "Aperto";
  76.   s += "<script>javascript:window.close()</script></html>\n";
  77.  
  78.   // Send the response to the client
  79.   client.print(s);
  80.   delay(1);
  81.  
  82.   // Il client verrà disconnesso quando la funzione ritorna e l'oggetto "client" viene distrutto
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement