リモートプロシジャーで温度等の情報を取得することを考えたが、よくわからん。
それでは1号機で作成したMariaDBの情報を介して、2号機のTFTDisplayに温度、湿度、気圧、照度の情報を表示させる。でやってみる。ひょっとしてこれが、簡単かも(技術に逃げているかも。。。)
【1】MariaDBインストール 1号機 上段ラズパイ
mariadbをインストール
■一括LAMPインストール apacheを選択
sudo apt-get -y install apache2 php php-dev php-fpm php-gd mariadb-server php-mysql phpmyadmin perlmagick
■cgi.loadファイルの設定。
シンボリックリンクを /etc/apache2/mods-enabled/cgi.loadに設定して cgi.loadを有効にする。
sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load
■ PHP 8.4 FPM有効【一応】
sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.4-fpm
sudo systemctl reload apache2
■apache2.conf 編集
sudo vim /etc/apache2/apache2.conf
内容は下記の通り、一番下に追加
Include /etc/phpmyadmin/apache.conf
■再起動
sudo service apache2 restart
■確認①apache
http://xxx.xxx.xxx.xxx/
■確認②phpmyadmin
http://xxx.xxx.xxx.xxx/phpmyadmin/
【2】ユーザ作成
■ログイン
sudo mysql
権限を付与すると上記は使用不能
■権限設定(すべて)/ユーザ作成
passwordの部分は変更すること。
CREATE USER 'miyamo'@'%' IDENTIFIED BY 'password';
■ルートユーザのパスワード変更
以下でphpmyadminにログイン成功。
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
・設定の反映
flush privileges;
quit
■外部接続可能とする。
sudo cp /etc/mysql/mariadb.conf.d/50-server.cnf /etc/mysql/mariadb.conf.d/50-server.cnf.bak
sudo vim /etc/mysql/mariadb.conf.d/50-server.cnf
以下を変更
bind-address = 127.0.0.1
↓
bind-address = 0.0.0.0
【3】ホスト名設定 上段ラズパイ、下段ラズパイ共に設定
■ホスト名設定
sudo cp /etc/hosts /etc/hosts.bak
sudo vim /etc/hosts
以下 追加
XXX.XXX.XXX.XXX miyamodb
【4】DB作成、テーブル作成
■ルートユーザでログイン ユーザ権限付与
sudo mysql -u root -p
GRANT ALL ON *.* TO 'miyamo'@'%';
■DB作成、テーブル作成
CREATE DATABASE miyamodb;
use miyamodb
create table room_env (id INT NOT NULL PRIMARY KEY, Temperature varchar(20), humidity varchar(20), air_pressure varchar(20), illuminance varchar(20),upd_datetime datetime default(CURRENT_TIMESTAMP));
■テーブル確認
show tables;
■テーブル構造確認
describe room_env;
■初期データ作成
insert into room_env values (1,'Temperature','humidity','air_pressure','illuminance',default);
quit
【5】mariadbclientのインストール 上段ラズパイ、下段ラズパイ共にインストール
上段ラズパイはクライアント入っていると出ます。
sudo apt-get install libmariadb-dev
sudo apt install -y mariadb-client
【6】上段ラズパイはMariaDBをPythonで操作。下段ラズパイもMariaDBをPythonで操作するので以下の操作を実施■上段&下段 Pythonライブラリをインストール
1)仮想環境を起動するためのファイル群を生成
python3 -m venv ~/vpy
2)仮想環境にライブラリをインストール。これで入った。
source ~/vpy/bin/activate
pip3 install mysql-connector-python
deactivate
■下段ラズパイ adafruit-circuitpython-rgb-displayドライバインストール
source ~/vpy/bin/activate
pip3 install --upgrade adafruit-python-shell
pip3 install adafruit-circuitpython-rgb-display
pip install adafruit-circuitpython-st7735r ;これは不要のはず。
pip install adafruit-circuitpython-busdevice
pip3 install --upgrade setuptools
sudo apt install python3-rpi.gpio python3-spidev python3-pip python3-pil python3-numpy
pip3 install --upgrade adafruit-python-shell
pip3 install Sphinx sphinx-rtd-theme Adafruit-PlatformDetect
pip3 install Adafruit-Blinka ;これも不要のはず。
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo -E env PATH=$PATH python3 raspi-blinka.py
---なんかうまくいかない。 以下を実行。
python3 -m pip install --force-reinstall adafruit-blinka
pip3 install RPi.GPIO
■画像処理関連 Trixieでの pillowは 以下とする。
依存関係をインストールしてからインストールするとうまくいく。あとpillowのverは11.0.0でないとtruetypefontのインポートでエラーとなる。
苦労しました。
sudo apt install libjpeg-dev zlib1g-dev libtiff-dev liblcms2-dev libwebp-dev libfreetype6 libfreetype6-dev
pip install Pillow==11.0.0
----以下で確認 blinkaテスト
source ~/vpy/bin/activate
python3 blinkatest.py
»==== 以下 確認結果
Hello, blinka!
Digital IO ok!
I2C ok!
SPI ok!
done!
blinkatest.pyのコード
import board
import digitalio
import busio
print("Hello, blinka!")
# Try to create a Digital input
pin = digitalio.DigitalInOut(board.D4)
print("Digital IO ok!")
# Try to create an I2C device
i2c = busio.I2C(board.SCL, board.SDA)
print("I2C ok!")
# Try to create an SPI device
spi = busio.SPI(board.SCLK, board.MOSI, board.MISO)
print("SPI ok!")
print("done!")
リブートするかを聞いてくるので、「y」でreboot
画像表示してみる 以下 参考URL
https://learn.adafruit.com/1-8-tft-display/python-usage
1)画像表示 test_image.py で保存&実行
import digitalio
import board
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw
from time import sleep
import adafruit_rgb_display.st7735 as st7735
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)
BAUDRATE = 24000000
# Create Display
disp = st7735.ST7735R(
board.SPI(),
rotation=270,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
print("step1")
# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
if disp.rotation % 180 == 90:
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
else:
width = disp.width # we swap height/width to rotate it to landscape!
height = disp.height
image = Image.new("RGB", (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image)
image = Image.open("/home/pi/blinka.jpg")
print("step2")
# Scale the image to the smaller screen dimension
image_ratio = image.width / image.height
screen_ratio = width / height
if screen_ratio < image_ratio:
scaled_width = image.width * height // image.height
scaled_height = height
else:
scaled_width = width
scaled_height = image.height * width // image.width
image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
# Crop and center the image
x = scaled_width // 2 - width // 2
y = scaled_height // 2 - height // 2
image = image.crop((x, y, x + width, y + height))
# Display image.
disp.image(image)
print("step end")
import digitalio
import board
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw
from time import sleep
import adafruit_rgb_display.st7735 as st7735
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)
BAUDRATE = 24000000
# Create Display
disp = st7735.ST7735R(
board.SPI(),
rotation=270,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
print("step1")
# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
if disp.rotation % 180 == 90:
height = disp.width # we swap height/width to rotate it to landscape!
width = disp.height
else:
width = disp.width # we swap height/width to rotate it to landscape!
height = disp.height
image = Image.new("RGB", (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image)
image = Image.open("/home/pi/blinka.jpg")
print("step2")
# Scale the image to the smaller screen dimension
image_ratio = image.width / image.height
screen_ratio = width / height
if screen_ratio < image_ratio:
scaled_width = image.width * height // image.height
scaled_height = height
else:
scaled_width = width
scaled_height = image.height * width // image.width
image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
# Crop and center the image
x = scaled_width // 2 - width // 2
y = scaled_height // 2 - height // 2
image = image.crop((x, y, x + width, y + height))
# Display image.
disp.image(image)
print("step end")
2)test_textimage.py で保存&実行
カラーコードは以下URLね。
https://rgb.to/html-color-names/1
ソース
import digitalio
import board
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import st7735
# First define some constants to allow easy resizing of shapes.
BORDER = 20
FONTSIZE = 24
# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D24)
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000
# Create Display
disp = st7735.ST7735R(
board.SPI(),
rotation=270,
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
)
print("step1")
# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
width = 160
height = 128
image = Image.new("RGB", (width, height))
print("step2")
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
print("step3")
# Draw a green filled box as the background
draw.rectangle((0, 0, width, height), fill=(0,0,0))
disp.image(image)
print("step4")
# Load a TTF Font
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE)
# font = ImageFont.load_default()
print("step5")
# Draw Some Text
text = "Hello World1!"
#(font_width, font_height) = font.getsize(text)
#draw.text(
# (width // 1 - font_width // 1, height // 2 - font_height // 2),
# text,
# font=font,
# fill=(255, 255, 0),
#)
draw.text(
(0, 64),
text,
font=font,
fill=(176, 224, 230),
)
print("step6")
# Display image.
disp.image(image)
text = "Hello World2!"
#(font_width, font_height) = font.getsize(text)
#draw.text(
# (width // 1 - font_width // 1, height // 1 - font_height // 1),
# text,
# font=font,
# fill=(255, 248, 220, 0),
#)
draw.text(
(0,100),
text,
font=font,
fill=(154, 205, 50),
)
# Display image.
disp.image(image)
print("step end")

