Hi guys!! I have been months working on a proyect of sending 64 bytes of analog values with a sensor… my goal is to see the graphic on ubidots with the decimal values…
The problem is: I am sending correctly the bytes with arduino to tts, but i dont know how to send that data to ubidots with my payload formatter (apparently im sending only 0 in all the values). Im sending u the screenshots.
Please help me cause i have been on this point for weeks and i feel that im stucked. I think the problem is on the line: values[i]=bytes[i]<<8;
Thanks for the help!!!
Oh, yes u are right. Sorry im new in TTS
This is the code that i have on my java payload formatter:
function decodeUplink(bytes,port) {
var ubiListOfValues = [];
var ubiPayload = {}
var values = [];
for (let i = 0; i < 64; i++)
{
values [i] = (bytes[i]<<8)
}
var initDate = new Date();
var initTs = initDate.getTime();
values.forEach((currentValue) => {
ubiListOfValues.push({"value": currentValue, "timestamp": initTs})
initTs += 100; // Add 100 ms to the initial timestamp.
});
ubiPayload.PulsoCardiaco = ubiListOfValues; // You need to replace "variableLabel"
return {
data: ubiPayload
};
}
I am receiving on the console in tts the numbers like this B7 B6 A7… (64 bytes)
And my decoder in ubidots is this one:
function format_payload(args){
var ubidots_payload = {};
// Log received data for debugging purposes:
console.log(JSON.stringify(args));
// Get Fcnt and Port variables:
ubidots_payload['f_cnt'] = args['uplink_message']['f_cnt'];
ubidots_payload['f_port'] = args['uplink_message']['f_port'];
// Get uplink's timestamp
ubidots_payload['timestamp'] = new Date(args['uplink_message']['received_at']).getTime();
// If you're already decoding in TTS using payload formatters,
// then uncomment the following line to use "uplink_message.decoded_payload".
// PROTIP: Make sure the incoming decoded payload is an Ubidots-compatible JSON (See https://ubidots.com/docs/hw/#sending-data)
var decoded_payload = args['uplink_message']['decoded_payload'];
Object.assign(ubidots_payload, decoded_payload);
return ubidots_payload
}
module.exports = { format_payload };
I am receiving a list of zeros on my ubidots dashboard…
If you have 64 bytes of data, why are you testing with 3?
Please look again at your post, consider everything you are assuming to be wrong and put yourself in the place of anyone answering to ensure you have given ALL the information that they may possibly need to help you, for free.
This thread will self-destruct at post 10. Ensure you use your lives wisely.
By taking a look at your payload formatter, I get the feeling that you are returning
data:UbiPayload
In order to match the Ubidots compatible JSON format, however that is not needed if you modify the payload formatter in the following way (which, by the way, was already tested)
function decodeUplink(bytes,port) {
var ubiListOfValues = [];
var ubiPayload = {}
var initDate = new Date();
var initTs = initDate.getTime();
bytes.forEach((currentValue) => {
ubiListOfValues.push({"value": currentValue, "timestamp": initTs})
initTs += 100; // Add 100 ms to the initial timestamp.
});
ubiPayload.PulsoCardiaco = ubiListOfValues; // You need to replace "variableLabel"
return {
ubiPayload
};
}
About the Ubidots decoder, this is the one for your purpose:
function format_payload(args){
var dotsList = args["uplink_message"]["decoded_payload"]["PulsoCardiaco"];
var ubidotsPayloadList = [];
dotsList.forEach(msg=> {
ubidotsPayloadList.push({"pulso-cardiaco": msg["value"], "timestamp": msg["timestamp"]});
})
return ubidotsPayloadList;
}
module.exports = { format_payload };
This will return ubidotsPayloadList which is a list with all the single dots containing both the timestamp and the “pulso-cardiaco” value. By doing so, your device will be updated with all those dots.
Feel free to reach back to us if you have any other question/doubt that keeps your IoT project from going further.
First of all, thank u very much for ur help, I really apreciate it:D
I am trying ur code, but I have this decode uplink message failure:
“TypeError: Object has no member ‘forEach’ at decodeUplink (:8:14(15))”
The problem is on the line bytes.forEach((currentValue) =>{
I am posting the screenshots of the message failure…
I’m sorry about that, I must have mixed the variables names in my head while pasting the code here.
Please replace:
bytes.forEach((currentValue) => {
for:
ubiListOfValues.forEach((currentValue) => {
At last, It should be like this:
function decodeUplink(bytes,port) {
var ubiListOfValues = [];
var ubiPayload = {}
var initDate = new Date();
var initTs = initDate.getTime();
ubiListOfValues.forEach((currentValue) => {
ubiListOfValues.push({"value": currentValue, "timestamp": initTs})
initTs += 100; // Add 100 ms to the initial timestamp.
});
ubiPayload.PulsoCardiaco = ubiListOfValues; // You need to replace "variableLabel"
return {
ubiPayload
};
}
Let’s debug this in depth. Please, follow these steps:
1 - Go to your TTS plugin on Ubidots and click the edit button, which looks like a tiny pencil:
2 - Go to the Decoder tab and add the following line of code as the first one to the Ubidots decoder function:
console.log(args);
With this in mind and the previous comments, the Ubidots decoder should look like this:
function format_payload(args){
//newly added line
console.log(args);
var dotsList = args["uplink_message"]["decoded_payload"]["PulsoCardiaco"];
var ubidotsPayloadList = [];
dotsList.forEach(msg=> {
ubidotsPayloadList.push({"pulso-cardiaco": msg["value"], "timestamp": msg["timestamp"]});
})
return ubidotsPayloadList;
}
module.exports = { format_payload };
This is just so we print the arguments every time the functions gets called and hopefully, that’ll help us have a better perspective.
3 - Go to the Logs tab.
4 - Click any of the “Message Logs” entries. The console will be displayed.
5 - Take a screenshot of the “Results” and please post it here. It would be helpful to paste here the text also.
6 - In the “Logs” section, please copy and paste here all the text (take into account that you would have to scroll to the right in order to see the full text)
This is not a fix to the current problem, however it’ll help us to conceive said fix.
I hope u are doing great, thank u a lot for ur help. I didnt see ur message and im grateful with u.
I have solved the problem, was the stupidest thing on the world. This is the right code:
function decodeUplink(input) {
// input has the following structure:
bytes = [] // FRMPayload (byte array)
//fPort: 1;
var ubiListOfValues = [];
var ubiPayload = {}
var values = [];
for (let i = 0; i < 64; i++)
{
values[i] = input.bytes[i]; //this line was the problem!!!!
};
}
var initDate = new Date();
var initTs = initDate.getTime();
values.forEach((currentValue) => {
ubiListOfValues.push({"value": currentValue, "timestamp": initTs})
initTs += 100; // Add 100 ms to the initial timestamp.
});
ubiPayload.PulsoCardiaco = ubiListOfValues; // You need to replace "variableLabel"
return {
data: ubiPayload
};
}
VERSION 2
function decodeUplink(input) {
// input has the following structure:
bytes = [] // FRMPayload (byte array)
//fPort: 1;
var ubiListOfValues = [];
var ubiPayload = {}
var values = [];
for (let i = 0; i < 64; i++)
{
values[i] = input.bytes[i];
}
var initDate = new Date();
var initTs = initDate.getTime();
values.forEach((currentValue) => {
ubiListOfValues.push({"value": currentValue, "timestamp": initTs})
initTs += 100; // Add 100 ms to the initial timestamp.
});
ubiPayload.PulsoCardiaco = ubiListOfValues; // You need to replace "variableLabel"
return {
data: ubiPayload
};
}
Finally I can continue my project!!
Thanks a lot Juan David, u are very kind!!