Gopal Kildoliya

SwarmSense with Mongoose OS

March 12, 2018

What is Mongoose OS?

Mongoose OS is an Operating System for connected devices which makes the IoT development very easy. It supports ESP32, ESP8266, CC3220, CC3200 microcontrollers.

It has built-in support for Generic MQTT/Restful connectivity protocols so you don’t have to be worried about connection management. It supports C/C++ and mJS JavaScript for prototyping and deployment. Mongoose OS comes with free GPLv2 license (requires to open end product’s source code). If you want your source code to be private, you can purchase the commercial license. For more details about Mongoose OS check out the official website.

In this blog, we will connect ESP32 (with Mongoose OS) to the SwarmSense IoT platform via MQTT protocol and send the data to the server.

Create Sensor Type and Sensor

First of all, create a sensor type if you don’t have any. Here we will create a sensor type which has two value fields (memory, uptime) and one config field (status). To create the sensor type got to: Admin > Manage Sensor Types > Create. Give the name, add two value fields and one config field. Now save it.

Create Sensor Type

Here we will control the LED on the ESP32 Devkit according to the “status” configuration field of the sensor and send the uptime and memory uses of the device to the server.

Once the sensor type is ready, create a sensor for this type and note down the Sensor ID and Key. These will be used to configure the MQTT in Mongoose OS.

Configure, build and flash

Now let’s move to the hardware. Mongoose OS uses “mos” tool in order to build and flash the firmware to the device. Check out the official documentation to install “mos” tool and build options.

The source code for this tutorial can be downloaded from GitHub repo. After downloading it, you have the configure the sensor details and server details in fs/conf8.json file. Update the following details in mqtt section.

"mqtt":{
     "server": "<swarmsense_server>",
     "user": "sensor_<sensor_id>",
     "pass": "<sensor_key>"
 },

For MQTT auth, the username will be like sensor_<sensor_id>. So if the sensor ID is 123def then the username will be "sensor_123def" and the password will be the sensor key. The server field will also have the port number. 1883 is default and 8883 while using SSL.

You have to provide the WiFi access point details in order to connect ESP32 to the internet.

"wifi":{
    "sta": {
        "ssid": "ssid",
        "pass": "password"
    }
},

You also have to add the sensor id in swarmsense section:

"swarmsense": {
    "sensor_id": "<sensor_id>"
}

For the tutorial, we are using mJS JavaScript for programming. Mongoose OS has built-in support for MQTT so you don’t have to be worried about the connection handling. The fs/init.js has the mJS code for this example. Check out the Mongoose OS MQTT library for more details about publish - subscribe methods.

In the fs/init.js, we subscribe to the sensor configuration. So whenever the configuration is changed, the callback function will be called. In the callback function we check the status config field and according to that, we toggle the led. We have added a timer for every minute that will publish data to the sensor.

Once the configuration is done, build the firmware with “mos” tool

sudo mos build --arch esp32

After the successful build, flash it :

sudo mos flash --port /dev/ttyUSB0

Make sure that you are using the correct USB port.

After the flash, esp32 will restart. You can see the logs via console:

sudo mos console --port /dev/ttyUSB0

It will connect to the given WiFi and then it will connect to the SwarmSense MQTT broker and start publishing the memory and uptime values.

Now to change the LED, you have to change the sensor configuration. For this create a dashboard and add “Toggle” widget and select the “status” config field of the sensor we created earlier. After saving the widget, you can toggle the LED by toggling the widget switch.

Whenever you change the configuration of a sensor, the configuration will be published to the sensors/<sensor_id>/configuration MQTT topic. So you can add handlers for this topic and do whatever you want to.

To publish the sensor values, publish it to sensors/<sensor_id>/values topic. You can also use the REST API to send the sensor values instead of MQTT.

What Next:-

This is just an introduction how you can use SwarmSense IoT Platform with Mongoose OS. Still, there are a lot of things you can do with SwarmSense IoT Platform. Try adding Alerts/Triggers to turn the LED on and off.

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2021 - BaseApp - All Rights Reserved

LATEST POSTS

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram