Getting the sensor id

Hi,

I’m using the “multiple devices” part of this https://electricimp.com/docs/resources/onewire/ for my device code. I’m connecting 3 sensors and it’s working perfectly.
I now need to write the agent side and will need the readings for each sensor’s serial and temperature.
These details are in line 151:
server.log(format(“Device: %02d Family: %02x Serial: %02x%02x%02x%02x%02x%02x Temp: %3.2f”, (device + 1), slaveId[7], slaveId[1], slaveId[2], slaveId[3], slaveId[4], slaveId[5], slaveId[6], tempCelsius));

But I’m not sure how to pass the 6 readings (serial and tempCelsius for the 3 sensors) to the agent.
Would appreciate your help with how to code this please.

Thanks,
Perla

Create an array to hold the data to be sent to the agent (this would be at the start of your device code):

`data <- [];`

Later, loop through each of your three sensors in turn

`// Create a sensor entry as a table
local sensor = {};

// Add an 'ID' key to hold a device's serial number as a string
sensor.ID <- format("%02x%02x%02x%02x%02x%02x", slaveId[1], slaveId[2], slaveId[3], slaveId[4], slaveId[5], slaveId[6]);

// Add the temperature data
sensor.temperature <- tempCelsius;

// Add sensor to the data array
data.append(sensor);`

After the loop, send the data array to the agent:

agent.send("incoming.data", data);

In your agent code, you will need to register a function to handle the incoming data:

`device.on("incoming.data", function(info) {
    // 'info' is an array containing three tables
    foreach (sensor in info) {
        server.log("Sensor " + sensor.ID + " reports a temperature of: " + sensor.temperature + "C");
   }
}`

This code just logs the data received from the device, but you’ll see how it can be extended to tweet the data, relay it to a web service like Xively or hold on to it until a mobile phone app requests the current data.

Thanks smittytone for your help! it works perfectly…

I have another question regarding the same code please. So, I tested it indoors it works perfectly but outdoors with temperature that can go down to -20 the temperature returned for the sensors is around 4093! And i have one measuring the soil temperature, it’s almost fixed on 0.37! Is it something related to the hardware or can it be fixed with the code?

Thanks

Look at the datasheet for your sensor and see what the specifications are. Maybe you are at the lowest temp limit possible?

I live in Minnesota, and I quit working at that temperature too :frowning:

hahaha…
It should be –55°C to +125°C, this is why I thought to check the software side…

Check this out:

This is for Netduino but with similar concept, please check the comments part under
Brad BerklandMarch 23, 2013 at 1:57 PM

What do you think?

Thanks for your support.

The temp only goes bad below -20, or any negative number?

If you look at this file:

Notice Table 1.
When temps are negative, it applies 2’s complement.

So, when the temp outside is -20, it will be around 4000.

Here is a simpler example of what happens with negative numbers & 2’s complement

5 … 0000 0101
4 … 0000 0100
3 … 0000 0011
2 … 0000 0010
1 … 0000 0001
0 … 0000 0000
-1 … 1111 1111
-2 … 1111 1110
-3 … 1111 1101
-4 … 1111 1100
-5 … 1111 1011

What complicates your sensor is that is can have different resolutions 9,10,11 or 12 bits. So they make bits 11-16 sign bits.

If your hex to decimal value is less than 2048, your temp is positive.

If your hex to decimal value is 2048 or higher, you have a negative temp.

Bitwise, you mask it with FFFF using XOR (exclusive OR) and add 1.

0000 1010 0010 = +10.125C Decimal 162

1111 0101 1110 = -10.125C Decimal 3934

1111 0101 1110
1111 1111 1111 XOR with FFFF


0000 1010 0001

Now add 1

0000 1010 0010

See, it is now the same as +10.125

But you know it is negative because it was greater than 2048

Perfect!. Thanks