Wednesday, 4 March 2015

Raspberry PI, Openelec and 1-wire temperature sensing

I've finally got back to writing up some blogs, so there are a few things I'm currently trying to achieve, and as always the solution to a problem involves Arduino, Raspberry PI and electronics!

This particular problem is my home heating system. After being in the new house for a year we're still struggling to have the heating system actually heat the house to a comfortable amount when it needs it. The built-in heating system uses a temperature sensor (Looks like a DS18B20) in the hallway (beside front door and a radiator), and most other rooms have radiator thermostats. The problem is that the hallway never really matches the temperature in the rest of the house, bedrooms are often cold and other rooms roasting hot. Therefore I had a plan to have temperature sensors around the house and provide an 'overview' on/off to the heating system, in theory when rooms are still too cold, forcing the heating to stay on should let these rooms warm up and rooms that are already up to temperature will remain there because of the radiator thermostat. So to do this I need a way of sensing temperatures.

I already have Raspberry PI's in a few of the rooms, sat behind TV's to provide TV/media playback, so I looked into using the 1-wire temperature sensor DS18B20 on the PI. Now luckily there is a nice 1-wire kernel module available so it's pretty easy, modprobe the 1-wire module, modprobe the 1-wire temperature module and you can then pull sensor values using a script. Pretty easy, and yep testing this it works great.
The commands used were simply:
modprobe w1-gpio
modprobe w1-therm
Which then allowed me to grab sensor values by prodding about in the '/sys/bus/w1/devices'

However, I use Openelec, not a standard Raspbian release. Now Openelec is great, it's a cut-down system that just works, it boots quickly and runs the hardware nicely. But because it is cut-down for speed a lot of modules, etc, have been removed, and in this case the 1-wire modules have been removed.
So I needed to get a solution to provide the modules in Openelec. To do this I needed to recompile it, which I didn't find the solution to very easily, so I've documented it here not just for myself but in case anyone else wants to try this. First you need the Openelec build environment, so clone their github repo:
git clone https://github.com/OpenELEC/OpenELEC.tv.git

You then do the initial build by running:
PROJECT=RPi ARCH=arm make release
And leave it (This took my server about 5 hours to complete). That gets you the same release that you're probably running now from the downloaded Openelec/official release. Now to custom the kernel, so you do:
PROJECT=RPi ARCH=arm ./scripts/unpack linux
Which then allows you to edit the kernel parameters like normal, so then running:
cd build.OpenELEC-RPi.arm-devel/linux-3.19.0
Followed by:
make menuconfig
Gets you to change kernel parameters. The changes I made were fairly simple, just:
CONFIG_W1=m
CONFIG_W1_MASTER_DS2490=m
CONFIG_W1_MASTER_DS2482=m
CONFIG_W1_MASTER_DS1WM=m
CONFIG_W1_MASTER_GPIO=m
CONFIG_W1_SLAVE_THERM=m
Adding those you can then recompile the kernel again like this:
make menuconfig
cp .config ../../projects/RPi/linux/linux.arm.confPROJECT=RPi ARCH=arm make release

And you're done. Copy the produced KERNEL and SYSTEM files (in the target folder) over to your PI in the /flash partition and boot up, should be all working at that point.

You can then connect the 1-wire sensors up and start to pull values. I'll add that to the next blog entry on how to connect the hardware and then start to retrieve the values.

(Edited: 22/05/2015 thanks to Chris Van Marle pointing out some stupid errors of mine!)

9 comments:

  1. Hi Andy,

    I'm also interested in monitoring temperature around my RasPi's running OpenELEC and I've found your blog, forum post and GitHub feature request.

    This blog seems to contain the most detailed information, but is still a bit confusing and I keep running into issues. Could you maybe explain a bit more in detail the steps of recompiling the kernel and obtaining the modules?

    What confuses me is that you're talking about the Pi (which uses PROJECT=RPi ARCH=arm) and uses the directory build.OpenELEC-RPi.arm-devel.

    Next you're using PROJECT=Generic ARCH=x86_64 in the unpack, which would unpack the linux package in build.OpenELEC-Generic.x86_64-devel/.

    Then you suddenly cd into build.OpenELEC-Generic.i386-devel/, another architecture.

    When I use make menuconfig, I don't get to enable the changes mentioned. I've tried adding these in .config myself and then running make again, but that didn't work.

    Looking forward to your suggestions.

    ReplyDelete
    Replies
    1. I think I've got it. At least the modprobe works now :-)

      Here's what I did, essentially, note that I've used the 5.0.8 branch and not master (devel):
      PROJECT=RPi ARCH=arm make release
      PROJECT=RPi ARCH=arm ./scripts/clean linux
      cd build.OpenELEC-RPi.arm-5.0.8/linux-3.17.8/
      make menuconfig
      cp .config ../../projects/RPi/linux/linux.arm.conf
      PROJECT=RPi ARCH=arm make release

      Delete
    2. Hi Chris,

      Many thanks, I'll fix the blog posts, yep I was doing a few builds at the time and was swapping between by i386 and RPi builds. Oops!

      Thanks for providing the update and the changes to get it working for you.

      Delete
  2. Hi Andy,
    Do we have to recompile everything again when a new version of Openelec is released?
    Is there any way to compile the modules and call them independent to Openelec? I still remember that that's possible on Linux systems?
    Thanks

    ReplyDelete
    Replies
    1. Hi there,
      Using the method above, yes you would need to pull the latest down from their github repo and then recompile and update it all again, as you're using a statically compiled main program binary.

      However, as per the update above, you'll see openelec have now added this to the main repo, so you don't need to do this yourself, just use the download edition of Openelec and the modules are in there ready.

      Delete
    2. Sorry, my mistake, check my follow-up post at:
      http://andyb2000.thebmwz3.co.uk/2015/05/w1-gpio-w1-therm-in-openelec.html

      Which states that the modules are now compiled into the mainline release of Openelec

      Delete
    3. Thanks Andy! It's really a good news!

      Delete
    4. Could you guys explain more how is it better to do now? Should we compile all at a new Update or just the Modules?

      Delete
    5. Hi Fatih,
      You don't need to compile now which is the great news. Just download Openelec from their website and the modules are already there for you to simply start using. Take a look at:
      http://andyb2000.thebmwz3.co.uk/2015/05/w1-gpio-w1-therm-in-openelec.html

      Delete