Pi/Node-Red Car

The goal of the Pi/Node-Red car project was to create a small vehicle that can be controlled from a smart phone . For the project we used:

  • 1 Car chassis for Arduino ($15)
  • 1 Pimoroni Explorer HAT Pro  ($23)
  • 1 Portable microUSB charger
  • 1 USB WiFi Adapter
  • 4 short alligator clips and 4 connectors
  • Duct tape

The Arduino car chassis may require a small amount of assembly. Rather than soldering connections we like to use short alligator clips. It is not recommended to wire DC motors directly to a Raspberry Pi so the Pimoroni Explorer HAT Pro is used to connect the 2 DC motors.

The Raspberry Pi and the portable microUSB charger are secured to the top of the car chassis with duct tape. The left motor is wired to the motor 1 connectors on the Explorer Hat, and the right motor is wired to motor 2 connectors. Note you may have to do a little trial and error on the Explorer HAT “+” and “-” motor connections to get both wheels spinning in a forward direction.

The Explorer HAT Node-Red library is installed by:

 cd $HOME/.node-red
npm install node-red-dashboard 

The Web dashboard presentation is configured in the “dashboard” tab. For this example we create 2 groups: a control group to drive the vehicle, and a light group to turn on the Explorer Pro lights. Use the “+group” button to add a group, and the “edit” to change an existing group.
dash_conf

To control a motor, an “Explorer HAT” node and a dashboard button node are dropped and connected together. All the configuration is done in the button node . The button configure options are:

  • the group the button will appear in (Controls)
  • the size of the button (3×1 = 50% of width and narrow)
  • Topic, motor.one or motor.twois used for motor control
  • Payload, -100 = reverse, 0=stop, 100 = forward

Control_conf

The Explorer HAT has 4 colored LEDs. To toggle the LEDS, the topic is light.color with 1=ON, and 0=OFF . We thought that it would be fun to also add some Web dashboard button to control the colored lights.

light_conf

The Node-Red dashboard user interface is accessed by: ipaddress:1880/UI, so for example 192.168.1.102:1880/ui. Below is a picture that shows the final vehicle logic and the Web dashboard.

 

final_logic2

 

 

 

littleBits/Pi Temperature Monitor

By using littleBits Proto modules it is possible to create Raspberry Pi projects with littleBits components. On this project we created a temperature monitor with a local indication and a remote Web page using Node-Red.

The parts used were:

  • 1 Raspberry Pi 3
  • 1 Explorer Hat (used for Analog Inputs)
  • 2 littleBits Proto bits
  • 1 littleBits Temperature bit
  • 1 littleBits Number bit
  • 1 littleBits Wire bit (optional, used for easier wiring)

The Wiring

To complete the littleBits circuit the first Proto bit was wired with 5V to the VCC and the data connectors. Pi GND was wired to the module’s GND. The second Proto bit had the data pin with to the Explorer HAT analog input 1.

tempcircuit

Node-Red Logic

On the Pi, the following Node-Red nodes were used:

  • Explorer Hat Node – reads the analog input
  • Function Node – only pass on analog input 1
  • Smooth Node – removes noise and jitter
  • Function Node – converts 0-5V to 0-100 C

temp_logic

The logic for the first function node, (to pass only analog input 1), was:

 if (msg.topic == "explorerhat/analog.1"){
return msg;
} 

We added a smooth node to remove noise in the analog input signal. The configuration that we used for the smooth node was:

temp_smooth

The logic for the second function, (to convert the voltage to a temperature), was:

msg.payload= (100/5)*msg.payload;
return msg;

Finally we used a Chart Dashboard node to show the results on a Web page. Our configuration for this was:

temp_gauge

To view the Web page use the URL of : http://the-pi-address:1880/ui

temp_pic_full

littleBits/Pi Internet Radio

The littleBits Proto bit allows littleBits components to be directly to connected to a Raspberry Pi (or Arduino).

The Goal

The goal of this project was to play Internet Radio and use littleBits to:

  • start the music, (and possibily change the music)
  • control the volume
  • use the littleBits speaker

For this project we used:

  • 2 Proto Bits
  • 1 Fork Bit
  • 1 Button Bit
  • 1 Slide Dimmer Bit
  • 1 Speaker Bit
  • 1 Bargraph Bit (optional)
  • 1 Wire Bit (optional)

The Wiring

music_setup

For the littleBits to Pi wiring, the input to the Fork Bit needs to have 5V on both the power and data pins, the GND pin of the Proto Bit is wired to GND on the Pi.

music_circuit

The output on the littleBits Button bit is wired into GPIO 23 on the Pi.

The Logic

For the logic we used Node-Red, Python would have probably been a better choice, but we wanted to see if it could improve our Node-Red skills.

To play Internet music we need to load the Node-Red mpd node:

cd $HOME/.node-red
npm install node-red-contrib-mpd

The Node-Red logic used only 3 nodes:

  • 1 – Raspberry Pi input node, to read button push
  • 1 – Function node, to do some action on the button push
  • 1 – MPD Output node, to play Internet Radio

radio_logic

There are a lot of possible options for what the littleBits button could do. For this example we simply wanted to load an Internet Radio URL, and then start playing. To find Internet Radio stations go to: https://www.internet-radio.com/.

music_func

The function node had a context variable, issetup,  that was used to load the music for the first time and then start playing.

// create an "is setup" variable
var issetup = context.get('issetup')||0;

if (msg.payload == "1") {
// if the setup hasn't been run, add a radio station playlist
if (issetup === 0) {
context.set('issetup',1);
var msg0 = { payload:"clear"};
var msg1 = { payload:"add http://185.33.21.112:11029" };
var msg2 = { payload:"play" };
return [ [ msg0, msg1, msg2] ];
}
}

Pi Node-Red Streetcar

For our streetcar design we wanted to :

  • manually control things with a Web interface on a smart phone, and
  • automatically have the streetcar stop at station, wait, then go in the reverse direction.

For this project we built everything in Lego, with Lego Mindstorm components. The programming was done in Node-Red which was running on a Raspberry Pi.

The Lego Mindstorms EV3 and NXT components can be wired into your Pi (or Arduino) projects by using some custom adapters. If you are brave you could cut one end off of the connector cables. We unfortunately didn’t have any extra cables so went the safe route with the adapters. There are a couple of different adapters that are available we used the breadboard version from Dexter Industries.

Using the following parts we were able to make an automated streetcar :

  • 1 Lego Mindstorms motor
  • 1 Lego Mindstorms touch sensors
  • 3 Lego Mindstorms connector cables
  • 3 Dexter Lego Mindstorms bread board adapter
  • 1 small breadboard
  • 1 Pi 3 (or Pi 2 with a network connection)
  • 1 Pimoroni ExplorerPro Hat
  • 2 long pieces of wire
  • lots of Lego blocks

Streetcar Design

Our goal for the streetcar project was to have two stations, then have the streetcar automatically stop and switch directions at each station.

full_street

Station 1 had the motor, a touch sensor, and the Raspberry Pi.

station1

Station 2 had a pulley wheel, a touch sensor and a small breadboard that wired back to the Pi at station 1. Our pulley string went above the streetcar. We needed to keep the string quite tight so the pulley would not slip.

station2_top1

The Wiring

All of the Lego Mindstorms wiring used the same pins on the left side of the connector. and they are labeled “ANG” and “GND”. We used an ExplorerHat Pro to connect the Lego Mindstorms Motor and touch sensors to the Raspberry Pi. As we mentioned earlier the station 2 touch sensor was wired into a small breadboard and then from there two wires connected it to the ExplorerPro. For our setup station 1 touch sensor was wired on Explorer Pro input 1, and station 2 was on input 2.

circuit

Node-Red Installation

To check that Node-Red is installed and working, go to a Terminal window and enter:

node-red-start &

If you are able to run Node-Red, the next step is to install the Pimoroni ExplorerHat node and a web dashboard node. Depending on your installation you might need to load the Node Package Manager, npm :

sudo apt-get update
sudo apt-get install npm

Then to install the added libraries:

\curl -sS get.pimoroni.com/explorerhat | bash
 cd ~/.node-red
 npm install node-red-contrib-explorerhat
 npm install node-red-dashboard

After you install these library nodes you will need to restart your Pi. Once Node-RED restarts, you use a web browser to build applications. If you are working directly on your Pi, enter 127.0.0.1:1880 in the URL address box of your browser.

Node-Red Manual Web Control

To make a web based manual control program, drag three button nodes from the left node panel onto the center panel. Then wire the button nodes to an ExplorerHat output node.

nr2

Double click on a button node to open an edit window. The edit window allows you to configure the dashboard, labels and button actions. To control the first ExplorerHat motor the topic is motor.one. The payload is between -100 and 100, and this corresponds to full reverse and full forward, with 0 being stopped.

After you have finished all your Node-Red configure click the Deploy button at the top right of your browser window. Deploying will run your program and enable the web dashboards. To access the web dashboards enter: http://your-ip-address:1880/ui. If you are unsure of your IP address go to a terminal window and enter : ifconfig.

screenshot

Node-Red Automatic Control Program

We found the automatic control logic to be a little tricker than the manual logic. Our first step was to see if we could read the Lego Mindstorms touch sensors. For this we connected an ExplorerHAT input node to a debug node. We manually pushed each touch sensor and we used the debug tab to check the results.

debug

To catch when the streetcar hits the touch sensor at station 1 we needed to create a function that looked at the topic explorerhat/input.1. If this topic’s playload is 1 or pushed then the ExplorerHat motor can be stopped by sending a message with the topic of motor.one and the payload=0. A similar function is created for the touch sensor at station 2 except the topic explorerhat/input.2 is monitored.

button1

After the streetcar hits a touch sensor we wanted it to pause, and then move in the opposite direction. A delay node was used for the pause. Following this a function node is used to send a message with a payload of the new speed and direction. For our project station 1 restarted with a speed of -70, and station 2 restarted with a speed of 70 (or +70).

reverse1

Our complete logic with both manual and automatic control is shown below:

full_logic

Remote Controlled Rocket Launcher

We’ve made a number of different versions of the mobile rocket launcher, and all of them have been fun. The version above is using the a PI 1 with a PiFace Digital module for the motor controls and a wireless keyboard for the controls.

The version shown below is using a PI 3, with an ExploreHAT Pro for the motor controls and a Python Web Server program for the controls. By using some simplified HTML tagging we were able to use an old PSP as a remote interface console.

launcher

Rocket Launcher Sample Code

We found the rocket launcher in a sale bin, but they call be purchased at: http://dreamcheeky.com/thunder-missile-launcher. The rocket launcher comes with it’s own Windows based program.

Below is some Python code that will control the rockets launchers turret and fire a missile. Based on this sample code you should be able to make some cool projects. A couple of notes from our testing:

  • you need to manually stop the turret motion once you start it. So you need to set your own wait time
  • you need 3-4 seconds between each missile firing. It might be a coincident but we damaged our first rocket launcher trying to issue fast firing commands.
 import usb
import sys
import time

device = usb.core.find(idVendor=0x2123, idProduct=0x1010)

# On Linux we need to detach usb HID first
try:
device.detach_kernel_driver(0)
# except Exception, e:
except Exception:

pass # already unregistered

device.set_configuration()

endpoint = device[0][(0,0)][0]

down = 1 # down
up = 2 # up
left = 4 # rotate left
right = 8 # rotate right
fire = 16 # fire
stop = 32 # stop

#device.ctrl_transfer(0x21, 0x09, 0x0200, 0, [signal])

while True:

print('r = right, l = left, u = up, d = down, f = fire ')
key = raw_input ('enter key:')
if (key == 'l'):
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, left, 0x00,0x00,0x00,0x00,0x00,0x00])
if (key == 'u'):
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, up, 0x00,0x00,0x00,0x00,0x00,0x00])
if (key == 'r'):
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, right, 0x00,0x00,0x00,0x00,0x00,0x00])
if (key == 'd'):
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, down, 0x00,0x00,0x00,0x00,0x00,0x00])
if (key == 'f'):
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, fire, 0x00,0x00,0x00,0x00,0x00,0x00])
time.sleep(4)
time.sleep(0.1)
device.ctrl_transfer(0x21, 0x09, 0, 0, [0x02, stop, 0x00,0x00,0x00,0x00,0x00,0x00]) 

 

 

 

Using QR Codes

The typical UPC-A barcode is visually represented by strips of bars and spaces, that encode a 12-digit number. A QR code (Quick Response Code) is a matrix barcode that can contain up to 4296 characters.

For Pi or Arduino projects QR codes could be used to document what the module is doing, or “if lost please call…”, or web links.

Create Your Own QR Codes

There are a number of different tools available to create your own QR codes. One of the simplest methods is to use Google Charts, and it is called by:

http://chart.apis.google.com?

The important parameters are:
cht=qr – chart type = qr
chs=<width>x<height> – chart size, and
chl=<data> – the data or URL to encode

An example of encoding “Hello World” in a 100×100 QR would be:

http://chart.apis.google.com/chart?chl=Hello+World&chs=100×100&cht=qr

A simple web form that can be used to create QR codes



Create QR Codes


<h1>Create QR Codes</h1>

Text to embed in QR Code</br>

</br>Image Size :</br>

100x100
150x150
200x200
250x250
300x300
350x350
400x400
500x500

</br>




html_code

After the image is generated it can be printed, cut to size and then taped to your equipment. If you have a number of Arduino or Pi modules QR codes could be an easy way to determine what is loaded on each module.

Create an Android QR Reader App

MIT’s AppInventer is a great option for Android smart phones and tablets.

For our application we used the following components:

  • 1 Button – to start QR scanning
  • 1 Textbox – to show QR scan results
  • 1 Button – to call a browser if the QR code is a Web link
  • 1 BarcodeScanner – to turn on the camera and process the QR code
  • 1 ActivityStarter – to launch the web browser

screen

The logic starts by defining the camera to be used for the QR scanning, and setting the ActivityStarter.Action to be a browser (android.intent.action.VIEW).

The BarcodeScanner1.AfterScan block puts the decoded QR data into the textbox. If the QR code starts with “http” then the “Open Link” button is enabled.

logic

Once our custom QR reader app is on our device we can start to customize it to our needs. The picture below shows the basic app reading a 100×100 QR code used on an Arduino project. Some future considerations that could be added to this simple app could things like: recording the geographic location of the device or is the data valid.

scan2

QR Codes and the Internet of Things (IoT)

For projects with a lot of sensors or devices QR codes can be helpful to identify and document what each device is used for. If the device is a source of data then a QR code could link to that specific devices data.

The picture below is an example of a solar powered weather device. The ESP8266 based Arduino module uses MQTT to send data back to a Pi node running Node-Red. The QR code on the side of the enclosure has a link to the Node-Red web page.

outside2

RFID Card Reader Apps

Blank RFID (Radio Frequency ID) cards along with a USB based RFID card reader can offer an easy and low cost solution for your Pi security projects.

For under $10 we were able use blank RFID cards (5 for $3) and a generic USB based RFID card reader ($6).

Using some simply Python code it is possible to make projects for security systems, door entry systems or control of remote devices.

powerswitch

RFID Cards and Readers

The RFID cards and readers need to work at the same frequency. For us we used the lower cost 125 KHz equipment.

The blank 125 KHz RFID cards have a predefined RFID number, this number is typically written on the card. These blank cards are be used directly, so no programming and writing to the card is required.

card

The USB card reader will momentarily light up and beep when it reads a card. The USB card reader act like a USB keyboard and it will pass the card’s RFID number as a series of key strokes. An <ENTER> key is sent at the end of the sequence.

Get RFID numbers and Build a Lookup Table

To verify the cards RFID number, open a terminal session and start swiping the cards. The cards ID numbers should appear at the command line  (and your system won’t know what to do with the numbers):


pi@raspberrypi:~ $ 0006241052
bash: 0006241052: command not found
pi@raspberrypi:~ $ 0003617750
bash: 0003617750: command not found

The next step is to create a simple text file (card_id.txt) with the valid RFID card numbers:

0006241052
0003617750
...

Create a Test Setup

For a simple test setup we used some Lego to security the card reader, Pi and a small breadboard.

setup

A simple LED circuit was used, with the LED wired to GPIO 7 (BCM pin 4), and the other side to GND.

circuit

Python Application

A simple Python application can be written to:

  • read the RFID card that is swiped
  • check it against a lookup fire (card_ids.txt)
  • if the card is valid do some action (like toggle an LED).
#
# swipecard.py - read swipecard and toggle a Pi output
#
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LEDpin = 4
GPIO.setup(LEDpin,GPIO.OUT)
while True:
print "Swipe RFID card to toggle I/O"
card_id = raw_input()
print "card id is:", card_id
id_file = open("card_ids.txt","r")
for line in id_file:
if (card_id == line.strip()):
GPIO.output(LEDpin, not GPIO.input(LEDpin))
print "Valid Card"