Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Questo sketch dimostra come impostare un semplice server del tipo HTTP.
- Il server imposterà un pin in base alla richiesta.
- http://server_ip/gpio/0 imposterà il pin LOW
- http://server_ip/gpio/1 imposterà il pin HIGH
- server_ip è l'indirizzo IP del modulo ESP8266 module
- e sarà stampato sulla seriale quando il modulo è connesso.
- */
- // includo la libreria
- #include <ESP8266WiFi.h>
- // Imposto SSID e Password WiFi al quale aggangiarmi
- const char* ssid = "Nome del tuo WiFi";
- const char* password = "Password del tuo wifi";
- // Creo un'istanza del server e specifica la porta da ascoltare
- WiFiServer server(80);
- void setup() {
- Serial.begin(9600);
- delay(10);
- // Connetto al WiFi
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- // Inizio il server
- server.begin();
- }
- void loop() {
- //Controllo se un client si è connesso
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Aspetto che il client invii qualche dato
- while (!client.available()) {
- delay(1);
- }
- // Leggo la prima linea della richiesta
- String req = client.readStringUntil('\r');
- client.flush();
- // Abbino la richiesta
- int val;
- if (req.indexOf("/gpio/0") != -1) {
- val = 0;
- } else if (req.indexOf("/gpio/1") != -1) {
- val = 1;
- } else {
- client.stop();
- return;
- }
- // Imposto il pin scelto secondo la richiesta
- if (val == 1){
- Serial.write("\xa0\x01\x01\xa2"); // CHIUDE RELAY
- } else if (val == 0){
- Serial.write("\xa0\x01"); // APRE RELAY
- Serial.write(0x00); // null terminates a string so it has to be sent on its own
- Serial.write(0xa1);
- }
- client.flush();
- // Preparo la risposta
- 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>";
- s += (val) ? "Chiuso" : "Aperto";
- s += "<script>javascript:window.close()</script></html>\n";
- // Send the response to the client
- client.print(s);
- delay(1);
- // Il client verrà disconnesso quando la funzione ritorna e l'oggetto "client" viene distrutto
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement