Sockets and MQTT now on Espruino!

Posted on
  • As the Espruino Pico KickStarter has now crossed the stretch goal for the implementation of sockets, I thought I'd better have a go at implementing them.

    I had a dig around yesterday and found an old branch, and an hour ago I actually managed to get a socket API working... And now, I have a very simple piece of code that publishes the temperature via MQTT:

    function mtStr(s) {
      return String.fromCharCode(s.length>>8,s.length­&255)+s;
    }
    
    function mtPacket(cmd, variable, payload) {
      return String.fromCharCode(cmd, variable.length+payload.length)+variable­+payload;
    }
    
    function mtpConnect(name) {
      return mtPacket(0b00010000, 
               mtStr("MQTT")/*protocol name*/+
               "\x04"/*protocol level*/+
               "\x00"/*connect flag*/+
               "\xFF\xFF"/*Keepalive*/, mtStr(name));
    }
    
    
    function mtpPub(topic, data) {
      return  mtPacket(0b00110001, mtStr(topic), data);
    }
    
    var client;
    
    function onConnected() {
      console.log('creating client');
      client = require("net").connect({host : "192.168.1.50", port: 1883}, function() { //'connect' listener
    
        console.log('client connected');
        client.write(mtpConnect("Espruino"));
        
        var intr = setInterval(function() {
          console.log("Publishing");
          client.write(mtpPub("a/b", E.getTemperature().toFixed(4)));
        }, 2000);
        
        client.on('data', function(data) {
          console.log("[MQTT]"+data.split("").map(­function(c) { return c.charCodeAt(0); }));
        });
        client.on('end', function() {
          console.log('client disconnected');
          clearInterval(intr);
        });
      });
    }
    
    
    // For CC3000 WiFi
    var wlan = require("CC3000").connect();
    wlan.connect( "BTHub4-5ZN2", "2f3b5659ad", function (s) { 
      if (s=="dhcp") {
        console.log("My IP is "+wlan.getIP().ip);
        onConnected();
      }
    });
    // Or the following for ethernet
    //SPI2.setup({mosi:B15,miso:B14,sck:B13}­);
    //var eth = require("WIZnet").connect(SPI2,B10);
    //onConnected();
    

    This is obviously pretty basic, but hopefully I'll get some better examples built up over time - maybe using the MQTT library on NPM (but the 11 source files there do make me wonder if it'll ever fit in a microcontroller!).

    This should work with the latest build when it gets automatically uploaded in an hour or so, on Espruino boards with both CC3000 WiFi and WIZnet wired Ethernet.

    And just to show that it does actually work on the KickStarter board - although this one is a very early prototype:


    1 Attachment

    • 20141120_153009.jpg
  • Just thought I'd also mention that you can now create a telnet server that allows remote access to Espruino's console, letting you write (and debug) code over the net!

    var server = net.createServer(function(c) {
      console.log('server connected');
      c.write('Your welcome message!\r\n');
      LoopbackA.setConsole();
      LoopbackB.pipe(c);
      c.pipe(LoopbackB);
    }).listen(23);
    

    This still needs work (coping with disconnects, multiple connections, and some attempt at a password), but it shows what can be done!

  • I really want to get going with this but have been put off getting a wiznet device because of the delivery cost to UK.

    I'm planning to wait until I have a few items to order and get them all at once from Mouser (delivered from US).

    Does anyone know of a better source (hopefully within UK, free delivery?)

  • Thanks Gordon for the nice work on the "Internet I/F".
    The WIZnet WIZ550io Module is available from many sources in EU also.
    Mouser is B2B and very USA focused - TAX, transport....
    But, more and more shops adopt our Modules, but "free shipping" is very often not available.
    Please check: RS, TME, SOS, Lextronic, Watterott, ... or our shop.wiznet.eu

  • I notice that Espruino uses the WIZnet WIZ550io module containing a W5500 chip.

    There is another module on the Wiznet website
    http://shop.wiznet.eu/modules/io-modules­/wiz820io.html this uses the W5200 chip.

    Do you know if the current library will work with this chip?
    Maybe Joachim "4jochen" might be able to comment on the differences between the two modules.

    I like the idea of the smaller module, shame it costs half the price of the module to get it shipped. Although in the UK RS do supply them see http://goo.gl/itfy4O

  • I'm afraid that right now Espruino won't work with anything except the W5500 chip. It shouldn't actually be a big deal to use the W5200 instead (and someone was looking at this) - it'd require a recompile of the binary though.

  • Here is a message I got from Wiznet, looks like the W5500 is the more modern chip. Shame there isn't a small module.

    Dear Martin Honeywill,

    Thank you for your request. You can see a quick overview of our chips
    at thie link:
    http://eucache.wiznet.co.kr/sub_modules/­en/product/Product_Line.asp?cate1=5&cate­2=7

    The W5500 has been released last year and is our recommendation for
    new projects. It is our most stable and cost optimized chip. In case
    you want to evaluate our chip, we can recommend to start with the
    WIZ550io module - http://wiznet.eu/WIZ550io

    At the moment we do not have any small footprint module like the
    WIZ820io - it is also not planned at the moment - the WIZ550io is not
    as small as the WIZ820io, but hopefully small enough to fit your
    needs.

    For any further information, please don't hesitate to contact us via
    support@wiznet.eu.

    Best regards, Felix Krieg

  • Hello, are there any plans to make this work with the ESP8266?

    Thx

  • Yes, it should 'just work'. Have you tried it and had problems?

    The whole idea of the network API is that the same code can be run on any network device - ESP8266, W5500, CC3000, and even (thanks to @net-tobi) GSM

  • Just tested the MQTT module on a very early version of Espruino on ESP32 and it worked first time without incident. Nice!!!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Sockets and MQTT now on Espruino!

Posted by Avatar for Gordon @Gordon

Actions