Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Thorsten von Eicken
    @tve
    so most likely some event happens that causes it to churn through memory. In my mqtt module I had to add something so it wouldn't publish if there was no connection so it wouldn't fill the TX buffer...
    HyGy
    @HyGy
    can U provide me the mqtt module?
    Mark Becker
    @MaBecker
    @tve adding modules to ESP8266_Board.py has no effect, build is still broken when using USE_GRAPHICS=1
    Mark Becker
    @MaBecker
    where to start to figure out why a build with USE_GRAPHICS=1 is looping ?
    HyGy
    @HyGy
    @tve can U please provide me the modifed tinymqtt?
    Ollie Phillips
    @olliephillips
    @HyGy, you can handle disconnects in your program with the library you have. I've had > 5 days of uptime to a server I know routinely disconnects clients ( I powered it down but I feel it would have run indefinitely). Check out this method which covers it, and seems to work.
    // Manage MQTT connection
      mqttSetup: function() {
        tempdeck.mqtt = null;
        tempdeck.mqtt = require("https://github.com/olliephillips/tinyMQTT/blob/master/tinyMQTT.min.js")
          .create(tempdeck.config.mqtt.server);
        tempdeck.mqtt.connect();
        tempdeck.mqtt.on("connected", function(){
          tempdeck.send = true;
          console.log("MQTT Server: '"+ tempdeck.config.mqtt.server +"', Topic: '" + 
                      tempdeck.config.mqtt.topic + tempdeck.config.serial + "'");
          tempdeck.mqtt.on("disconnected", function(){
            tempdeck.send = false;
            console.log("Disconnected");
            tempdeck.mqttSetup();
          });
        });
      },
    obviously, you also add an if test and do not publish if tempdeck.send != true
    HyGy
    @HyGy
    @olliephillips I think there was no disconnect event, I checked the list, but its that I need to put a logick to handle this.
    Thorsten von Eicken
    @tve
    My version of the mqtt library:
    /*
     * tinyMQTT.js
     * Stripped out MQTT module that does basic PUB/SUB
     * Intended for devices running Espruino, particularly the ESP8266
     * Ollie Phillips 2015
     * MIT License
     * Modified by Thorsten von Eicken 2015
    */
    
    var MQTT = function(server){
      this.server = server;
    };
    var sfcc = String.fromCharCode;
    
    // event handler receiving data, assumes that the entire message is available !?
    function mqttData(data) {
      var cmd = data.charCodeAt(0);
      if ((cmd >> 4) === 3) {
        // publish message
        var var_len = (data.charCodeAt(2) << 8) | data.charCodeAt(3);
        var msg = {
          topic: data.substr(4, var_len),
          message: data.slice(4+var_len),
          //dup: (cmd & 0b00001000) >> 3,
          //qos: (cmd & 0b00000110) >> 1,
          retain: cmd & 0b00000001
        };
        this.emit('message', msg);
      }
    };
    
    // encode a string for mqtt by prefixing the length (16-bits)
    function mqttStr(str) {
            return sfcc(str.length >> 8, str.length&255) + str;
    };
    
    // write an mqtt packet
    function mqttWrite(wr, cmd, variable, payload) {
      var l = variable.length+payload.length;
      wr(l < 128 ? sfcc(cmd, l) : sfcc(cmd, 0x80+(l>>7), l&0x7f));
      wr(variable);
      wr(payload);
    };
    
    MQTT.prototype.connect = function(id, clean){
      var mq = this;
      var onC = function() {
        console.log("mqtt connected");
        mq.wr = cl.write.bind(cl); // write method
        // send connect message
        var v =
          mqttStr("MQTT") +        // protocol name
          "\x04" +                 // protocol level, 4=v3.1.1
          (clean?"\x02":"\x00") +  // connect flags, 02=clean session
          "\x00\x00";              // keep-alive timeout, 0=disable
        mqttWrite(mq.wr, 0b00010000, v, mqttStr(id));
        // register callbacks
        mq.emit("connected");
        cl.on('data', mqttData.bind(mq));
        cl.on('end', function() { mq.wr = null; mq.emit("disconnected"); });
      };
      var cl = require("net").connect({host : mq.server, port: 1883}, onC);
      mq.cl = cl;
    };
    
    MQTT.prototype.subscribe = function(topic) {
      mqttWrite(this.wr, 0b10000010,
                sfcc(0, 1),                // packet identifier
                mqttStr(topic) + sfcc(0)); // QoS 0
    };
    
    MQTT.prototype.publish = function(topic, data) {
      mqttWrite(this.wr, 0b00110001, // QoS0, retain=1
                mqttStr(topic),
                data);
      this.emit("published");
    };
    
    MQTT.prototype.ready = function() { return this.wr !== null; }
    
    MQTT.prototype.disconnect = function(){
      if (this.wr) {
        this.wr(sfcc(14<<4)+"\x00");
        this.cl.end();
        this.wr = null;
      }
    };
    
    // Exports
    exports.create = function (server) {
      return new MQTT(server);
    };
    I use the ready() function before publishing.
    HyGy
    @HyGy
    @tve thanks
    Ollie Phillips
    @olliephillips
    and it minifies to about the same size, but also handles broker availability - very nice @tve
    Thorsten von Eicken
    @tve
    thanks! late night hacking...
    Ollie Phillips
    @olliephillips
    Espruino ESP8266 Wifi setup utility in Go. Premise is "abstract all the hard bits". This is my first stab - just Wifi - program/sketch load I can build in - have it already in Espruingo.
    Flashing would be the Holy Grail. Looks a bit hard (working through esptool.py) Help/advice welcome.
    https://github.com/olliephillips/esp-up
    Les Sears
    @lessears
    @tve - I have the deep sleep code completed and preliminary testing is going well. For my application it will be sending temperature data every hour or so. I have code in the onInit() that sets up the wifi connection, reads the sensor, then calls deep sleep. Back of the napkin calculation show that I could run for about 50 days on 4 AAs with sending data once an hour.
    @tve - There's really very little code for calling the deep sleep function. Are you interested in including it in your code?
    Thorsten von Eicken
    @tve
    sure
    BTW, you might want to save the wifi settings as opposed to configuring them each time, which may well use more power
    I'm more interested in light sleep than deep sleep, 'cause it preserves memory
    what does your napkin calculation look like?
    Les Sears
    @lessears
    not scientific, but basically, last week a drain 4AAs by letting it run and doing a sensor reading and data post every second until the batteries died, that ran for about 20 hours, so 20 60 60 = 72,000 "readings", so once an hour would be the 72,000 / 60 /24 for for about 50 days - rough ballpark.
    agree on saving the wifi parameters. Unbfortunately I do not have the equipment to monitor actual power usage, so it will all be real world test driven
    ```
    Les Sears
    @lessears
    For some reason I have difficulty getting the code markdown to work reliably in Gitter
    /*JSON{
      "type"     : "staticmethod",
      "class"    : "ESP8266",
      "name"     : "deepSleep",
      "generate" : "jswrap_ESP8266_deepSleep",
      "params"   : [
        ["micros", "JsVar", "Number of microseconds to sleep."]
      ]
    
    }*/
    
    void   jswrap_ESP8266_deepSleep(JsVar *jsMicros) {
    
        jsiConsolePrintf("In deepSleep()\n");
        if (!jsvIsInt(jsMicros)) {
        jsExceptionHere(JSET_ERROR, "Invalid microseconds.");
             return;
        }
        int sleepTime = jsvGetInteger(jsMicros);
        jsiConsolePrintf("sleepTime: %d\n", sleepTime);
    
        system_deep_sleep(sleepTime);
    
    }
    Thorsten von Eicken
    @tve
    cool!
    Les Sears
    @lessears
    Probably should remove or comment those console prints
    I was thinking that maybe have a couple of methods for convenience that would take other units of time, such as seconds or hours - microseconds is an awful small interval.
    but don't want to waste precious memory on it if not necessary
    Les Sears
    @lessears
    Oh, I think I messed up that calculation now that I look at it
    72,000 readings and only reading 24 times a day would be 3000 days, so figure off by factor of 10 at worst and I get about a year out of 4 AAs
    hard to concentrate in the evenings after a full day of work and family. :)
    Les Sears
    @lessears
    getResetInfo() is even showing "deep sleep" as the reset reason
    Thorsten von Eicken
    @tve
    power consumption while asleep is not zero, so you have to factor that in. Your power regulator and capacitors will slurp current too. Self-discharge in one year needs to factor in also :-).
    How long does it take for your esp to associate with your network from a reset? It takes mine ~12 seconds on my home AP :-(. My outdoor AP is better and it's about half that, I think.
    (Same SSID with same encryption settings, go figure)
    Les Sears
    @lessears
    Takes about 1-3 seconds on average to get connection
    What device would I need to be able to monitor actual power usage over time? Could it be done with an oscilloscope? - been looking for an excuse to buy one.
    Les Sears
    @lessears
    Gonna run it all night doing a deep sleep every 5 minutes and waking up long enough to connect and take five readings - about 40 secs total every 5 mins
    Thorsten von Eicken
    @tve
    oscilloscope can definitely tell you, simplest thing is to connect a 1Ohm resistor in series with the battery and measure voltage drop. Not the most exact but pretty good approximation.
    Les Sears
    @lessears
    cool
    thanks
    HyGy
    @HyGy
    @tve I cheded your mqtt lib, can U please put it in the username and password and port handling? I need authentication.
    Les Sears
    @lessears
    @tve - I was wrong about the 1 - 3 seconds to connect, average seems closer to 4-5 secs.
    jdschuitemaker
    @jdschuitemaker
    @HyGy I am wondering what the status is on your efforts to get the Nextion display talking with an ESP8266. Did you make any progress?
    HyGy
    @HyGy
    @jdschuitemaker espruino/Espruino#549
    It is does not work good yet.
    I got some data but it is corrupt. Don't know what is wrong.
    I asked @gfwilliams but nothing yet.