| 1 | * Location Logging |
| 2 | #+TITLE: Ninfacyzga-01 Manual |
| 3 | #+AUTHOR: Steven Baltakatei Sandoval |
| 4 | #+EMAIL: baltakatei@gmail.com |
| 5 | ** About |
| 6 | This document was created by Steven Baltakatei Sandoval on |
| 7 | ~2020-06-29T12:14Z~ under a [[https://creativecommons.org/licenses/by-sa/4.0/][Creative Commons BY-SA 4.0 license]]. It |
| 8 | was updated by Steven Baltakatei Sandoval on ~2020-10-16T23:32Z~ |
| 9 | |
| 10 | ** Narrative |
| 11 | Ninfacyzga-01 records (logs) its position in time and space using a |
| 12 | [[https://en.wikipedia.org/wiki/Satellite_navigation_device][GPS receiver]]. The NMEA location data produced by the receiver is |
| 13 | converted into the more commonly used GPS data storage formats of GPX |
| 14 | and KML. All three types of data are then compressed and encrypted |
| 15 | against a set of public keys. The encrypted data is then written to |
| 16 | disk. Data produced by the receiver is segmented into 60-second chunks |
| 17 | before being processed and written to disk. |
| 18 | ** Description |
| 19 | *** Hardware |
| 20 | **** Raspberry Pi Zero W |
| 21 | See the [[https://www.raspberrypi.org/pi-zero-w/][OEM]] webpage for this product. |
| 22 | **** PiZ UpTime 2.0 |
| 23 | See the [[https://alchemy-power.com/piz-uptime-2-0/][OEM]] webpage for this product. |
| 24 | *** Software |
| 25 | ~bklog~ : A bash script that saves its stdin stream to a tar file. The |
| 26 | file may be compressed by ~gzip~ and encrypted by ~age~. It is an |
| 27 | executable file contained within this repository at ~exec/bklog~. It |
| 28 | should be copied to ~$HOME/.local/bin~. |
| 29 | |
| 30 | [[https://tracker.debian.org/pkg/gpsd][~gpsd~]] : A background daemon app capable of interfacing with the |
| 31 | [[https://ozzmaker.com/berrygps-berrygps-imu-quick-start-guide/][Ozzmaker BerryGPS-IMU]]'s GPS submodule. Installed and initialized by |
| 32 | ~apt~. Should be installed along with the ~gpsd-clients~ package. |
| 33 | |
| 34 | ~gpspipe~ : A command line app that polls ~gpsd~ and produces a stream |
| 35 | stdout consisting of GPS data lines in NMEA format. Installed via |
| 36 | ~apt~. |
| 37 | |
| 38 | ~gpsbabel~ : A command line app that converts GPS data from one format |
| 39 | into another. ~bklog~ may be used to convert NMEA data into GPX and |
| 40 | KML. Installed via ~apt~. |
| 41 | |
| 42 | ~gzip~ : A simple command line app that compresses stdin into a |
| 43 | smaller stdout stream. |
| 44 | |
| 45 | ~age~ : A simple command line app that encrypts stdin against public |
| 46 | keys specified in its options. Produces encrypted stdout. Is an |
| 47 | executable file contained within this repository at ~exec/age~. It |
| 48 | should be copied to ~$HOME/.local/bin~. |
| 49 | |
| 50 | **** Narrative |
| 51 | ~bklog~ may be used to log location data by receiving stdout produced |
| 52 | by ~gpspipe~. ~bklog~ contains options that allow file writes to be |
| 53 | performed at adjustable time intervals (default: 300 seconds), |
| 54 | compressed (with ~gzip~), and encrypted (with ~age~). Files are |
| 55 | written in the form of appendages to a ~tar~ archive saved to a |
| 56 | specified location. The NMEA data produced by ~gpspipe~ may be |
| 57 | processed via a ~gpsbabel~ command string specified as an option to |
| 58 | ~bklog~, assuming ~gpsbabel~ is installed. |
| 59 | |
| 60 | *** Output |
| 61 | |
| 62 | Several output file formats have been tested with ~bklog~. |
| 63 | |
| 64 | **** File Formats |
| 65 | ***** NMEA |
| 66 | ~NMEA~ is an acronym for National marine Electronics Association. The |
| 67 | NMEA format described in this document follows the NMEA 0183 |
| 68 | standard. It is a newline-delimited streaming text format that encodes |
| 69 | global positioning system (GPS) data such as WGS84 location, time and |
| 70 | date information, satellite count, accuracy, and other |
| 71 | information. Each line is an "NMEA sentence". Descriptions of various |
| 72 | NMEA sentences can be found on [[http://aprs.gids.nl/nmea/][this]] webpage. |
| 73 | |
| 74 | See the [[https://en.wikipedia.org/wiki/NMEA_0183][Wikipedia page for NMEA 0183]] for this. This file format is the |
| 75 | default output of the ~gpspipe -r~ command. |
| 76 | ***** GPX |
| 77 | See the [[https://en.wikipedia.org/wiki/GPS_Exchange_Format][Wikipedia page]] for this. [[http://wiki.gis.com/wiki/index.php/WGS84][WGS84]] is the datum used. An NMEA file |
| 78 | may be converted to this format using ~gpsbabel~. |
| 79 | ***** KML |
| 80 | See the [[https://en.wikipedia.org/wiki/Keyhole_Markup_Language][Wikipedia page]] for this. [[http://wiki.gis.com/wiki/index.php/WGS84][WGS84]] is the datum used. An NMEA file |
| 81 | may be converted to this format using ~gpsbabel~. |
| 82 | |
| 83 | **** Encryption Method |
| 84 | See [[file:../setup/README.org][Main Setup]] procedures. |
| 85 | |
| 86 | ** Operating Procedures |
| 87 | *** Initial Startup |
| 88 | See OEM (Ozzmaker) [[https://ozzmaker.com/berrygps-berrygps-imu-quick-start-guide/][quickstart guide for the BerryGPS-IMU]]. |
| 89 | **** Physical Setup |
| 90 | |
| 91 | BerryGPS-IMU must be electrically connected to the correct pins on the |
| 92 | GPIO header of a Raspberry Pi Zero W. |
| 93 | |
| 94 | *Optional*: stack together with PiZ Uptime 2.0 module. No GPIO pins |
| 95 | conflict so a simple stacking and soldering with long header pins is |
| 96 | possible. |
| 97 | |
| 98 | **** Software Setup |
| 99 | ***** Install Executables |
| 100 | Follow the [[file:../setup/README.org][Main Setup]] procedures to obtain required files from this |
| 101 | repository. |
| 102 | |
| 103 | Install ~gpsd~, ~gpsd-clients~, and ~gpsbabel~. |
| 104 | |
| 105 | : $ sudo apt install gpsd gpsd-clients gpsbabel |
| 106 | |
| 107 | ***** Setup Serial for BerryGPS |
| 108 | The Ozzmaker BerryGPS-IMU unit requires that the serial console be |
| 109 | disabled and the serial port enabled. (see [[https://ozzmaker.com/berrygps-setup-guide-raspberry-pi/][ref]]). |
| 110 | |
| 111 | : $ sudo raspi-config |
| 112 | |
| 113 | Navigate to ~5 Interfacing Options~, then ~P6 Serial~. |
| 114 | |
| 115 | When prompted "Would you like a login shell to be accessible over |
| 116 | serial?", answer ~No~. |
| 117 | |
| 118 | When prompted "Would you like the serial port hardware to be |
| 119 | enabled?", answer ~Yes~. |
| 120 | |
| 121 | ***** Setup ~gpsd~ |
| 122 | ~gpsd~ needs to know which serial port to look at for NMEA location |
| 123 | data generated by the GPS unit. This can be done by modifying the |
| 124 | ~gpsd~ configuration file at ~/etc/default/gpsd~. |
| 125 | |
| 126 | : sudo nano /etc/default/gpsd |
| 127 | |
| 128 | Make sure the following lines are present: |
| 129 | |
| 130 | #+BEGIN_EXAMPLE |
| 131 | START_DAEMON="true" |
| 132 | USBAUTO="false" |
| 133 | DEVICES="/dev/ttyAMA0" |
| 134 | GPSD_OPTIONS="-n" |
| 135 | #+END_EXAMPLE |
| 136 | |
| 137 | NOTE: The ~DEVICES=~ line must be adjusted if the the ~ninfacyzga~ |
| 138 | device is configured to also track time. |
| 139 | |
| 140 | NOTE: The ~-n~ option causes ~gpsd~ to begin polling GPS devices |
| 141 | without waiting for a client to connect. This is important if ~gpsd~ |
| 142 | provides data to other applications (ex: ~gpspipe~, ~chrony~). |
| 143 | |
| 144 | Restart ~gpsd~ via: |
| 145 | |
| 146 | : $ sudo systemctl restart gpsd.service |
| 147 | |
| 148 | Run ~gpsmon~ to verify that location data is being recorded. |
| 149 | |
| 150 | : $ gpsmon |
| 151 | |
| 152 | ***** Automatic Start Configuration |
| 153 | It is recommended to create a daily ~cron~ job that executes a bash |
| 154 | script that starts ~bklog~ jobs to record location along with other |
| 155 | types of data recording. An example of such a script is below: |
| 156 | |
| 157 | #+BEGIN_EXAMPLE |
| 158 | #!/bin/bash |
| 159 | |
| 160 | # Log location |
| 161 | gpspipe -r | /bin/bash "$HOME/.local/bin/bklog" -c -e -z "UTC" -t "/dev/shm" \ |
| 162 | -r age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \ |
| 163 | -r age1ce3pvzrqfcn2pc6zqzglc8ac8yjk3fzukpy08cesqjjwns53xywqmaq7xw \ |
| 164 | -R "$HOME/.config/bklog/recipients" -w ".nmea" -b "300" -B "day" \ |
| 165 | -o "$HOME/Sync/Evanescent_Location" -l "location" \ |
| 166 | -p "gpsbabel -i nmea -f - -o gpx -F - " ".gpx" \ |
| 167 | -p "gpsbabel -i nmea -f - -o kml -F - " ".kml" & |
| 168 | # Log pressure |
| 169 | python ~/.local/bin/bmp388.py | /bin/bash "$HOME/git-OC/ninfacyzga-01/exec/bklog" \ |
| 170 | -c -e -z "UTC" -t "/dev/shm" \ |
| 171 | -r age1kza7pfshy7xwygf9349zgmk7x53mquvedgw9r98qwyyqhssh830qqjzlsw \ |
| 172 | -r age1ce3pvzrqfcn2pc6zqzglc8ac8yjk3fzukpy08cesqjjwns53xywqmaq7xw \ |
| 173 | -R "$HOME/.config/bklog/recipients" -w ".txt" -b "300" -B "day" \ |
| 174 | -o "$HOME/Sync/Evanescent_Location" -l "pressure" & |
| 175 | #+END_EXAMPLE |
| 176 | |
| 177 | This script, if it were saved at |
| 178 | ~"$HOME/.local/bin/cron/dailylog.sh"~ would then be added as a |
| 179 | line in the ~crontab~ file as shown below: |
| 180 | |
| 181 | #+BEGIN_EXAMPLE |
| 182 | $ crontab -e |
| 183 | 0 0 * * * /bin/bash ~/.local/bin/cron/dailylog.sh |
| 184 | @reboot /bin/bash ~/.local/bin/cron/dailylog.sh |
| 185 | #+END_EXAMPLE |
| 186 | |
| 187 | In the example script, the options are: |
| 188 | |
| 189 | : -c : tells bklog to compress output |
| 190 | : -e : tells bklog log to encrypt output |
| 191 | : -r : tells bklog to interpret the next argument as a pubkey string |
| 192 | : -R : tells bklog to interpret the next argument as a directory |
| 193 | : where public keys may be found (first line of each file is |
| 194 | : read). |
| 195 | : -o : tells bklog to write output files to the directory represented |
| 196 | : -t : tells bklog to interpret the next argument as a directory |
| 197 | : for storing temporary files |
| 198 | : by the next argument |
| 199 | : -p : tells bklog a command string through which output is piped |
| 200 | : before being compressed and encrypted. Also expected is a |
| 201 | : file extension to be appended before the compression and |
| 202 | : encryption file extensions. |
| 203 | : -w : tells bklog to save the unprocessed stdin with a specified |
| 204 | : file extension (instead of the default '.stdin'). |
| 205 | : -b : tells bklog how long each buffer round (time between file |
| 206 | : writes) lasts in seconds. |
| 207 | : -B : specifies the time-to-live for the bklog script. A valid value may |
| 208 | : one of the time elements such as "day" or "hour". |
| 209 | : -l : specfies a custom string to be used in output file names to |
| 210 | : help differentiate tar files produced via bklog from different |
| 211 | : sources of data. |
| 212 | : -z : specifies a time zone to be used to determine the script time-to-live. |
| 213 | : By default, bklog uses whatever time is specified by the TZ |
| 214 | : environment variable. |
| 215 | |
| 216 | ***** Log Transfer Configuration |
| 217 | See [[file:../setup/README.org][Main Setup]] procedures. |
| 218 | |
| 219 | Log files may be shared to other machines via ~syncthing~. See [[https://docs.syncthing.net/][this]] |
| 220 | manual for how to set up a shared folder and add Ninfacyzga-01 as a |
| 221 | device. Syncthing's directory synchronization capability allows a |
| 222 | remote machine to delete files from Ninfacyzga-01 by deleting from the |
| 223 | shared folder that they both share. |
| 224 | |
| 225 | When log files are removed from Ninfacyzga-01 is not within the scope |
| 226 | of this document. |
| 227 | |
| 228 | ***** Key Generation |
| 229 | See [[file:../setup/README.org][Main Setup]] procedures. |
| 230 | |
| 231 | *** Normal Startup |
| 232 | Turn on Ninfacyzga-01 by supplying 5VDC power to the Raspberry Pi. No |
| 233 | further interaction should be required. |
| 234 | *** Normal Operation |
| 235 | No interaction beyond continually supplying approximately 100mA of |
| 236 | 5VDC power and occasionally removing log files to conserve disk space |
| 237 | is required. |
| 238 | **** Log Transfer |
| 239 | Log files may be transferred by use of ~syncthing~ shared folders. |
| 240 | **** Automatic Updates |
| 241 | The ~automatic-upgrades~ package, if installed, should automatically |
| 242 | install security patches to packages installed via ~apt~. |
| 243 | *** Normal Shutdown |
| 244 | The system may be shutdown via SSH by running: |
| 245 | |
| 246 | : $ sudo shutdown -r 0 |
| 247 | |
| 248 | *** Unscheduled Shutdown |
| 249 | Ninfacyzga-01 as described and setup should tolerate unscheduled power |
| 250 | loss. Log files being written every 60 seconds means, at most, 60 |
| 251 | seconds worth of location data may be lost. |
| 252 | *** End of Life Disposal |
| 253 | See [[file:../setup/README.org][Main Setup]] procedures. |