You are reading a single comment by @heri16 and its replies. Click here to read the full conversation.
  • Partially ported AWS IoT library.
    As there is currently no support for client-cert X.509 auth yet on Espruino, use with socat.

    Created pull requests on github:
    github.com/espruino/EspruinoDocs­/pull/193
    github.com/espruino/EspruinoDocs­/pull/194

    Managed to connect to AWS IoT by offloading tls to socat.

    root@192.168.2.1>
    socat tcp-listen:8883,fork,reuseaddr openssl-connect:A4B6O0T19QBXJ.iot.ap-northeast-1.amazonaws.com:8883,method=TLS1.2,cert="$HOME/Downloads/61a57a0f66-certificate.pem.crt",key="$HOME/Downloads/61a57a0f66-private.pem.key",cafile="$HOME/Downloads/VeriSign-Class 3-Public-Primary-Certification-Authority-G5.pem"
    
    function initEthernet(cb) {
      SPI2.setup({ mosi:B15, miso:B14, sck:B13 });
      var eth = require("WIZnet").connect(SPI2, B10);
      eth.setIP();
      global.eth = eth;
      if('function' === typeof cb){cb();}
    }
    
    function initMQTT(cb) {
      var mqtt = require("MQTT").create("192.168.2.1", { port: 8883 });
      mqtt.on('connected', function() {
        mqtt.subscribe("$aws/things/+/shadow/#");
        mqtt.on('publish', function (pub) {
          console.log("topic: "+pub.topic);
          console.log("message: "+pub.message);
        });
        if('function' === typeof cb){cb();}
      });
      mqtt.connect();
      global.mqtt = mqtt;
    }
    
    function initAwsIotDevice(cb) {
      var awsIot = {};
      awsIot.device = require('aws-iot-device-sdk-device');
      var device = awsIot.device({ host: '192.168.2.1', port: 8883 });
    
      device.on('connect', function() {
        console.log('connect');
        device.subscribe('$aws/things/+/shadow/#');
        device.publish('$aws/things/espruino1/shadow/test', JSON.stringify({test_data:1}));
        if('function' === typeof cb){cb();}
      });
    
      device.on('message', function(topic, payload) {
        console.log('message', topic, payload.toString());
      });
    }
    
    function initAwsIotThingShadow(cb) {
      var awsIot = {};
      awsIot.thingShadow = require('aws-iot-device-sdk-thing');
    
      var thingShadows = awsIot.thingShadow({ host: '192.168.2.1', port: 8883 });
      var rgbLedLampState = {"state":{"desired":{"red":187,"green":114,"blue":222}}};
    
      thingShadows.on('connect', function() {
        thingShadows.register('espruino1');
      });
    }
    
    //E.on('init', initEthernet);
    //E.on('init', initMQTT);
    //save();
    initEthernet(function() {
      console.log(global.eth.getIP());
      initMQTT(function() {
        console.log("AWS IoT Ready");
      });
    });
    
    root@192.168.2.1>
    mosquitto_pub -h 192.168.2.1 -p 8883 -d -t '$aws/things/espruino1/shadow/test' -m "Hello World"
    

    AWS IoT device library is working.
    Will continue to port AWS IoT shadow things library from nodejs.

About

Avatar for heri16 @heri16 started