You are reading a single comment by @samppa and its replies. Click here to read the full conversation.
  • Here is my code:

    function getA(pin, randomize) {
      if (randomize) return Math.round(100*Math.random());
      else return Math.round(100*analogRead(pin));
    }
    
    function returnJsonStr(res, json) {
      res.writeHead(200, {'Content-Type': 'application/json',
                          'Connection' : 'close',
                          'Content-Length': json.length});
      res.write(json);
      //console.log("Returned JSON: " + json);
    }
    
    function returnError(res, msg) {
      res.writeHead(400, {'Content-Type': 'text/plain',
                          'Connection' : 'close',
                          'Content-Length': msg.length});
      res.write(msg);
      //console.log("Returned error: " + msg);
    }
    
    var next_state = 1;
    function swap() {
      LED1.write(next_state);
      next_state = !next_state;
    }
    
    function pageHandler(req, res) {
      //console.log("Client request: " + JSON.stringify(req));
      if (req.url == "/favicon.ico") {
        res.writeHead(200, {'Content-Type': 'image/x-icon'});
        res.write(favicon);
        //console.log("Client served with favicon.ico");
      } else if (req.url == "/led") {
        swap();
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write("Led toggled.");
        console.log("Client toggled LED");
      } else {
        var r = false;
        if (req.url.substring(0, 5) == "/rand") r = true;
        var content = "sensors(" + 
            JSON.stringify([
              { 
                sensor : "A0", 
                value : getA(A0, r) },
              { 
                sensor : "A1", 
                value : getA(A1, r) },
              { 
                sensor : "A2", 
                value : getA(B0, r) },
              { 
                sensor : "A3", 
                value : getA(B1, r) },
              { 
                sensor : "A4", 
                value : getA(A4, r) },
              { 
                sensor : "A5", 
                value : getA(A5, r) },
              { 
                sensor : "A6", 
                value : getA(A6, r) },
              { 
                sensor : "A7", 
                value : getA(A7, r) }
            ]) + ")";
        returnJsonStr(res, content);
        // console.log("Client served. (rand:"+r+") E.getErrorFlags: " + E.getErrorFlags());
      }
      res.end();
    }
    
    function reboot() {
      console.log("Restarting to recover error..");
      E.enableWatchdog(1);
      while(1);
    }
    
    var favicon = "\0\0\x01\0\x01\0\x10\x10\x10\0\x01\0\x04\x00\xf0\0\0\0\x16\0\0\x00\x89PNG\x0d\x0a\x1a\x0a\0\0\0\x0dIHDR\0\0\0\x10\0\0\0\x10\x08\x06\0\0\0\x1f\xf3\xffa\0\0\x00\xb7IDAT8\x8d\xa5S\xc1\x0d\x03!\x0csN\xb7\x91w\xcaP\xde)3\xd1G\x09\x0a\x85\xab\xa8\xea\x0f\x02\x82c\x1b0\x92x\x82\xbb\xb7:\x8f\x08D\x84\xd5\xb5\x1b\x00H\xb6>N\x04uN\x12\x92\x10\x11S\xcd]\x0b\xbf\xa9\xe9\x8a\x00\xa0I\x1a*\x06A\x97\xb7\x90\xd4\x8e$A\x12\xee\xde\xb2vR\x90$\xc8q\xf6\x03\xbc\x15Ldw]\x88zpc\xab*\x8c\x08H\xb2A\x90\x1e\x97\xce\x1bd3\x00\xb8v\x9b\xa7p\xf7\xb6\x10\x9cb\xc9\xe0Wd\x06\x17\x80v\xe2\xfb\x09\x17\x00H\xfa\x8b\xc0\xba\x9c\xe3CU\xf1\xc8@\xd2\x08fW\xf8i3?U\x12\x18z\x16\xf5A\x9ddc_\xee\xbd~e{*z\x01|\xcdnfT\x03\x0an\0\0\0\x00IEND\xaeB`\x82";
    
    var WIFI_NAME = "xxx";
    var WIFI_OPTIONS = { password : "xxx" };
    
    function onInit() {
      console.log("Connecting to WiFi");
      var wifi = require("EspruinoWiFi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
        if (err) {
          console.log("ERROR connect():"); 
          console.log(err);
          setTimeout(function() {reboot();}, 5000);
          return;
        } else {
          console.log("Connected");
          wifi.getIP(function (err, ip) {
            if (err) {
              console.log("ERROR getIP():"); 
              console.log(err); 
              setTimeout(function() {reboot();}, 5000);
              return; 
            }
            wifi.at.debug();
            console.log("Web server at: " + JSON.stringify(ip));
            var http = require("http");
    //        E.enableWatchdog(2);
            http.createServer(pageHandler).listen(80);
          });
        }
      });
    }
    save();
    
About

Avatar for samppa @samppa started