Streaming Video Server

There are some reasonably priced video surveillance products available on the market. If however you have a USB Web Cam and a Raspberry Pi (or equivalent or an old PC) you can make your own.

If you are working in the Unix world I would recommend motion , it’s super easy to setup and it’s got lots of added features if you want to turn them on. To install:

sudo apt-get install motion

Once you have motion installed you’ll need to tweek some of it’s parameters by:

sudo nano /etc/motion/motion.conf

The /etc/motion/motion.conf file contains a lot of cool parameters to tweek, some of the more important ones are:

# Image width (pixels). Valid range: Camera dependent, default: 352
width 800

# Image height (pixels). Valid range: Camera dependent, default: 288
height 600

# Maximum number of frames to be captured per second.
framerate 1

# Maximum framerate for stream streams (default: 1)
stream_maxrate 1

# Restrict stream connections to localhost only (default: on)
stream_localhost off

The speed of your hardware and network will determine how many frames per second you can use.

To run the video server enter:

sudo motion

The motion package has a built in web server that is accessed by: http://your_ip:8081

livevideo

Hardware Setup

There are lots of options for the mounting of your video server. I like to use Lego Pi cases about ($8) and then use some Lego to secure the rest of the components. Below is an example where I used a PIR (Pyroelectric Infrared) module ($3) to manually turn the motion software on/off, and I added a USB card reader to enable/disable the system.

OLYMPUS DIGITAL CAMERA
Homemade Security System

Performance and Tuning

Ideally it would be nice to run at 24 frames per second (or better) to get smooth video action. Unfortunately I found that the Raspberry Pi 3 would often freeze up at this refresh rate with a USB camera. The linux command line tool vmstat can be useful to show your CPU and I/O status. Below is some sample vmstat output:

$ vmstat # Pi 3 video server at 24 fps
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r b swpd free buff cache si so bi bo in cs us sy id wa st
 1 0 0 593552 17808 269856 0 0 41 57 803 180 7 1 74 18 0

The id is the idle time (74%), which isn’t bad. The problem however id wa, waiting for I/O.

You will have to do some trial and error to get a refresh rate that works best for you.

Port Forwarding

With the basic setup you will be able to access the video server from your local LAN/WAN. If however you want to access your video server from the Internet then you will need to allow a connection through your router to your video server, this is called port forwarding.

Unfortunately the configuration of port forwarding will vary from manufacturer to manufacture, so please see the documentation for your specific devices.

 

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

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"

 

 

Android Controlled Gondola

fullsetupUsing the Lego Mindstorms NXT robotics kit we built a remote control gondola. The MIT App Inventor package was used to create an Android phone app that controlled the gondola.

For this project we used:

• 1 Lego Mindstorms NXT brick with 1 motor and cable
• 1 Android phone
• lots of Lego bricks
• some string
• 2 adjustable mens or womens belts

The Lego Mindstorms motor should be connected in either the B or C connection on the brick.

You will need to make two gondola platforms. One with the Lego Mindstorms brick and motor, and a second platform with a wheel that can spin freely. You can have a lot of fun creating different gondola platforms. We found that some of the important points in the construction to be:

the platforms needed to be firmly secured. We put our platforms on chairs and we used belts to stop them from moving (Figure 2). A carpeted floor will help stop the chairs from slipping.

Small gondola. It’s tempting to want to make a big gondola, but we found that big gondolas had more string slippage and you had to have the gondola stations closer together.

Use wheels with grooves or rims. If you use wheels with grooves or rims the gondola string is less likely to slip out.station2

gondola

The Lego NXT brick needs to have Bluetooth turned on. Check the top left corner of the Brick’s screen to see if the Bluetooth symbol is showing. If it is not showing go into the Bluetooth options and turn visibility on.

For the Bluetooth pairing the Lego NXT needs to be turned on and your Android phone will need to have Bluetooth enabled. From your Android Bluetooth setting select “Search for Devices”. The Lego NXT brick will appear as NXT on your Android phone. On the phone select “pair with device”, and the phone pairing dialog will come up. The pairing request dialog may look different for different versions of Android, but the key is to enter the correct pairing PIN.

When the pairing is started the NXT will beep and prompt you for the pairing passkey. For the pin code (passkey) use the default of: 1234, and finish with the check mark. The pin code of 1234 is used on both the NXT and on the Android phone.

AppInventor has a set of components that will talk to the Lego Mindstorm NXT (or EV3) bricks. Below are pictures showing the our screen layouts and the key code elements.

blocks

app_layout

Wii Controlled Lego Rover

Use your Raspberry Pi (or a Linux PC) to talk to a Lego NXT rover and then use a Wii remote to drive the rover.

Python Libraries

There are 2 sets of libraries that we used:

  • nxt-python : to talk to the Lego Mindstorm NXT
  • cwiid : to talk to Wii remotes

To install these libraries:

wget https://nxt-python.googlecode.com/files/nxt-python-2.2.2.tar.gz
tar -zxvf nxt-python-2.2.2.tar.gz
cd nxt*
sudo python setup.py install
sudo apt-get install python-cwiid

The Lego NXT brick needs to have Bluetooth turned on. Check the top left corner of the Brick’s screen to see if the Bluetooth symbol is showing. If it is not showing go into the Bluetooth options and turn it on.

nxt_bt

The next step is to ensure that the Raspberry Pi can see the Lego NXT. The Pi can scan Bluetooth devices with the command:

hcitool scan

If the Bluetooth is working then you will see the Bluetooth address of the NXT., (for example: 00:16:53:04:23:3D). Using the NXT’s Bluetooth address you can pair the Pi and the Lego NXT by:

sudo bluez-simple-agent hci0 00:16:53:04:23:3D

For the pin code use the default of: 1234
The NXT will beep and prompt you for the pairing passkey.

nxt_pair

After the Raspberry Pi is paired with the Lego NXT brick you are able to use Python to read the NXT sensors and control the motors. No NXT coding is required. In the Python NXT directory there are some examples, mary.py  is a good test example because it does not require any sensors or motors.

Our full Python code to control the NXT Rover with a Wii remote is below:

import cwiid
import time
import nxt.locator
from nxt.motor import *

print 'Looking for a Lego NXT ... may take up to 15 seconds...'
b = nxt.locator.find_one_brick()
print 'Lego NXT Connected'

left = Motor(b, PORT_B)
right = Motor(b, PORT_C)

print 'Press 1+2 on your Wiimote now...'
wii = cwiid.Wiimote()
time.sleep(1)
wii.rpt_mode = cwiid.RPT_BTN 
print 'WII Remote Connected'

while True:
 buttons = wii.state['buttons']
 if (buttons & cwiid.BTN_LEFT):
 right.brake()
 left.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_RIGHT):
 left.brake()
 right.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_UP):
 right.run(power = 100,regulated=False)
 left.run(power = 100,regulated=False)
 time.sleep(0.1)

if (buttons & cwiid.BTN_DOWN):
 left.brake()
 right.brake()
 time.sleep(0.1)

 

 

littleBits Rover

Create a remote control littleBits rover using the littleBits wireless transmitter (w12) Bit ($40) and the wireless receiver (w11) Bit ($40). These wireless bits can also be  found in some of the littleBits kits, (for example the Gizmo and Gadgets kit).

The wireless modules can pass up to 3 signals. Depending on what you are trying to remotely control you could mix and match between dimmers, buttons and toggle switches. If you add some littleBits proto modules you could come up with some interesting designs.

Step 1 : Build the Remote

To create the wireless remote for the a Rover we needed to control the left and right motors , for this we used the following components:

  •  1- littleBits mounting plate (comes with base set)
  • 1 – littleBits fork module ($12)
  • 2 – littleBits dimmer($8) or slide switches ($10)
  • 1 – littleBits wireless transmitter
  • 1-  littleBits power module with battery ($6)

We connected the power module to a fork module and then the fork module powered our dimmer modules. We mounted all the components on a littleBits mounting plate, and we taped the battery to the back.

frontremote

backremote

The littleBits motor circuit is quite simple, the only important thing is to ensure that you have the wheels turning in the same direction.

motor1.png