Use AWS IoT Device SDK with Espruino and ESP8266

281 Views Asked by At

I'm trying to use the NodeMCU with Espruino IDE and Javascript to send data to AWS IoT Core. However, there's a problem because the Espruino doesn't find the aws-iot-device-sdk module. How can I import it? Or what's an alternative for what I want to do?

Below is the code:

var wifi = require("Wifi");
var awsIot = require('aws-iot-device-sdk');
var WIFI_SSID = "<Wifi>";
var WIFI_OPTIONS = {
  password : "<Password>"
};

var device = awsIot.device({
   keyPath: 'xxxxxxxxxx-private.pem.key',
  certPath: 'xxxxxxxxxx-certificate.pem.crt',
    caPath: 'rootCA.pem',
  clientId: 'nodejs-thing',
      host: 'xxxxxxxxxxxxxx-ats.iot.us-east-2.amazonaws.com'
});

wifi.stopAP(); // disable Wi-Fi AP

wifi.connect(WIFI_SSID, WIFI_OPTIONS, function(err) {
  if (err) {
    console.log("ERROR: Connection failed, error: " + err);
  } else {
    console.log("INFO: Successfully connected");
    console.log(wifi.getStatus());
    console.log(wifi.getIP());

    // set hostname and make the NodeMCU available
    // through espruino.local (ping or for using the
    // Espruino IDE over Wi-Fi
    wifi.setHostname("espruino");

    // save the Wi-Fi settings and they'll be used
    // automatically at power-up.
    wifi.save();
  }
});

device
  .on('connect', function() {
    console.log('connect');
    //device.subscribe('topic_1');
    device.publish('topic_1', JSON.stringify(
      { 
        user: 'user',
        device_id: '02',
        timestamp: '00:00:00',
        temp: 45
      }));
  });

device
  .on('message', function(topic, payload) {
    console.log('message', topic, payload.toString());
  });

Thank you so much in advance.

1

There are 1 best solutions below

0
On

I think you're out of luck I'm afraid (on ESP8266). There's no TLS/HTTPS support in Espruino for ESP8266 because of lack of RAM. However there is support on the official Espruino WiFi board, or even the ESP32 port of Espruino.

There's also no 'aws-iot-device-sdk' in Espruino, but as I understand it it's just an MQTT connection so you can create a secure socket with https://www.espruino.com/Internet#tls and then pass that into the MQTT library: https://www.espruino.com/MQTT

You can specify certificates like this: https://www.espruino.com/Reference#l_tls_connect

Otherwise you could use something like a Raspberry pi to run a local unencrypted MQTT server that connects to the Amazon server.