Raspberry Pi running from Batteries

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

Parts:
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.

PiBattery01

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).

PiBattery02

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.

PiBattery03

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.

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

PiBattery05

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).

battery_3day

Mopi Reading (after 3 days use)

mopi3day

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

mopi3dayb

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

voltchart1

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

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)

new-limits

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.

new-limits-warning

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

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

new-limits-shutdown

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.

new-limits-shutdown2

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.

new-limits-shutdown3

Recharging the battery.

Ok, time to shutdown the pi and recharge my battery.

cahrger

Building the Prototype box

Installed 12v supply, 5v backbone, master off, RTC  and Dual PSU safe shutdown reset triggers. Next analog/digital sensors (smoke/co2, light, lazers, 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).

proto-working

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.

WEB_RPiB340C2

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

I had to check my old guides to refresh myself:

1. Select Raspian and press enter
sipi02_install_002

2. Press Y
sipi03_install_003

3. Wait for the install to complete.
sipi02_install_004

sipi02_install_005

sipi02_install_006

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

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

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

7. I followed the prompts to change the device password

sipi02_install_010

sipi02_install_011

sipi02_install_012

8. I reviewed the Internationalisation Options
sipi02_install_013

9. Change Locale.
sipi02_install_014

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

11. em_AD.UTF-8 UTF-8
sipi02_install_016

12. Select Australia.
sipi02_install_017

13. Select Sydney
sipi02_install_018

14, Under Advanced change the Hostname (computer name)
sipi02_install_019

15. I entered “sipi02”.
sipi02_install_020

16. I selected Enable SSH (remote login)
sipi02_install_021

17. Select Finish and Yes to reboot.
sipi02_install_022

sipi02_install_023

18. After the raspberry Pi rebooted I could login with the user name “pi” and my new password
sipi02_install_024

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.

sipi02_install_025

sipi02_install_026

sipi02_install_027

sipi02_install_028

sipi02_install_029

sipi02_install_030

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

ifconfig

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”

dtparam=i2c1=on
dtparam=spi=on

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.

mo_pi_01

mo_pi_02

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.

wittypi

wittypi_02

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.

wittypi_03

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.

wittypi_04

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
1
7

34. SD Card Check

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

df

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


fsck -fy /dev/mmcblk0p5
reboot

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):
#: TYPE NAME SIZE IDENTIFIER
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):
#: TYPE NAME SIZE IDENTIFIER
0: Apple_HFS Macintosh 10.10 HD +499.0 GB disk1
Logical Volume on disk0s2
532F6F24-17C1-4187-8637-45392549B220
Unencrypted
/dev/disk5 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
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/simon/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/simon/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
SPI
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

 

todo

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/

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.

Code:

            --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

Comments:

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);
end

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
  end
  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
end

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")

Results:

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

Arduino GPS Code for GPS/GPRS/GSM Module V3.0 (SKU:TEL0051) loaded

Arduino GPS Code for GPS/GPRS/GSM Module V3.0 (SKU:TEL0051) loaded.

20140727-031035-11435603.jpg

http://www.dfrobot.com/wiki/index.php/GPS/GPRS/GSM_Module_V3.0_%28SKU:TEL0051%29#How_to_drive_the_GPS_Mode_via_USB_port

Quite a bit of research to get the boards code, configure the arduino IDE and latest GitHub code.

Now I have to go outside to get a GPS signal.

20140727-031152-11512745.jpg

Update: Was able to send a text message from the Arduino/gsm module Telstra pre paid sim via power pack alone. It turns out we need to redirect pins 3/4/5 to 11/12/13 :)

20140727-162112-58872675.jpg

20140727-162734-59254900.jpg

Checking (and verifying) the internet on a raspberry pi with a bash script

This is how I check the internet with a bash script on a raspberry pi. It verifies the internet by downloading a file from a webserver (e.g http://www.your-domain/pi/test/simple_text_file.txt
(change this to a file of your choice)).

Script: CheckInternet.sh

    #!/bin/bash
    printf "BASH Version: $BASH_VERSION\n"

    printf "Testing Internet Connection (http://www.your-domain.com):\n\n"
    # ping -q -t 10 -w1 -c1 www.your-domain.com
    wget -q --tries=2 --timeout=20 http://www.your-domain.com
    if [[ $? -eq 0 ]]; then
    printf "Internet appears online.\n\n"
    TMP_FILE="./simple_text_file.txt"
    if [ -f $TMP_FILE ]
    then
    echo "Deleting old temp files.."
    sudo rm ./simple_text_file.txt
    fi
    printf "Downloading file from the internet.\n"
    sudo wget http://www.your-domain/pi/test/simple_text_file.txt
    if [ -f $TMP_FILE ]
    then
    printf "File downloaded ok.\n"
    printf "File Contents:\n"
    echo "----------------------------------------------------------"
    printf "\n"
    cat $TMP_FILE
    printf "\n\n"
    echo "----------------------------------------------------------"
    else
    printf "File failed to download.\n"
    fi
    else
    printf "Internet appears offline, unable to download files."
    fi

    printf "Tiding up..\n\n"
    sudo rm ./simple_text_file.txt

    printf "Goodbye\n\n"

To call this script from the command line do this.

    sudo bash CheckInternet.sh

Alternatively you can schedule emails by adding this to your “/etc/crontab” file


    # run every 15 minutes
    */15 * * * * root bash /usr/bin/simon/CheckInternet.sh

    # run every 5 minutes
    */5 * * * * root bash /usr/bin/simon/CheckInternet.sh

Or you can run this on pi start-up by adding the python call to your “/etc/rc.local” file.

    bash /usr/bin/simon/CheckInternet.sh

Make sure you make you file executable.

Send Email from Raspberry Pi

This is how I am sending emails (boot and 15 min runtime notifications) from my Pi

Python Script: /usr/bin/simon/SendEmail.py

You will need to type in your mail server Username/Password/IP/Port to get this to work.


    #!/usr/bin/python
    import smtplib

    EMAIL_USER = 'your-email@your-domain.com'
    EMAIL_PASS = 'your_email_password_goes_here'
    SMTP_SERVER = '192.0.0.1'
    SMTP_PORT = 587

    def send_email(recipient, subject, msg):
    smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtpserver.set_debuglevel(0)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.login(EMAIL_USER, EMAIL_PASS)
    header = 'To:' + recipient + '\n' + 'From:' + EMAIL_USER + '\n' + 'Subject:' + subject + '\n'
    header = header + '\n' + msg + '\n\n'
    msg = header + '\n' + msg + '\n\n'
    smtpserver.sendmail(EMAIL_USER, recipient, msg)
    smtpserver.close()

    send_email('recipient-name@gmail.com','Subject Goes Here', 'Email contents goes here.')

To call this script from the command line do this.

    sudo python SendEmail.py

Alternatively you can schedule emails by adding this to your “/etc/crontab” file


    # run every 15 minutes
    */15 * * * * root python /usr/bin/simon/SendEmail.py

    # run every 5 minutes
    */5 * * * * root python /usr/bin/simon/SendEmail.py

Or you can run this on pi start-up by adding the python call to your “/etc/rc.local” file.

    python /usr/bin/simon/SendEmail.py