• Task: control the LED on the board using a browser.
    Device: ESP32 WROOM DevKit v1, firmware from the espruino_2v04_esp32 folder.
    Work in the Espruino Web IDE for Windows 64 bit - 0.72.1.
    The code:

    var wifi = require('Wifi');
    var http = require('http');
    var ssid = 'Point';
    var password = '12345678';
    var port = 80;
    
    function apiRequest(req, res) {
      console.log('apiRequest start');
      var a = url.parse(req.url, true);
      console.log("PATH: ", a.pathname, "METHOD", req.method);
      if (req.method === "POST") {
        switch(a.pathname) {
          case '/setLed':
            var data = "";
            req.on('data', (d)=> { data = d; });
            req.on('end',  ()=> {
              console.log('START Post setLed', data);
              digitalWrite(D2, data === 'true' ? true : false);
              res.writeHead(200);
              res.end();
              console.log('END Post setLed');
            });
            break;
        }
      }
    }
    
    function onInit() {
      digitalWrite(D2, false);
      setTimeout(connectWifi, 1000);
      wifi.on('disconnected', ()=> {
        console.log('WIFI disconnected');
      });
      wifi.on('connected', ()=> {
        console.log('WIFI connected');
      }); 
    }
    
    function connectWifi() {
      console.log('WIFI connecting...');
      wifi.connect(ssid, {password: password}, (err)=> {
        if(err) console.log(err);
        else {
          http.createServer(apiRequest).listen(port);
          console.log(`Web server running at http://${wifi.getIP().ip}:${port}`);
        } 
      });
    }
    
    onInit();
    

    I make a request to the server with the web app and the script works (the LED lights up / goes out)

    In console:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    >
    WIFI connecting...
    ERROR: Wifi: event_handler STA_START: esp_wifi_connect: 12298(SSID is invalid)
    WARNING: Wifi:startMDNS - espressif
    Web server running at http://172.22.50.110:80
    WIFI connected     //First connect
    apiRequest start   // First request
    PATH:  /setLed METHOD POST
    START Post setLed true
    END Post setLed
    >E (27443) event: post event to user fail!
    WARNING: Wifi:startMDNS - espressif
    WARNING: Wifi:stopMDNS
    WIFI connected   // Second connect. 
    

    After second connect, sometimes I can do second reqest, sometimes the device freezes
    p.s. I read post

    http://forum.espruino.com/comments/15068150/

About

Avatar for Stenya @Stenya started