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?

監視カメラ ホップ としようとしたが、最新OSだと色々と問題があるらしいので 2号機 入れ直し。

Last updated at Posted at 2024-12-26

1.以下の理由からOSを Bullseyeにダウングレードする。
GPIOライブラリが一部非対応
pipで仮想環境が必要
Bookwormに対応していないパーツがある 
参考サイト
 https://sozorablog.com/raspberrypi_initial_setting/
なるほど。カメラがpythonライブラリで動かなかったのはこのせいだと思う。
まあ色々やった記録がキータにあるので問題無し。
はじめからやり直し。

1.2号機 Mysqlクライアント設定

sudo apt-get install libmariadb-dev
sudo apt install -y mariadb-client

2.関連ライブラリインストール adafruit以降はTFT関連ライブラリ。

sudo pip3 install  mysql-connector-python
sudo apt install python3-rpi.gpio python3-spidev python3-pip python3-pil python3-numpy
sudo pip3 install --upgrade adafruit-python-shell
sudo 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

3.画像表示 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

4.テキスト表示
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

5.まずは接続できるかを確認。

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.ホスト名設定

sudo cp /etc/hosts /etc/hosts.bak
sudo vim /etc/hosts

 以下 追加
XXX.XXX.XXX.XXX miyamodb

4.room_env_disp2.py 実行で 以下で表示された。 

IMG_0040.jpg

5.cronに登録 5分毎に実行。 

sudo crontab -e

*/5 * * * * /usr/bin/python3 /home/pi/room_env_disp2.py >> /tmp/cron.log 2>&1

6.FW設定
1).インストール

sudo apt install ufw

2).有効化

$ sudo ufw enable

3).ポート開放 http,https,samba,vnc,ssh,mysql

sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 445
sudo ufw allow 5900
sudo ufw allow 22
sudo ufw allow 3306

4).リロード

sudo ufw reload

5).ルール確認

sudo ufw status

7.人感センサー設定
1).wakeonlan.pyの作成&実行。Cronからの実行で、うまくいかなかったので記述変えました。
node.js 忘れてた。
https://qiita.com/m_sunafukin77/items/b6c988f5e7a8f367b071

#!/usr/bin/ python3
from datetime import datetime
import time
import RPi.GPIO as GPIO
import subprocess

INTERVAL = 3
SLEEPTIME = 20
GPIO_PIN = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN, GPIO.IN)
cmd = "sudo node /home/pi/etherwake3.js"

if __name__ == '__main__':
    try:
        print ("cancel CTRL+C")
        cnt = 1
        while True:
            # sencer touch
            if(GPIO.input(GPIO_PIN) == GPIO.HIGH):
                print(datetime.now().strftime('%Y/%m/%d %H:%M:%S') +
                      ":" + str("{0:05d}".format(cnt)) + " human look")
                print("pc wake")
                subprocess.check_call(cmd, shell=True) 
                break
            else:
                print(GPIO.input(GPIO_PIN))
                time.sleep(INTERVAL)
    except KeyboardInterrupt:
        print("stop runnig...")
    finally:
        GPIO.cleanup()
        print("GPIO clean")

2)クーロンの登録

crontab -e

ひまじんの在宅タイミングに合わせて実行。
0 8 * * * /usr/bin/python3 /home/pi/wakeonlan.py
0 11 * * * /usr/bin/python3 /home/pi/wakeonlan.py
0 14 * * * /usr/bin/python3 /home/pi/wakeonlan.py
0 18 * * * /usr/bin/python3 /home/pi/wakeonlan.py

8.USB設定
1)パーテイションのUUIDを記録

$sudo blkid /dev/sda1
/dev/sda1: UUID="xxxxxxxxxxxxxxxxxxxxx" TYPE="ext4" PARTUUID="xxxxxx-01"
$sudo blkid /dev/sda2
/dev/sda2: UUID="xxxxxxxxxxxxxxxxxxxxx" TYPE="ext4" PARTUUID="xxxxxx-02"

2)シンボリックリンク確認

$ls /dev/disk/by-uuid/ 

3)マウントポイント設定
$sudo mkdir /usb
$cd /usb
$sudo mkdir 21g
$sudo mkdir 10g

4)マウント
$sudo vim /etc/fstab
以下を追加。

fstab
UUID="xxxxxxxxxxxxxxxxxxxxx"  /usb/21g ext4 defaults,noatime 0 0
UUID="xxxxxxxxxxxxxxxxxxxxx"  /usb/11g ext4 defaults,noatime 0 0

5)リブート
$df -h で確認これで入れ直し完了。

8.Samba設定忘れてた。
https://qiita.com/m_sunafukin77/items/930317457a7c845b28cb

9.時刻同期もね
https://qiita.com/m_sunafukin77/items/76b9536db4beb4eb72d6
以下で そんなもん無いとでるが、無視。

sudo apt remove systemd-timessyncd

以下 モジュールがないとでてエラーとなるが、時刻は同期されている。

udo apt install nptdate
sudo ntpdate ntp.nict.jp && date
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?