Troubleshooting SPI on Raspberry Pi (in NodeJS)

When starting to use the SPI (Serial Peripheral Interface Bus) on the Raspberry PI, especially using NodeJS, it can be quite frustrating when you do not get any results (or get seemingly random data) without any obvious explanation or helpful error messages. Then this “SPI loopback” trick might come in handy.

Since SPI seems to be more straightforwards using Python in comparison to NodeJS I will focus the example code on NodeJS. The exact same approach should however be equally valid for Python.

Follow these steps to troubleshoot/debug your SPI connection on your Raspi:

1. First of all, make sure SPI is actually enabled

The SPI master driver is disabled by default on Raspian. To enable it, remove the blacklisting for spi-bcm2708 in /etc/modprobe.d/raspi-blacklist.conf, or use $ raspi-config.

If raspi-blacklist.conf looks something like this:

blacklist spi-bcm2708

Change it to (to prevent SPI from being blacklisted):

#blacklist spi-bcm2708

Reboot or load the driver manually with:

$ sudo modprobe spi-bcm2708

When you have followed these steps, or you simply want to check if SPI is enabled, run the following command:

$ ls /dev

The output should contain something like “spidev0.0” if SPI is indeed enabled.

raspberry-pi_check-spi-enabled_mikael-leven

 

2. Connect SPI in loopback / “debug mode”

SPI is using two data wires, MOSI and MISO. It might not be apparent what these abbreviations actually stand for. MOSI stands for Master Out Slave In (i.e. it is the input on whatever device you are trying to connect to your RasPi) and MISO stands for Master In Slave Out (i.e. the output of the device connected to your Pi, or we could call this “the Raspi Input”). Ok, so now we understand the meaning, but how will this help us debugging? If the MOSI is intended to receive data from your RasPi and the MISO is intended to send data back to your RasPi, we could simply jumper these two pins to get a “loopback” connection. Whatever data that is sent out from the Pi should get received back.

raspberry-pi_gpio_spi_loopback_nodejs_mikael-leven

 

3. The code – test SPI using NodeJS

This example uses the rpio GPIO library for NodeJS. Copy and paste the following code into a new file (or download the gist).

var rpio = require('rpio');

var rxBuffer = rpio.spiTransfer(new Buffer('HELLOSPI'), 8);

for (var i = 0; i <= 7; i++) { 
 process.stdout.write(String.fromCharCode(rxBuffer[i]) + (i == 7 ? '\n' : ' '));
};

Run your script as sudo:

$ sudo node your-script.js

The result should look like this:

$ sudo node your-script.js
H E L L O S P I
$ _

If you receive an error message make sure you execute node as sudo. Should you get an empty response make sure your loopback jumper wire is properly connected.

When you receive the text “H E L L O S P I” you know SPI is properly configured on your Raspberry Pi. The next step is to connect an SPI enabled device in the other end (instead of the loopback) begin writing your actual program. Good luck!

Two Great News From Heroku

Heroku has announced two interesting features:

Heroku Private Spaces Beta opens up for additional security and control in your cloud solution. Read more about Heroku Private Spaces: Private PaaS, delivered as-a-Service.

Heroku Flow enables Continuous Delivery via GitHub, making CD visual, easy to manage, and accessible to all team members. Read more about Heroku Flow – a new and flexible way to structure, support, and visualize your CD workflow.

Great Way To Visualize and Communicate A/B testing, and to Make Good Use of “Big Data”

Pendlingskollen_Visualizing_A-B-Test_Using-BigData_MIkaelLeven

Visualization of A/B testing. Old traffic control system to the left, actual arrival time in the middle and the new predictive algorithm to the right. Currently the new algorithm has a distinct advantage with a score of 23,010 vs. 1,504 for the old system.

This is a great example that shows how we can benefit from “big data” to achieve a greater good, wether the information comes from historical data, is fed live through M2M sensors (machine-to-machine/Internet of Things) or a combination of the two.

In this example, StockholmsTåg use quite insipid information (at least when looking at it as isolated data) – the weight of a railway car – to make insightful predictions when there is a disturbance in the railway traffic. This is possible thanks to placing the insipid data into a broader perspective where the data comes to life and becomes meaningful. This way they are able to determine which railway wagons that contains the greatest number of people, so when there is obstacles on the tracks they can predict how this will effect the traffic in general and accordingly prioritize those wagons with most people, and by doing so they minimize the overall social effect of the inevitable (the obstacles itself is hard to avoid).

Pendlingskollen_Visualizing_A-B-Test_Scoring_MIkaelLeven

A/B test scoring. In this sample the new system has gained one point, illustrated by the green area.

And what is particularly interesting in this case is how they visualize their A/B testing, by giving the old and the new system a score based in their performance, and how they communicate this transparently directly to their end-users (the commuters).

StockholmsTåg has empowered their data to better be able to predict the effects of upcoming disturbances in the railway traffic, and they also found a great way of visualizing this. In the long run it also means they can have a more proactive approach towards their customers regarding disturbances and the potential workarounds, and thanks to this greater detail the commuters can make more well-founded decisions to make the most out of their time.

http://pendelprognos.technology/