■2号機の設定
1.まずは接続できるかを確認。
source ~/vpy/bin/activate
mysql -h 192.168.1.XX -P 3306 -u miyamo -p
use miyamodb
select * from room_env
で内容表示されるので、環境的にはOK。
2.pythonを room_env_disp2.py で保存作成。
import digitalio
import board
import RPi.GPIO as GPIO
from PIL import Image, ImageDraw, ImageFont
from adafruit_rgb_display import st7735
import mysql.connector
# 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)
# db connect
def main():
# mysql connect
cnt = mysql.connector.connect(
host='miyamodb',
port='3306',
db='miyamodb',
user='miyamo',
password='miyamoXXXX',
charset='utf8'
)
db = cnt.cursor(buffered=True)
#room_env select
sql = 'select temperature, humidity, air_pressure, illuminance from room_env where id = 1'
db.execute(sql)
for (temperature, humidity, air_pressure, illuminance) in db:
print(f"{temperature} {humidity} {air_pressure} {illuminance}")
# Draw Some temp
text = f"{temperature}"
print(text)
(font_width, font_height) = font.getsize(text)
draw.text(
(0, 1),
text,
font=font,
fill=(176, 224, 230),
)
# Display image.
disp.image(image)
#Draw Some humi
text = f"{humidity}"
print(text)
(font_width, font_height) = font.getsize(text)
draw.text(
(0, 32),
text,
font=font,
fill=(154, 205, 50),
)
# Display image.
disp.image(image)
# Draw Some air_p
text = f"{air_pressure}"
print(text)
(font_width, font_height) = font.getsize(text)
draw.text(
(0, 64),
text,
font=font,
fill=(176, 224, 230),
)
# Display image.
disp.image(image)
# Draw Some illum
text = f"{illuminance}"
print(text)
(font_width, font_height) = font.getsize(text)
draw.text(
(0,100),
text,
font=font,
fill=(154, 205, 50),
)
# Display image.
disp.image(image)
# db.close
db.close()
# cnt.close
cnt.close()
if __name__ == "__main__":
main()
3.仮想環境で実行
source ~/vpy/bin/activate
room_env_disp2.py 実行で 以下で表示された。
4.cronに登録 5分毎に実行。
sudo crontab -e
*/5 * * * * cd /home/pi/ && /home/pi/vpy/bin/python room_env_disp2.py >> /tmp/cron.log 2>&1