Reading: The sample program database.py illustrates some of the techniques you need in your own programs as described below.
In this lab, you will connect to and access a remote PostgreSQL database.
Your BeaglePlay will need Internet access for Parts 2 and 3 of this lab.
In the last lab you created a database local to your BeaglePlay that holds temperature readings. Specifically, it records the time a reading was made and a temperature in degrees Celsius. Now it is necessary to augment that information with GPS location data as well.
The idea is that the location of your BeaglePlay might vary when it is deployed in the field (imagine it is in your vehicle as you drive around). With your device offline, it could gather temperature data from the onboard temperature sensor and GPS data from an onboard GPS unit and store that data in the local database. In the second part of this lab, you will connect to a remote database to upload your accumulated readings.
Proceed as follows:
The easiest thing to do is to delete your existing table and recreate a new one from scratch. This is reasonable because you don't have any actual data of interest in your table. In "real life" it might be necessary to make alterations to your table that don't destroy the data it contains. The steps for doing that are a bit more complicated, so we'll skip them here.
Connect to your local database with psql as you did in last week's lab. Then do:
debian=> DROP TABLE reading;
Modify your CREATE TABLE statement (in the file you created last week) to include two additional attributes: "latitude" with type NUMERIC(8, 6) and "longitude" with type NUMERIC(9, 6). These fields will hold your GPS location.
Modify your program from last week (I recommend making a copy first) so that it populates these new fields with some data. The precise location that you use isn't critical right now. Run your program to generate a file containing the necessary INSERT statement, and the executing that file with psql to insert data into your modified table.
Rename your program to observer.py. This is the program that observes temperature information and stores it locally. We will make further enhancements to this program later.
In this part, you will create a new Python program that reads data from your local database and inserts it into a remote database in the Azure cloud.
The idea is that your device will collection data and store it locally when it is offline, but when it comes online, it will transfer the data it collected to the cloud for later analysis. Other devices will be doing the same thing, so it is important that the records you send to the cloud include your system ID (as generated in Lab #2).
Proceed as follows:
Write a new program called archivist.py that connects to the local database and uses a SELECT command to read all the records stored there. For the first step, print those records to the console to verify that you are able to get this part working.
Modify the archivist program so that it also connects to the cloud database using this connection information:
host = springtail.postgres.database.azure.com port = 5432 dbname = temperature user = observer password = *********
In modern Python development it is considered a best practice to include "type hints" on all function parameters and results (also class attributes, but that isn't relevant here). Modify your program to include type hints in these places. You may also include type hints elsewhere if you wish, but it is not required (and not recommended when the types are "obvious").
Install the mypy type checker on your BeaglePlay using the following commands:
$ cd cis-2730 $ source venv/bin/activate $ pip install mypy
You can now run mypy over your file to statically (i.e., without running your program) check the typing information.
$ mypy observer.py $ mypy archivist.py
Submit your two programs (observer.py and archivist.py) to Canvas. Be sure they contain type hints as appropriate.
Last Revised: 2025-03-05
© Copyright 2025 by Peter Chapin <peter.chapin@vermontstate.edu>