Including Device ID with every measurement


Hi Everyone

Sorry for such a newbie question…I am trying to add the Device ID to every sensor measurement so that I can parse different devices in the Azure Cloud. There is probably a very basic answer to this, but I am not a programmer by trade and have been having issues wrapping my head around the topic.

I am using 2 Sensor Nodes to run a basic PoC on electricIMP and Azure IoT. The code I am using is the Sensor Node example code which is doing everything I need it to for the basic measurement statistics. I can see how the sensor data is stored in an array and how a time stamp (“ts”) is added to every measurement in the array before sending to the agent. I am struggling with how to add the Device ID along with the ts to identify which sensor is providing which measurements.



Actually, even easier than that - you can read it on the agent without needing to ask the device, which minimizes data traffic. @Gerrikoio’s suggestion will also work though.



…on the agent will show you the same thing.


Thanks for the feedback. What I am after is the way I would need to add the deviceID to an existing collection before sending from the device to the agent for loading.

Below is the code example I have. I am trying to add the deviceID information to the data collection by including the device ID with each reading. I know that the hardware.getdeviceid(); works in the instance as I have it running in another set of code. But I am having a tougher time figuring out how include it with the readings collected over intervals.

function _parseReadings(readings) {
// add time stamp to reading
local data = {“ts” : time()};
// add device ID to reading
local deviceID <- hardware.getdeviceid();
// log error or store value of reading
foreach(reading in readings) {
if (“err” in reading) {
} else if (“error” in reading) {
} else {
foreach(sensor, value in reading) {
data[sensor] <- value;
deviceID[sensor] <- value;
return data;


Maybe telling you what you already know… but first things first. Start by understanding the imp architecture. Communication between agent and device is one-to-one, so not much point sending the same data, such as device id, with each sensor reading update. Suggest you send device id to agent when device boots up and then agent stores this data and uses when it wants to send data to Azure Cloud as the Azure Cloud needs to know where data came from.

From device to agent just use the agent.send method to send data to the agent. Data is formatted as a json object. In your case you can simply send the sensor data array to the agent and then let the agent do the grunt work. So in device code you could use something like:

agent.send(“myData”, {“ts”:time(), “data”:data[sensor]});

Then in agent code you would use, what’s called an “event handler” to monitor when device sends data. In this case you would have something like:

device.on(“myData”, myData_CallbackHandler);

You then create a callback function in your agent code for this purpose:

function myData_CallbackHandler(DevData) {
// DevData now contains timestamp as DevData.ts and array as
// Here you can add in a UUID / device id etc.
// Here you can parse / create an identity for each sensor reading if you do not want to send the sensor array data directly to Azure. Here too you would send as JSON.
// you can then use the Azure library functions to send your data to the Azure cloud


Posting the entire code would help too; it seems like you’ve got multiple arrays, indexed by “sensor”, which store readings?

That can be a valid way to do things, but not knowing what you’re trying to achieve, I don’t know (eg an array of tuples is often easier to handle, but can take more RAM on the device and in transit).


Thanks to both of you for replying. I got further assistance from the electricImp team and got the devices operational!