doc:appunti:hardware:raspberrypi_thermostat
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
doc:appunti:hardware:raspberrypi_thermostat [2017/01/02 23:44] – niccolo | doc:appunti:hardware:raspberrypi_thermostat [2022/01/12 16:51] – [Installing the old Adafruit LCD library] niccolo | ||
---|---|---|---|
Line 107: | Line 107: | ||
===== Adafruit LCD library ===== | ===== Adafruit LCD library ===== | ||
+ | |||
+ | To drive the PCD8544 LCD module we used the **Adafruit_Nokia_LCD** Python library, version 0.1.0 (this was in 2016, in 2021 we used version 0.2.0, which proved to work well and uses the same dependencies). It depends upon other Python libraries: | ||
+ | |||
+ | * **Adafruit_Nokia_LCD 0.1.0** | ||
+ | * **Adafruit_Python_GPIO 1.0.4** | ||
+ | * **Adafruit_PureIO 0.2.1 ** | ||
+ | * **RPi.GPIO 0.6.3** | ||
==== System software and libraries ==== | ==== System software and libraries ==== | ||
- | Some packages are required | + | Some packages are installed |
< | < | ||
- | apt-get install python-rpi.gpio | + | apt-get install |
</ | </ | ||
- | Then download and install the **[[https:// | + | We used version **0.2.1** of the **Adafruit-PureIO** library, more recent versions may have requirements which are not easy to satisfy on Debian 8 Jessie environment. |
+ | |||
+ | < | ||
+ | pip install --download | ||
+ | pip install / | ||
+ | </ | ||
+ | |||
+ | Download | ||
< | < | ||
Line 135: | Line 149: | ||
===== Configuration Optimization ===== | ===== Configuration Optimization ===== | ||
- | ==== Disabling the serial | + | ==== Disabling |
- | We decided to use GPIO 14 and 15 to drive the relays board, so we had to **prevent the use of the serial line** which uses that pins, otherwise there was an annoying flickering of the realys at boot time. Following | + | We decided to use GPIO 14 and 15 to drive the relays board, so we had to **prevent the use of the serial line** |
+ | |||
+ | We found some relevant info on this [[http:// | ||
+ | |||
+ | The **kernel console** on serial line is activated by some **''/ | ||
< | < | ||
console=ttyAMA0, | console=ttyAMA0, | ||
</ | </ | ||
- | |||
- | In Debian 8 Jessie we have the following part to remove: | ||
< | < | ||
console=serial0, | console=serial0, | ||
</ | </ | ||
+ | |||
+ | Also **removing the login shell** is different upon the version of the operating system. | ||
in **Debian 7 Wheezy** (running **sysvinit**) we commented out the line which spawns a login process from **''/ | in **Debian 7 Wheezy** (running **sysvinit**) we commented out the line which spawns a login process from **''/ | ||
Line 162: | Line 180: | ||
</ | </ | ||
+ | Do not use the **raspi-config** option to diasble the serial line, because it disables the serial device **''/ | ||
==== Kernel modules ==== | ==== Kernel modules ==== | ||
Line 172: | Line 191: | ||
</ | </ | ||
+ | Starting with Raspbian based upon Debian 8 Jessie, the I2C interface is disabled using the [[https:// | ||
+ | |||
+ | < | ||
+ | dtparam=i2c_arm=off | ||
+ | </ | ||
==== Headless Configuration ==== | ==== Headless Configuration ==== | ||
Line 544: | Line 568: | ||
* **bot.notifyOnMessage(handle)** => **bot.message_loop(handle)** | * **bot.notifyOnMessage(handle)** => **bot.message_loop(handle)** | ||
* **telepot.glance2(msg)** => **telepot.glance(msg)** | * **telepot.glance2(msg)** => **telepot.glance(msg)** | ||
+ | ===== 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** 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: | ||
+ | |||
+ | * **/ | ||
+ | * **/ | ||
+ | |||
+ | ==== Debian Packages ==== | ||
+ | |||
+ | The extra Debian packages required by the ProTherm software are: | ||
+ | |||
+ | * **busybox-syslogd** | ||
+ | * **ntp** | ||
+ | * **snmpd**, **rrdtool** | ||
+ | * **nginx**, **php-fpm**, | ||
+ | * **python3-pip** | ||
+ | * **python3-daemon**, | ||
+ | * **python3-python-telegram-bot** | ||
+ | * **python3-rpi.gpio** | ||
+ | * Required by the adafruit-circuitpython-pcd8544 library: | ||
+ | * **python3-serial** | ||
+ | * **python3-usb** | ||
+ | |||
+ | ==== Operating System Customization ==== | ||
+ | |||
+ | This is a brief check-out list of customizations required by the operating system. See above for more details. | ||
+ | |||
+ | * Disabled the **serial line console** from **/ | ||
+ | * Disabled **login on serial console** with '' | ||
+ | * Using **raspi-config**: | ||
+ | * Interface Options => I2C => No (this will insert '' | ||
+ | * Interface Options => SPI => Yes (this will insert '' | ||
+ | * Localisation Options => Timezone => **Europe/ | ||
+ | * Localisation Options => Locale => **en_US.UTF-8** | ||
+ | * Copied **PF Tempesta Seven** TTF fonts into ''/ | ||
+ | |||
+ | === CPU overload and kworker === | ||
+ | |||
+ | There is a problem with the old **Raspberry Pi model B** and the new Debian Bullseye, with **top** you can view several processes takin about 10% od the CPU during idle time: | ||
+ | |||
+ | < | ||
+ | kworker/ | ||
+ | kworker/ | ||
+ | </ | ||
+ | |||
+ | I turned out that an idle eth0 causes that, just disable eth0 to restore the CPU to 0.3%: | ||
+ | |||
+ | < | ||
+ | ifconfig eth0 down | ||
+ | </ | ||
+ | |||
+ | 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 **/ | ||
+ | |||
+ | < | ||
+ | denyinterfaces eth0 | ||
+ | </ | ||
+ | |||
+ | * **[[https:// | ||
+ | * **[[https:// | ||
+ | |||
+ | === Nginx and PHP === | ||
+ | |||
+ | You can verify that the web server and PHP are running: | ||
+ | |||
+ | < | ||
+ | systemctl status nginx.service | ||
+ | systemctl status php7.4-fpm.service | ||
+ | </ | ||
+ | |||
+ | Check also if the socket **/ | ||
+ | |||
+ | < | ||
+ | index index.php index.html index.htm index.nginx-debian.html; | ||
+ | |||
+ | location ~ \.php$ { | ||
+ | include snippets/ | ||
+ | # With php-fpm (or other unix sockets): | ||
+ | fastcgi_pass unix:/ | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you want Nginx to log to syslog instead that into a file, edit **/ | ||
+ | |||
+ | < | ||
+ | access_log syslog: | ||
+ | error_log syslog: | ||
+ | </ | ||
+ | |||
+ | ==== Installing the new Adafruit LCD library ==== | ||
+ | |||
+ | **WARNING**: | ||
+ | |||
+ | **WARNING**: | ||
+ | |||
+ | {{.: | ||
+ | |||
+ | |||
+ | Nowdays (2021), to operate the Nokia LCD, we should use the **[[https:// | ||
+ | |||
+ | To download the library **adafruit-circuitpython-pcd8544** and its dependencies without installing them: | ||
+ | |||
+ | < | ||
+ | mkdir -p / | ||
+ | cd / | ||
+ | pip3 download adafruit-circuitpython-pcd8544 | ||
+ | </ | ||
+ | |||
+ | Some dependencies (libraries) exist as Debian Bullseye packages, but the version does not meet the requirements, | ||
+ | |||
+ | < | ||
+ | dpkg --purge python3-sysv-ipc | ||
+ | dpkg --purge python3-ftdi1 | ||
+ | </ | ||
+ | |||
+ | To avoid automatic dependency resolution (and related download/ | ||
+ | |||
+ | < | ||
+ | pip3 install --no-deps adafruit_circuitpython_pcd8544-1.2.6-py3-none-any.whl | ||
+ | pip3 install --no-deps Adafruit_Blinka-6.18.0-py3-none-any.whl | ||
+ | pip3 install --no-deps adafruit_circuitpython_busdevice-5.1.1-py3-none-any.whl | ||
+ | pip3 install --no-deps adafruit_circuitpython_framebuf-1.4.8-py3-none-any.whl | ||
+ | pip3 install --no-deps Adafruit_PlatformDetect-3.19.1-py3-none-any.whl | ||
+ | pip3 install --no-deps Adafruit_PureIO-1.1.9-py3-none-any.whl | ||
+ | pip3 install --no-deps pyftdi-0.53.3-py3-none-any.whl | ||
+ | pip3 install --no-deps rpi_ws281x-4.3.1-cp39-cp39-linux_armv6l.whl | ||
+ | pip3 install --no-deps sysv_ipc-1.1.0-cp39-cp39-linux_armv6l.whl | ||
+ | </ | ||
+ | |||
+ | We can test the library using the **[[https:// | ||
+ | |||
+ | <code python> | ||
+ | dc = digitalio.DigitalInOut(board.D23) | ||
+ | cs = digitalio.DigitalInOut(board.D8) | ||
+ | reset = digitalio.DigitalInOut(board.D24) | ||
+ | </ | ||
+ | |||
+ | Our LCD hardware works at best with **display.bias = 5** and **display.contrast = 57**. | ||
+ | |||
+ | ==== Installing the old Adafruit LCD library ==== | ||
+ | |||
+ | As reported above, the new **Adafruit CircuitPython PCD8544** library is more CPU intensive than the old discontinued one **[[https:// | ||
+ | |||
+ | We download the library from its **GitHub repository** and install it using **pip3**: | ||
+ | |||
+ | < | ||
+ | cd / | ||
+ | git clone https:// | ||
+ | cd Adafruit_Nokia_LCD/ | ||
+ | pip3 install . | ||
+ | </ | ||
+ | |||
+ | ==== Installing the Telegram Bot Python library ==== | ||
+ | |||
+ | FIXME: Into the Debian 11 Bullseye distribution there is the **python3-python-telegram-bot** package, which is a Telegram Bot Python library actively mantained, whereas the old Telepot library was abandoned. So we need to port our program to the new library and update these instruction. | ||
+ | |||
+ | We wish to save locally the Telegram client library **telepot**, | ||
+ | |||
+ | < | ||
+ | pip3 download telepot | ||
+ | ... | ||
+ | Successfully downloaded telepot aiohttp urllib3 typing-extensions | ||
+ | attrs async-timeout multidict chardet yarl idna | ||
+ | </ | ||
+ | |||
+ | The **library** and its **dependencies** will be downloaded (not installed) in current directory. Now we install the dependencies from the Debian respository, | ||
+ | |||
+ | < | ||
+ | apt-get install python3-aiohttp python3-urllib3 python3-chardet python3-idna | ||
+ | </ | ||
+ | |||
+ | Finally we install the telepot package from the local copy: | ||
+ | |||
+ | < | ||
+ | pip3 install telepot-12.7-py3-none-any.whl | ||
+ | </ | ||
+ | |||
+ | ==== 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 **/ | ||
+ | |||
+ | First of all we create an //udev// rule; the rule will force //systemd// to create a " | ||
+ | |||
+ | < | ||
+ | # Inform systemd when the kernel device / | ||
+ | # Systemd will create a systemd device named " | ||
+ | # can be used to trigger the start of a service. | ||
+ | ACTION==" | ||
+ | </ | ||
+ | |||
+ | 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**, | ||
+ | |||
+ | 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): | ||
+ | |||
+ | < | ||
+ | systemctl list-units --all --type=device | ||
+ | ... | ||
+ | dev-spidev0.0.device | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | 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 **/ | ||
+ | |||
+ | < | ||
+ | [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=/ | ||
+ | ExecStart=/ | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | </ | ||
+ | |||
===== Web references ===== | ===== Web references ===== | ||
doc/appunti/hardware/raspberrypi_thermostat.txt · Last modified: 2022/01/12 17:11 by niccolo