0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

1号機で作成したMariaDBの情報を介して、2号機のTFTDisplayに温度、湿度、気圧、照度の情報を表示させる【準備】。

Last updated at Posted at 2024-12-13

リモートプロシジャーで温度等の情報を取得することを考えたが、よくわからん。
それでは1号機で作成したMariaDBの情報を介して、2号機のTFTDisplayに温度、湿度、気圧、照度の情報を表示させる。でやってみる。ひょっとしてこれが、簡単かも(技術に逃げているかも。。。)

【1】MariaDBインストール
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 7.3 FPM有効【一応】

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.3-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

権限を付与すると上記は使用不能
■権限設定(すべて)/ユーザ作成 5年前とはコマンンド変わっていた。
CREATE USER 'miyamo'@'%' IDENTIFIED BY 'password';
GRANT ALL ON . TO 'miyamo'@'%';

■ルートユーザのパスワード変更(初期パスワードが不明の為、Verが変わっていて/var/log/mysql.logが存在しない。導入時に設定したパスワードはルートユーザのパスワードとは違うかも。
とにかく以下で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 -p

■DB作成、テーブル作成
create databae 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ライブラリをインストール
pythonのライブラリをインストールする方法は仮想環境からに代わっていたんだ、ひょっとしてTFTのドライバも
導入がうまくいかなかったのはそのせいだ。pythonのほうが扱いやすいので、下段ラズパイも pythonでいく。
参考サイト:
https://qiita.com/toiee_kame/items/c3781abb53f385dae4f9

1)仮想環境を起動するためのファイル群を生成

python3 -m venv ~/vpy

2)仮想環境にライブラリをインストール。これで入った。

source ~/vpy/bin/activate
pip3 install  mysql-connector-python
deactivate

■下段ラズパイ adafruit-circuitpython-rgb-displayドライバインストール
digitalio,boardのライブラリは raspi-blinka.pyを導入することでimport可能となる。

source ~/vpy/bin/activate
sudo apt install python3-rpi.gpio python3-spidev python3-pip python3-pil python3-numpy
pip3 install --upgrade adafruit-python-shell
pip3 install adafruit-circuitpython-rgb-display
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo -E env PATH=$PATH python3 raspi-blinka.py

リブートするかを聞いてくるので、「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,
)

# 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("blinka.jpg")

# 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)

表示した。アダフルーツ社 なかなかやるな。 
IMG_0035.jpg

2)テキスト表示
ttributeError: 'FreeTypeFont' object has no attribute 'getsize'
要することに、メソッドが無くなっているのでpilのVerを下げろということですな。
とりあえず、Verを下げる。

pip install Pillow==9.5.0

カラーコードは以下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,
)

# 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))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a green filled box as the background
draw.rectangle((0, 0, width, height), fill=(0,0,0))
disp.image(image)

# Load a TTF Font
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONTSIZE)

# 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),
)

# 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),
)

# Display image.
disp.image(image)

よし、準備OK
IMG_0036.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?