IOT - Bosch
  • Introduction
  • Devices
    • Bosch XDK 110
      • Introduction
      • Operating System
      • Hardware
        • Sensors
          • Accelerometer
            • C
          • Gyroscope
            • C
            • Mita
          • Magnetometer
            • C
          • Environmental
            • C
            • Mita
          • Ambient Light
            • C
            • Mita
          • Acoustic
            • C
      • Software
        • XDK WorkSpace
          • Structure
          • Debug
          • Supported languages
            • C
              • Static Library (.a)
            • XDK Live - Mita
            • MicroFlo
      • Connectivity
        • Bluetooth Low Energy (BLE)
          • Overview
          • General Info
          • Implementation
            • C
            • XDK Live
        • WI-FI
          • OverView
          • Implementation
            • C
            • XDK Live
        • WI-FI Enterprise
      • Protocols
        • CoAP
          • Overview
          • Implementation -TBD
        • HTTP
          • Overview
          • Structure and Methods
          • Implementation
            • C - Rest
            • C - Post
            • XDK Live
        • HTTPS
          • Overview
          • Implementation TBD
        • LWM2M
          • Overview
          • Implementation TBD
        • MQTT
          • Overview
          • Implementation
            • C
            • XDK Live
        • USB
          • Overview
          • Implementation TBD
      • Data Storage
      • XDK Extension Bus
      • Community
      • Applications
        • Language C
          • HomeAssitant - MQTT
            • Prerequisites
            • Server
            • Device
          • IOTA-MQTT-XDK
            • Prerequisites
            • XDK110
            • Mqtt JSON to MAM
            • SensorHub
            • Demo
        • Language XDK Live
          • MQTT
            • Hello World
            • HomeAssistant
              • Prerequisites
              • Server
              • Device
            • Docker-HomeAssistant
          • HTTP
            • Roku Remote Control
              • Roku API
              • MITA
                • Example
    • Bosch AMRA
    • Bosch GLM 100C
    • Bosch FLEXIDOME IP panoramic
    • Bosch GLM 100C
    • Bosch Rexroth Nexo
    • Bosch Rexroth PRC 7000
    • Bosch RRC / CT100
    • Bosch Rexroth IoT Gateway
  • Bosch IOT Solutions
    • Bosch IOT Projects
      • Smart Home
      • Industry 4.0
      • Smart Cities
      • Connected-mobility
    • Bosch IOT Suite
      • Bosch Analytics
      • Bosch IOT Hub
      • Bosch Iot Permission
      • IoT Remote Manager
      • IoT Rollouts
      • IoT Things
      • Demo TBD **
    • BPM and BRM
  • IOTA
    • Introduction
      • Tangle
      • Glossary
      • Differences with other tech
      • How does iota work
      • Developers
    • Qubic
      • What is Qubic?
      • Target
      • Qubic Protocol
    • Ecosystem
    • Applications
      • Python
      • XDK110
        • Prerequisites
        • XDK110
        • Mqtt JSON to MAM
        • SensorHub
    • Bosch/IOTA
  • ByteBall
    • SmartContract
    • Use Case
      • BoshCoins
Powered by GitBook
On this page
  • Installation
  • Explanation

Was this helpful?

  1. IOTA
  2. Applications
  3. XDK110

Mqtt JSON to MAM

PreviousXDK110NextSensorHub

Last updated 5 years ago

Was this helpful?

This module would help us to read the data that is comming form MQTT parse to a Json and then send it to the Tangle as MAM.

Installation

To start usring first we ned to give the next command

$ cd XDK110-Bosch/C/IOTA-XDK110-MAM/IOTAMAM/
$ npm install

Explanation

We need to modify the next file so we can connect to mqtt.

The file is named

$ nano index.js

Let understand this code. See the comments on the code.

//We define that this program is going to execute in a strict mode
'use strict'

 //Now we need to bring some libraries to work with MAM, we need to use public, private or restricted. Then bring the iota.lib.js
 //this one we previously download it.
const MAM_public = require('./lib/attachDataPublic.js')
const MAM_private = require('./lib/attachDataPrivate.js')
const MAM_restricted = require('./lib/attachDataRestricted.js')
const IOTA = require('iota.lib.js')
//We need to define 2 objets one a IOTA and a mqtt
const iota = new IOTA()
const mqtt = require ('mqtt');

//We need to define the mqtt connection
//We user:password@mqttbroker:port
var client  = mqtt.connect('mqtt://xdk110:xdk110@broker.hivemq.com:1883');
var jsonData = null;

//This fuction would parse the string to json
function getmyjson(myjson){
  jsonData = JSON.parse(myjson);
//  console.log(jsonData);
}

//connect and subscribe to topic
client.on('connect', function () {
  client.subscribe('XDK110/sensor/status');

//  console.log('client has subscribed successfully');
});


// get data when we receive something from the subscribe
client.on('message', function (topic, message){
  //console.log('Received message %s %s', topic, message)
  getmyjson(message);
});

let timeLoop,date,
    i=1

if( process.argv[2] == undefined){          //Getting the time in seconds for the loop
  timeLoop = 60000                       //default 1 minute
} else {
  timeLoop = process.argv[2]*1000
}


//Create a JSON as message and parse to MAM

function start(){
    const time = Date.now();
        let message = { 'Message' : i,
            'id' : jsonData.Ident,
            'location' : {'lat' : 20.674605, 'lng' : -103.393228},
            'timestamp' : time,
            'humidity' :    jsonData.Humidity,
            'pressure' :    jsonData.Pressure,
            'temperature' : jsonData.Temperature,
            'light' : jsonData.Light,
            'noise' : jsonData.Noise,};
    switch(process.argv[3]){                                //Getting the mode of the stream (Public:1, Private:2, Restricted: 3)
        case '1': MAM_public.attach(message);break;
        case '2': MAM_private.attach(message);break;
        case '3': MAM_restricted.attach(message);break;
        default: MAM_public.attach(message)
    }
    console.log('Start sending data to Tangle...')
    let messageS = JSON.stringify(message)
    console.log('Message: %s',messageS)
    console.log('Message in trytes: ' + iota.utils.toTrytes(messageS))
    console.log('--------------------------------------------------------------------------------------------------------------------')
    i++
}

setInterval(function(){
    start()
},timeLoop)
index.js