Raspberry Pi 4 Model B

メール送信
センサモジュール
自動起動
ピン接続

Raspberry Pi

【免責】 製作したことの結果については、
一切責任を負いかねますのでご了承下さい。

メール送信

#!/usr/bin python3

import smtplib
from email.utils import formatdate
from email.mime.text import MIMEText

if __name__ == "__main__":
pmail_addr = "username"
pmail_pass = "password"
SMTP = "mailserver"
PORT = 587

from_addr = pmail_addr
to_addr = "receiver"
subject = "title"
body = "letterbody"

msg = MIMEText(body, "plain", "utf-8")

msg["From"] = from_addr
msg["To"] = to_addr
msg["Date"] = formatdate()
msg["Subject"] = subject

try:
print("sending")
send = smtplib.SMTP(SMTP, PORT)
send.ehlo()
send.login(pmail_addr, pmail_pass)
send.send_message(msg)
send.close()
except Exception as e:
print("error: " + str(e))
else:
print("{0} completion".format(to_addr))

センサモジュール

BME280 温湿度 気圧

I2C 有効化
$ sudo raspi-config

pip モジュール
$ pip install bme280
$ pip install smbus2
$ i2cdetect -y 1

#!/usr/bin python3

from bme280 import bme280
from bme280 import bme280_i2c

def main():
    bme280_i2c.set_default_i2c_address(0x76)
    bme280_i2c.set_default_bus(1)

    bme280.setup()
    data_all = bme280.read_all()

    print("%7.2f C" % data_all.temperature)
    print("%7.2f %%" % data_all.humidity)
    print("%7.2f hPa" % data_all.pressure)
    return

if __name__ == "__main__":
    main()

自動起動

Raspbian

$ apt-get install chkconfig
$ chkconfig cron
$ sudo systemctl enable cron
$ sudo systemctl start cron
$ crontab -e

/tmp/crontab.GkcL6Y/crontab

*/10 * * * * python3 raspberry.py

ピン接続

  GPIO BME280
1 3.3V VIN
6 GND GND
5 SCL1 SCL
3 SDA1 SDA

DATASHEET
Raspberry Pi 4 Model B
BME280 BOSCH
August 15, 2022
The following clause.
マイコン制御部門