You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Code is also attached as file... ;-)

    // /ESP8266/server/MyPICOMon00R2.js
    
    var gIsL = true; // ...is logging
    var gIsC = true; // ...terminal is console vs. display
    var gLog = function(v) { console.log(v); };
    
    var gTrm = null, T_C = {w:240, h:320, lh:10}; // terminal config
    
    var gCtl = // initialization controller
    { is:[], i:-1 
    , adi: function(i) { this.is.push(i); }
    , ini: function(ok,str) { if (this.i >= 0) {
        if (gIsL) gLog("is["+this.i+"]"+((str)?str:"")+": "+((ok)?"ok":"failed")); } 
        if (ok) { if (++this.i < this.is.length) { var _t = this; 
          setTimeout(function(){ _t.is[_t.i](function(ok,txt){ _t.ini(ok,txt); }); },1); }  
                } else { /* is[i]() failed */ } }
    , run: function() { this.i = -1; this.ini(true); }
    };
    
    var gSeq = // sequence controller
    { fs:[], bsy:false, t:null
    , ini: function() { this.fs = []; this.bsy = false; }
    , ads: function(f,r,t) { this.fs.push([f,r,t]); 
        if (!this.bsy) { this.exe(); } }
    , exe: function() { if ((this.bsy = (this.fs.length > 0))) { 
        var s = this.fs.splice(0,1)[0];
        s[0](this.nex.bind(this)); } }
    , nex: function(f,r,t) { if (f) { this.fs.splice(0,0,[f,r,t]); }
        setTimeout(this.exe.bind(this),1); }
    }; gCtl.adi(function(cb){ gSeq.ini(); cb(true,"Seq"); });
    
    gTrm = // terminal
    { c:T_C 
    , cl: 0 
    , p: function(l,nl) { var c = this.c;
        if (gIsC) { gLog(l); return; } // terminal is console
        gD.setColor(0,0,0);
        if (this.cl + c.lh > c.h) {
          gD.fillRect(0,this.cl,c.lh,this.cl+c.lh-­3);
          this.cl = 0;
        }
        gD.fillRect(0,this.cl,c.w-1,this.cl+c.lh­-1);
        gD.setColor(1,1,1); gD.drawString(l,0,this.cl);
        if (nl) { this.cl += c.lh; }
        gD.setColor(0,0,0); gD.fillRect(0,this.cl,c.w-1,this.cl+c.lh­-1);
        gD.setColor(1,0,0); gD.fillRect(0,this.cl,c.lh,this.cl+c.lh-­3);
      } 
    };
    
    /* disabling display for test without display
    var gD = null, DSP_C = // display
    { md:require("ILI9341")
    , spi:SPI1
    , spiC: {sck:A5, miso:A6, mosi:A7, baud:1000000}
    , dc:B10, cs:B1, rst:B13
    };
     */
    
    var gW = null, WIFI_C = // wifi config
    { md:require("ESP8266WiFi_0v25")
    , ser:Serial2, bd:115200, serC:{tx:A2, rx:A3}
    , ssid:"myWLAN", lak:"myCredential" 
    , ip:"", p:80 
    };
    
    var http = null, HTTP_C = // http configs / requests
    { md:require("http") 
    , urls:
      [ "http://www.pur3.co.uk/hello.txt"
      , "http://weather.noaa.gov/pub/data/foreca­sts/marine/coastal/pz/pzz535.txt"
      , "http://192.168.0.103/esp/json.jsn"
      , "http://l92.168.0.103:1337/"
      ]
    };
    
    /* disabling display for test without display
    gCtl.adi(function(cb){ var c = DSP_C; // display initialization
      c.spi.setup(c.spiC);
      gD = c.md.connect(c.spi, c.dc, c.cs, c.rst, function(){ 
        gD.clear();
        cb(true,"DSP");
      }); }
    );
     */
    
    gCtl.adi(function(cb){ var c = WIFI_C; // wifi multi level initialization
      c.ser.setup(c.bd, c.serC);
      gW = c.md.connect(c.ser, function(err){
        if (err) { cb(false,"wifi.reset: " + err); }
        else { cb(true,"wifi.reset"); }
      }); }
    );
    gCtl.adi(function(cb){ var c = WIFI_C;
      gW.connect(c.ssid, c.lak, function(err){
        if (err) { cb(false,"wifi.connect: " + err); }
        else { cb(true,"wifi.connect"); }
      }); }
    );
    
    // get url content and display raw in console
    function getc(udx,nex) { var c = HTTP_C;
      if (gIsL) gLog(c.urls[udx] + ":");
      c.md.get(c.urls[udx], function(res){
        res.on('data', function(dta){
          gLog(dta);
          if (nex) nex();
        });
      });
    }
    
    // get url content and and show in display
    function getd(udx) { var c = HTTP_C;
      gLog("---> " + c.urls[udx] + ":");
      var d = true, l = "", ol, nl;
      c.md.get(c.urls[udx], function(res){
        res.on('data', function(dta){
          l = l + dta;
          var ls = l.split("\n"), lm = ls.length - 1, lx = -1;
          while (++lx < lm) { ol = (l = ls[lx]).length;
            if ((d = d && (l != "$$"))) {
              while (ol > (nl = (l = l.replace(" OR LESS","|<")).length)) ol = nl; 
              while (ol > (nl = (l = l.replace(" TO ","~")).length)) ol = nl; 
              while (ol > (nl = (l = l.replace(" TO","~")).length)) ol = nl; 
              gTrm.p(l,true);
            } }
          l = ls[lm]; ls = null; 
        });
      });
    }
    
    // get info - time (from node server), ESP9266 version, IP, and access point
    function getInfo() {
      // gSeq.ads(function(nex){ getc(2,nex); });
      gSeq.ads(function(nex){
        gW.getVersion(function(e,v){
          if (e) { gTrm.p("get Version failed: " + e,true); }
          else { gTrm.p("Version: " + v,true); }
          nex();  
      }); });
      gSeq.ads(function(nex){
        gW.getIP(function(e,ip){
          if (e) { gTrm.p("get IP failed: " + e,true); }
          else { gTrm.p("IP: " + ip,true); }
          nex();  
      }); });
      gSeq.ads(function(nex){
        gW.getConnectedAP(function(e,ap){
          if (e) { gTrm.p("get AP failed: " + e,true); }
          else { gTrm.p("AP: " + ap,true); }
          nex();
      }); });
    }
    
    // start after upload by invocation in console or
    // start after save and re-powering
    function onInit() { gCtl.run(); }
    

    1 Attachment

About

Avatar for allObjects @allObjects started