Embedded Documentation

Last updated: Oct 17th, 2016

Getting Started

Mesosfer Backend as a Service provide solution to manage your IoT devices. Generally, We devide our service into 2 types. First type is device that works with an operating system such as RaspberryPi, Intel Edison, OrangePi, etc. Second is Realtime Operating System such as Arduino, CC3200, ESP8266, etc. Mesosfer uses MQTT (Message Queuing Telemetry Transport) protocol as communication between devices that consume small payload and reduce power consumption.

OS

This scope is for devices that works with operating system such as : ubuntu, raspbian, windows, etc. We provide any language for communication with Mesosfer Backend as a Service.

NodeJS

You have to complete these following steps to execute code attached.

  • Create EMBEDDED apps in mesosfer backend as a service.
  • Create a bucket then define your columns.
  • Create device data.
  • SSH your device then install environment.
  • Install NodeJS.
  • Create folder then enter in that folder.
  • Install mqtt client npm install mqtt
  • Create file publisher.js then put script in publisher scope below.
  • Create file consumer.js then put script in consumer scope below, if you want to consume data you published.
Publisher

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://api.mesosfer.com', { username:'Your username', password:'Your password' })
client.on('connect', function () {
    console.log('mqtt connect')
})
client.publish('Your topic', '{"appId":"Your appId", "schemaId":"Your schemaId", "bucket":"Your bucket", "metadata":{Your metadata}}')
client.end()

Consumer

var mqtt = require('mqtt')
var client  = mqtt.connect('mqtt://api.mesosfer.com', { username:'Your username', password:'Your password' })
client.on('connect', function () {
    client.subscribe('Your topic')
})
client.on('message', function (topic, message) {
    console.log(message.toString())
    client.end()
})

Python

You have to complete these following steps to execute code attached.

  • Create EMBEDDED apps in mesosfer backend as a service.
  • Create a bucket then define your columns.
  • Create device data.
  • SSH your device then install environment.
  • Install NodeJS.
  • Create folder then enter in that folder.
  • Install mqtt client pip install paho-mqtt
  • Create file publisher.js then put script in publisher scope below.
  • Create file consumer.js then put script in consumer scope below, if you want to consume data you published.
Publisher

import paho.mqtt.publish as publish

publish.single("Your topic", '{"appId":"Your appId", "schemaId":"Your schemaId", "bucket":"Your bucket", "metadata":{Your metadata}}', hostname="api.mesosfer.com", port=1883, auth={'username':"Your username", 'password':"Your password"})

Consumer

import paho.mqtt.client as mqtt

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("Your topic")

def on_message(client, userdata, msg):
    print "Topic: ", msg.topic+'\nMessage: '+str(msg.payload)

client = mqtt.Client()
client.username_pw_set("Your username", "Your password")
client.on_connect = on_connect
client.on_message = on_message

client.connect("api.mesosfer.com", 1883, 60)
client.loop_forever()

RTOS

This scope is for RTOS devices, intended to serve real-time applications which process data as it comes in, typically without buffering delays, such as : Arduino, ESP8266, Intel Galilleo, etc.

Arduino

You have to complete these following steps to execute code attached.

  • Create EMBEDDED apps in mesosfer backend as a service.
  • Create a bucket then define your columns.
  • Create device data.
  • Install & Open Arduino IDE.
  • Open Sketch->Import Library->Manage Library then search & install pubsubclient.
  • Put script below into your sketch.
  • Change the value of MQTT_MAX_PACKET_SIZE in PubSubClient.h to 512.
  • Compile & Deploy.
ESP8266

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "Your SSID";
const char* password = "Your SSID Password";
const char* mqtt_server = "api.mesosfer.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

/// consumer
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print(topic);
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    
    // Attempt to connect broker
    if (client.connect(clientId.c_str(), "Your device username", "Your device password")) {
      Serial.println("connected");
      client.subscribe("Your topic or channel");
      client.publish("Your topic or channel", "{\"appId\":\"Your appId\",\"schemaId\":\"Your schemaId\",\"bucket\":\"Your bucet\",\"metadata\":{Your metadata}}");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
}

Web Socket

Mesosfer provide realtime communication in web application. We call it web socket. It receive data from publisher in realtime. If you want to implement mesosfer web socket, insert this code below to your environment.

code

<html>
<head><title></title></head>
<body>
<script src="browserMqtt.js"></script>
<script type="text/javascript">
var client = mqtt.connect('tcp://api.mesosfer.com:3000', { username:'Your username', password:'Your password' });
client.on('connect', function () {
client.subscribe('Your topic')
});
client.on("message", function(topic, payload) {
alert([topic, payload].join(": "));
client.end();
});
</script>
</body>
</html>