Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Brewing Beer With a Raspberry Pi: Measuring Temperature

DZone's Guide to

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.

· IoT Zone
Free Resource

Cisco IoT makes digital transformation a reality in factories, transportation, and utilities. Learn how to start integrating with Cisco DevNet.

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 freezing of beer.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:

  1. All these components are cheap, and I found them from local online electronic stores.
  2. 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.
  3. 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.
  4. The colors of the DS18B20 wires are described in the component documentation.
  5. When connecting your solution to your Pi, make sure you connect the wires to GPIO pins correctly.
  6. 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.

Visual Studio 2015: Create new Windows IoT Core application

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:

  1. When the background application is run, initialize everything to read sensors.
  2. Add the code for the application shutdown so we politely leave the application.
  3. Use a timer to read values from sensors.
  4. 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:

Temperature readings from sensors

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

  1. Brewing Beer With a Raspberry Pi: Measuring Temperature
  2. Brewing Beer With a Raspberry Pi: Moving to ITemperatureClient Interface
  3. Brewing Beer With a Raspberry Pi: Measuring Cooling Rate
  4. Brewing Beer With a Raspberry Pi: Making Cooling Rate Calculations Testable  
  5. Brewing Beer With a Raspberry Pi: Reporting Measurements to Azure IoT Hub
  6. Brewing Beer With Raspberry Pi: Stream Analytics
  7. Brewing Beer With Raspberry Pi: Visualizing Sensor Data  
  8. Brewing Beer With Raspberry Pi: Building a Universal Windows Application

Cisco is a software company. Surprised? Don’t be. Join DevNet to explore APIs, tools, and techniques that developers are using to add collaboration, IoT, security, network priority, and more!

Topics:
iot ,raspberry pi ,beer ,windows 10 iot core ,tutorial

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}