2

I am in a middle of an IOT project. I have to design the dashboard. I am facing difficulties in setting up the dashboard. I am using thingsboard.io I want to know how to send data from thingsboard server through a knob widget(numeric data) to arduino using esp8266 and mqtt and to send that data back to thingsboard gauge widget. Like I want to send and receive data. I have some issues in arduino codes. If anyone can help. That would be appreciated. Thanks

I have code in java script for doing the same task but that is working on command prompt. Can someone convert this code to C for arduino and esp8266.

var mqtt = require('mqtt');
const ACCESS_TOKEN = process.argv[2];

var client  = mqtt.connect('mqtt://demo.thingsboard.io',{
    username: ACCESS_TOKEN
});

var controlValue,
    realValue = 25;

client.on('connect', function () {
    console.log('connected');
    client.subscribe('v1/devices/me/rpc/request/+');
    console.log('Uploading temperature data once per second...');
    setInterval(publishTelemetry, 1000);
});

client.on('message', function (topic, message) {
    console.log('request.topic: ' + topic);
    console.log('request.body: ' + message.toString());
    var requestId = topic.slice('v1/devices/me/rpc/request/'.length),
    messageData = JSON.parse(message.toString());
    if (messageData.method === 'getValue') {
        if(controlValue === undefined) {
            client.publish('v1/devices/me/rpc/response/' + requestId, JSON.stringify(realValue));
        } else {
            client.publish('v1/devices/me/rpc/response/' + requestId, JSON.stringify(controlValue));
        }
    } else if (messageData.method === 'setValue') {
        controlValue = messageData.params;
        console.log('Going to set new control value: ' + controlValue);
    } else {    
        client.publish('v1/devices/me/rpc/response/' + requestId, message);
    }
});

function publishTelemetry() {
    emulateTemperatureChanging();
    client.publish('v1/devices/me/telemetry', JSON.stringify({temperature: realValue}));
}

function emulateTemperatureChanging() {
    if(controlValue !== undefined) {
        if(controlValue >= realValue) {
            realValue += (Math.random() + (Math.abs(controlValue - realValue)/30));
        } else {
            realValue -= (Math.random() + (Math.abs(controlValue - realValue)/30));
        }
    }
}
  • 1
    This sounds like the kind of situation where you should search online for an existing example that comes close to your needs, gain some experience using it as is, and then start modifying it to do what you personally need. – Chris Stratton Mar 22 '18 at 18:11
  • 1
    Adding the code makes the question better, but you're just asking for someone to port it to your new platform. This is not a free pool of engineering resource - If you want to buy coding time there are sited for that. What you can ask here is a specific question about part of your problem. If you have a purely software problem, StackOverflow would be more suitable. – Sean Houlihane Mar 23 '18 at 8:23
  • 1
    Yeah I just want to know how to connect with thingsboard for receiving data from knob on serial monitor. I think that would be more specific. Thanks – Ahmed Mar 26 '18 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.