# One-wire DS1820 thermometer with BeagleBone Black & libmicrohttpd

Preface

Some time ago I’d an opportunity to play around with BBB. The program presented in this article is collecting data from DS18B20 sensors and stores them in sqlite database. Data is accessible via http (jQuery mobile). I was in hurry while writing the code, so article’s content might seem a bit messy.

Additionally you may set an alarm, whenever temperature exceeds defined boundaries you’ll be notified by email.

Requirements

DTBO, w1 driver

Reading data is handled by "w1" kernel module. In order to load that module with proper configuration I have used the device tree structure available in linux kernel from the version 3.8. as below:

/dts-v1/;
/plugin/; 

/ {
    compatible = "ti,beaglebone", "ti,beaglebone-black";
    part-number = "BB-W1";
    version = "00A0"; 

    /* state the resources this cape uses */
    exclusive-use =
        /* the pin header uses */
        "P9.22",
        /* the hardware IP uses */
        "gpio0_2"; 

    fragment@0 {
               target = < &am33xx_pinmux >;
               __overlay__ {
                    dallas_w1_pins: pinmux_dallas_w1_pins {
                        pinctrl-single,pins = < 0x150 0x37 >;
                    };
               };
    }; 

    fragment@1 {
               target = < &ocp >;
               __overlay__ {
               onewire@0 {
                   compatible      = "w1-gpio";
                   pinctrl-names   = "default";
                   pinctrl-0       = < &dallas_w1_pins >;
                   status          = "okay"; 

                   gpios = < &gpio1 2 0 >;
               };
         };
    };
};

That configuration was compiled to dtbo object which is loaded as kernel module, commands:

dtc -O dtb -o BB-W1-00A0.dtbo -b 0 -@ BB-W1-00A0.dts 
cp BB-W1-00A0.dtbo /lib/firmware/ 
echo BB-W1:00A0 > /sys/devices/bone_capemgr.9/slots 

You may check your configuration by executing:

cat /sys/bus/w1/devices/DEVICE_ID/w1_slave

Wiring

The picture below presents how to wire aith arduino, but the same schema may be applied to beaglebone black. We just connect to specified GPIO port (i.e P9.22).

Server

The main program works the following way:

  • Program is executed in a http server mode (using libmicrohttpd)
  • The thread (which collects the data from sensors) is run and saves data to sqlite db
    (if not exists, then is created)
  • If htttp request comes, it is parsed and proper action is performed
  • If sensor will notice temperature out of bound then the mail is sent to the user. (via msmtp)

Built-in http server also handles images, js, css, html files.
Current data are update via post request (every 2 seconds is sent from the client – polling).

Click to see source code

Frontend

Frontend is fairly simple. Just open your browser on address pointing to your BBB and you’re done.