Category Archives: Raspberry Pi

Matters relating to the development of Raspberry Pi applications, hardware and software

Machine Vision with a Raspberry Pi

In this blog we will describe the steps needed to do some machine vision using the Raspberry Pi Zeros we described in the earlier blog. Here at Cranfield University we are building these amazing devices into our research. In this case we are interested in using the Pi as a device for counting pedestrians passing a site – trying to understand how different design choices influence people’s choice of walking routes.

Contents:
Background
Toolkits
Kerberos
   Installation of Kerberos
   Configuration of Kerberos
   Configuration of the Pi
   Output and Data Capture from Kerberos
Epilogue

Background:

top
In the earlier blog we showed how to set up the Raspberry Pi Zero W, connecting up the new v2 camera in a case and connecting power. Once we had installed Rasbian on a new microSD card all was ready to go.

A bit of research was needed to understand the various options for machine vision on a Pi. There are three levels we might want. First a simple motion detection with the camera would give a presence or absence of activity, but not much more. This could be useful when pointing the camera directly at a location. Second, we can use more sophisticated approaches to consider detecting movement passing across the camera’s view, for example left to right or vice versa. This could be useful when pointing the camera transverse to a route along which pedestrians are travelling. Thirdly, and with the ultimate sophistication, we could try and classify the image to detect what the ‘objects’ passing across the view are. Classifier models might for example detect adults, young persons, and other items such as bicycles and push buggies etc. Needless to say, we wanted to start off easy and then work up the list!

Looking at the various software tools available, it is clear that many solutions draw on OpenCV (Open Source Computer Vision Library) (https://opencv.org). OpenCV is an open source computer vision and machine learning software library, built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception. There are many other potential libraries for machine vision – for example, SOD (https://sod.pixlab.io), and other libraries such as Dlib (http://dlib.net). OpenCV can be daunting, and there are wrappers such as SimpleCV (http://simplecv.org) to try and simplify the process.

Toolkits:

top
We then looked at options for toolkits that use these basic building blocks. A useful reference is Jason Antman’s blog here https://blog.jasonantman.com/2018/05/linux-surveillance-camera-software-evaluation/. Although not Jason’s final choice, the tool that stuck out to us was Kerberos (https://kerberos.io), developed by Cedric Verstraeten and grown out of his earlier OpenCV project (https://github.com/cedricve/motion-detection).

Kerberos:

top
Kerberos has a number of key resources:
Main home website – https://kerberos.io
Documentation – https://doc.kerberos.io
Git – https://github.com/kerberos-io
Helpdesk – https://kerberosio.zendesk.com
Corporate – https://verstraeten.io

Although the full source for Kerberos is available, and also a docker implementation, what we really liked was the SD image for the Raspberry Pi Zero – so really made for the job.

Installation of Kerberos:

top
We downloaded the cross-platform installer from the Kerberos website. This is based on the Etcher tool, used to install Rasbian so familiar to any Pi user. In our case we selected the Mac installer, downloading an installer dmg file (c.80Mb). Then, ensuring the Micro SD card destined for the Pi was in a flash writer dongle attached to the Mac, we were able to easily install the image. The Etcher app asks a couple of questions on the way about the WiFi network SSID and WiFi and system passcodes, as well as a name for the device, and writes these details onto the SD card with the rest of the image. As a result, on inserting the SD card and booting the Pi with the Kerberos image, the device started up and connected correctly and without issue on the WiFi network. A check on the router on our closed network showed the device had correctly registered itself at IP address 192.168.1.24.

Management of the Pi and camera is achieved via app running on a web server on the Pi. So to access our device, we entered browsed the URL http://192.168.1.24/login.

Configuration of Kerberos:

top
The dashboard app provides complete control over the operation of the Pi and camera.The image here shows the ‘heatmap’ camera view, and statistical graphs and charts of timings of activations.
. To configure the many settings we headed over to https://doc.kerberos.io for the documentation. The concept is that the image processing is undertaken on the ‘Machinery’ configuration, and that the ‘Web’ then controls access to the results.

Selecting ‘Configuration’ we could start adjusting the settings for the Machinery as we required. There are default settings for all the options.
However, the settings you will use depend on the application for the device. We followed the settings for ‘People Counter‘ recommended both in the docs, and a subsequent blog. It seems that there the settings are very sensitive, so one has to adjust until the desired results are obtained.

Being on a Raspberry Pi, one can also ssh connect directly to the device on a terminal connection (eg from terminal on the Mac, or via Putty from a PC). Connect to the device with the command:

ssh root@192.168.1.22
cd /data/machinery/config

This takes you to the location of the configuration files, as written out by the web app. Below are the settings we used to get the People Counter working (the values here correspond to the settings in the web app).

less config.xml
<?xml version="1.0"?>
<kerberos>
    <instance>
        <name type="text">Dream</name>
        <logging type="bool">false</logging>
        <timezone type="timezone">Europe-London</timezone>
        <capture file="capture.xml">RaspiCamera</capture>
        <stream file="stream.xml">Mjpg</stream>
        <condition file="condition.xml" type="multiple">Time</condition>
        <algorithm file="algorithm.xml">BackgroundSubtraction</algorithm>
        <expositor file="expositor.xml">Hull</expositor>
        <heuristic file="heuristic.xml">Counter</heuristic>
        <io file="io.xml" type="multiple">Script,Disk</io>
        <cloud file="cloud.xml">S3</cloud>
    </instance>
</kerberos>
less capture.xml
<?xml version="1.0"?>
<captures>
    <IPCamera>
        <url type="text">rtsp://admin:888888@192.168.0.13/tcp/av0_1</url>
        <frameWidth type="number">640</frameWidth>
        <frameHeight type="number">480</frameHeight>
        <delay type="number">500</delay>
        <angle type="number">0</angle>
    </IPCamera>
    <USBCamera>
        <frameWidth type="number">640</frameWidth>
        <frameHeight type="number">480</frameHeight>
        <deviceNumber type="number">0</deviceNumber>
        <fourcc type="text">MJPG</fourcc>
        <delay type="number">500</delay>
        <angle type="number">0</angle>
    </USBCamera>
    <RaspiCamera>
        <frameWidth type="number">640</frameWidth>
        <frameHeight type="number">480</frameHeight>
        <delay type="number">0</delay>
        <angle type="number">0</angle>
        <framerate type="number">30</framerate>
        <sharpness type="number">0</sharpness>
        <saturation type="number">0</saturation>
        <contrast type="number">0</contrast>
        <brightness type="number">50</brightness>
    </RaspiCamera>
    <VideoCapture>
        <frameWidth type="number">640</frameWidth>
        <frameHeight type="number">480</frameHeight>
        <path type="text">0</path>
        <delay type="number">500</delay>
        <angle type="number">0</angle>
    </VideoCapture>
</captures>
less stream.xml
<?xml version="1.0"?>
<streams>
    <Mjpg>
    	<enabled type="bool">true</enabled>
    	<streamPort type="number">8889</streamPort>
    	<quality type="number">75</quality>
    	<fps type="number">30</fps>
    	<username type="text"></username>
    	<password type="text"></password>
    </Mjpg>
</streams>
less condition.xml
<?xml version="1.0"?>
<conditions>
    <Time>
        <times type="timeselection">7:00,18:30-7:00,18:30-7:00,18:30-7:00,18:30
-7:00,18:30-7:00,18:30-7:00,18:30</times>
        <delay type="number">10000</delay>
    </Time>
    <Enabled>
    	<active type="bool">true</active>
        <delay type="number">5000</delay>
    </Enabled>
</conditions>
less algorithm.xml
<?xml version="1.0"?>
<algorithms>
    <DifferentialCollins>
        <erode type="number">5</erode>
    	<threshold type="number">15</threshold>
    </DifferentialCollins>
	<BackgroundSubtraction>
		<shadows type="text">false</shadows>
		<history type="number">5</history>
		<nmixtures type="number">5</nmixtures>
		<ratio type="number">1</ratio>
		<erode type="number">6</erode>
		<dilate type="number">15</dilate>
    	<threshold type="number">20</threshold>
    </BackgroundSubtraction>
</algorithms>
less expositor.xml
<?xml version="1.0"?>
<expositors>	
	<Rectangle>
	    <region>
		    <x1 type="number">0</x1>
		    <y1 type="number">0</y1>
		    <x2 type="number">640</x2>
		    <y2 type="number">480</y2>
		 </region>
	</Rectangle>
	<Hull>
	    <region type="hullselection">640,286|640,243|539,223|332,226|184,22
9|188,323|188,392|210,478|640,480|640,316|640,308|640,297</region>
	</Hull>
</expositors>
less heuristic.xml
<?xml version="1.0"?>
<heuristics>
	<Sequence>
	    <minimumChanges type="number">20</minimumChanges>
	    <minimumDuration type="number">2</minimumDuration>
        <noMotionDelayTime type="number">1000</noMotionDelayTime>
	</Sequence> 
	<Counter>
	    <appearance type="number">3</appearance>
	    <maxDistance type="number">400</maxDistance>
	    <minArea type="number">25</minArea>
	    <onlyTrueWhenCounted type="bool">true</onlyTrueWhenCounted>
	    <minimumChanges type="number">15</minimumChanges>
        <noMotionDelayTime type="number">0</noMotionDelayTime>
		<markers type="twolines">211,281|213,442|396,284|397,439</marke
rs>
	</Counter>
</heuristics>

Note the settings above for the twolines markers on the video image – used for counting pedestrians passing from left to right, and from right to left, (coordinate position 0,0 is the top left corner)

less io.xml
<?xml version="1.0"?>
<ios>
    <Disk>
        <fileFormat type="text">timestamp_microseconds_instanceName_regionCoord
inates_numberOfChanges_token.jpg</fileFormat>
        <directory type="text">/etc/opt/kerberosio/capture/</directory>
        <markWithTimestamp type="bool">true</markWithTimestamp>
        <timestampColor type="text">white</timestampColor>
        <privacy type="bool">false</privacy>
        <throttler type="number">0</throttler>
    </Disk>
    <Video>
        <fps type="number">30</fps>
        <recordAfter type="number">5</recordAfter>
        <maxDuration type="number">30</maxDuration>
        <extension type="number">mp4</extension>
        <codec type="number">h264</codec>
        <fileFormat type="text">timestamp_microseconds_instanceName_regionCoord
inates_numberOfChanges_token</fileFormat>
        <directory type="text">/etc/opt/kerberosio/capture/</directory>
        <hardwareDirectory type="text">/etc/opt/kerberosio/h264/</hardwareDirec
tory>
        <enableHardwareEncoding type="bool">true</enableHardwareEncoding>
        <markWithTimestamp type="bool">true</markWithTimestamp>
        <timestampColor type="text">white</timestampColor>
        <privacy type="bool">false</privacy>
        <throttler type="number">0</throttler>
    </Video>
    <GPIO>
        <pin type="number">17</pin>
        <periods type="number">1</periods>
        <periodTime type="number">100000</periodTime>
        <throttler type="number">0</throttler>
    </GPIO>
    <TCPSocket>
        <server type="number">127.0.0.1</server>
        <port type="number">1337</port>
        <message type="text">motion-detected</message>
        <throttler type="number">0</throttler>
    </TCPSocket>
    <Webhook>
        <url type="text">http://localhost/api/v1/webhook</url>
        <throttler type="number">0</throttler>
    </Webhook>
    <Script>
        <path type="text">/data/run.sh</path>
        <throttler type="number">0</throttler>
    </Script>
    <MQTT>
        <secure type="bool">false</secure>
        <verifycn type="bool">false</verifycn>
        <server type="number">127.0.0.1</server>
        <port type="number">1883</port>
        <clientId type="text"></clientId>
        <topic type="text">kios/mqtt</topic>
        <username type="text"></username>
        <password type="text"></password>
        <throttler type="number">0</throttler>
    </MQTT>
    <Pushbullet>
        <url type="text">https://api.pushbullet.com</url>
        <token type="text">o.mC5LPVCvPCphtSsEgWZQpFM86w9ciWQ3</token>
        <throttler type="number">10</throttler> 
    </Pushbullet>
</ios>

Configuration of the Pi:

top
Another configuration required was to tun off the bright green LED on the Raspberry Pi as it draws attention when the unit is operating. To turn OFF the LEDs for Zero, we followed the instructions at https://www.jeffgeerling.com/blogs/jeff-geerling/controlling-pwr-act-leds-
raspberry-pi. Note that unlike other Raspberry Pi models, the Raspberry Pi Zero only has one LED, led0 (labeled ‘ACT’ on the board). The LED defaults to on (brightness 0), and turns off (brightness 1) to indicate disk activity.

To turn off the LEDs interactively, the following commands can be run each time the Pi boots.

# Set the Pi Zero ACT LED trigger to 'none'.
echo none | sudo tee /sys/class/leds/led0/trigger
# Turn off the Pi Zero ACT LED.
echo 1 | sudo tee /sys/class/leds/led0/brightness

To make these settings permanent, add the following lines to the Pi’s ‘/boot/config.txt’ file and reboot:

# Disable the ACT LED on the Pi Zero.
dtparam=act_led_trigger=none
dtparam=act_led_activelow=on

Note the ‘/’filesystem is made read-only by default in the Kerberos build. To temporarily fix this to force read write for the root ‘/’ filesystem, type:

mount -o remount,rw /

Now the config.txt file can be edited normally, eg in the editor nano, and then the Pi can be rebooted.

cd /boot
nano config.txt
reboot

Output and Data Capture from Kerberos:

top
To obtain data from the tool, we are using the ‘script’ setting in io.xml, which runs the script ‘/data/run.sh’ (a bash script). This script just writes the data receives (a JSON structure) out to disk.

#!/bin/bash

# -------------------------------------------
# This is an example script which illustrates
# how to use the Script IO device.
#

# --------------------------------------
# The first parameter is the JSON object
#
# e.g. {"regionCoordinates":[308,250,346,329],"numberOfChanges":194,"timestamp":"1486049622","microseconds":"6-161868","token":344,"pathToImage":"1486049622_6-161868_frontdoor_308-250-346-329_194_344.jpg","instanceName":"frontdoor"}

JSON=$1

# -------------------------------------------
# You can use python to parse the JSON object
# and get the required fields

echo $JSON >> /data/capture_data.json

coordinates=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['regionCoordinates']")
changes=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['numberOfChanges']")
incoming=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['incoming']")
outgoing=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['outgoing']")
time=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['timestamp']")
microseconds=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['microseconds']")
token=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['token']")
instancename=$(echo $JSON | python -c "import sys, json; print json.load(sys.stdin)['instanceName']")

printf "%(%m/%d/%Y %T)T\t%d\t%d\t%d\t%d\n" "$time" "$time" "$changes" "$incoming" "$outgoing" >> /data/results.txt

Note the use of the parameters to convert the Julian timestamp to a readable date/time.

When an event triggers the system (someone walking past the camera view) two actions follow, an image is saved to disk, and the script is run, with a parameter of the JSON structure. The script then processes the JSON. The script here both writes out the whole JSON structure to a the file ‘capture_data.json’ (this is included as a debug and could be omitted), and also extracts out the data elements we actually wanted and writes these to a CSV file called ‘results.txt’.

A sample of ‘capture_data.json’ look like this:

{"regionCoordinates":[413,323,617,406],"numberOfChanges":1496,"incoming":1,"outgoing":0,"name":"Dream","timestamp":"1539760397","microseconds":"6-928567","token":722,"instanceName":"Dream"}
{"regionCoordinates":[190,318,636,398],"numberOfChanges":2349,"incoming":1,"outgoing":0,"name":"Dream","timestamp":"1539760405","microseconds":"6-747074","token":814,"instanceName":"Dream"}
{"regionCoordinates":[185,315,279,436],"numberOfChanges":1793,"incoming":0,"outgoing":1,"name":"Dream","timestamp":"1539760569","microseconds":"6-674179","token":386,"instanceName":"Dream"}

A sample of ‘results.txt’ looks like this:

10/17/2018 08:17:08	1539760628	917	0	1
10/17/2018 08:17:18	1539760638	690	0	1
10/17/2018 08:18:56	1539760736	2937	0	1
10/17/2018 08:19:38	1539760778	3625	1	0
10/17/2018 08:22:05	1539760925	1066	1	0
10/17/2018 08:24:06	1539761046	2743	0	1
10/17/2018 08:24:45	1539761085	1043	1	0
10/17/2018 08:26:11	1539761171	322	0	1

Epilogue:

top
This blog has shown how the Kerberos toolkit has been used with an inexpensive Raspberry Pi for detecting motion and also directional movement across the camera view. The data captures a JSON data structure for each event triggered, and a script extracts from this the data required, which is saved off to disk for later use.

There are still issues to grapple with – for example reduce false positives, and perhaps more importantly not missing events as they occur. The settings of the configuration machinery are very sensitive. The best approach is to successively vary these settings (particularly the expositor and heuristic settings) until the right result is obtained. Kerberos has a verbose setting for event logging, and inspecting the log with this switched on reveals that the Counter conditions are very sensitive – so many more people may be walking past the camera than are being directly logged as such (e.g. motion activations may be greater than count events).

The commands below show how to access the log – it is also shown in the ‘System’ tab of the web dashboard. The command ‘tail -f’ is useful as it shows the log update in real time – helpful if the video live feed screen is being displayed alongside on-screen. Then you can see what is and isn’t being logged very easily.

cd /data/machinery/logs
tail -f log.stash

Ultimately, the Raspberry Pi may not have enough power to operate full classifier models, such as that developed by Joseph Redmon with the Darkweb YOLO tool he developed (‘You Only Look Once’) (https://pjreddie.com/darknet/yolo/). However, Kerberos itself has a cloud model that provides post-processing of images in the cloud on AWS servers, with classifier models available – perhaps something to try in a later blog.

Setting up a Raspberry Pi Zero W

Here on GeoThread, we have taken delivery of one of the new Raspberry Pi Zeros for some projects – so here is a blog on how we set it all up for some of the work we get up to at Cranfield University. We’ve had quite a few projects with these devices over the years – reported here on GeoThread, starting back in 2016 with the Raspberry Pi 3, with various tutorials presented. The Raspberry Pi Zero is the latest in the line of these excellent, inexpensive microcomputers, see https://www.raspberrypi.org/products/raspberry-pi-zero/. Following on from the Pi A and B series devices, the Zero is a great entry level machine for learning coding and software development. We have a Raspberry Pi Zero W – the ‘W’ meaning it also has wireless and Bluetooth connectivity.

To get us going, we bought a kit that contained not only the Pi, but also an acrylic case and a power supply. There are many such kits available – we selected the Vilros offering. We also bought the new Raspberry Pi Camera v2 to fit in the case.

Raspberry Pi Zero, Vilros Starter Kit

Note that we have also bought (shown at the bottom centre), an HDMI Mini Type C (male) to HDMI Normal Type A (female) Monitor Cable Display Adapter – the Raspberry Pi Zero only has a HDMI Mini socket. In addition, you may also need to buy a single (or dual) Micro USB Male To USB 2.0 Female OTG (USB On the Go) cable for connecting a USB keyboard (or a USB keyboard and mouse). The HDMI and USB cables/adapters are to be used to get the unit up and running in the first instance, using a keyboard, mouse and monitor. Once the configuration is complete and the WiFi is connected, we will run the Raspberry Pi remotely and so disconnect the keyboard and screen.

We also bought one of the new ‘version 2’ cameras for the Raspberry Pi. This is an 8MPixel camera made by Sony, and a huge improvement over the original camera. It can fit into the case. The case has three alternative lids – shown here is the version with the camera mounting and aperture built in.

Raspberry Pi Zero and unboxing the camera v2

One thing to note immediately is that the Raspberry Pi Zero has a different camera cable connector size to the earlier models of the Pi. This means that you will need to obtain a new ribbon cable connector to replace the one provided. You can easily buy these separately, but the Vilros kit (like other kits) thoughtfully provide this.

Raspberry Pi Zero Camera v2 and case

Raspberry Pi Zero camera socket

The camera fixing has a small plastic bar that can be gently prised outwards to allow the old connector ribbon cable to be slipped out. The new cable can then be slotted in and the plastic bar pushed back into place to secure it. Be sure to insert the cable the right way up to allow contact.This is a delicate operation, but the cable is reasonably robust.

Raspberry Pi Zero Camera v2 ribbon cables

In the image above, the old connector has been removed, and the new one inserted. Note the new connector is very short, allowing it to be fitted inside the case. We are nearly ready to assemble the parts together in the case.

Raspberry Pi Zero Case Camera v2 – ready for fitting

The camera, with its new connector, can now be fitted to the Raspberry Pi Zero. The Zero has another plastic clip for slotting in the ribbon cable. As before, prise away the locking bar and gently insert the cable. When tightly fitted (the right way up to allow contact again), the bar can be pushed back in to grip the cable in place.

Raspberry Pi Zero Case Camera v2 – fitting the camera into the case

Once fitted, the case can be put together. Note that the Vilros kit comes with a small heatsink that can be fitted to the Pi to cool the 1GHz ARM CPU chip. However, with the camera fitted in the case, there is no room for the heatsink also. Time will tell if that is an issue!

Raspberry Pi Zero Case and Camera v2, fully Assembled

The Vilros kit comes with the power supply, fitted with the customary MicroUSB plug.

Raspberry Pi Zero Power Supply

Note that the power socket is the socket to the right, as shown.

Raspberry Pi Zero sockets

Before the case can be clicked into place, there is one last thing needed – the operating system. This is installed on an SD card.
We followed the very clear instructions on the Raspberry Pi website, here https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/3. We downloaded the latest Raspbian img file and flashed it onto a new Micro-SD card. To do this on a MacBook Pro, we used the Etcher app (v1.4.4).

SD card Flashing with Etcher

Once the Micro SD card was flashed (e.g. the img file was copied onto it with Etcher), we could insert it carefully into the SD socket on the Raspberry Pi Zero. At this point the case was all clipped together.
We are now ready for the next stage of the project!

IOT Project – Using an ESP32 device to monitor a web service

There is a lot of interest in the Internet of Things here at Cranfield University. Especially now there is a new generation of super-cheap ESP8266 and ESP32 devices which can be deployed as IoT controllers. Many of these devices are now also available with in-built OLED screens – very helpful for showing messages and diagnostics.

We will use one of these devices to develop our project.

Contents
The project
The device
Coding the EPS32
Configuring the development environment
Connecting to the device
USB drivers
OLED Screen drivers
Configuring the Arduino IDE
Uploading the Source Code
The Source Code
– – Authorisation
In operation
Next steps

The project

top
Nowadays, web services are used for all sorts of applications – for providing access to data and functionality online. A useful application then for the EPS32 is for it to act as a monitor for a web service, repeatedly polling the service to see if it is operating correctly. If the web service goes down, we need to know – the EPS32 can keep an eye on the service and report any problems.

This project describes how an EPS32 device can be configured and programmed to monitor a web service. The web service we will monitor is developed (in node.js) with an API that includes a ‘current status’ call – if all is well calling this returns a success message which we can capture.

The device

top
For this project, we are using the TTGO-WiFi-Bluetooth-Battery-ESP32-Module-ESP32-0-96-inch-OLED-development-tool from Aliexpress, although these devices are widely available from many retailers. This particular model also has a battery holder for a long-use LIPO battery on the rear of the board.

Coding the EPS32

top
There are a few options for coding the ESP devices. Most easily, it is possible to use the Arduino development environment (with a few tweaks). Another possibility is using the Atom implementation at platformio: https://platformio.org/platformio-ide. We used the Arduino tool.

Configuring the development environment

top
The Arduino development environment by default does not have the libraries and configurations to allow it to programme the EPS32. There are a few steps needed to enable this.

Connecting to the device

top
The EPS32 board has a micro-USB port, permitting connection to the programming computer. We used an Apple Mac laptop for programming – so a micro-USB to USB-C cable/convertor was required.

USB drivers

top
The EPS32 device needs a software driver installed on the programming computer. We used the Silicon Labs drivers available online here. There are other alternatives (some commercial) for drivers – notably the Mac-usb-serial drivers online.

OLED Screen drivers

top
The EPS32 device also has an in-built OLED screen. Although very small, at 128*64 pixels, this mono display is quite large enough to show text messages with different fonts, simple bitmap graphics, progress bars and drawing elements (lines, rectangles etc.) – amazing! However, a library is needed to allow access to this device. We used the ThingPulse OLED library.

The library can be downloaded as a zip file from GitHub to the library folder in the Arduino installation folder. On the Mac for example this is:

/Users/USER/Documents/Arduino/libraries/esp8266-oled-ssd1306-master


This library comes with lots of example programmes showing how to programme the screen, how to encode bitmaps, add progress bars, create fonts etc. From the code below, it can be seen that the Sketch ‘SSD1306SimpleDemo.ino‘ offers a great starting point for learning, referencing for example the ways described at Squix for encoding images and fonts. I selected the Roboto Medium font and encoded the WiFi graphic (using this online tool).
When completed, the two header files for fonts and images respectively were:
fonts header file – fonts.h
images header file – images.h

Configuring the Arduino IDE

top
Configuring the Arduino IDEHaving installed these drivers and libraries, the Arduino IDE then needs to be configured.

To do this, the board was set as a device of type ‘ESP32 Arduino’ -> ‘ESP32 Dev Module’, the baud rate set to 115200. Having installed the USB driver above, the port could be set to ‘/dev/cu.SLAB_USBtoUART’.

Uploading the Source Code

top
The Arduino IDE allows one to compile and upload code to the device. Critically, one has to hold down the ‘Boot’ button on the device as the programme is uploaded (for a few seconds) to allow the device code to be uploaded. If the boot button is NOT held down, there will be errors reported and the code will not be uploaded! (this took ages to work out!!)

The Source Code

top
In coding the device in the Arduino development environment, one can refer usefully to the Arduino code reference. The final working code is shown below. Note the calls to the Serial monitor to allow debugging information to be shown while the device is connected to the computer.

// TTGO WiFi &amp; Bluetooth Battery ESP32 Module - webservices checker
// Import required libraries
#include "Wire.h"
#include "OLEDDisplayFonts.h"
#include "OLEDDisplay.h"
#include "OLEDDisplayUi.h"
#include "SSD1306Wire.h"
#include "SSD1306.h"
#include "images.h"
#include "fonts.h"
#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
// The built-in OLED is a 128*64 mono pixel display
// i2c address = 0x3c
// SDA = 5
// SCL = 4
SSD1306 display(0x3c, 5, 4);

// WiFi parameters
const char* ssid = "MYSSID";
const char* password = "MYWIFIKEY";

// Web service to check
const int httpPort = 80;
const char* host = "MYWEBSERVICE_HOSTNAME";

void setup() {
	// Initialize the display
	display.init();
	//display.flipScreenVertically();
	display.setFont(Roboto_Medium_14);

	// Start Serial
	Serial.begin(115200);
	// Connect to WiFi
	display.drawString(0, 0, "Going online");
	display.drawXbm(34, 14, WiFi_Logo_width, WiFi_Logo_height, 			 WiFi_Logo_bits);
	display.display();
	WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED) {
	 	 delay(500);
	 	 Serial.print(".");
	}
	Serial.println("");
	Serial.println("WiFi now connected at address");
	// Print the IP address
	Serial.println(WiFi.localIP());
	display.clear();
}

void loop() {
	Serial.print("\r\nConnecting to ");
	Serial.println(host);
	display.clear();
	display.setTextAlignment(TEXT_ALIGN_LEFT);
	display.drawString(0, 0, "Check web service");
	display.display();
	Serial.println("Check web service");

	// Setup URI for GET request
	String url = "SPECIFIC_WEBSERVICE_URL";
	// if service is up ok, return string will contain: 'Service running'

	WiFiClient client;
	if (!client.connect(host, httpPort)) {
		Serial.println("Connection failed");
		display.clear();
		display.drawString(0, 0, "Connection failed");
		display.display();
		return;
	}

	client.print("GET " + url + " HTTP/1.1\r\n");
	client.print("Host: " + (String)host + "\r\n");
	// If authorisation is needed it can go here
	//client.print("Authorization: Basic AUTHORISATION_HASH_CODE\r\n");
	client.print("User-Agent: Arduino/1.0\r\n");
	client.print("Cache-Control: no-cache\r\n\r\n");

	Serial.print("GET " + url + " HTTP/1.1\r\n");
	Serial.print("Host: " + (String)host + "\r\n");
	// If authorisation is needed it can go here
	//Serial.print("Authorization: Basic AUTHORISATION_HASH_CODE\r\n");
	Serial.print("User-Agent: Arduino/1.0\r\n");
	Serial.print("Cache-Control: no-cache\r\n\r\n");

// Here's an alternative form if the service API uses HTTP POST
/*
client.print("POST " + url + " HTTP/1.1\r\n");
client.print("Host: " + (String)host + "\r\n");
// If authorisation is needed it can go here
//client.print("Authorization: Basic AUTHORISATION_HASH_CODE\r\n");
client.print("User-Agent: Arduino/1.0\r\n");
client.print("Cache-Control: no-cache\r\n\r\n");
*/

	// Read all the lines of the reply from server
	delay(500);
	bool running = false;
	while (client.available()) {
		String line = client.readStringUntil('\r\n');
	 	Serial.println(line);
	 	if (line == "Service running") {
	 		running = true;
		}
	}
	if (running == true) {
		display.drawString(0, 25, "Service up OK");
	 	display.display();
		delay(3000);
	} else {
	 	display.drawString(0, 25, "Service DOWN");
	 	display.display();
	 	delay(3000);
		// Text/email administrator
	}

// Here's some alternative methods to read web output
/*
while (client.available()) {
	 Serial.print("&gt;");
	 char c = client.read();
	 Serial.print(c);
	 Serial.print("&lt;");
}
*/
/*
int c = '\0';
unsigned long startTime = millis();
unsigned long httpResponseTimeOut = 10000; // 10 sec
while (client.connected() &amp;&amp; ((millis() - startTime) &lt; 	 	 	 httpResponseTimeOut)) {
	 if (client.available()) {
	 	 c = client.read();
	 	 Serial.print((char)c);
	} else {
	 	 Serial.print(".");
	 	 delay(100);
	}
}
*/

	Serial.println();
	Serial.println("Closing connection");
	Serial.println("=================================================");
	Serial.println("Sleeping");
	display.clear();
	display.drawString(0, 0, "Closing connection");
	display.display();
	delay(1000);
	display.clear();
	client.stop();
	// progress bar
	for (int i=1; i<=28; i++) {
	 	float progress = (float) i / 28 * 100;
	 	delay(500); // = all adds up to delay 14000 (14 sec)
		// draw percentage as String
	 	display.drawProgressBar(0, 32, 120, 10, (uint8_t) progress);
	 	display.display();
	 	display.setTextAlignment(TEXT_ALIGN_CENTER);
	 	display.drawString(64, 15, "Sleeping " + String((int) progress) + "%");
	 	display.display();
	 	display.clear();
	 	Serial.print((int) progress);Serial.print(",");
	}
	delay (1000);
}

Authorisation

top
Note that the connection to the service can use either HTTP GET or POST according to need (POST is considered a better approach). A further embellishment for security is if the web service uses authorisation (username and password to connect). If it does, then a hash of the combination of username and password can be passed in the header as shown in the code. To do this we use the excellent Postman tool. Postman allows one to manually create a connection conversation with an API server, including say a basic authorisation, and then view the full code of this – which can be copied into the Arduino code as shown above.

Note that it is critical to have a second carriage return at the end of the HTTP conversation (shown as ‘\r\n‘ in the code – so the last item has ‘\r\n\r\n’ for the blank line). Without this blank line it will not work!

In operation

top
Here is a short video of the device in operation. Excuse the use of image stabilisation – original video was filmed handheld.

The code is designed to open a connection, check on the status of the web service, then sleep for a period before repeating in an endless loop.

Next steps

top
This code currently only flashes up on the tiny screen when the service is found to be up or down. To be really useful, the tool should be able to alert one or more administrators – perhaps by push messaging to their mobile phones, or email.

The next stage can add this capability using approaches using Prowl, and Avviso. Perhaps the subject of a future blog posting.

Using SQLite on a Raspberry Pi

There is a lot of interest in the amazing Raspberry Pi 3 computer here at Cranfield University. Sometimes, an application we build – for example on the Raspberry Pi – needs to store data – for example we might wish to store data from sensors. For this we need a database. However, again sometimes the overhead of having a full-blown database server up and running is too much. We need a simple, localised way to store and retrieve data. Fortunately there is a great solution to this, ‘SQLite’ (https://www.sqlite.org).

This brief tutorial shows how to setup and configure SQLite on a Raspberry Pi, and gives and example if it in use.

To start with, as with every time we use the Pi, we should update and upgrade the distribution – to ensure all the software within the Debian distribution are up-to-date. As described on the Raspberry Pi pages (https://www.raspberrypi.org/documentation/raspbian/updating.md), this is pretty straight forward.

First, update your system’s package list by entering the following command:

sudo apt-get update

Next, upgrade all your installed packages to their latest versions with the command:

sudo apt-get dist-upgrade

Once this is done, we are ready to install SQLite:

sudo apt-get install sqlite3

We will use the database to store data from within programming code – for example a software application that stores off sensor data. However, in the first instance, we can also run SQLite interactively to ‘create’, ‘read’, ‘update’ and ‘delete’ data directly in SQL tables. SQLite comes with a command line interpreter (CLI), that provides a command prompt to enter in commands. If we create a new database, we can try this out:

sqlite3 sensor_db

This starts up SQLite and will, at the same time, also create a new database – a single file, in this case called ‘sensor_db’ in your current folder.

Now we can create a database structure in our new database:

CREATE TABLE IF NOT EXISTS readings (
'temperature' REAL NOT NULL,
'humidity' REAL NOT NULL,
'datetime_int' INTEGER NOT NULL,
'sensor_id' INTEGER NOT NULL);

Note the semi-colon ending the statement. Now we can check that was created OK:

PRAGMA table_info([readings]);

Let’s place a couple of dummy data items (rows) into the table:

INSERT INTO readings (temperature, humidity, datetime_int, sensor_id) values (18.5,45.3,strftime('%s','now'),1);
INSERT INTO readings (temperature, humidity, datetime_int, sensor_id) values (19.4,42.8,strftime('%s','now'),1);

Now select the data to make sure it was stored correctly. Note we are using one of the two means of storing dates, here using integers not strings (see http://www.sqlitetutorial.net/sqlite-date/).

SELECT temperature, humidity, datetime(datetime_int, 'unixepoch'), sensor_id FROM readings;

The data should be shown…

To quit the interactive mode:

.exit

SQLite is a powerful database solution for small applications. Programming it is just like its larger server-based equivalents. It is worth spending time reading the SQLite tutorials for further information (http://www.sqlitetutorial.net). Another good tutorial is https://www.tutorialspoint.com/sqlite/index.htm.

Connecting a Raspberry Pi to eduroam wifi

Raspberry Pi connected to eduroam wifi

Connecting to eduroam

One of the things we want to do here at Cranfield University, is to connect Raspberry Pi computers to the University WiFi network – called ‘Eduroam’. The Eduroam system is used by all universities in the UK. However, the built in WiFi on board the new Raspberry Pi 3 doesn’t seem to connect to the Eduroam WiFi network on campus, out of the box. Clicking on the WiFi icon in the top right of the Pi’s desktop shows the eduroam network as a greyed out option. On the Cranfield campus, the local networks ‘Cranfield Web’ and ‘Cranfield Setup’ are both available to join. Here are some instructions on how to connect the Pi to the Eduroam WiFi network.

Select Cranfield Web/Setup, open a browser and go to the web address http://cat.eduroam.org. You may be prompted for a username and password, your Cranfield University username and password should be supplied here.

Once at cat.eduroam.org, select Cranfield University from the list of institutions when prompted and then press the button titled ‘Linux’. This should download a small script which you should run. You can either run this by clicking the file once it has downloaded at the bottom of your browser or navigating to its location within your filesystem and running it from there.

If the script does not initially run (but simply opens in a text editor), you may need to add executable permissions to the downloaded script. Navigate to the location of your script in a terminal window and run the command:

sudo chmod u+x <filename>

When the scripts runs it will prompt you with some dialogue boxes that ask for your eduroam username and password. Your eduroam username should take the form <username>@cranfield.ac.uk (where <username> is your regular Cranfield username) and you should use your regular Cranfield password.

The script will eventually tell you that it was unable to update your network settings, however the important config we need will be stored in a .cat_installer folder in your home directory. Using a terminal window, navigate to this folder:

cd .cat_installer

 You will see a file cat_installer.conf. The contents of this file need to be copied and pasted into the file wpa_supplicant.conf which resides in /etc/wpa_supplicant/. Navigate to this folder:

cd /etc/wpa_supplicant

You will need to edit the wpa_supplicant.conf file using superuser permissions. You can edit this file using the text editor nano:

sudo nano wpa_supplicant.conf

Using your right mouse button (ctrl+v doesn’t work here), paste in the config that you copied earlier at the bottom of the file.

Save this file (in nano, press ctrl+O), then exit (ctrl+x).

You will now need to restart the system:

sudo shutdown –r now

When the system boots back up to desktop again, you should now be connected to eduroam. Note, clicking on the wifi icon in the top right of the screen will show that you are connected to eduroam, but it will still be greyed out. This is fine.

Be aware that this configuration will have your password stored in plaintext in the wifi configuration file. It is, therefore, essential that access to your Pi is configured with a secure password as anyone able to the sudo command on the Pi will be able to read this file. If you are planning for several people to have access to a terminal/ssh on your Pi, it is recommended you connect the device to the network via a wired ethernet connection.

This tutorial is one of the following set of related Raspberry Pi tutorials, in order:
1. Unboxing the Raspberry Pi 3 – Pi Society
2. Raspberry Pi 3: Hello World – Pi Society
3. Raspberry Pi 3: Operating LEDs – Pi Society
4. Making the Raspberry Pi work, the next steps – Pi Society
5. Connecting a Raspberry Pi to eduroam wifi

Making the Raspberry Pi work, the next steps – Pi Society

There is a lot of interest in the amazing Raspberry Pi 3 computer here at Cranfield University. Once you have the basics for the Raspberry Pi in place, there are a few tips and tricks you can follow to make the Pi work better for you, explained below. The assumption here is that you already have the Pi connected up to a monitor, and have a keyboard and mouse plugged in and are running Jessie Raspian (if not, see our earlier Pi tutorials). the topics covered here are:
Setting a system password | Getting WiFi running | Accessing the Pi from another computer | Moving files to and from the Pi from another computer – scp | Installing and updating software on the Pi | Setting aliases | Learning Python

Setting a system password

Top
The default account on the Pi is in fact username ‘pi’, and there is no password for the account by default. It is good practice to set a password, particularly once you activate WiFi for the Pi. To do this, Select Menu > Preferences > Raspberry Pi Configuration > System. Select the password option and enter in a secure password.

While you are at this dialogue box, you can explore the other options there – for example, under ‘Localisation’ you can set the Pi’s timezone appropriately.

Getting WiFi running

Top
WiFi on the Pi is very straight forward if you have a home router running WPA security. At the graphical interface, in the op right corner of the screen is the WiFi icon. Right mouse click the WiFi icon and select the first ‘WiFi Networks Settings’ option. In the Network Settings dialogue, in the ‘configure’ drop-down, select ‘SSID’ and search for your router, select it and press OK. If there is a WiFi password required, select the WiFi icon again, locate the connection you just made in the list and double click – a dialogue appears for you to enter in the password. Once done, hopefully the connection is made and you will be online.

To get the Pi to connect wirelessly to the campus WiFi network ‘EduRoam’ is rather more tricky – see https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=86253
Using the Jessie graphical user interface of the Pi, you should be able to follow the same procedure to get the Pi online with Eduroam. If you intend to do this however, you must ensure that users all have passwords set, and that the hostname is something other than ‘pi’!

Accessing the Pi from another computer

Top
To start with you have to connect a monitor and keyboard/mouse up to the pi to connect to it. However, ultimately you may want to have it running on its own, and so then be able to connect to it from a different computer – on the command line, or graphically.

Assuming the Pi is using WiFi and is online, find out what its IP address is. To do this, you can either consult the WiFi router’s web console to look for its connected clients, or you can type:
sudo ifconfig
In a home WiFi situation, the IP address will likely be something like ‘192.168.1.nn’ (e.g. 192.168.1.11).

From your other computer (e.g. a PC or Mac), you can now run an ‘ssh‘ (secure shell) client. To do this, on the PC you might use the excellent freeware tool ‘putty’; on a Mac you can fire up the terminal and at the prompt enter ‘ssh ‘. Make the connection by incorporating the pi user name (which by default is ‘pi’) into the IP address, thus in a mac terminal window for the user name ‘pi’, you would type:
ssh pi@192.168.1.11

Sometimes accessing via the command line is just not enough, and you may want to run an app with a graphical interface from another computer. If you use the secure shell (ssh) to log in to the Pi, you may be able to carry the ‘X-windows’ session over to the computer using the ‘-X’ parameter, thus:

ssh –X pi@192.168.1.11

On a Mac running Sierra, you may have to run with a ‘-Y’ instead, (assuming you have Quartz installed):

ssh –Y pi@192.168.1.11

On Windows computers, you can install and use Xming for the X-windows, and then use putty as noted above for ssh sessions. Putty has a dialogue tick box option to carry X11 over to the local session.

To test this works, install the standard set of X11 apps, then run say xclock, thus:

sudo apt-get install x11-apps
xclock

All the above can be a bit fiddly – and a much easier method is to use a VNC (Virtual Network Computing) server. In former times to do this you would install a software package called ‘tightvncserver’ – and there are lots of instructions out on the web to do this – and you can still do this if you wish. However, as from Sept 2016, the Raspberry Pi 3 now includes a VNC server by default, from RealVNC – (see https://www.realvnc.com/docs/raspberry-pi.html). Although installed it is not activated by default. To activate it, select Menu > Preferences > Raspberry Pi Configuration > Interfaces. Ensure the VNC tick box is Enabled and then reboot the Pi. From now on, VNC Server will start automatically whenever your Pi is powered on.

Now, on the computer you want to connect to the Pi, you will also need to install the RealVNC viewer. Visit ‘https://www.realvnc.com/download/viewer/‘, download and install the appropriate viewer. Note you will need to know and enter the IP address of the Pi, as well as your account and password.

Moving files to and from the Pi from another computer – scp

Top
Very often you will want to copy files to and from the Pi – here’s how:

If you have the ssh tools running as above, you can just as easily use the ‘secure copy’ command ‘scp’ to move files to and from the Pi from your controlling computer, as described at https://www.raspberrypi.org/documentation/remote-access/ssh/scp.md.

Copying a file from your computer TO the Pi

scp myfile.txt pi@192.168.1.11:

or to copy the file to a specific folder:
scp myfile.txt pi@192.168.1.3:myfolder/
this copies the file to the /home/pi/myfolder/ directory on your Raspberry Pi

Copying a file from your Pi TO the computer

scp pi@192.168.1.11:myfile.txt .

Installing and updating software on the Pi

Top
You will need to keep the software installed up to date on the Pi. Being open source, it seems that there are constantly updates that need installing – so this is a procedure that should be run through on a regular basis. The tool needed to do this is called ‘apt’.

Open a terminal and enter (‘apt-get’ is all one word, with no spaces)
sudo apt-get update
then
sudo apt-get upgrade
This can take a little while – don’t interrupt until it is finished, and keep an eye on it as it may ask questions needing a response. The apt command is described fully here.

This command line tool is very powerful and can be used to install software as well as keep it updated. For example, to install the package ‘curl’, you type:
sudo apt-get install curl

If you need to update the kernel software too, you can type:
sudo apt-get install rip-update
sudo rip-update

Curl is a useful command-line tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP).

If you prefer a software package manager tool with a graphical interface, you can try ‘aptitude’:
sudo aptitude

Setting aliases

Top
Using the Pi, you quickly realise that the command line, using the ‘terminal’, offers you powerful and efficient command tools for configuring and running the computer. Did you know you can set up shortcuts to help make the use of these commands even more efficient. An example we shall use here is to set an alias of ‘ll’ to run the full command ‘ls –al’ – a little shortcut that speeds up making full file listings on the screen – so that then, every time you type ‘ll’ the command ‘ls –al’ gets run.

Each time you start a terminal session, a configuration file in your home folder named ‘.bashrc’ is read – and shortcuts can be placed into this file. If you have a look in this file (it is a plain text file) you can actually see that there is a line in it making a call to incorporate any aliases defined in a companion file called ‘.bashrc_aliases’. Note this latter file doesn’t exist by default, so enter the following:

cd
nano .bashrc_aliases

This starts up the text editor ‘nano’ (any text editor would do the job as well!) and creates the file – now enter a new line in it:
alias ll=’ls =al’

Save the file out and exit (in nano this is Ctrl+O, Ctrl+X)

Now close the terminal window (type ‘exit’). Next time you run terminal session, type ‘ll’ to see the file listing – try and see.

Be sure to visit https://www.raspberrypi.org/ to learn more about these amazing machines.

Learning Python

Top
The Pi offers a fantastic means to learn programming – using languages such as Python. There are so many Python tutorials out there that we don’t write our own here. However, here are a few guidelines to get you up and running on the excellent quick Python tutorial from Magnus Lie Hetland. This is a good starting point for anyone who has done a spot of coding before as it is short, sweet and to the point!

The tutorial is online here

Firstly you need a decent editor (did you know there were so many!). Sadly, our favourite editor PyCharm is not yet posted to the Pi.

At the command line you can use ‘nano’:
nano myfile.py

Better still use a graphical editor – assuming it is installed, use say ‘Leafpad’
(the ‘&’ symbol means run editor as a background process):
leafpad myfile.py &

Best of all, use the Idle3 editor on the Pi:
idle3 myfile.py &

Secondly, run the code with the python command as below, (or in Idle – just press ‘F5’ to run the source code).
python myfile.py
Then you can follow Magnus’ examples through! For example, copy and run this code:

# Exercise 2
# Part 1:
print ("Part 1\nThis program continually reads in numbers\nand adds them together until the sum reaches 100:")
total = 0
goes = 0
while total < 100: # Get the user's choice: number = int(input("enter a number > "))
total += number
goes += 1
print ("The sum is now", total, "and it took you", goes, "turns.")
# Part 2:
print ("\nPart 2\nThis program receives a number of values\nand prints the sum")
total = 0
number = int(input("enter the number of values to enter > "))
for counter in range(0, number):
# Get the user's choice:
number = int(input("enter a number > "))
total += number
print ("The total was", total)

This tutorial is one of the following set of related Raspberry Pi tutorials, in order:
1. Unboxing the Raspberry Pi 3 – Pi Society
2. Raspberry Pi 3: Hello World – Pi Society
3. Raspberry Pi 3: Operating LEDs – Pi Society
4. Making the Raspberry Pi work, the next steps – Pi Society
5. Connecting a Raspberry Pi to eduroam wifi