I have started a home automation project, based on Raspberry Pi B3, ESP8266 Controller, Python and 1-wire devices. Unfortunately my experiences with the RasPi 1-wire interface have been disappointing. Slow and not very reliable. Sometimes sensors were not detected or sensor data changes haven't been recognized. After some internet searches I decided to replace the RasPi 1-wire interface with the DS2482 device, a I2C to 1-wire bridge. The device is connected to the host via I2C bus and has a "high level" programming interface. The other side of the device manages the 1-wire bus in detail, including the bus signal timing, without any host load. As I didn't find a Python library for that device, I finally came up with my own driver for the bridge.
|1wire test board
The 'DS2482rsf.py' driver needs four basic I2C commands provided by the host system. The RasPi-I2C interface and the smbus library as well as the PIGPIO library (that's the one I use) support the commands. The Micropython version was developed for the ESP8286 controller with Micropython firmware, however, the driver should be working on every platform that supports Python or Micropython and has an I2C interface. If the I2C command syntax is different, you only have to modify the I2C write and read calls.
The driver class includes subclasses to support specific 1-wire devices.
- class DS18B20
- 1-wire thermometer
- class DS24SN
- DS2401, DS2411 Silicon Number devices [usefull to get the on/off status of an electrical component]
- class DS2406
- Digital input/output device
How to use the DS2482 driver
Connect, install and run
In a nutshell you want to do the following
- Install the PIGPIO daemon.
- Connect a DS2482-100 or a DS2482-800 device to the Raspi via I2C bus. Don't forget the 5V (Raspi) to 3,3V (DS2482) level shifter.
- Connect 1-wire devices (DS18B20, DS2401, DS2411, DS2406) to the DS2482.
- Enable the RasPi I2C interface and check that the DS2482 is recognized.
- Download the DS2482 driver files.
- Get the 1-wire device IDs (see below).
- Modify the run file, depending on the 1-wire devices and their IDs that you have connected.
Due to a bug (reported by flor66, thanks) the Micropython code is unfortunately not available yet
If you prefer other I2C libraries instead of PIGPID e.g. RasPi-I2C, please replace the pi.i2c... calls in the listed functions by the appropriate calls of the other library.
- def I2C_write_byte
- def I2C_readbyte
- def i2c_connect
- def class_terminate
Getting the device ID
1-wire devices are addressed by their individual 64 bit ID. Getting the ID of a device is a driver function.
- Connect the device to the 1-wire bus and disconnect all other devices. The device in question should be the only device on the bus.
- Execute ds2482_run.py and input "y" when you are asked for a single device test. The 64 bit ID will be shown as 8 ROM bytes, each as hex and decimal number. Don't forget to take a note of the number. The example screenshot below shows error messages because there were more than one device connected to the bus.