Raspberry Pi running from Batteries


Read this first https://fearby.com/blog/?p=2202

1x Raspberry Pi Model 2B
1x MoPi: Hot-Swap Mobile Power for the Pi (Manual)
1x 12V18AH Sealed Lead Acid Battery from JayCar (Manual)

1x 2 Watt 12 Volt Solar Panels – Amorphous 2 Watt 12 Volt Solar Panels – Amorphous

Read this post to see how my pi is configured and setup.

The Pi is running a public facing website here: http://sipi02.fearby.com

Step 1: Running on battery.

This is my battery and pi on my work bench.

Bench covered in parts

This is a close up of my Raspberry Pi + WittyPi (RTC and auto startup/shutdown hat) + MoPi (dual power and safe shutdown on low battery hat).

Raspberry Pi


I am not going to detail how to setup the MoPi but here is the MoPi output from the MoPi configured to be run from Lead Acid batteries.

Mo Pi Sheild photo

The default 6 cell Lead Acid battery profile says there is a low power condition when the battery is at 10.42v and the minimum charge s 9.48v.

Status Screen.

This is the battery reading I have from the Pi itself.

MoPi Voltage

After 3 days of 24/7 use (saving an image every minute) and running a web server for the camera the battery has fallen from 12.73v to 12.28v.

Cheap multimeter reading (after 2 days use).


Mopi Reading (after 3 days use)

Mopi Current VOltage

I will ignore my multimeter and trust the Mopi to decide when to power down the Pi.


I would expect the MoPi to shut down the pi when the battery falls another 2,456mv (that may be in 18~25 days based on current trends (pun intended)).

Voltage CHart

Step 2:  Have the battery in Standby and top up the battery with Solar.

I have a cheapie “2 Watt 12 Volt Solar Panels – Amorphous 2 Watt 12 Volt Solar Panels – Amorphous”.  I have no idea if this will be enough to charge up the battery during the day? I have no idea about the solar stuff.

solar panel

Time to ask the experts at http://www.eevblog.com/forum/beginners/raspberry-pi-running-from-batteries-and-solar/

Update 30th March 2016

Ian.M at EEV Blog informed me that the lowest the battery should be discharged to is 10.5v.  I have edited the “/etc/default/simbamond” file to now specify safer low and shutdown power levels.  The “Sinabamond” service is a battery monitoring service (read about it here)


If you edit the “/etc/default/simbamond” you may need to run the following command before and after you edit the file

  • sudo service simbamond stop
  • sudo service simbamond restart

Now when the MoPi detects the battery is below the low level the lights flash and broadcast messages appear on the terminal.


If the battery gets below the critical it shuts down the pi (safely).

Testing it (letting the battery getting below the critical limit).


The good think about the MoPi it shuts down the Pi safely and turns off the power (all power).

I must be getting a few low power spikes as the spikes because the Pi seems to be shutting down early.

I adjusted my max, good, low and critical values to 1200 11500 11100 and 11000 to test the levels of where my battery was at and the MoPi flashed to say it was shutting down then decided not to.  I ran a few volt outputs and the voltage detection was dipping.


You may want to space your voltages enough to prevent voltage dipping triggering an early shutdown.

Ok, this is the voltages that I will set my pi to shutdown at.


Recharging the battery.

Ok, time to shut down the pi and recharge my battery.


Building the Prototype box

Installed 12v supply, 5v backbone, master off, RTC  and Dual PSU safe shutdown reset triggers. Next analogue/digital sensors (smoke/co2, light, lasers, temp, humidity, pressure, moisture, motor and proximity motion sensor and vibration sensor), speaker and 240v relay for external control. Wifi, SMS and push notification code ready.

I killed a MoPi dual power input Pi board (silly me put the wrong colour shrink wrap protective layer over the top) and when I turned on the power with reverse polarity much blue smoke was released.

The main compartment will we air right with silica bags and protected from the elements with loads of styrofoam. I don’t think I need active cooling (therms electric peltier element).



Updating NodeJS from v0.12.6 to 5.0.0

Word on the street there is a new security patch about to drop for NodeJS that will fix DDoS and SSH issues.

I am running an older version of NodeJS (v0.12.6) from August 2015. NodeJS has jumped to version v5.1.0 in 5 months.

I am feeling brave and I decided to upgrade my NodeJS by running these commands.

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Then I updated my node modules.

sudo npm install npm@latest -g

Now to re ran my NodeJS service. Dang I received this error.

Error: Module version mismatch. Expected 47, got 14.
at Error (native)
at Object.Module._extensions..node (module.js:450:18)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at bindings (/redactedpath/node_modules/bcrypt/node_modules/bindings/bindings.js:76:44)
at Object. (/redactedpath/node_modules/bcrypt/bcrypt.js:3:35)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)

Thanks to Google I found this guide in seconds.

First, remove bcrypt.

sudo node remove bcrypt

The update CCC (from gcc v4.8.2 to v4.8.5).

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update
$ sudo apt-get install gcc-4.8 g++-4.8
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8

Then reinstall bcrypt.

sudo node install bcrypt

Now you should be able to run your node server.

node server.js


Raspberry Pi 2 Setup

Here is a quick guide to setting up a Raspberry pi 2 (OS, Wifi etc) using a pre loaded NOOBS Raspian OS on a Micro SD card.


I ordered the Raspberry Pi 2 Model B WiFi Pack from Wiltronics.

    • This Raspberry Pi 2 Model B WiFi Pack includes a Raspberry Pi 2 Model B computer, durable clear case, a power adaptor, a USB WiFi antenna, an HDMI cable and an 8GB micro SD Card with NOOBS OS loaded.

Pi Kit

Plug in a keyboard, mouse, wifi, HDMI and power and you are good to go.

I had to check my old guides to refresh myself:

1. Select Raspian and press enter

2. Press Y

3. Wait for the install to complete.



4. The Pi will ask to reboot (this is what it looks like)

5. The first program that will run is the Raspberry Pi Configuration.

6. I tried to expand the file systems but my card was already expanded.

7. I followed the prompts to change the device password





8. I reviewed the Internationalisation Options

9. Change Locale.

10. tip use space to deselect UK and select em_AD.UTF-8 UTF-8.

11. em_AD.UTF-8 UTF-8

12. Select Australia.

13. Select Sydney

14, Under Advanced change the Hostname (computer name)

15. I entered “sipi02”.

16. I selected Enable SSH (remote login)

17. Select Finish and Yes to reboot.


18. After the raspberry Pi rebooted I could log in with the username “pi” and my new password

19. If you are new to Linux and are not sure of any Terminal commands (like DOS commands) check out this Linux Commands Cheat sheet from http://linoxide.com/linux-command/linux-commands-cheat-sheet/

Linux Commands Cheat Sheet
Linux Commands Cheat Sheet

Booting up the GUI desktop.

20. GUI Desktop

You can type “Startx” to start the Raspian Desktop.

21. Desktop

The Pi Desktop is easy to use and navigate.







22. Internet Check

I plugged in in a spare USB TP-Link TL-WN821N wireless adapter, followed the wireless setup and verified I had a network connection by typing the following commands.

ping www.google.com


I did indeed have an internet connection.

23. Update/Upgrade

I dropped back out of X and ran the following commands to update the Pi’s firmware and software.

sudo apt-get update
sudo apt-get upgrade
sudo reboot

Then after reboot.

sudo rpi-update
sudo reboot

This took a while to download and install 130MB of data.

24. FTP Server

I setup an FTP server.

25. I2C Tools

Installed i2c-tools and configuring it. Troubleshooting here.

Added the following to “/boot/config.txt”


sudo apt-get install python-smbus
sudo apt-get install i2c-tools

26. Installed Python2 GPIO

sudo apt-get install python3-rpi.gpio

27. MoPI (Dual Power supply board)

Setup my MoPi

sudo apt-get install simbamond

FAQ’s here, here and here.

I did things a bit out of order and I think I needed to power up the MoPi/Pi from batteries (and no mains power) for 2iC to detect. But all is god, I can power up and shutdown my Pi from 6xAA batteries.



28. Install Python Camera module

sudo apt-get install python3-picamera

29. Python

Installed Python PIP

sudo apt-get install python3-pip

30. Monit System Monitoring

Installed Monit System Monitoring Web server to keep and eye on the MoPi from a web server (config here)

sudo apt-get install monit

More info here https://www.the-hawkes.de/monitor-your-raspberrypi-with-monit.html

31. Temperature Sensors

Add temperature sensing service to Monit (guide here)

sudo apt-get install lm-sensors

32. NGINX Web Server, PHP and Perl

Ok, now I am going to install a web server (NGINX, PHP and Perl) using this guide http://youtu.be/I_2yGGPus90

sudo -i
apt-get update
apt-get install nginx php5-fpm php5-cgi php5-cli php5-common libfcgi-perl


33. Python and raspberry Pi GPIO Pin Library

sudo apt-get install python-dev
sudo apt-get install python-pip
sudo pip install RPi.GPIO

33. Witty Pi – Realtime Clock and Power on/off scheduling board. Good review here.



wget http://www.uugear.com/repo/WittyPi/installWittyPi.sh
sudo sh installWittyPi.sh

Everything installed for me.

sudo shutdown -r now

On reboot I noticed warning on boot up about GPIO pin conflicts with my MoPi. Maybe the MoPi and Witty Pi are clashing? I changed GP17 on the Witty Pi and rebooted. I still had a conflict so I set GP4 on the Witty Pi to also be custom. After a reboot I still had MoPi warning.


I am now researching how to redirect the GOI pins that the Witty Pi uses.

Do’h, I just realised that the Pi is powered by Mains USB on the pi and the MoPi warning may just be a warning about the MoPi not controlling the external power. I removed the mains power ad instead connected up the MoPi to a 9v DC supply.


I reset the Witty Pi pins to their defaults and powered up the Pi via the Pi MoPI power button. All MoPi warnings have gone. Now to resume the WittyPi setup.

I will possibly use the MoPi to power down the Pi when battery gets low and use the Witty Pi to boot up the Pi every day and to use the Realtime clock.

tip: Make sure the folder you install witty pi to is spelled exactly like “%HOME%/wittyPi” and not “%HOME%/WittyPi”. My daemons would not start because it way looking for “/home/pi/wittyPi” and not “/home/pi/WittyPi”.

Now I configured the Witty Pi’s time.

sudo ./wittyPi.sh

34. SD Card Check

Ok run the following command to get the location of your boot filesystem


Now run this command to check your boot disk (replace the boot disk with the one identified above).

fsck -fy /dev/mmcblk0p5

You may notice potential corrupted files and fixes from the output above.

sudo dosfsck -w -r -l -a -v -t /dev/mmcblk0p5

Also run a check after reboot.

sudo shutdown -F -r now

You can run these commands to review any errors.

cat /var/log/fsck/checkfs
cat /var/log/fsck/checkroot

35. Filesystem Update
sudo apt-get update
sudo apt-get upgrade
sudo reboot

Then after reboot.

sudo rpi-update
sudo reboot

35. SD Card Backup

Type “distil list” to show your connected disks. My Pi is “disk5”

diskutil list
/dev/disk0 (internal, physical):
0: GUID_partition_scheme *500.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_CoreStorage Macintosh 10.9 HD 499.4 GB disk0s2
3: Apple_Boot Recovery HD 650.0 MB disk0s3
/dev/disk1 (internal, virtual):
0: Apple_HFS Macintosh 10.10 HD +499.0 GB disk1
Logical Volume on disk0s2
/dev/disk5 (external, physical):
0: FDisk_partition_scheme *7.9 GB disk5
1: Windows_FAT_16 RECOVERY 858.8 MB disk5s1
2: Linux 33.6 MB disk5s3
3: Windows_FAT_32 boot 62.9 MB disk5s5
4: Linux 7.0 GB disk5s6

Clone the SD Card (this command has no feedback so please wait).

sudo dd if=/dev/disk5 of=/Users/blog/Backup/SiPi02/Jan3rd2016.dmg

Eject the SD Card

sudo diskutil eject /dev/rdisk5

How to restore a Disk Image to the SD Card.

diskutil unmountDisk /dev/disk5
sudo dd if=/Users/blog/Backup/SiPi02/Jan3rd2016.dmg of=/dev/disk5
sudo diskutil eject /dev/rdisk5

36. No back to reading the MCP3008 Analog to Digital Converting chip.

The following commands were taken from this video.

wget https://github.com/Gadgetoid/py-spidev/archive/master.zip
unzip master.zip
cd py-spidev-master
sudo python3 setup.py install

Enable SPI at boot (video guide here)

sudo rasps-config
advanced options
Enable: Yes, Yes Finish
sudo reboot


Note:I completed the code above but my python script could not see spider (it was if the module was not installed. This resolved it.

cd ~
sudo git clone git://github.com/doceme/py-spidev
cd py-spidev/
sudo python setup.py install



Part 3 (convert data):

Part 4 (advanced):

Part 5 (humidity sensor)

Comfast CF-WU755P WIFI

This guide may help me: https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=109919&p=755521

I might try this guide: https://blog.samat.org/2014/12/15/realtek-8188eu-based-wi-fi-adapters-on-the-raspberry-pi/


Making JSON more readable in the Terminal with Curl extension jq

jq banner


When developing apps and games you may need to read JSON files quickly in the Terminal, this can be a pain.

JSON is a right pain to read in the Mac terminal but Cloudant have some great tips for viewing JSON here.

First of all here is a random JSON file displayed in the mac terminal using curl.

Terminal Command:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'

json default

What a mess.


Install the ./jq lightweight command line JSON processor from http://stedolan.github.io/jq/

Install (type this in the terminal and install)

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install jq

fyi: Homebrew is a packaged installer (read more http://brew.sh/) and check out http://braumeister.org/ to see what other packages you can install.


Now lets try the curl command again with ” ! jq .” added to the end of the curl request.

Terminal Command:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq .

color coded json in terminal

If you just want a web based tool try the Postman extension for Google Chrome.


Simple lua function to catch a nil when checking json values

Simple lua function to catch a nil when checking json values


function IsInString(theString, theText)
    print("IsInString (theString: '" .. theString .. "', theText: '" .. theText .. "').");
    if (tonumber(string.find(theString, theText)) == nil) then
        print(" String Not Found.");
        return false;
        -- check the pos if the string to confirm (not nil is not enough)
        if (tonumber(string.find(theString, theText)) > 1) then
            print(" String Found.");
            return true;
            print(" String Not Found.");
            return false;

I was doing this when processing json data but kept getting nil in the second elseif (nil was not being caught higher up.

if (event.response == nil) then
        elseif (event.response == "") then
            -- code
        elseif (tonumber(string.find(event.response, "ok")) > 1) then
            if (string.find(event.response, "true") > 1) then
                -- code


Simple Accelerometer Data Conversion to Degrees

Here is a handy lua Corona Labs code chunk to convert xyz accelerometer data to degrees.


I am building an augmented reality app that needs to know when the user tilts and rolls a phone around so it can update the user interface with augmented reality elements. Corona already has the accelerometer acquisition code but now I want to display this data in human readable degrees (e.g if you were looking half way up the sky you would expect 45° and the horizon 0°)

A shout out goes to Doug McFarland here for the original c/Arduino code (http://wizmoz.blogspot.com.au/2013/01/simple-accelerometer-data-conversion-to.html) that I converted to Lua.


            --Input Data
            local xAxisData = --insert xaxis sensor data here
            local yAxisData = --insert yaxis sensor data here
            local zAxisData = --insert zaxis sensor data here
            --Convert raw 0.0 ~ 1.0 sensor datad to degrees.
            local xAngle = math.atan(tonumber(xAxisData) / (math.sqrt((tonumber(yAxisData) * tonumber(yAxisData))) + (tonumber(zAxisData) * tonumber(zAxisData))))
            local yAngle = math.atan(tonumber(yAxisData) / (math.sqrt((tonumber(xAxisData) * tonumber(xAxisData)) +  (tonumber(zAxisData) * tonumber(zAxisData)))))
            local zAngle = math.atan(math.sqrt((tonumber(xAxisData) * tonumber(xAxisData)) + (tonumber(yAxisData) * tonumber(yAxisData))) / tonumber(zAxisData));
            xAngle = xAngle * 180.00
            yAngle = yAngle * 180.00
            zAngle = zAngle * 180.00
            xAngle = xAngle / 3.141592
            yAngle = yAngle / 3.141592
            zAngle = zAngle / 3.141592
            --Round down a bit and display
            print("X Degrees: " .. tostring( math.round(xAngle,1)) .. " (Original X: " .. tonumber(xAxisData) .. ")")
            print("Y Degrees: " .. tostring( math.round(yAngle,1)) .. " (Original Y: " .. tonumber(yAxisData) .. ")")
            print("Z Degrees: " .. tostring( math.round(zAngle,1)) .. " (Original Z: " .. tonumber(zAxisData) .. ")")

Output Data:

    Accelerometer X Data: -0.0264 = -1&deg
    Accelerometer Y Data: -68 = -68&deg
    Accelerometer Z Data: 0.3682 = 68&deg


This does not look too handy to some but this is gold in plotting the horizon and calculating far away objects with trigonometry.

Don’t forget to detect what orientation the mobile device is in (portrait, landscape, upside down portrait and upside down landscape). You will need to factor these display modes into the calculation before converting to degrees. Sometimes it is easier to just develop portrait or landscape apps (not both).


Calculating the distance and bearing between two GPS points

I have been searching for a solution to calculate the distance and bearing of a target gps location for a while now. This page always popped up but my brain hurt every time I tried to convert this to lua.


I am happy to say I have cracked it and here is my code.

function Geo_Angle( lat1, lon1, lat2, lon2)
  local dLon = math.rad(lon2-lon1);
  local y = math.sin(dLon) * math.cos(math.rad(lat2));
  local x = math.cos(math.rad(lat1)) * math.sin(math.rad(lat2)) - math.sin(math.rad(lat1)) * math.cos(math.rad(lat2)) * math.cos(dLon);
  local brng = math.deg(math.atan2(y, x));
  return ((brng + 360) % 360);

The distance code was created by roaminggamer else on the Corona forums (can’t find their name).

function Geo_Distance(lat1, lon1, lat2, lon2)
  if lat1 == nil or lon1 == nil or lat2 == nil or lon2 == nil then
    return nil
  local dlat = math.rad(lat2-lat1)
  local dlon = math.rad(lon2-lon1)
  local sin_dlat = math.sin(dlat/2)
  local sin_dlon = math.sin(dlon/2)
  local a = sin_dlat * sin_dlat + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * sin_dlon * sin_dlon
  local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
  -- 6378 km is the earth's radius at the equator.
  -- 6357 km would be the radius at the poles (earth isn't a perfect circle).
  -- Thus, high latitude distances will be slightly overestimated
  -- To get miles, use 3963 as the constant (equator again)
  local d = 6378 * c
  return d

Here is how I call the code

print("Bearing between Tamworth and Armidale: " .. tonumber(GeoAngle(-31.1063157, 150.9307341,-30.5143425, 151.66696439999998)))
print("Distance between Tamworth and Armidale: " .. tonumber(GeoDistance(-31.1063157, 150.9307341,-30.5143425, 151.66696439999998)) .. "km")


Corona Simulator[4155:693467] Bearing between Tamworth and Armidale: 47.07677793374
Corona Simulator[4155:693467] Distance between Tamworth and Armidale: 96.419986257454km

When comparing my results with this site my bearing results are within 0.18677793374 degree and 0.078686257454 km for distance (good enough for my simple augmented reality app).

I tried to trick it by putting the source/dest either side of GMT and it did not measure the longest route around the globe.

Corona Simulator[4288:936166] Bearing between -179 lon and 179 lon: 270
Corona Simulator[4288:936166] Distance between -179 lon and 179 lon: 222.6341993844km

Also putting a source/dest near each pole calculated the approximate right distance between the coordinates.

Corona Simulator[4288:936166] Bearing between -89 lat and 89 lat: 180°
Corona Simulator[4288:936166] Distance between 89 lat and 89 lat: 19814.443745211km