You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Created a wlan singleton that lets conn(ect), req(uest), and disconn(ect) with hiding the lower level details.

    The application code connects and then repeats requesting data every 10 seconds until stopped. Holding BTN1 down on upload enables (detailed) logging, and holding BTN1 down on display of the data (in the console) stops requesting data.

    Nice to see in the the log is the self recovery of the Espruino CC3000 module with a CC3000 power cycle when something is going down (badly...).

    This is console output, that shows a going down also on the very first request.

    -: 0.462: Started
    -: 0.466: Connecting to wlan device dev CC3000....
    -: 3.080: Connecting to wifi access point myLAN...
    =undefined
    -: 9.527: Connected myLAN with connect
    -: 19.647: Connected myLAN with 192.168.0.107 dhcp
    -: 19.652: Sending request
    ERROR: Timeout in CC3000 driver (4103)
    ERROR: Connect failed (err -1)
    CC3000 WiFi is not responding. Power cycling...
    -: 38.705: Connected ssid with connect
    -: 48.790: Connected ssid with 192.168.0.107 dhcp
    -: 48.795: Sending request
    -: 50.253: >{"generatedA
    -: 50.259: >t":"2015-01-13T20:00:42.1182683+00:00","locationName":"Reading",
    -: 50.265: >"crs":"RDG","platformAvailable":true,"nrccMessages":"","trainSer
    -: 50.271: >vices":[{"origin":{"locationName":"Banbury","crs":"BAN"},"destin
    ...
    .....
    ...
    -: 50.405: >==","subsequentCallingPoints":"","operator":"First Great -: 50.411: >","operatorCode":"GW"}]}
    -: 50.456: str: {"generatedAt":"2015-01-13T20:00:42.1182683+00:00","locationName":"Reading","crs":"RDG","platformAvailable":true,"nrccMessages":"","trainServices":[{"origin":{"locationName":"Banbury","crs":"BAN"},"destination":
    ...
    .....
    ...
    {"locationName":"Reading","crs":"RDG"},"destination":{"locationName":"Redhill","crs":"RDH"},"std":"20:04","etd":"On time","platform":"5","serviceID":"L\/a2l5ZPlBQBdrXWmusOrA==","subsequentCallingPoints":"","operator":"First Great Western","operatorCode":"GW"}]} --- Memory free: 1667  -  usage: 583
    -: 60.506: Sending request
    ERROR: Timeout in CC3000 driver (4103)
    ERROR: Connect failed (err -1)
    CC3000 WiFi is not responding. Power cycling...
    -: 79.562: Connected ssid with connect
    -: 89.694: Connected ssid with 192.168.0.107 dhcp
    -: 89.699: Sending request
    -: 91.040: >{"generatedA
    -: 91.046: >t":"2015-01-13T20:01:23.7612682+00:00","locationName":"Reading",
    -: 91.052: >"crs":"RDG","platformAvailable":true,"nrccMessages":"","trainSer
    ...
    .....
    ...
    -: 91.224: >=","subsequentCallingPoints":"","operator":"First Great Western"
    -: 91.230: >,"operatorCode":"GW"}]}
    -: 91.281: str: {"generatedAt":"2015-01-13T20:01:23.7612682+00:00","locationName":"Reading","crs":"RDG","platformAvailable":true,"nrccMessages":"","trainServices":[{"origin":{"locationName":"Reading","crs":"RDG"},"destination":
    ...
    .....
    ...
    {"locationName":"Basingstoke","crs":"BSK"},"std":"20:07","etd":"On time","platform":"2","serviceID":"Afgn5KsSGhmTNcVkIcxyxA==","subsequentCallingPoints":"","operator":"First Great Western","operatorCode":"GW"}]}
    -: 91.301: getData():...
    -: 91.318: data: {"generatedAt":"2015-01-13T20:01:23.7612682+00:00","locationName":"Reading","crs":"RDG","platformAvailable":true,"nrccMessages":"","trainServices":[{"origin":{"locationName":"Reading","crs":"RDG"},"destination":
    ...
    .....
    ...
    {"origin":{"locationName":"Reading","crs":"RDG"},"destination":{"locationName":"Basingstoke","crs":"BSK"},"std":"20:07","etd":"On time","platform":"2","serviceID":"Afgn5KsSGhmTNcVkIcxyxA==","subsequentCallingPoints":"","operator":"First Great Western","operatorCode":"GW"}]} --- Memory free: 1640  -  usage: 610
    -: 91.338: CC3000 / myLAN disconnected.
    -: 91.341: stopped
    

    This is the code (also attached as file):

    // 20150113
    // - better log (hold BTN1 down on upload to enable conditional logging)
    // - wlan singleton with conn(ect), req(uest), disconn(ect)
    // - repetitive loading of data until BTN1 is held down (again) on recieving
    
    var wlanCreds = {id:"myLAN", pw:"myWPA2Key"}; 
    
    /** log() is global short cut for console.log(). 
        s: is string or object;
        e: (optional): 
           - falsy (default if absent): 0,false for plain log (shows - at line begin);
           - truey: 1,true for error (shows E at begin as indicator);
        o: (optional): shows o (object) in extra console.log to show o's structure;
           tip: if o can be undefined, pass it also as part of s too: log("obj:" + o,0,o)
     */
    var LOG = digitalRead(BTN1);
    var log = function(s,e,o) { 
      var p = "" + (Math.round((getTime() - log._t0) * 1000) / 1000) + "000";
      var d = p.indexOf("."); console.log(((e) ? "E: " : "-: ") + ((p < 0) ? 
        p.substr(p.length - 3) + ".000" : p.substr(0,d + 4)) + ": "+ s);
      if (typeof o !== undefined) { console.log(o); }
    }; log._t0 = getTime();
    
    var WLAN_DEV = { id:"CC3000", dev:require("CC3000") };
    var HTTP = require("http");
    
    var wlan = {
      dId: null,
      lId: null,
      d: null,
      c: false,
      conn: function(dev,creds,ok,err) {
        if (LOG) log("Connecting to wlan device dev " + dev.id + "....");
        this.d = dev.dev.connect();
        if (this.d) {
          this.dId = dev.id;
          if (LOG) log("Connecting to wifi access point " + creds.id + "...");
          var _this = this;
          this.d.connect( creds.id, creds.pw, function (s) {
            if (s == "dhcp") {
              if (LOG) log("Connected " + creds.id + " with " + _this.d.getIP().ip + " " + s);
              _this.lId = creds.id;
              _this.c = true;
              ok();
            } else {
              if (LOG) log("Connected " + creds.id + " with " + s);
            }
          });
        } else {
          log("Did not get wlan dev object / not connected to dev " + dev.id,1);
          if (err) { err(); }
        }
      },
      req: function(url,ok,err) {
        if (LOG) log("Sending request");
        HTTP.get(url, function(res) {
          var str = "";
          res.on('data', function(data) {
            if (LOG) log(">" + data);
            str += data;
          });
          res.on('close', function() {
            if (LOG) log("str: " + str);
            ok(str);
          });
        });
      },
      disconn: function() {
        this.d.disconnect();
        this.c = false;
        if (LOG) log(this.dId + ' / ' + this.lId + ' disconnected.');
      }
    };
    
    var getData = function(){
      wlan.req("http://54.148.129.10/ldbwsService.php",function(str){
        if (LOG) log("getData():...");
        // var obj = JSON.parse(str);
        var mem = process.memory();
        log("data: " + str + " --- Memory free: " + mem.free + "  -  usage: " + mem.usage);
        if (digitalRead(BTN1) === 0) {
          setTimeout("getData();",10000);
        } else {
          wlan.disconn();
          log("stopped");
        }
      });
    };
    
    log("Started");
    wlan.conn(WLAN_DEV,wlanCreds,function(){
      getData();
    });
    
    

    1 Attachment

About

Avatar for allObjects @allObjects started