1-wire sensor order

I have no idea how 1-wire works, but just looking at the sketch so I can modify it for multiple sensors (and make it as generic as possible), it occurred to me that the order the sensors are discovered may be important.

So firstly, how are the 1-wire device addresses discovered?  Is it first come first served, or is there a specific order?

The reason for asking is that if the order they are discovered and added to the address array can vary between power cycles, the input feeds will be from different devices after a power cycle.

If that is true, the only way to ensure consistency is to hard code the addresses.

Can anyone tell me how the 1-wire devices are discovered?

Schism's picture

Re: 1-wire sensor order

The order is deterministic, based on the unique address of each sensor.

I recommend you discover your addresses and use them directly, because this is the only way to prevent inputs switching over.

Say your system develops a dodgy wire - with address based sketches you'll see the actual sensor dropping off. If you use a sketch that just cycles through the sensors on the bus, although they'll always be in the same order, if one is intermittent it will shuffle all your subsequent inputs around.

borpin123's picture

Re: 1-wire sensor order

@schism "The order is deterministic, based on the unique address of each sensor." if it is based on the unique address then it must be ordered in some way based on the address.  If it is deterministic then it would be based on which one was found first (which is what I suspected and what the rest of your answer implies).

Had not thought of the case of a sensor dropping out in any case, so it looks like I will have to hard code.  Shame you cannot send setup data to the EmonTx.

Schism's picture

Re: 1-wire sensor order

Correct - you will always get the sensors returned in the same order, for any given set of sensors -  a new one might appear first, last, or in the middle of the list depending on its address.

You'll see there are sensor discovery sketches in github, which make it easy to find out your addresses (I then keep a log of these in a spreadsheet as I deploy them, to save the bother in case I need to move a sensor to a new node, for some reason...

Robert Wall's picture

Re: 1-wire sensor order

I was looking at the Data Sheet to try to find the answer - there's a hint there that the sensor replies after a random time. I wondered whether this in fact determines the order they respond, i.e. the first to respond gets the first slot in the address array, etc, but there is no definitive statement. As you say, the only sure way is to get the individual address and use that. 

Schism's picture

Re: 1-wire sensor order

I think the library we're using guarantees the ordering (rather than some property of the sensors themselves). I haven't looked, but assume it waits a certain period of time for sensors to call in, orders the array based on the address, and goes from there...

borpin123's picture

Re: 1-wire sensor order

Ok, I think you are right, the sensors always seem to respond in the same order.

I now have 8 temperature sensors up and running.  I actually cannot see why this could not be put in by default as they would just return 0 where there is no value/sensor.  I have it set so the strut has space for 9 although only 8 are fitted/discovered.

I used a constant for the max number of sensors and so only one thing needs to be changed in the sketch to vary the max number of sensors it will report.

Robert Wall's picture

Re: 1-wire sensor order

The library issues the "Search ROM [F0h]" command, and for that the data sheet refers to the "iButton® Book of Standards at www.maxim-ic.com/ibuttonbook."

cybergibbons's picture

Re: 1-wire sensor order

The order is due to the search algorithm that 1-Wire uses. Essentially it says "Everyone show me your first bit". Everything on the bus tries at the same time. The bus will return a logical AND of the values returned - if you get a 1, everything has a 1. If you get a 0, it could be everything is 0 or a mixture. 

So then the master asks for the complement of the first bit. If you had a 1 the first time, and now a 0, you know all devices have 1 in this position. If you had a 0 the first time, and now a 1, you know all devices have a 0 in this position. If you get 0 and then 0, you know there is a mixture. (1 and 1 is an error!).

So if you know there is a mixture of 1s and 0s, you the master then says "For the next bit, only devices with a 1 as their MSB respond". This then means that you know that the devices from this point in the search all have 1 as their MSB. You keep on going down this tree until you have the entire address of the first device.

To get the next address, you do the same search again, but take a different branch.

The implementation of this is in software so the order from one implementation to another could differ. 

All details in this app note:

http://www.maximintegrated.com/app-notes/index.mvp/id/187

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.