User Tools

Site Tools


doc:appunti:hardware:raspberrypi_thermostat

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
doc:appunti:hardware:raspberrypi_thermostat [2022/01/08 18:43] – [Installing the Adafruit LCD library] niccolodoc:appunti:hardware:raspberrypi_thermostat [2022/01/12 17:11] (current) – [Installing the old Adafruit LCD library] niccolo
Line 570: Line 570:
 ===== Installing on Raspberry Pi OS 2021-10-30 Bullseye ===== ===== Installing on Raspberry Pi OS 2021-10-30 Bullseye =====
  
-The latest version of the ProTherm software should work on Debian 11 Bullseye, this means using **Python 3.9** both for the **ProTherm** package and for the **Telegram bot** daemon. Also the **Adafruit libraries** for the LCD are now required to be compatible with Python 3.+The latest version of the ProTherm software should work on Debian 11 Bullseye, this means using **Python 3.9** both for the **ProTherm** package and for the **Telegram bot** daemon. Also the **Adafruit libraries** used to drive the LCD should run on Python 3.
  
 I copied the **Raspberry Pi OS Lite** image onto the SD card, before the first bootstrap I enabled remote ssh and WiFi by creating two files into the boot partitions: I copied the **Raspberry Pi OS Lite** image onto the SD card, before the first bootstrap I enabled remote ssh and WiFi by creating two files into the boot partitions:
Line 615: Line 615:
 </code> </code>
  
-I turned out that an idle eth0 causes that, just disable it to restore the CPU to 0.3%. FIXME How to make the workaround permanent?+I turned out that an idle eth0 causes that, just disable eth0 to restore the CPU to 0.3%:
  
 <code> <code>
 ifconfig eth0 down ifconfig eth0 down
 </code> </code>
 +
 +To leave the interface down at bootstrap you must configure the **dhcpcd** daemon, which is repsponsible for network configuration in Raspberry Pi OS. Edit the file **/etc/dhcpcd.conf** and add the following line:
 +
 +<file>
 +denyinterfaces eth0
 +</file>
  
   * **[[https://forums.raspberrypi.com/viewtopic.php?t=317429|kworker processes consume CPU with the latest Raspberry Pi OS]]**   * **[[https://forums.raspberrypi.com/viewtopic.php?t=317429|kworker processes consume CPU with the latest Raspberry Pi OS]]**
Line 652: Line 658:
 </file> </file>
  
-==== Installing the Adafruit LCD library ====+==== Installing the new Adafruit LCD library ====
  
-Nowdays (2021), to operate the Nokia LCD, we used the **[[https://github.com/adafruit/Adafruit_CircuitPython_PCD8544|Adafruit CircuitPython PCD8544]]** Python library. Detailed instructions can be found on the page **[[https://learn.adafruit.com/nokia-5110-3310-monochrome-lcd|Nokia 5110/3310 Monochrome LCD]]**. In the past (2016) it was used the **[[https://github.com/adafruit/Adafruit_Nokia_LCD|Adafruit Nokia LCD]]** library, which is now phased-out. FIXME: It seems that the new library is very slow (about four times slower than the old one), I opened an **[[https://github.com/adafruit/Adafruit_CircuitPython_PCD8544/issues/16|issue on GitHub]]**.+**WARNING**: Do not use this library on the old Raspberry Pi model B, it is too CPU intensive! Use the old one. 
 + 
 +**WARNING**: The new library is more CPU intensive than the old one. I opened an **[[https://github.com/adafruit/Adafruit_CircuitPython_PCD8544/issues/16|issue on GitHub]]**. So I decided to give-up with the new library and use the old one (see below for installation tips). Here you can see a CPU utilization graph, the first half is using the old library, the second half is using the new one: 
 + 
 +{{.:raspberrypi:protherm-cpu-percent.png?direct&640|CPU usage with different Adafruit Python libraries}} 
 + 
 + 
 +Nowdays (2021), to operate the Nokia LCD, we should use the **[[https://github.com/adafruit/Adafruit_CircuitPython_PCD8544|Adafruit CircuitPython PCD8544]]** Python library. Detailed instructions can be found on the page **[[https://learn.adafruit.com/nokia-5110-3310-monochrome-lcd|Nokia 5110/3310 Monochrome LCD]]**. In the past (2016) it was used the **[[https://github.com/adafruit/Adafruit_Nokia_LCD|Adafruit Nokia LCD]]** library, which is now phased-out.
  
 To download the library **adafruit-circuitpython-pcd8544** and its dependencies without installing them: To download the library **adafruit-circuitpython-pcd8544** and its dependencies without installing them:
Line 694: Line 707:
  
 Our LCD hardware works at best with **display.bias = 5** and **display.contrast = 57**. Our LCD hardware works at best with **display.bias = 5** and **display.contrast = 57**.
 +
 +==== Installing the old Adafruit LCD library ====
 +
 +As reported above, the old and discontinued library **[[https://github.com/adafruit/Adafruit_Nokia_LCD|Adafruit Nokia LCD]]** is less CPU intensive than the new **Adafruit CircuitPython PCD8544**. Fortunately enough, the old library does run with Python 3, and I was able to use it on my old Raspberry Pi model B over the operating system derived from Debian 11 Bullseye. So the average CPU usage is **lower than 10%**, instead than **above 30%**!
 +
 +The library is not available from the **[[https://pypi.org/|PyPI]]** repository, so it must be downloaded from the GitHub repository. The library depends upon **Adafruit_GPIO**, which in turn requires **Adafruit_PureIO** and **spidev**. The two Adafruit libraries are available from PyPI, for **spidev** we installed the Debian packaged version.
 +
 +<code>
 +apt install python3-spidev
 +mkdir -p /usr/local/download/Adafruit-GPIO
 +cd /usr/local/download/Adafruit-GPIO
 +pip3 download Adafruit-GPIO
 +pip3 install Adafruit_PureIO-1.1.9-py3-none-any.whl
 +pip3 install Adafruit_GPIO-1.0.3-py3-none-any.whl
 +</code>
 +
 +We download the library from its **GitHub repository** and install it using **pip3**:
 +
 +<code>
 +cd /usr/local/download
 +git clone https://github.com/adafruit/Adafruit_Nokia_LCD.git
 +cd Adafruit_Nokia_LCD/
 +pip3 install .
 +</code>
 +
 +The dependencies tree is the following:
 +
 +  * Adafruit_Nokia_LCD
 +    * Adafruit_GPIO
 +      * Adafruit_PureIO
 +      * spidev
  
 ==== Installing the Telegram Bot Python library ==== ==== Installing the Telegram Bot Python library ====
Line 719: Line 763:
 pip3 install telepot-12.7-py3-none-any.whl pip3 install telepot-12.7-py3-none-any.whl
 </code> </code>
 +
 +==== Starting the protherm service at bootstrap ====
 +
 +Raspberry Pi OS based on **Debian 11 Bullseye** uses **systemd** to start services on bootstrap. The **protherm** service requires the **/dev/spidev0.0** device to be available on start, otherwise it starts with the LCD disabled. Unfortunately the timing at which the kernel enables the device cannot be predicted, so we have to take some precautions to let systemd wait for the spidev device before starting protherm.
 +
 +First of all we create an //udev// rule; the rule will force //systemd// to create a "systemd device" when the kernel device becomes available. The rule is created into a file named **/etc/udev/rules.d/protherm-spidev.rules**:
 +
 +<file>
 +# Inform systemd when the kernel device /dev/spidev0.0 becomes available.
 +# Systemd will create a systemd device named "dev-spidev0.0.device", which
 +# can be used to trigger the start of a service.
 +ACTION=="add", SUBSYSTEM=="spidev", KERNEL=="spidev0.0", TAG+="systemd"
 +</file>
 +
 +We use three keywords that must matched (because of the **%%==%%** sign) during an udev event. If the event is of type device **add**, the device belongs to the **spidev** subsystem and the kernel device name is **spidev0.0**, //udev// adds the string **systemd** to the **TAG** key associated to the device itself. 
 +
 +Systemd includes the **systemd-udevd.service** which will notice every device tagged witht the string **systemd** and dynamically creates a **device unit**. In this case the unit will be called **dev-spidev0.0.device**. You can ask systemd to list all the existing device units (because of the **%%--all%%** option, also the short name is listed):
 +
 +<code>
 +systemctl list-units --all --type=device
 +...
 +  dev-spidev0.0.device   loaded active   plugged /dev/spidev0.0
 +...
 +</code>
 +
 +If the systemd device does not exist, it probably means that the udev rule did not worked as expected. Thanks to this systemd device unit, we can write a protherm systemd unit that will start the service only after the device is instantiated by the kernel. The systemd unit will be **/etc/systemd/system/protherm.service**:
 +
 +<file>
 +[Unit]
 +Description=Programmable Thermostat Service
 +After=syslog.target
 +# Wait for spidev0.0 systemd device to be available.
 +After=dev-spidev0.0.device
 +Requires=dev-spidev0.0.device
 +
 +[Service]
 +Type=simple
 +WorkingDirectory=/tmp/
 +ExecStart=/usr/local/sbin/protherm -f
 +
 +[Install]
 +WantedBy=multi-user.target
 +</file>
  
 ===== Web references ===== ===== Web references =====
doc/appunti/hardware/raspberrypi_thermostat.1641663780.txt.gz · Last modified: 2022/01/08 18:43 by niccolo