Controlling hardware on the Raspberry Pi with NodeJS
This blog is to catalogue my experience setting up a new Raspberry Pi 3 with a set of Grove tools.
Introduction
Grove is the brand name for a range of sensors and devices which share a certain standardised socket. The magic comes from the ability to plug directly into special shields. While more expensive than using a breadboard, Grove makes prototyping faster and less error prone.
https://www.dexterindustries.com/grovepi/
They were originally designed for Arduino pin headers, but they are also compatible with Netduino, Beaglebone, Intel Edison and Raspberry Pi. Each device has its own special Grove drivers and collectively they share a number of shields.
Important Note: All projects which use Grove sockets are possible using non-Grove devices. This requires the technical know-how to wire these devices to the correct pins on the Raspberry Pi and care of pull-up/pull-down resistor requirements. The GrovePi merely takes care of this for us.
Choosing a Raspberry Pi
The Grove Shield supports all full-sized Raspberry Pi variants, since version 1.0b all full Raspberry Pis have shared the same pinout.
The smaller, cheaper and more energy-efficient Raspberry Pi Nano could be used once a project has been successfully validated and the software has been tested.
Bill of Materials
1 Raspberry Pi 3 ($35.00)
1 NOOBs SD card ($3.00)
These are both available via the Raspberry Pi Foundation’s resellers. Any full-sized RPi will do.
Grove Pi Shield ($60.00)
Grove Starter Kit ($80.00)
These are both available from Seeed Studio and resellers.
Installation:
Order the NOOBS SD-Card. Trust me :). It comes with the latest built-in Raspbian image, along with the option install a number of others over network.
For this tutorial I used Raspbian Pixel v4.0.
Setting up SSH
If you have a machine that you normally program on, is recommended to enable SSH on the Raspberry Pi which allows you to access the command-line remotely. This will also allow you to update the software once it is deployed without needing to re-attach a monitor, keyboard and mouse :).
Since Pixel was updated, SSH is no longer on by default, meaning that you either need to enable it in the installed OS via monitor+keyboard+mouse, or by writing a file named ssh to the boot partition of the SD card. More info =here=.
Grove Pi Software
Grove Pi supplies a set of libraries for the various sensors and devices that they sell.
There are numerous languages to use, each with varying support. Python has the most example projects, and is a great start if you’re new to hardware. I chose to use Javascript as it’s currently the language I want to practice the most :).
Installing NodeJS
The version of NodeJS shipped with Raspbian is out of date and won’t run GrovePi libraries. Additionally, npm is not installed by default. Update the version of NodeJS via the instructions on the NodeJS website. NodeJS versions can be switched by using version managers like nvm or n or by modifying the PATH directly.
All examples here were tested using Node version 7.x.
Confirm the installed version by running node -v from the command line.
Installing the Grove Pi Software
I followed the manual steps here https://www.dexterindustries.com/GrovePi/get-started-with-the-grovepi/setting-software/.
Documentation for the NodeJS libraries is available =here=.
Note that to use the NodeJS software, you will need to navigate to the lib directory and run npm install. This will install any nodejs dependencies. If this does not complete then it’s possible that the ‘activated’ version of NodeJS is outdated, please see the previous steps.
Testing the software
Next navigate to the /test folder and run the file with node, e.g. node test.js. Ensure that the Grove devices are connected to the right pins.
I was a fan of Netduino, and then I consolidated my dev machines down to a single MacBook Pro. I decided to write this blog on my experience getting a development environment working.
CrossOver
Will attempt this at some stage. CrossOver is not free, and since education is a big field for IoT, I wanted to see if I could do this without using paid software.
Wine
This looked promising at first, but using USB ports seemed like it could be an issue.
VirtualBox
Success!
1. Installing Virtual Box
Install from here: https://www.virtualbox.org/wiki/Downloads => OSX Hosts.
Run the installer and follow the instructions/steps.
2. Get Windows
2.1 Download a legit, free, copy
Microsoft provides free Windows VMs for the purpose of testing Microsoft Edge. Please read the license included with each VM to ensure that it complies with your intended usage.
Navigate to https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/ => Select any Windows version and make sure to select the VirtualBox download (.ovf extension).
They expire after 90 days which means that the will not start-up.
To work around this, it is recommended to make a snapshot once you are set up.
If you have a Windows license, use a full version instead :).
2.2 Install Windows
Double click the .ovf file to import the Virtual Machine into VirtualBox.
Once the VM has imported and Windows has started successfully, take a snapshot. This gives you something to roll back to, should there be an issue in the later steps.
3. Enable USB in VirtualBox
Enable USB access in VirtualBox. VirtualBox allows you to add filters to allow individual devices. Don’t forget to to restart the VM between adding/changing these!
Right Click VM -> Settings -> Ports -> USB -> Enable USB 1.1 Controller
4. Install Visual Studio Express 2013 and Tools
For this section I followed the ‘Development Environment’ steps from http://www.netduino.com/downloads/.
Known Issues:
.NET MF plug-in for VS2013 downloads as a .zip file
This seems to be an issue with some of the ‘smarts’ in Windows 10. Rename the netmfvs2013.zip file to a .vsix file and run. It should automatically be opened by Visual Studio.
Build Error: 0x80131700 File = MMP.
On a fresh install, it is common to only have .Net 4.0 installed. However the .Net MicroFramework uses part of the .NET 2.0 CLR.
This can be fixed by installed .NET 3.5 SP1 (http://www.microsoft.com/downloads/en/details.aspx?FamilyID=ab99342f-5d1a-413d-8319-81da479ab0d7&displaylang=en).
More details here http://forums.netduino.com/index.php?/topic/913-build-error-0x80131700/.
5. Set up workspace
Open Visual Studio Express 2013. You should be able to create a new project using a .Net Micro Framework Template.
Under File, go New Project -> Installed -> Templates -> Visual C# -> Micro Framework and select Netduino Application (Universal).
6. Deploy your first program
6.1 Write your first Program
Navigate to program.cs.
Within main, paste the following code (or write your own :))
``` OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
while (true)
{
Thread.Sleep(1000);
led.Write(!led.Read());
} ```
6.2 Configure the Project
Double click Properties in the Solution Explorer.
Under Application, ensure that the Target Framework is set to .NET Micro Framework 4.3
Under .NET Micro Framework, ensure that Transport is set to USB and Device is set to your Netduino.
Now hit deploy, and your code should load on to the device and begin running immediately.
Known Issues:
DebugPort.GetDeviceProcess() called with no argument
The first deploy has been known to fail. If the error message DebugPort.GetDeviceProcess() called with no argument appears, deploy to the Emulator, then switch back to USB and deploy again. See here for more details:
The Emulator can be found under .NET Micro Framework -> Transport -> Emulator, in the Project Properties.
7. Take a snapshot!!
You are now all set up ;). Please remember to take a snapshot if you are using one of Microsoft’s free VMs, so that you restore to a working state after the 6 months is up :).
8. Rinse and Repeat… or not.
If you are a school/library and need to get this working for a whole classroom, at this point you can export the VirtualBox image and copy it around the classroom. Much easier than following these steps again :).
Appendix: Updating the Netduino firmware
My instructions above are using the latest tools / firmware (as of writing this).
My Netduino had been flashed with the .NetMF 4.2.0 firmware, so I had to upgrade to 4.3.1 once I had the VirtualBox VM running. Here are a few tips on upgrading the firmware.
This video shows the process I went through: https://www.youtube.com/watch?v=D3YndaRcFNk.
This thread (http://forums.netduino.com/index.php?/topic/10479-netduino-plus-2-firmware-v431/) contains detailed instructions. However to access the download you need to create a blog account, which didn’t work for me in Chrome.
An alternate link to the firmware is http://static.netduino.com/downloads/netduinoupdate/NetduinoUpdate_4.3.2.3.zip, found on http://www.netduino.com/downloads/.
I also installed the STDFU drivers + tools (http://www.netduino.com/downloads/dfusedemo_3.0.3.zip) but I’m unsure whether this helped.
Finally, if you aren’t able to get the Netduino to be recognised by the VM, try enabling all usb devices in the VirtualBox control panel.
I’m creating this blog because I have a few things that are bursting to be written down. If all goes well, these will make it onto the web over the next few months.
The first real post should be easy. I’m currently travelling around SEA so I’ll likely write about my attempts at development without a laptop.