[PYTHON] I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value

Trigger

――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?

System overview diagram

Untitled Diagram (1).png

Caution

――Since this article was written based on a memo made through trial and error, there may be omissions in the procedure ...

What I used

Preparing OS for Raspberry Pi (working on Win10)

Export OS to SD card

-Download and install Raspberry Pi Imager --Start and execute by specifying the OS and the sd card to write

Generate 2 files at the root of the SD card where the OS was written

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"
}

Setting after starting Raspberry Pi (linux / raspberrypi work)

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.

ssh access (Linux)

linux


ssh [email protected]
# pass:raspberry

Raspberry pi initial setting (raspberry pi)

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

Ssh connection with public key

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

BME280 setting

BME280 connection

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

Communication of BME280

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.

Preparing to get measurements in Python

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 %

Get tokens for LINE notifications

-I got the token by referring to Send LINE message with Python.

Additional coding for LINE notifications

Changed temperature / humidity function from console output to value return

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

Call the LINE notification function with the data acquisition function

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)

Added function for sending LINE notifications

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

Scheduled execution settings

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

Execution result

resised.jpg

Impressions I made

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

Serial communication

――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.

Register specifications

――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

How to use this system?

――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"

Other

――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.

Articles / sites that I referred to

-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

I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
A simple system that automatically shoots with object detection and sends it to LINE
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
I made a Line bot that guesses the gender and age of a person from an image
I made a LINE bot that tells me the type and strength of Pokemon in the Galar region with Heroku + Flask + PostgreSQL (Heroku Postgres)
I made a pet camera that is always connected with WebRTC (Nuxt.js + Python + Firebase + SkyWay + Raspberry Pi)
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
I made a twitter app that decodes the characters of Pricone with heroku (failure)
I made a Docker Image that reads RSS and automatically tweets regularly and released it.
I made a web application that maps IT event information with Vue and Flask
A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
I made a LINE BOT with Python and Heroku
I don't like to be frustrated with the release of Pokemon Go, so I made a script to detect the release and tweet it
A script that pings the registered server and sends an email with Gmail a certain number of times when it fails
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
I made an API with Docker that returns the predicted value of the machine learning model
I automatically collected my Qiita post data with Raspberry Pi and notified it regularly with Slack
I made a LINE BOT that returns parrots with Go
Production of temperature control system with Raspberry Pi and ESP32 (1)
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a surveillance camera with my first Raspberry PI.
Building a distributed environment with the Raspberry PI series (Part 2: PiServer analysis and alternative system design)
A list of functions that I came across with 100 Numpy knocks and thought "This is convenient!"
Raspberry Pi? What's that? Level of hardware Inexperienced college students created a system to grasp if there are people in the office
I tried to automate the watering of the planter with Raspberry Pi
I made a web server with Raspberry Pi to watch anime
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
I made a slack bot that notifies me of the temperature
I made a program that automatically calculates the zodiac with tkinter
Periodically log the value of Omron environment sensor with Raspberry Pi
I made a chatbot with Tensor2Tensor and this time it worked
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC