You are reading a single comment by @L0cutus and its replies. Click here to read the full conversation.
  • Hello,
    I'm trying some code from page 246 (Espruino Pico+ESP8266):

    // Test ESP8266
    var WIFI_NAME = "MYSSID";
    var WIFI_KEY =  "WIFIPWD";
    var wifi;
    var json;
    
    var homepage = '<html><body>'+
    '<h1>My Espruino</h1>'+
    '<a href="/getTemp">Temperature</a></br>'+
    '<form action="/?led=0" method="post">'+
    '<input type="submit" value="LEDS OFF"/></form>'+
    '<form action="/?led=1" method="post">'+
    '<input type="submit" value="LED RED ON"/></form>'+
    '<form action="/?led=2" method="post">'+
    '<input type="submit" value="LED GREEN ON"/></form>'+
    '<form action="/?led=3" method="post">'+
    '<input type="submit" value="LED RED+GREEN ON"/>d</form>'+
    '</body></html>';
    
    
    function onInit() {
      USB.setConsole(true);
      Serial1.setup(115200, { tx:B6, rx: B7});
      wifi = require("ESP8266WiFi_0v25").connect(Serial1, function(err) {
        if(err) throw err;
        console.log("Connecting to WiFi");
        wifi.connect(WIFI_NAME, WIFI_KEY, function(err) {
          if(err) {
            console.log("Connection error: "+err);
            return;
          }
          onConnected();
        });
      });
    }
    
    function onConnected() {
      console.log("Connected");
      require("http").createServer(onPageRequest).listen(80);
      wifi.getIP(function(err,ip){
        console.log("Your IP address is http://" + ip);
      });
    }
    
    function onPageRequest(req, res) {
      console.log("Serving " + req.url);
      var r = url.parse(req.url, true);
      if (r.pathname == "/") {
        // controllo passaggio parametri
        if (r.method == "POST" && r.query && r.query.led)
          digitalWrite([LED2,LED1], r.query.led);
        console.log(r.method);
        // Onora la richiesta della pagina
        res.writeHead(200, {"Content-Type!": "text/html"});
        res.end(homepage);
      } else if (r.pathname == "/getTemp"){
        res.writeHead(200, {"Content-Type!": "text/html"});
        res.end('<html><head>'+
                '<meta http-equiv="refresh" content="2">'+
                '</head><body>'+E.getTemperature().toFixed(2)+
                '</body></html>');
      } else {
        res.writeHead(404);
        res.end("404 - Not Found");
      }
    }
    

    Unfortunately it doesn't work as it 'see' only GET as a method, not the POST, sorry for my english and my worst HTML knowledge ;-)

About

Avatar for L0cutus @L0cutus started