You are reading a single comment by @Stephen- and its replies. Click here to read the full conversation.
  • Just as a follow up I am testing with a pico wifi, eMQ as the broker, I see random errors with this code on the device:

    var server = "ubuntu.home"; // the ip of your MQTT broker
    var options = { // all optional - the defaults are below
      client_id : getSerial(), // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()`
      keep_alive: 60, // keep alive time in seconds
      port: 1883, // port number
      clean_session: true,
      username: "stephen", // default is undefined
      password: "*****",  // default is undefined
      protocol_name: "MQTT", // or MQIsdp, etc..
      protocol_level: 4, // protocol level
    };
    var mqtt = require("MQTT").create(server, options /*optional*/);
    
    mqtt.on('connected', function() {
      mqtt.subscribe("test/espruino");
    });
    
    mqtt.on('publish', function (pub) {
      console.log(JSON.stringify(pub));
      console.log("topic: "+pub.topic);
      console.log("message: "+pub.message);
    });
    
    var WIFI_NAME = "****";
    var WIFI_OPTIONS = { password : "****" };
    
    var wifi = require("EspruinoWiFi");
    
    function getPage() {
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        console.log("Response: ",res);
        res.on('data', function(d) {
          console.log("--->"+d);
          mqtt.publish("test/espruino", d);
        });
      });
    }
    
    function makeid()
    {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm­nopqrstuvwxyz0123456789";
    
        for( var i=0; i < 5; i++ )
            text += possible.charAt(Math.floor(Math.random()­ * possible.length));
    
      console.log('makeid: '+text);
      return text;
    }
    
    function postIt()
    {
      var msg = "MSG: " + makeid();
      mqtt.publish("test/espruino", msg);
    }
    
    wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
      if (err) {
        console.log("Connection error: "+err);
        return;
      }
      console.log("Connected!");
      wifi.getIP(function (err, res) {
        if (!err) {
          console.log(res.ip);
          mqtt.connect();
        } else {
          console.log(err);
        }
      });
      getPage();
      ts = setInterval(function() {postIt();}, 20000);
    });
    

    It seems to randomly add \xD0\x00 to the message.

    Console.log

     1v91 Copyright 2016 G.Williams
    >
    =undefined
    Connected!
    192.168.1.226
    Client connected
    Response:  httpCRs {
      "headers": {
        "Date": "Tue, 04 Apr 2017 19:19:23 GMT",
        "Server": "Apache/2.4.18 (Ubuntu)",
        "Last-Modified": "Fri, 15 Nov 2013 15:42:26 GMT",
        "ETag": "\"d-4eb390b887c80\"",
        "Accept-Ranges": "bytes",
        "Content-Length": "13",
        "Connection": "close",
        "Content-Type": "text/plain"
       },
      "httpVersion": "1.1",
      "statusCode": "200",
      "statusMessage": "OK"
     }
    --->Hello World!
    MQTT connection accepted
    {"topic":"test/espruino","message":"from­ server","dup":0,"qos":0,"retain":1}
    topic: test/espruino
    message: from server
    makeid: gRQRa
    undefined
    Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined
     at line 1 col 88
    ...lish",c),a.emit("message",c.topic,c.m­essage);else if(d!==f.P...
                                  ^
    in function called from system
     at line 2 col 28
      console.log("topic: "+pub.topic);
                               ^
    in function called from system
    MQTT unsupported packet type: 0
    [MQTT]0,13,116,101,115,116,47,101,115,11­2,114,117,105,110,111,77,83,71,58,32,103­,82,81,82,97
    makeid: sJZwi
    {"topic":"test/espruino","message":"MSG:­ sJZwi\xD0\x00","dup":0,"qos":0,"retain":­0}
    topic: test/espruino
    message: MSG: sJZwiÐ
    makeid: kL0LJ
    {"topic":"test","message":"","dup":0,"qo­s":0,"retain":0}
    topic: test
    message:
    Uncaught Error: Unknown Timeout
     at line 2 col 149
    ...ACK)if(clearTimeout(a.ctimo),c=c.char­CodeAt(3),0===c)a.conne...
                                  ^
    in function called from system
    makeid: GJTbH
    {"topic":"test/espruino","message":"MSG:­ GJTbH\xD0\x00","dup":0,"qos":0,"retain":­0}
    topic: test/espruino
    message: MSG: GJTbHÐ
    makeid: pUTU1
    undefined
    Uncaught Error: Field or method "topic" does not already exist, and can't create it on undefined
     at line 1 col 88
    ...lish",c),a.emit("message",c.topic,c.m­essage);else if(d!==f.P...
                                  ^
    in function called from system
     at line 2 col 28
      console.log("topic: "+pub.topic);
                               ^
    in function called from system
    MQTT unsupported packet type: 6
    [MQTT]101,115,116,47,101,115,112,114,117­,105,110,111,77,83,71,58,32,112,85,84,85­,49
    makeid: 6QAbL
    {"topic":"test/espruino","message":"MSG:­ 6QAbL\xD0\x00","dup":0,"qos":0,"retain":­0}
    topic: test/espruino
    message: MSG: 6QAbLÐ
    makeid: demXD
    {"topic":"tes","message":"","dup":0,"qos­":0,"retain":0}
    topic: tes
    message:
    MQTT unsupported packet type: 7
    [MQTT]116,47,101,115,112,114,117,105,110­,111,77,83,71,58,32,100,101,109,88,68
    > 
    
About

Avatar for Stephen- @Stephen- started