Skip to content

Hotplug sensor examples #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 16, 2022
Merged

Conversation

PaulskPt
Copy link
Contributor

@PaulskPt PaulskPt commented May 15, 2022

This is my first PR. I was guided all the way towards this PR by @FoamyGuy.
This PR adds two examples that both have a functionality to hotplug external sensors.
Both examples use the new TabLayout class that @FoamyGuy recently has added to the Adafruit_CircuitPython_DisplayIO_Layout library. The second example uses the Touch capabilities of the PyPortal.

The particularities of the in this PR presented 'hotplug sensor' algorithm are:

  • when a sensor is disconnected the script will not crash. The script will notify the user: a) that the sensor has found disconnected; ask the user to check the wiring;
  • In the mean time the main loop of the script in main() will continuously check for reconnection;
  • If the sensor is reconnected the script will continue to get data from the sensor as the script did before.
    The first example uses two external devices/sensors:
    a) an Adafruit TMP117 sensor;
    b) an external RTC module, DS3231.
    both connected through I2C.

The second example is an expanded version of the first example.
As the first example also this script is able to read data from an external temperature sensor.
This script is able to use an external RTC. The preferred setting is to use a NTP server.
The script uses the Adafruit TouchScreen library. For the touch of the Tabs to be reliable a calibration is advised. I made use of the calibration script by CedarGrove Studios ( https://github.com/CedarGroveStudios/Touchscreen_Calibrator/blob/main/touch_calibrator_built_in.py).
The calibration results have to be inserted at line 410 ( ts = adafruit_touchscreen.Touchscreen()), replacing what is there at 'calibration=((6815, 60095), (10520, 58007)),'
Both example scripts use a gVars class to set/get globally used variables. This eliminates the need to use global variables.
The gVars class has been created because we were confronted with pylint warnings about using global variables.
Both example scripts show the date and time in the Tab 'Three'.
Both examples have a gVars flag "temp_in_fahrenheit". When set to 'True' the temperature will be displayed in degrees Fahrenheit. When set to 'False' in degrees Celsius. See image #4 below.
They display the temperature from the sensor in Tab 'Four'.
The second example script will notify the user in Tab 'Three' how many mm:ss a next NTP synchronization is due. Current resync is every 5 minutes.
This is done because we experienced that the internal clock runs ahead in time. Resync with NTP is crucial.
The second example, because of connecting to the internet, needs user-filled-in data from the file secrets.py.
A file secrets.py with default, unpersonalized, values is added to this PR.
This example also uses as a background image the image: BGimage4.bmp which is inside the subfolder /images.
About debugging: Both example scripts use a gVars variable "my_debug" which, when set to 'True' will have the script output more info about it's state/situation (I prefer to call it: 'debug information'). In the first example this flag is set in line 146. In the second example in line 172.
Initial clock setting: in both examples the clock will be set at startup by setting the globally used gVars variable "default_dt". In the first example this is in line 149. In the second example in line 175. The second example, when not using an external RTC device (determined by the setting of gVars flag "use_ntp" in line 200), it will use the date and time from an NTP server. The internal clock is set in line 341: "ntp.set_time(tz_offset)". If exists a valid time from NTP then "default_dt" is set in line 363.
About performance: The time for the second example script to become in a 'Ready...' state it takes about 54 seconds from powering up. Tapping on Tab 'Four' to display the temperature value - only the first time - takes about 5 seconds. After this one can tap from one Tab to the other with no significant delays.
ToDo: investigate/experiment how to improve the time from boot to the 'Ready...' moment;
During a recent Deep Dive with @FoamyGuy, viewers of this stream were enthousiastic about this sensor 'hotplug' feature. That is why @FoamyGuy advised me to create this PR.

The images below are taken while running the second example (the one using date and time from an NTP server).

IMG_5112

IMG_5113

IMG_5114

2022-05-16_temp_in_degs_F

PaulskPt added 15 commits May 12, 2022 12:47
According suggestions of @FoamyGuy, I took out 4 out of the 5 examples I created. I also took out the /images/bgimg4.bmp. This all for the sake of being able to publish a PR in a next step.
mod dict pages to use different Tab texts when using a PyPortal model Titano
Index to dictionary pages has to start at 1, not at 0 (which is a 'dummy)
Mods necessary to eliminate global variables as much as possible.
Deleted flags: rtc_present, t_sensor_present. Replaces them by tests like: 'if rtc is not None'
Now all globals used before are contained in an added gVars class.
Example using datetime from NTP server, Touch interface. Using gVars class to set/get globally used variables.
Added BGimage4.bmp and it's .license file
@tekktrik tekktrik requested a review from a team May 16, 2022 02:18
PaulskPt added 3 commits May 16, 2022 14:11
In gVars class added flag "temp_in_fahrenheit". In second example modified algorithm to display the temperature sensor value in degrees Celsius or in degrees Fahrenheit depending the flag.
See lines 211 , 667-670 and 725-726.
As just done with the second example. Added possibility to switch temperature displayed in degrees Celsius or degrees Fahrenheit by setting the gVars flag variable "temp_in_fahrenheit"
Now example adafruit#1 displays the temperature in degrees Celsius while example adafruit#2 displays the temperature in degrees Fahrenheit.
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change request to make the usage of Arial font optional or if it remains non-optional then we'll need to add the font file and a license for it to the repo.

Beyond that, these changes are looking good to me. Tested successfully on PyPortal Titano.

Added the Arial-16.bdf font and it's license file
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Tested successfully on PyPortal Titano. @PaulskPt Thanks again for making this example and working through all of the checks and things to share it in repo.

@FoamyGuy FoamyGuy merged commit 772c3bf into adafruit:main May 16, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 17, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout to 1.19.2 from 1.19.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_DisplayIO_Layout#73 from PaulskPt/hotplug_sensor_examples
  > Patch .pre-commit-config.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants