April 15, 2021

Homekit, temperature with DS18B20

Updated 2021-04-15 !

A cheap and simple way to measure temperature, and have it displayed in the Home app, is to connect one wire (1W) DS18B20 (DS)temperature sensors direct to the Raspberry Pi(RPi) and install a DS plugin.


This assumes that You have Homebridge up and running on a Raspberry Pi 

Check this post !!  "ANY" temp source can be HomeKit enabled.

Basic setup
Enabling 1W
Start with enabling the 1W interface on the RPi, in a terminal session, editing the config file with 

$ sudo nano /boot/config.txt

add 

dtoverlay=w1-gpio

at the end of the file. (If it is not already there)

Shutdown the RPi with 


$ sudo shutdown -h now

Connect the DS
Disconnect the power and connect, to the header pins(HP), one or several DS according to the picture. 

Red     HP 1, +3,3 V
Yellow  HP 7, Data
Black   HP 9, Ground

RPi 1-Wire pinout is discussed here

Check the DS
Connect the power and start a terminal session with command

$ ls  /sys/bus/w1/devices/
28-051693ec41ff  w1_bus_master1

One or several "28-" folders will show and this corresponds to the connected DS id. 
In every "28-" folder there will be a file w1-slave which holds the actual temperature.

So checking the connected DS:s temperature, type

cat /sys/bus/w1/devices/28-051693ec41ff/w1_slave
7f 01 4b 46 7f ff 0c 10 ba : crc=ba YES
7f 01 4b 46 7f ff 0c 10 ba t=23937

the last row holds the value "23937" which should be divided with 1000, so the temperature is 23,937 degrees Celsius and the installation is OK.

Alternative connection
In my case I have a Razberry Z-wave-controller installed on the RPi and this board covers the header pins 1-16. Therefore we move the default 1W input from GPIO 4, HP 7, to GPIO 24, HP 18, by changing the 1W configuration in /boot/config.txt to 


dtoverlay=w1-gpio,gpiopin=24

So instead connect the DS cables to

Red     HP 17, +3,3 V
Yellow  HP 18, Data
Black   HP 20, Ground

If You want more than one 1W input, for example also using GPIO 25, just change the config to 

dtoverlay=w1-gpio,gpiopin=24,gpiopin=25

Homebridge plugin installation
The plugin is installed
 via the UX interface or manually with

$ sudo npm install -g homebridge-ds18b20

and then You have to edit the homebridge configuration file .hombridge/config.json  adding the DS id as an accessory.

So with the DS id

28-051693ec41ff 

add the information

"accessories": [
 {
  "accessory": "DS18B20",
  "name": "Temperature Sensor",
  "device": "28-051693ec41ff"
 }
],

The name "Temperature Sensor" will appear in the Home app so change it to something useful and then do a restart  with

$ sudo reboot

If You done the all things right the temperature will show in the Home app.

The link to the Telldus and Z-Wave setup and below 
my complete config.json 

{
  "bridge": {
   "name": "Homebridge",
   "username": "CC:22:3D:E3:CE:30",
   "port": 51826,
   "pin": "031-45-154"
  },
  "description": "Z-Wave and Telldus Live plugin",
  "accessories": [
   {
    "accessory": "DS18B20",
    "name": "Temperature Sensor",
    "device": "28-051693ec41ff"
   }
   ],
   "platforms": [
    {
     "platform": "ZWayServer",
     "url": "http://localhost:8083/",
     "login": "admin",
     "password": "admin"
    },
    {
     "platform": "Telldus",
     "name": "Telldus Liv!e",
     "public_key": "FExxxxxxxUNU",
     "private_key": "ZUXxxxxxxxxxx6EF",
     "token": "88xxxxxxxxx0b7d",
     "token_secret": "a57xxxxxxxxxxxd5f"
    }
   ]
 }

And a link to update the Homebridge installation.

HB autostart at power on and failure, installed with a script !! , is found here.

Other HomeKit posts on this Blogg

22 comments:

  1. Superb! Please ignore my other comment, I can see the answers here. Thanks again for your time writing this up!

    ReplyDelete
  2. Hello, thx for this, it works. but is it possible to set an offset? my ds1820 shows 4 degree to low.

    ReplyDelete
    Replies
    1. Sorry to say, No. Strange You have to set an offset, because according to the spec it is "±0.5°C Accuracy from -10°C to +85°C". A Chinese "wannabe" ? ;-)

      Delete
    2. yea, ok....i will get another one......

      Delete
  3. Thank You,
    I'v got in my HomeApp the sensor, no temperature,
    but Homebridge Logs says:
    [26.12.2021 klo 0.35.09] [homebridge-temperature-ds18b20] This plugin threw an error from the characteristic 'Current Temperature': Unhandled error thrown inside read handler for characteristic: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined. See https://git.io/JtMGR for more info.

    Any suggestions
    Bw Ari

    ReplyDelete
    Replies
    1. Hi ! What response did you get from the "cat" command ?

      Delete
  4. Hi, I'm such a newbie... What do You mean by "cat"?

    ReplyDelete
    Replies
    1. No problem. I'm referring to the part above, "Check the DS"

      Delete
  5. Offcourse!
    pi@raspberrypi:~ $ cat /sys/bus/w1/devices/10-0008022996ff/w1_slave
    2b 00 4b 46 ff ff 05 10 e4 : crc=e4 YES
    2b 00 4b 46 ff ff 05 10 e4 t=21437

    I.e. working

    ReplyDelete
    Replies
    1. A little confused...... according to my knowledge the DS18B20 should have a file prefix of "28". Your device have "10" as the prefix and checking the documentation this is a DS18S20/DS1920?

      Delete
  6. Oh, these are rather old ones. I didn't know these are 'S'-versions.
    Is there some fatal difference between them? Both seems to give 9bytes.
    But they are working (two pieces now) in RPi.
    Got also HOOBS running, but it reports just the same errors.
    An error (or two) comes when pointing Temperature in HomeApp.
    HOOBS dashboard says 0oC.

    Maybe must go shopping 18B20.
    Ari

    Maxim explains:
    https://www.maximintegrated.com/en/design/technical-documents/app-notes/4/4377.html

    ReplyDelete
  7. Yes, you better shop the right ones.

    ReplyDelete
  8. Hi!
    I'v just got new ones, but the error continues...

    Is there something wrong in Homebridge config?:
    {
    "bridge": {
    "name": "Homebridge 1EDD",
    "username": "0E:ED:96:6E:1E:DD",
    "port": 51620,
    "pin": "884-02-145"
    },
    "accessories": [
    {
    "accessory": "DS18B20TemperatureSensor",
    "name": "Koesensori",
    "device": "28-6e6bbc0164ff"
    }
    ],
    "platforms": [
    {
    "name": "Config",
    "port": 8581,
    "platform": "config"
    }
    ]
    }

    Yours Ari

    ReplyDelete
    Replies
    1. Looks OK, is the fault reporting exactly the same as before?

      Delete
    2. It is,
      and in HOOBS also...
      Which "path" argument is not a string and where?

      Delete
    3. I would try with; 1/Delete the config for the plugin 2/Delete the plugin 3/Reinstall the plugin. 4/If this don't work I would create an issue at the plugins Github page.

      Delete
  9. Thank You for your time, I will do that!
    Ari

    ReplyDelete
  10. Hey there, I'm getting the error "not plugin was found for the accessory "DS18B20" in your config.json". What plugins do I need to follow your code just for the temp sensor? I only installed homebridge + httptemperature. Homebridge listes the plugin as installed, and I have this as the config:
    {
    "accessory": "HttpTemperature",
    "name": "Temp outside1",
    "url": "http://home:1880/temp1",
    "http_method": "GET",
    "field_name": "Temperature"
    }
    and this is in my homebridge config:
    {
    "accessory": "DS18B20",
    "name": "Temperature Sensor",
    "device": "28-051693ec41ff"
    }
    + the accessory above (it gets automatically added). Thanks for any help!

    ReplyDelete
    Replies
    1. Seems to me you not have installed the right plugin "homebridge-ds18b20" ? The "http Temp" is not used for ds18b20.

      Delete
  11. also, the device id is the id that i retrieved from the command line, not copied from your device id

    ReplyDelete

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !