Brewing Beer With a Raspberry Pi: Measuring Temperature
When it comes to freezing beer during the brewing process, a Raspberry Pi and Windows 10 IoT Core provide you with what you need to watch the temperature.
Join the DZone community and get the full member experience.
Join For Freei have raspberry pi 2 with windows 10 iot core, and i plan to use it for some brewing activities. in this blog post, i introduce how to measure temperature with a raspberry pi using ds18b20 thermal sensors. this post is also an example of how easy it is to get started with your iot stuff using microsoft tooling.
in practice, i use this solution to monitor the freezing of eisbock beer. making 20l of beer freeze takes time. depending on the weather outside, it’s 10-18 hours. using this solution, i don’t have to open the bucket after every couple of hours to see if ice is forming, and i can also estimate how long it takes for beer to freeze.
update : the source code of my temperaturestation solution is available at github . the solution contains the iot background service and a web application you can use right away. readings are reported to mssql or azure iot hub. documentation is available at this temperaturestation wiki . feel free to try out the solution.
electronics
this solution is pretty easy if you know at least something about electronics. i used the blog post thermal sensor connected via i2c by tomáš greňo for inspiration. it describes all this stuff in great detail, and if you are using linux on your raspberry pi, then you can find everything you need to read temperatures there.
raspberrypi 2 and two thermal sensors solution to control the freezing of beer.
the solution is also ready for my conference trips, and it survives travel.
some advice:
- all these components are cheap, and i found them from local online electronic stores.
- ds2482s-100 and ms-dip-to-so-8 reduction are so small that you better use the right tools for or ask for help from a friend who has the tools and who knows how to use them.
- if you plan to connect more sensors to the ds2482s-100, make sure the ds2482s-100 chip you buy supports the number of sensors you have.
- the colors of the ds18b20 wires are described in the component documentation.
- when connecting your solution to your pi, make sure you connect the wires to gpio pins correctly.
- if possible, pack your sensor well so there’s not much chance you accidentally break it.
from this point on, i assume you have your sensor solution done and connected to the raspberry pi. also, i assume you have a development box running on windows 10 and you also have visual studio with universal windows applications and windows 10 iot templates installed.
reading temperatures
run visual studio and create a new iot background application project.
when the project is created, open the nuget package manager and add the rinsen.onewire package to your solution. rinsen.onewire is the implementation of the onewire protocol by fredrik rinsén . it’s written in c# and has built-in support for ds18b20 and ds18s20 sensors.
now let’s write some code. in short, we have to write something like this:
- when the background application is run, initialize everything to read sensors.
- add the code for the application shutdown so we politely leave the application.
- use a timer to read values from sensors.
- write values somewhere (the debug window in our case).
here’s the sample code for the startuptask class that visual studio creates automatically.
public sealed class startuptask : ibackgroundtask
{
private timer _timer;
private onewiredevicehandler _handler;
private ienumerable<ds18b20> _devices;
private bool _isclosing = false;
public void run(ibackgroundtaskinstance taskinstance)
{
taskinstance.canceled += taskinstance_canceled;
_handler = new onewiredevicehandler(false, false);
_devices = _handler.onewiredevices.getdevices<ds18b20>();
_timer = new timer(temperaturecallback, null, 0, 5000);
while (!_isclosing)
{
task.delay(2000).wait();
}
}
private void temperaturecallback(object state)
{
var now = datetime.now;
foreach (var device in _devices)
{
var result = device.gettemperature();
debug.writeline(now + " " + device.onewireaddressstring +
": " + result);
}
}
private void taskinstance_canceled(ibackgroundtaskinstance sender,
backgroundtaskcancellationreason reason)
{
_isclosing = true;
if (_timer != null)
{
_timer.dispose();
_timer = null;
}
if (_handler != null)
{
_handler.dispose();
_handler = null;
}
sender.getdeferral().complete();
}
}
build it, deploy it to the raspberry pi and run it on visual studio. you should see output like this:
if you get the following error...
exception thrown: 'system.io.filenotfoundexception' in rinsen.iot.onewire.dll
winrt information: slave address was not acknowledged.
... then try to play with the ad0 and ad1 arguments of the onewiredevicehandler class. by default, these boolean arguments are true. i had to set these to false to make my solution work.
wrapping up
windows 10 iot core and visual studio tooling make it very easy to build iot solutions. of course, we don’t always get away from problems so easily like we did right now, as there aren't libraries for everything we want to do, but still, we have very good tooling that makes development way easier. although we wrote temperature readings to the debug window, we can go further and send the values to some database or a microsoft azure service.
brewing beer with a raspberry pi: table of contents
- brewing beer with a raspberry pi: measuring temperature
- brewing beer with a raspberry pi: moving to itemperatureclient interface
- brewing beer with a raspberry pi: measuring cooling rate
- brewing beer with a raspberry pi: making cooling rate calculations testable
- brewing beer with a raspberry pi: reporting measurements to azure iot hub
- brewing beer with raspberry pi: stream analytics
- brewing beer with raspberry pi: visualizing sensor data
- brewing beer with raspberry pi: building a universal windows application
Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments