――It's getting hot this year, but how hot should you use the cooler? ――It would be easier if you measure the discomfort index and get a notification saying "use the cooler because it's dangerous!" ... --A system that can save lives in the summer of Japan, where there is a risk of heat stroke even indoors ... ――I was doing it if I just connected the sensor to the Raspberry Pi and communicated, so why not make it?
――Since this article was written based on a memo made through trial and error, there may be omissions in the procedure ...
-Download and install Raspberry Pi Imager --Start and execute by specifying the OS and the sd card to write
ssh Empty file is OK wpa_supplicant.conf
wpa_supplicant.conf
country=JP
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="SSID"
psk="Encryption key"
}
You can connect from Win10 with teraterm etc., but since programming was basically done with Linux I am preparing to work by connecting to Raspberry Pi from Linux (Ubuntu) with ssh.
linux
ssh [email protected]
# pass:raspberry
raspberrypi
#sudo user creation
sudo adduser kabu0404
sudo usermod -aG sudo kabu0404
su kabu0404
#Various updates
sudo apt update
sudo apt upgrade
#Time zone/Locale setting
sudo timedatectl set-timezone Asia/Tokyo
sudo localectl set-locale ja_JP.UTF-8
#Finally sudo raspi-Maybe I messed with config
linux
#Ssh in the past-keygen -Since I was trsa, send the existing public key
cd ~/.ssh
scp ./id_rsa.pub [email protected]:~/.ssh/
raspberrypi
cd ~
chmod 700 .ssh
cd ~/.ssh
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys
rm -fv ~/.ssh/id_rsa.pub
--Connect via breadboard as shown in the table below
BME280 side | Pin number |
---|---|
VDD | 1(3.3V) |
GND | 9(GND) |
CSB | --- |
SDI | 3(I2C1_SDA1) |
SDO | 9(GND) |
SCK | 5(I2C1_SCL1) |
raspberrypi
# spi/Enable i2c & fix locale
sudo raspi-config
sudo chmod 666 /dev/i2c-1
#Check the connection status
i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
#Make sure you are connected to 76
#Check register contents
i2cdump -y 1 0x76
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
80: 8a 70 89 8e 83 0f fc 06 34 6e 56 66 32 00 8a 8e ?p??????4nVf2.??
90: 17 d6 d0 0b 5e 1d f0 ff f9 ff ac 26 0a d8 bd 10 ????^??.?.?&????
a0: 00 4b 40 00 00 00 00 00 00 00 00 00 33 00 00 c0 [email protected]..?
b0: 00 54 00 00 00 00 60 02 00 01 ff ff 1f 60 03 00 .T....`?.?..?`?.
c0: 00 00 00 ff 00 00 00 00 00 00 00 00 00 00 00 00 ................
d0: 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 `...............
e0: 00 67 01 00 14 05 00 1e 57 41 ff ff ff ff ff ff .g?.??.?WA......
f0: ff 00 00 00 00 00 00 80 00 00 80 00 00 80 00 80 .......?..?..?.?
#Register settings
#Check the value of the operation setting register
echo -n '0xF5:' ; i2cget -y 1 0x76 0xF5 ; \
echo -n '0xF4:' ; i2cget -y 1 0x76 0xF4 ; \
echo -n '0xF2:' ; i2cget -y 1 0x76 0xF2
#Operation setting system register value setting
# i2cset -y 1 0x76 [Internal address] [value]
i2cset -y 1 0x76 0xF5 0xA0 ; \
i2cset -y 1 0x76 0xF4 0x27 ; \
i2cset -y 1 0x76 0xF2 0x01
#If you don't want to do anything special, the above settings should be OK
#Acquisition of temperature / humidity / atmospheric pressure values
i2cdump -r 0xF7-0xFE -y 1 0x76
No size specified (using byte-data access)
0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef
f0: 54 0b 00 84 00 00 74 62 T?.?..tb
#It should be OK if you can get the value like this
#It seems that data can be output neatly if you do well using the values of other registers.
The kit is a product of Akizuki Denshi, but since the sensor itself is the same, I use SWITCHSCIENCE sample code.
raspberrypi
mkdir i2ctest
cd i2ctest/
wget https://github.com/SWITCHSCIENCE/samplecodes/archive/master.zip
unzip master.zip
#What you need after deployment
# i2ctest/BME280/Python27/bme280_sample.Because it is only py
#Copy and paste the code contents to the file touched under any Dir is enough
cd BME280/Python27/
sudo python bme280_sample.py
temp : 28.37 ℃
pressure : 1003.83 hPa
hum : 51.99 %
-I got the token by referring to Send LINE message with Python.
bme280_sample.py
#Function for temperature
def compensate_T(adc_T):
#Omission
# print "temp : %-6.2f ℃" % (temperature)
return temperature
#Humidity function
def compensate_H(adc_H):
#Omission
# print "hum : %6.2f %" % (var_h)
return var_h
bme280_sample.py
def readData():
#Omission
temp = compensate_T(temp_raw)
#Do not use atmospheric pressure
# compensate_P(pres_raw)
hum = compensate_H(hum_raw)
#Calculate the discomfort index and send a notification if it exceeds 80
discomfortIndex = 0.81 * temp + 0.01 * hum * ( 0.99 * temp - 14.3 ) + 46.3
if discomfortIndex > 80:
sendNotice(temp, hum, discomfortIndex)
-Coding with reference to Send LINE message with Python
bme280_sample.py
def sendNotice(temp, hum, discomfortIndex):
url = 'https://notify-api.line.me/api/notify'
token = #Set token for notification
headers = {'Authorization' : 'Bearer ' + token}
now = datetime.datetime.now()
message = \
'\r\n date:' + now.strftime('%Y/%m/%d') + \
'\r\n time:' + now.strftime('%H:%M:%S') + \
'\r\n index:%5.2f' % (discomfortIndex) + \
'\r\n Temperature:%5.2f ℃' % (temp) + \
'\r\n Humidity:%5.2f %' % (hum)
payload = {'message' : message}
r = requests.post(url, headers=headers, params=payload)
Set periodic execution with cron by referring to Before using crontab for the first time with RaspberryPi3
lineNoticeCron
#Example of running every 5 minutes
*/5 * * * * kabu0404 sudo python /home/kabu0404/i2ctest/BME280/Python27/bme280_sample.py
LINE Nortify ――Once you get the token, all you have to do is send a request, so it's easy. ――It seems that you can send notifications to the group talk room, so it may be quite usable. I will read the official docs when I have time
――I2C? SPI? What was that? I had a hard time because it was a level. I still don't understand. ――It seems that it is practically essential to try something with Raspberry Pi, so it seems better to study a little more.
――It was a part that I could skip, but I was a little stuck in how to read it.
――The current understanding is below, please point out if you make a mistake
--hoge <n: m>
and fuga [n: m]
mean to use the values of the nbit to mbit.
--hoge <0>
and fuga [0]
are omitted because it is redundant to write 0: 0 if the value used is only one digit.
Python ――Simply not enough study
――Place it in the house of a family who dislikes coolers and notify the group line that "It may be hot but you may not have a cooler!" ――Place it outside the window and notify that "it's cool enough to turn off the cooler"
――It looks interesting to combine IFTTT and smart home appliances (though I don't have one) ――I wonder if you can't do anything interesting in combination with the GPS that has been connected.
-Set up "Raspberry Pi Zero W" only on Windows 10 PC -Sample code of SWITCHSCIENCE -How to use the temperature / humidity / barometric pressure sensor module with BME280 -Send LINE message with Python -About Raspberry Pi Zero -Before using crontab for the first time with RaspberryPi3 ――I wrote it as far as I can remember, but I feel that I searched for various other things.
Recommended Posts