Leggere lo stato di un pin su un nodo della rete tramite una pagina web

E’ possibile leggere un pin su un nodo tramite una richiesta get di jquery con una pagina web del proprio server locale.
Per fare questo abbiamo bisogno di una pagina web così strutturata:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script>
        function cmd(arg) {  
            url = "http://192.168.1.30/"+arg
            $("#console").html($("#console").html()+"<BR>Requesting..."+url);
            
            $.get(url, function( data, status ) {
                $("#console").html($("#console").html()+"<BR>Response<pre>"+data+"</pre>");
              });
       }
    </script>
</head>
<body>
    <a href="https://forum.arduino.cc/index.php?topic=476291.0">
	https://forum.arduino.cc/index.php?topic=476291.0</a>
<p>
    <button onclick="return cmd('ledon')" form="form1" value="Submit">LED ON</button>
    <button onclick="return cmd('ledoff')" form="form1" value="Submit">LED OFF</button>
    <button onclick="return cmd('info')" form="form1" value="Submit">info</button>
 <br><br>
    </p>
    <div id="console" name="console" width=200 height=300>Ready.</div>
 
</body>
</html>

Carichiamo adesso su un esp-01 un web server con questo codice :

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "+++++++++";
const char* password = "+++++++++++++";

//
// ESP8266 Web Server / LED Driver Example : https://learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/example-sketch-ap-web-server
// Connecting with WPA:  https://www.arduino.cc/en/Tutorial/ConnectWithWPA
// https://forum.arduino.cc/index.php?topic=476291.0 


/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 13;//D7

int ledState = LOW;


ESP8266WebServer server(80);

//
// Setup WiFi Configuration
//
void setupWiFi()
{
  WiFi.begin(ssid,password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("Connected! Open http://");
  Serial.print(WiFi.localIP());
  Serial.println(" in your browser");
}

//
// Setup Hardware Configurations
//
void setupHardware()
{
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

//
// Setup HTTP Server
//
void setupHttpServer()
{

  server.begin();
  
  //
  // LED OFF Methods
  //
  server.on("/ledoff", HTTP_GET, []() {
    setLedOff();
  });
  
  server.on("/ledoff", HTTP_OPTIONS, []() {
    server.sendHeader("access-control-allow-credentials", "false");
    server.sendHeader("access-control-allow-headers", "x-requested-with");
    server.sendHeader("access-control-allow-methods", "GET,OPTIONS");

    server.send(204);
  });

  //
  // LED ON Methods
  //
  server.on("/ledon", HTTP_GET, []() {
    setLedOn();
  });
  
  server.on("/ledon", HTTP_OPTIONS, []() {
    server.sendHeader("access-control-allow-credentials", "false");
    server.sendHeader("access-control-allow-headers", "x-requested-with");
    server.sendHeader("access-control-allow-methods", "GET,OPTIONS");

    server.send(204);
  });
  
  //
  // Info Methods
  //
  server.on("/info", HTTP_GET, []() {
    sendInfo();
  });
  
  server.on("/info", HTTP_OPTIONS, []() {
    server.sendHeader("access-control-allow-credentials", "false");
    server.sendHeader("access-control-allow-headers", "x-requested-with");
    server.sendHeader("access-control-allow-methods", "GET,OPTIONS");

    server.send(204);
  });
}

//
// Main Setup
//
void setup()
{
  setupHardware();
  setupWiFi();
  setupHttpServer();
}


//
// Generates response for LED OFF Request
//
void setLedOff()
{
    ledState = LOW;
    digitalWrite(LED_PIN, ledState);
  
    String json = "{";
    json += "\"ledstate\":" + String(ledState ? "ON" : "OFF");
    json += "}";
    
    server.send(200, "text/json", json);
    json = String();  
}

//
// Generates response for LED ON Request
//
void setLedOn()
{
    ledState = HIGH;
    digitalWrite(LED_PIN, ledState);
    
    String json = "{";
    json += "\"ledstate\":" + String(ledState ? "ON" : "OFF");
    json += "}";
    
    server.send(200, "text/json", json);
    json = String();    
}

//
// Generates response for Info Request
//
void sendInfo()
{    
    String json = "{";
    
    json += "\"ssid\":" + String(WiFi.SSID()) + ",";
    json += "\"ip\":" ;
    json += WiFi.localIP().toString();
    json += ",";
    json += "\"mac\":" + String(WiFi.macAddress()) + ",";
    json += "\"ledstate\":" + String(ledState ? "ON" : "OFF");
    json += "}";
    
    server.send(200, "text/json", json);
    json = String();  
}

//
// Main Loop
//
void loop()
{
  server.handleClient();
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *