Homebrewery Build Details
I have finally gotten around to posting up some of the photos from the build process. Check out the link above – Homebrew Build Details.
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $e->getMessage(); }
I have finally gotten around to posting up some of the photos from the build process. Check out the link above – Homebrew Build Details.
Because of some difficulty with displaying the code in WordPress (and the generally confusing schemes used to identify GPIO pins), I put together a quick tool to build the device tree file for the one-wire interface. You simply input the desired GPIO pin and the device tree overlay will be generated for you. See the link on the top menu or click here.
In a previous tutorial, we covered how to connect and read from DS18B20 sensors with your Beaglebone. I’d now like to cover the process of displaying live-updating DS18B20 temperatures in your browser using a Beaglebone Black. We’ll be using Socket.IO, a simple library that can be used to quickly communicate between a web browser and the server using the web sockets protocol. This tutorial will display all connected DS18B20 sensors in a nicely formatted table. This is geared towards developing a basic understanding how to use nodeJS with Socket.IO and the DS18B20 sensor. It can be used as a groundwork to use temperature readings in graphs and other formats. I use very similar code to turn on or off my connected SSR and pump relays.
I will assume you already have the following installed:
The Beaglebone and Raspberry Pi nodeJS development community is quite active. This means there are a lot of libraries available that make it simple to do tasks on your Beaglebone. For example, we will be using a package specifically for reading DS18B20 temperatures.
In a SSH session, move the current directory to your webserver’s document root (i.e .e /var/www
) and install the necessary Node packages.
1 2 |
$ cd /var/www $ npm install -g ds18b20 socket.io socket.io-client |
The necessary packages should now be installed into the directory ./node_modules/
Using your favorite text editor or IDE, let’s create the app.js file. This file is the node script that will be performing most of the work. It is responsible for the periodic reading of sensor values and sending them to connected web browsers.
Copy and paste the following into the file app.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//Include necessary node packages var io = require('/mnt/sd/apache/node_modules/socket.io').listen(3000), ds18b20 = require('ds18b20'); var interval = 3000; //enter the time between sensor queries here (in milliseconds) //when a client connects io.sockets.on('connection', function (socket) { var sensorId = []; //fetch array containing each ds18b20 sensor's ID ds18b20.sensors(function (err, id) { sensorId = id; socket.emit('sensors', id); //send sensor ID's to clients }); //initiate interval timer setInterval(function () { //loop through each sensor id sensorId.forEach(function (id) { ds18b20.temperature(id, function (err, value) { //send temperature reading out to connected clients socket.emit('temps', {'id': id, 'value': value}); }); }); }, interval); }); |
Save and close as app.js.
Next you will need to create the file index.html
. This file is loaded by your web browser and contains code to handle variables emitted from socket.IO.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<!DOCTYPE html> <html> <head> <!-- include bootstrap, jquery for easy div manipulation --> <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"> <script src="node_modules/socket.io-client/dist/socket.io.min.js"></script> <script> var socket = io.connect('http://bb:3000'); //enter the IP of your beaglebone and port you are using in app.js socket.on('sensors', function (data) { //append sensors to table data.forEach(function (d) { var html = '<tr><td>' + d + '</td><td id="' + d + '"></td></tr>'; $('#temps').append(html); }); }); //update corresponding row with sensor value socket.on('temps', function (data) { $('#' + data.id).html(data.value); }); </script> </head> <body> <h1>DS18B20 Temps</h1> <div class="container"> <table class="table" id="temps"> <thead> <tr> <th>Sensor ID</th> <th>Temperature (celsius)</th> </tr> </thead> </table> </div> </body> </html> |
Save and close as index.html.
You’re ready to test out your node script! Start the script using node app.js
. You’ll need to leave open your current terminal window as the process is linked to your login when you run it like this.
Open up index.html
in your web browser. In a few seconds, you should see live updating temperature readings from each connected DS18B20 sensors.