{"id":493,"date":"2013-12-31T15:21:24","date_gmt":"2013-12-31T14:21:24","guid":{"rendered":"http:\/\/hirt.se\/blog\/?p=493"},"modified":"2015-11-19T14:54:02","modified_gmt":"2015-11-19T13:54:02","slug":"accessing-1-wire-protocols-from-java","status":"publish","type":"post","link":"https:\/\/hirt.se\/blog\/?p=493","title":{"rendered":"Accessing 1-wire Protocol Devices from Java"},"content":{"rendered":"<p><em>More Raspberry Pi fun with Java! For an Adafruit LCD Shield library, check out my <a href=\"https:\/\/hirt.se\/blog\/?p=464\">previous blog<\/a>.<\/em><\/p>\n<p>So, instead of just playing around for fun, I decided to actually do something useful. In this case I wanted a little remote sensor station, which I can plug-in wherever I want to, and communicate with over http over GSM.<\/p>\n<h2>Building It<\/h2>\n<p>First of all you need to bunker up on as many <a href=\"http:\/\/www.lawicel-shop.se\/prod\/Temp-Sensor-WP-DS18B20_854353\/Temperatur_48058\/SWE\/SEK\">DS18B20<\/a>s and as many <a href=\"http:\/\/www.lawicel-shop.se\/prod\/AM2302-DHT22-TempHum-Sens_923901\/Temperatur_48058\/SWE\/SEK\">AM2302<\/a>\/DHT11\/DHT22\u2019s you think you\u2019ll need. There are plenty of articles out there on how to configure them. I decided to do it a tiny bit differently to most examples by hooking up the power pin to the 5V rail (both since the specs indicate that you can run longer cable lengths if you do so and since more current can be sourced from the 5V rail). The data is pulled up to the 3.3V rail, just like in all the examples. Note that a bunch of <a href=\"http:\/\/learn.adafruit.com\/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing\/hardware\">DS18B20s<\/a> can be hooked up in parallel and communicated with over a single GPIO port. Also note that this is NOT possible with the DHT sensors, which will need one GPIO port per sensor. <\/p>\n<p>Ultimately I decided to have two (optional) DS18B20s and one AM2302 on my board, and I used an Adafruit Perma Proto to hook it all up. Mostly because they are <a href=\"http:\/\/www.adafruit.com\/products\/590\">pretty<\/a>. \ud83d\ude09 To connect the AM2302, I used a <a href=\"http:\/\/www.lawicel-shop.se\/prod\/Molex-Jumper-4-Wire-Assembly_991348\/Sparkfun_64668\/SWE\/SEK\">Molex jumper assembly<\/a>, and for the DS18B20s I decided to use RJ-11s. I can highly recommend the RJ-11s! The connectors take a bit of space on the card, and they do require you to mutilate the poor Perma Proto a bit (drill larger holes), but attaching\/removing sensors is a joy. Here is a picture of the thingy with all the sensors connected:<\/p>\n<p><a href=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2013\/12\/sensor_board.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"sensor_board\" style=\"border-left-width: 0px;border-right-width: 0px;border-bottom-width: 0px;border-top-width: 0px\" border=\"0\" alt=\"sensor_board\" src=\"https:\/\/hirt.se\/blog\/wp-content\/uploads\/2013\/12\/sensor_board_thumb.jpg\" width=\"644\" height=\"482\"><\/a> <\/p>\n<p>In my build I will also use an <a href=\"https:\/\/hirt.se\/blog\/?p=464\">LCD shield<\/a> for local control and readouts without necessarily having access to a monitor. I also added a few buttons and LEDs, mostly to satisfy <a href=\"http:\/\/www.youtube.com\/watch?v=l2qgpXpCw6M\" title=\"my youngest son\">my youngest son<\/a>.<\/p>\n<p>I guess those could come in handy for diagnostics later. \ud83d\ude42<\/p>\n<p>Now, the 1-wire protocol used by the DS18B20 (Dallas 1-wire) and the 1-wire protocol used by the DHT sensors (AM2302, DHT11, DHT22) are quite different. Both of these require a bit of time sensitive bit-banging. Fortunately, that work has already been done for us. <\/p>\n<p>For the DS18B20 there are two modules available which will expose all the DS18B20s as files under \/sys\/bus\/w1\/devices. Simply add w1-gpio and w1-therm to \/etc\/modules. Note that you must have the DS18B20s on GPIO port 4 for the module to work.<\/p>\n<p>For the DHT sensors there is no module, but I&#8217;ve adapted the Adafruit native library <a href=\"http:\/\/learn.adafruit.com\/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging\/software-install\">described here<\/a>, to create a native library for Java. Read the README.txt provided in the zip for more information.<\/p>\n<h2>Java Access<\/h2>\n<p>Once you have added the modules and put native libraries on the library path, there is only a tiny bit of configuration left to do. The library should automatically discover all DS18B20s you have. If you only happen to have one AM2302 on GPIO-pin 22, you are good to go. If not, you need to edit the property file dhtsensors.properties in the w1.jar to correspond to your setup:<\/p>\n<blockquote>\n<p># Specify devices to use with the dht library in this file.<br \/># If you have no such devices, just comment everything out.<br \/>#<br \/># Type can be one of the following:<br \/># 2302 (for AM2302)<br \/># 11 (for DHT-11)<br \/># 22 (for DHT-22)<br \/>sensor0.pin=22<br \/>sensor0.type=2302<\/p>\n<\/blockquote>\n<p>If you have no DHT sensors, simply comment out the lines. If you have more than one, simply add two more lines for every sensor, indicating which type and what pin they are on. For example: <\/p>\n<blockquote>\n<p>sensor0.pin=22<br \/>sensor0.type=2302<br \/>sensor1.pin=23<br \/>sensor1.type=2302<br \/>sensor2.pin=24<br \/>sensor2.type=22<\/p>\n<\/blockquote>\n<p>To test that everything is set up properly, simply run:<\/p>\n<blockquote>\n<p><font style=\"background-color: #ffffff\">sudo java -Djava.library.path=&lt;library path&gt; \u2013jar w1.jar<\/font><\/p>\n<\/blockquote>\n<p>Note that Pi4J isn&#8217;t needed on the classpath. We&#8217;re relying on the native library and the w1-gpio mod to provide the necessary interfaces.<\/p>\n<p>The output should look something along the lines of this:<\/p>\n<blockquote>\n<p>Found 4 sensors! <\/p>\n<p>Temperature(28-000004f7960a):23,38\u00b0C<br \/>Temperature(28-000004f78caf):23,56\u00b0C<br \/>Temperature(2302@22-temperature):23,00\u00b0C<br \/>Humidity(2302@22-hygrometer):36,50%  <\/p>\n<p>Temperature(28-000004f7960a):23,38\u00b0C<br \/>Temperature(28-000004f78caf):23,56\u00b0C<br \/>Temperature(2302@22-temperature):23,00\u00b0C<br \/>Humidity(2302@22-hygrometer):36,50%  <\/p>\n<p>Temperature(28-000004f7960a):23,44\u00b0C<br \/>Temperature(28-000004f78caf):23,56\u00b0C<br \/>Temperature(2302@22-temperature):23,10\u00b0C<br \/>Humidity(2302@22-hygrometer):36,50% <\/p>\n<\/blockquote>\n<h2>Library Usage<\/h2>\n<p>The code is very simple. To get all the sensors, do:<\/p>\n<blockquote>\n<p>Set&lt;Sensor&gt; sensors = Sensors.getSensors(); <\/p>\n<\/blockquote>\n<p>To print the sensors like in the example, do:<\/p>\n<blockquote>\n<p>System.out.println(String.format(&#8220;Found %d sensors!&#8221;, sensors.size()));<br \/>while (true) {<br \/>&nbsp;&nbsp;&nbsp; for (Sensor sensor : sensors) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(String.format(&#8220;%s(%s):%3.2f%s&#8221;, sensor.getPhysicalQuantity(), <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sensor.getID(), sensor.getValue(), sensor.getUnitString()));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.sleep(1000);<br \/>&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp; System.out.println(&#8220;&#8221;);<br \/>&nbsp;&nbsp;&nbsp; System.out.flush();<br \/>}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<\/blockquote>\n<p>That\u2019s really all there is to it.<\/p>\n<h2>Conclusion<\/h2>\n<p>A simple Java library to read a few different kinds of temperature and hygrometer sensors was introduced.<\/p>\n<p>Downloads:<\/p>\n<ul>\n<li><a href=\"http:\/\/hirt.se\/downloads\/software\/rpi\/w1.zip\">w1.zip (new, read the README.txt)<\/a> <\/ul>\n","protected":false},"excerpt":{"rendered":"<p>More Raspberry Pi fun with Java! For an Adafruit LCD Shield library, check out my previous blog. So, instead of just playing around for fun, I decided to actually do something useful. In this case I wanted a little remote sensor station, which I can plug-in wherever I want to, and communicate with over http [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-493","post","type-post","status-publish","format-standard","hentry","category-java-mission-control-fair-dj"],"_links":{"self":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=493"}],"version-history":[{"count":9,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions"}],"predecessor-version":[{"id":670,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=\/wp\/v2\/posts\/493\/revisions\/670"}],"wp:attachment":[{"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirt.se\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}