Skip to content
The Great Mojave Rover
The Great Mojave Rover

A build log sharing my experiences learning how to build a rover.

  • About The Great Mojave Rover
  • Rover Build Blog
  • Knowledge Base
The Great Mojave Rover

A build log sharing my experiences learning how to build a rover.

March 25, 2018

Raspberry Pi Recording Up time to Mission Control

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.

  1. 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.
  2. Select your Image using the little folder icon in the middle. This time we are using the LITE version.
  3. Choose the Correct Drive.  It’s NOT C
  4. 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.
 
Write the image to the SD Card
 

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

  1. Put the card in the Pi
  2. Connect your monitor
  3. Connect your keyboard and mouse
  4. Plug in the power

Step 3: Configure the Pi

  1. 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
  2. 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
  3. 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.
  4. Press enter on N1 to change the hostname. I set mine to RoverPi
  5. Once you set that you will go back to the main menu select 2 again and press enter
  6. Select N2 to set up WiFi (Use the arrows and enter)
  7. Enter your SSID or WiFi name.
  8. Enter your WiFi password
  9. Use the arrow key to select Localisation options.
  10. 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
  11. Select en_US.UTF-8 and tab to ok and press enter.  This will take a few seconds
  12. But first, cut the red wire,  just kidding.
  13. Go back to Localisation and select time zone.  I picked US, Pacific New
  14. 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
  15. Finally go back to Localisation and pick WiFi, I choose US United States.
  16. Use the arrows and tab keys to finish the utility and allow your Pi to rebootNow to Check your work
  17. Above in the log scroll, you will see your IP address, is right before the second to last OK
  18. Login to your pi
  19. 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.........
  20. Press Ctrl-C to stop the ping.   If you got a time in ms, your network is working
  21. Now let’s test the keyboard Press Shift-4
    $
  22. If you did not get a dollar sign and expected one, your localization settings are incorrect.
  23. 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.

  1. Go to your home directory. The cd command will take you there, pwd will tell you.
    CD
    PWD
    
    /home/pi
  2. Now start nano and type in the code.
    sudo nano recorduptime.py
  3. 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)
  4. Go ahead and give it a try.
    sudo python3 recorduptime.py
  5. Now check Mission Control and see if it has been written to the database.  You should see a record or two from the RoverPi.
  6. 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.

  1. From your RoverPi type:
    sudo nano /etc/rc.local
  2. 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 &
  3. 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

Compute Rover Code

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

March 2018
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Feb   Apr »

Categories

  • Compute
  • Design
  • Environmental Sensors
  • Full Stack Sensor Project
  • How-To
  • Rover
  • Rover Testing
  • Solar Testing
  • Uncategorized
  • Update

Recent Posts

  • The full stack sensor project
  • Quick update.
  • Solar test 16 is having a hard time.
  • Wiring done working on Mission Control
  • Solar Testing Still working and wiring of motors done
©2025 The Great Mojave Rover | WordPress Theme by SuperbThemes