In the last two posts, we talked about how to set up Mission Control with Raspberry Pi 3b+, Apache, PHP, and MariaDB. In the second post in the series, we set up the PHP page to accept web requests and write those requests to the database. This post will show you how to set up the Rover Pi and Automatically start a Python Script to record the uptime.
What you need for this experiment.
1. Raspberry Pi Zero W and a 16GB or more card (I am using the Raspberry Pi Zero W with a 32gb Samsung EVO card) Note: If you need to purchase a new Pi Zero W, get the kit that included the HDMI adapter and the Mini USB to Regular USB. The Pi Zero has a mini HDMI connection)
2. Wireless network
3. Some way to power things and to view and type.
Now to get started. There are going to be a bunch of steps here but don’t worry I did this on a clean Pi so it should work.
Note: all these steps are done from a Windows PC and the Pi directly. You will need to google to do it from a Mac or Linux.
Step 1. Prep your Pi’s
Download and create a SIM card with the latest version of Raspbian. You will need to download both versions. I put the Lite version on the Rover and the desktop version for Mission Control.
- Use Win32 Disk Imager to make your SD card work from the downloaded image.
Note: Make sure you have the right drive letter before clicking Write. I unplugged all my USB drives before I plugged in the SD card. - Select your Image using the little folder icon in the middle. This time we are using the LITE version.
- Choose the Correct Drive. It’s NOT C
- Pick Write.
I will give you a warning that you won’t read, so pick OK.
Note: When it’s finished Windows will ask you if you want to format the drive. Please Say no.
Step 2: Building Mission Control
Mission Control will be the Raspberry Pi 2B,3B or 3B+ and will act as the Web and Database server.
Assemble your Pi
- Put the card in the Pi
- Connect your monitor
- Connect your keyboard and mouse
- Plug in the power
Step 3: Configure the Pi
- After your Raspberry Pi Zero W boots, you will be presented with a login screen. The default username is PI and the default password is raspberry
Raspbian GNU/Linux 9 raspberrypi tty1 raspberrypi login: PI Password: raspberry
- After logging in we need to set up the localization and WiFi. To do that we will use the raspi-config utility.
sudo raspi-config
- When you start the utility you will be presented with a menu. Let’s start with 2 Network Options. Use the arrow keys to highlight 2 Network Options and press enter.
- Press enter on N1 to change the hostname. I set mine to RoverPi
- Once you set that you will go back to the main menu select 2 again and press enter
- Select N2 to set up WiFi (Use the arrows and enter)
- Enter your SSID or WiFi name.
- Enter your WiFi password
- Use the arrow key to select Localisation options.
- Set your Local to the right one, Mine is en_US.UTF-8 UFT-8 you may want to unselect any selected here as well, use the spacebar to select. Use the Tab key to select Ok Note it may take a second or 3 to bring up the list
- Select en_US.UTF-8 and tab to ok and press enter. This will take a few seconds
- But first, cut the red wire, just kidding.
- Go back to Localisation and select time zone. I picked US, Pacific New
- Go back to Localisation and select Keyboard layout. I picked Generic 105-key (intl) PC, then pick Other, and select English (US) Then US English, then default layout and no compose key
- Finally go back to Localisation and pick WiFi, I choose US United States.
- Use the arrows and tab keys to finish the utility and allow your Pi to rebootNow to Check your work
- Above in the log scroll, you will see your IP address, is right before the second to last OK
- Login to your pi
- Let’s ping google.com you should see some replies, i put the first part of the reply in the line below.
ping goggle.com 64 bytes from.........
- Press Ctrl-C to stop the ping. If you got a time in ms, your network is working
- Now let’s test the keyboard Press Shift-4
$
- If you did not get a dollar sign and expected one, your localization settings are incorrect.
- Now to update your Pi with the latest updates.
sudo apt-get update sudo apt-get dist-upgrade
Python Script to record uptime to Mission Control
The Python script will use Python3, luckily, it’s already installed. We will use nano to type in the code.
- Go to your home directory. The cd command will take you there, pwd will tell you.
CD PWD /home/pi
- Now start nano and type in the code.
sudo nano recorduptime.py
- Here is the code: You will need to change 172.16.42.99 to the IP address of your Mission Control computer.
import urllib.request import socket import os import time gw = os.popen("ip -4 route show default").read().split() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect((gw[2], 0)) ipaddr = s.getsockname()[0] while True: uptime = os.popen("awk '{print $1}' /proc/uptime").readline() with urllib.request.urlopen('http://172.16.42.99/RecordUptime.php?Hostname='+ socket.gethostname() +'&IP='+ s.getsockname()[0] +'&UpTime='+ uptime) as response: html = response.read() time.sleep (120)
- Go ahead and give it a try.
sudo python3 recorduptime.py
- Now check Mission Control and see if it has been written to the database. You should see a record or two from the RoverPi.
- Stop the recorduptime.py by pressing Ctrl-C
Getting your Python Script to Automatically start at the boot of your Raspberry Pi
Autostarting is not as bad as the internet makes it out to be, you just need to edit one file, and you’re done. The file you need to edit is /etc/rc.local.
- From your RoverPi type:
sudo nano /etc/rc.local
- Right before exit 0, add the following line:
Don’t forget the & that makes it run as its own process in the background.python3 /home/pi/recorduptime.py &
- Reboot your pi and check Mission control to see if you start getting readings.
sudo reboot
That is all there is to recording up time on your Pi. Now to figure out solar and batteries.
Check out the previous posts on:
INSTALLING APACHE, PHP, AND MARIADB ON A RASPBERRY PI 3B+ SETTING UP THE DATABASE