Per some previous posts (starting here) I have pulled some functionality from a project that I am working on into a little demo application - Node.Js Physical Interface Demo.
Over the past days I have refined the framework that I am using for my web server. I have chosen to use a fairly primitive approach to providing web service as I have based my application on the http module. This gives me very atomic control though with some added complexity.
In order to attack that complexity I have refined my own framework for http service. There are to scripts that are purely server...server.js which talks to the port and form.js which handles page and form interaces asynchronously. Two additional scripts provide an interface between server and form, serverExits and formExits. As the name implies there are a number of exits embedded in Server and Form that allow a application to customize some key handling aspects.
Everything else is pure application. To add a web page you create a script that includes application logic AND to handlers...serverHandler and formHandler. serverHandler executes the asynchronous recieve of a form which is then processed by formHandler. A matching html file is created in the html directory, with some meta tags that allow substitutions of variables, and Bob is Your Uncle.
I will write more about this later. Remember that my target is an embedded application with its head being a small LCD touchscreen.....
Evolution of a Blog
This blog has evolved as I have as a maker. It starts at the beginning of my journey where I began to re-tread my tires in the useful lore of micro electronics and the open-source software that can drive them. While building solutions around micro-electronics are still an occasional topic my more recent focus has been on the 3D Printing side of making.
Thursday, November 7, 2013
Thursday, October 31, 2013
Beaglebone Black Image Setup for 'The App'
Below is the procedure that I follow to create an Operating System mage on which my app runs. This assumes we are starting with a distribution image that has been flashed to the internal drive of the BBB, dd'ed off that image having booted from the external SD drive, and then having used the img copy to create a new bootable SD card. I know, there is probably an easier way to accomplish creating a new bootable image from a distribution file!
In any case then we do the following:
First make sure that the system date and time is accurate:
Then update the package indexes for both the OS and for NPM:
Now install all the OS level stuff that we will be needing:
The version of git on the BBB needs to be updated so download the tarball with the code and then do the following:
There is a problem with npm such that it will incorrectly fail thinking that it does not have the right version of Python installed. Fix this via the below edits:
Now all the npm packages that we need can be installed:
Following sections, for RTC, are obsoleted by getting the date and time via the Arduino. At this point, assuming we are wired for our I2C devices, we can set up our Real Time Clock. This is, of course, assuming that when you run the first of the below commands you see something at address 0x68! If there is, test it by doing the rest of the commands (get current network time, create the RTC device, write the time to the RTC, pull it off the RTC):
ntpdate -b -s -u pool.ntp.org
echo ds3231 0x68 >/sys/bus/i2c/devices/i2c-1/new_device
If all is well above (note that you need to check the device [/dev/rtc?] that was created to ensure you have the right name), create the following file “rtc” in “/etc/init.d”:
And have it run automatically:
Now make sure the board time is set, then load it to the RTC:
ntpdate -b -s -u pool.ntp.org
hwclock -f -w /dev/rtc
Now we can create our source directories for the application. First create a directory named "AppNameHere", cd into it, and execute the following command:
The above should have created all the other directories and startup files that will be needed. At this point you should be able to execute the command line configuration tool:
If everything is working you will see a list of default parameters.
DONE!
In any case then we do the following:
First make sure that the system date and time is accurate:
ntpdate -b -s -u
pool.ntp.org
Then update the package indexes for both the OS and for NPM:
opkg update
npm update
Now install all the OS level stuff that we will be needing:
opkg install
python-compiler
opkg install
python-misc
opkg install
python-multiprocessing
opkg install
xdotool
opkg install
gettext
The version of git on the BBB needs to be updated so download the tarball with the code and then do the following:
tar -xvf
git-1.8.?.tar.gz
cd
git-1.8.?
./configure
--without-python
make
make install
There is a problem with npm such that it will incorrectly fail thinking that it does not have the right version of Python installed. Fix this via the below edits:
nano
/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js
Change:
if
(semver.gte(version, '2.5.0') && semver.lt(version, '3.0.0')) {
getNodeDir()
} else {
failPythonVersion(version)
}
To:
getNodeDir()
Now all the npm packages that we need can be installed:
npm install -g node-gyp
npm install -g i2c
npm install
-g socket.io
npm install
-g descriptive-statistics
npm install -g
exec-sync
npm install
-g zipstream
npm install -g line-by-line
npm install -g serialport2
npm install -g line-by-line
npm install -g serialport2
Following sections, for RTC, are obsoleted by getting the date and time via the Arduino. At this point, assuming we are wired for our I2C devices, we can set up our Real Time Clock. This is, of course, assuming that when you run the first of the below commands you see something at address 0x68! If there is, test it by doing the rest of the commands (get current network time, create the RTC device, write the time to the RTC, pull it off the RTC):
i2cdetect -y -r 1
ntpdate -b -s -u pool.ntp.org
echo ds3231 0x68 >/sys/bus/i2c/devices/i2c-1/new_device
hwclock -f -w /dev/rtc
hwclock -f -s /dev/rtc
hwclock -f -s /dev/rtc
If all is well above (note that you need to check the device [/dev/rtc?] that was created to ensure you have the right name), create the following file “rtc” in “/etc/init.d”:
#! /bin/sh
# /etc/init.d/rtc
### BEGIN INIT INFO
# Provides: Time sync
#
Required-Start:
$remote_fs $syslog
#
Required-Stop:
$remote_fs $syslog
#
Default-Start: 2
3 4 5
#
Default-Stop:
0 1 6
#
Short-Description: Updates time from rtc
### END INIT INFO
# If you want a
command to always run, put it here
# Carry out
specific functions when asked to by the system
case "$1"
in
start)
echo "Starting time
sync"
echo ds3231 0x68
>/sys/bus/i2c/devices/i2c-1/new_device
hwclock -f /dev/rtc -s
;;
*)
echo "Usage:
/etc/init.d/rtc {start}"
exit 1
;;
esac
exit 0
Make the above created file executable:
chmod +x rtc
And have it run automatically:
update-rc.d rtc
defaults
Now make sure the board time is set, then load it to the RTC:
ntpdate -b -s -u pool.ntp.org
hwclock -f -w /dev/rtc
Now we can create our source directories for the application. First create a directory named "AppNameHere", cd into it, and execute the following command:
git clone
https://github.com/ThamesWill/AppNameHere.git dev
Fix for possible authentication issues:
Fix for possible authentication issues:
http://derekmolloy.ie/fixing-git-and-curl-certificates-problem-on-beaglebone-blac/
Once this has completed (note that you will need an id on github) you should execute the following script from the download:
Once this has completed (note that you will need an id on github) you should execute the following script from the download:
dev/install
The above should have created all the other directories and startup files that will be needed. At this point you should be able to execute the command line configuration tool:
./config
If everything is working you will see a list of default parameters.
One last change is to point the cloud9 environment at the source code we just downloaded. Do this by editting the following file replacing the "/etc/lib/cloud9" entries with the path to the dev directory created above.
nano /lib/systemd/system/cloud9.service
You will now need to either reboot or tell systemctl about your changes and then restart cloud9:
You will now need to either reboot or tell systemctl about your changes and then restart cloud9:
systemctl --system daemon-reload
systemctl restart cloud9
nano /lib/systemd/system/cloud9.service
Finally, make sure that you have updated the hosts file to match your server name for The App configuration
Finally, make sure that you have updated the hosts file to match your server name for The App configuration
DONE!
Wednesday, October 30, 2013
Implementing an I2c Workaround
The i2c library that I am using (kelly/node-i2c) works great...but...after some period of observations it causes node.js to fail with a segmentation fault. I am not sure this is a problem with the base library or with how I am using it with the Analog Digital Converter that I am using. I suspect that it is the latter but I don't have the technical skills to diagnose the problem. Given the former I decided to implement a workaround as described below.
Since failures generally only occurred after a period of time I decided to create a background process that could be started for the period of observation and then killed until the next period is started.
I am using an event to start the observation period by sending a message to the wireServer process started above. The message tells the server where to look for the I2c device. Once this is done I setup a handler for readings coming back from the server.
Once the I2c interface is started node will no longer respond to messages sent to the background process! This means I have no way of asking it to die gracefully. That being missing I use a little less graceful approach and kill it at the O/S level.
Finally we launch the observation perio.
The background process that the above launches, and then communicates with, is shown below:
wireServer = require('child_process').fork('wireServer.js',
[global.config.i2cDevice, global.config.i2cAddress, global.config.i2cCommand]);
wireServerPid = wireServer.pid;
utilities.trace(false, "test - wireServer started as PID " + wireServerPid);
eventEmitter.on('startObserve', function(message){
wireServer.send({"message":"init",
"i2cDevice":global.config.i2cDevice,
"i2cAddress":global.config.i2cAddress,
"i2cCommand":global.config.i2cCommand});
// Each message will be a voltage reading that we then pass to the below routine
wireServer.on('message', function (message) {
observe(message.voltageIn);
});
});
execSync('kill ' + wireServerPid);
utilities.trace(false, "test - Current wireServer running as PID " + wireServerPid + " killed")
eventEmitter.emit('startObserve', '');
The background process that the above launches, and then communicates with, is shown below:
// Load the library we need to get voltage measurements using i2c
var i2c = require('i2c');
var i2cDevice = "/dev/i2c-" + process.argv[2];
var i2cAddress = process.argv[3];
var i2cCommand = Number(process.argv[4]);
// Convert the i2c address from character to hex
i2cAddress = parseInt("0x" + i2cAddress);
var wire = new i2c(i2cAddress, {device: i2cDevice, debug: false});
var seq = 0;
wire.on('data', function(data) {
observe(data.data);
});
// Look for an init message from test to start our observations
process.on('message', function (message) {
wire.stream(i2cCommand, 4, 3);
});
// Take a voltage reading and send it on to test
function observe(buffer) {
var voltageIn = 0;
voltageIn = (buffer[0] << 8) + buffer[1];
// Did we get a negative value? If so make it a zero.
if (voltageIn > 2048) {
voltageIn = 0;
}
else {
voltageIn = voltageIn / 2048;
}
if (seq == 5000 || seq === 0) {
console.log(datetimeStamp() + " - wireServer - active");
seq = 1;
}
process.send({seq:seq++, voltageIn: voltageIn });
}
Integrating a Joystick via I2c with a GPIO Detected Button - Software
The code used to integrate with the Joystick hardware described by the previous post is shown below. Note that the entire working demo is avalable on GitHub.
- Bring in the "i2c" ibrary (Kelly / Node-I2C) used to interface with I2C devices such as our Analog Digital Converter (ADC). I have configured the device to be on address 0x6A and I know that it is /dev/i2c-1 from using i2cdetect
- Bring in the "onoff" library (fivdi / onnoff) that we will use to detect the pressing of the select button on the Joystick. Note that bonescript on the BeagleBone Black would accomplish this with an interupt but since I am doing my demo on an Raspberry Pi I pulled in the onoff library.
- Bring in the "events" library (node events) that I use to poll for I2c and Select Button states.
- React to an event requesting a read of the Y axis. Use the I2c library to read 4 bytes in response to the command "0xB0" which is a request to read 12 bits of data from channel 2.
- React to an event requesting a read of the X axis. Use the I2c library to read 4 bytes in response to the command "0xD0" which is a request to read 12 bits of data from channel 3.
- The observe routine translates the buffer returned from the I2c library into a usable voltage reading and stashes it in one of our two axis variables
- The "startObserve" event triggers a repeating event that will do our actuall polling for the Joystick state. It launches an interval event every 25 ms that toggles between the X and Y axis getting an update for each every 50 ms. The current state of the axis's are sent to the client every cycle. The state of the joystick select button is also checked every iteration and if it is depressed a snapshot is sent to the client for the current X and Y axis positions.
- Once all the event handlers have been registered we kick things off by emitting startObserve!
| (1) | var i2c = require('i2c'); var address = 0x6A; var wire = new i2c(address, {device: '/dev/i2c-1', debug: false}); |
| (2) | var Gpio = require('onoff').Gpio, button = new Gpio(4, 'in', 'both'); button.debounceTimeout = 1000; |
| (3) | var events = require('events'); var eventEmitter = new events.EventEmitter(); |
| (4) | eventEmitter.on('measureY', function(message){ wire.readBytes(0xB0, 4, function(err, res) { observe("Y", res); }); }); |
| (5) | eventEmitter.on('measureY', function(message){ wire.readBytes(0xD0, 4, function(err, res) { observe("Y", res); }); }); |
| (6) | function observe(xy, buffer) { var voltageIn = (buffer[0] << 8) + buffer[1]; if (voltageIn > 2048) { voltageIn = 0; } else { voltageIn = voltageIn / 2048 * 5 * 22.6; } if (xy == "X") { voltageInX = voltageIn } else { voltageInY = voltageIn } } |
| (7) | eventEmitter.on('startObserve', function(message){ var toggle = "X"; intervalId = setInterval(function() { if (toggle == "X") { eventEmitter.emit('measureX', ''); toggle = "Y"; } else { eventEmitter.emit('measureY', ''); toggle = "X"; } plot(); if (button.readSync() == 1) { console.log("Sent " + voltageInX + " - " + voltageInY); socket.emit('buttonPressed', { dp1: voltageInX, dp2: voltageInY }); buttonPressed = false; } }, 25); |
| (8) | eventEmitter.emit('startObserve', ''); |
Integrating a Joystick via I2c with a GPIO Detected Button - Hardware
- The circuit board for the Joystick has the button wired from ground so the signal would go from high to low. I wanted a rising signal so broke the default connection (as shown by the picture) and wired the button to the 5v supply.
- The output from the button is connected to a pull down resistor...
- ...and since the output 5v I route it through a voltage divider to make it safe for the Raspberry Pi.
- Each axis of the Joystick delivers a voltage that indicates the position of the controller. These outputs are connected to the Analog Digital Convertor (ADC) using channels 2 and 3.
- The output from the ADC is connected to the Raspberry Pi using the SLC and SDA connections.
Friday, October 11, 2013
Using Socket.IO on Node.Js with RGaph to Animate a Report
“The App” demo uses Socket.IO for
easy real-time communications between the server process running under Node.js and
the client process running JavaScript in the browser as described by the
example on this page.
- Initialization of the client and server libraries and opening of a connection on port 7300.
- Server sends maxSustained to the client as this is needed while the data points are being received. The payload of this message also includes the type of graph we are preparing (best second or all of second).
- Client sends “ready” to the server to ack the maxSustained message. The server has a socket.on for the ready message.
- On receipt of the “ready” message the server starts sending the data for the graph. On the client the each “graph” message is received and its payload of data points added to an array.
- Once all the data points have been dispatched to the client the server sends a “done” message with some additional data in the payload to be used on the web page being presented. On receipt of this message the client presents the graph.
Thursday, October 10, 2013
Node.Js, Socket.IO, Physical Interface, Screen Recording
Here is a screen capture of the demo application that I have been, and will be, discussing. It's a simple example but demonstrates a fair amount of functionality behind the scenes.
- The "ADC Test Session" demonstrates the app reading voltage levels from an ADC via I2c and displaying a graph of both the realtime data and of a high water mark achieved over a second.
- The "Joystick Test Session" demonstrates the app reading voltages from a joystick via the same ADC as above and detecting a button push via a GPIO.
Some of the things that I will be discussing include the following:
- Node.Js Async Processing Example
- Node.Js Web Page Processing (Using 'html' Library)
- Using Socket.IO to animate a client side graph
- Integrating a Realtime Clock and an ADC via I2c
- Integrating a Joystick via I2c with a GPIO detected button
Subscribe to:
Posts (Atom)

.jpg)