[RaspberryPi] [python] Inform LINE of room temperature with temperature sensor + IFTTT

environment

1. Preparation of temperature sensor

Since it uses a 1-wire sensor, add the following to / etc / modules of Raspberry Pi

w1-gpio
w1-therm

Add the following to /boot/config.txt

dtoverlay=w1-gpio-pullup

Sensor connection

--Pin 1 (GND) of the sensor is pin 6 (GND) of Raspberry Pi --Pin 2 (DQ) of the sensor is pin 7 (GPIO4) of Raspberry Pi --Sensor pin 3 (VDD) is Raspberry Pi pin 1 (3.3V)

To The 2nd and 3rd pins of the sensor are connected by a pull-up resistor. Reference: http://blog.livedoor.jp/victory7com/archives/33399310.html

20170820002519.png 20170819235926.jpg

Use the lsmod command to check the connection. If the connection is successful, it will be as follows.

cmd


pi@raspberrypi:~/Documents/work$ lsmod | grep w1
w1_therm                6401  0 
w1_gpio                 4818  0 
wire                   32619  2 w1_gpio,w1_therm

The acquired temperature is written to "/ sys / bus / w1 / devices / 28-XXXXXXXX / w1_slave". (XXXX of 28-XXXXXXXX changes depending on the terminal you are using)

cmd


pi@raspberrypi:~/Documents/work$ cat /sys/bus/w1/devices/28-00000723fabd/w1_slave 
b1 01 4b 46 7f ff 0f 10 8d : crc=8d YES
b1 01 4b 46 7f ff 0f 10 8d t=27062

The current temperature is the value of "t =" divided by 1000.

2. Preparation of IFTTT

Go to IFTTT and create a recipe from My Applet.

Set "this" to "Webhooks" and "that" to "line"

After setting, go to https://ifttt.com/services/maker_webhooks/settings and go to the URL provided to enable the key.

--event is the event name set when setting Webhooks --value is the value you want to send to LINE

By executing the curl command following "You can also try it with curl from a command line.", It will be sent to the set LINE destination.

3. Combine with python

With RaspberryPi, get the temperature with python, request the URL of the Webhooks set with IFTTT, and send the room temperature to LINE.

Install the library to get the temperature

cmd


pip install w1thermsensor

Get and send with the following code

python


from w1thermsensor import W1ThermSensor
import urllib.request, json

sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, "(Sensor device name)")
temperature_in_celsius = sensor.get_temperature()

url = "https://maker.ifttt.com/trigger/(Webhooks event name)/with/key/(Webhooks key)"
method = "POST"
headers = {"Content-Type" : "application/json"}

obj = {"value1" : temperature_in_celsius}
json_data = json.dumps(obj).encode("utf-8")

request = urllib.request.Request(url, data=json_data, method=method, headers=headers)
with urllib.request.urlopen(request) as response:
  response_body = response.read().decode("utf-8")

Match THERM_SENSOR_DS18B20 with the model number of the sensor you are using. The device name of the sensor is the XXXX part of 28-XXXXXXXX. After that, if you set this to Coulomb etc., you can get the room temperature regularly.

Recommended Posts

[RaspberryPi] [python] Inform LINE of room temperature with temperature sensor + IFTTT
Measure CPU temperature of Raspberry Pi with Python
Create another version of Python conda environment with one command line
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 2
Getting Started with Python Basics of Python
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Implementation of Dijkstra's algorithm with python
Coexistence of Python2 and 3 with CircleCI (1.0)
Basic study of OpenCV with Python
The story of making a university 100 yen breakfast LINE bot with Python
Basics of binarized image processing with Python
[Examples of improving Python] Learning Python with Codecademy
Execute Python script with cron of TS-220
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
Easy introduction of speech recognition with Python
[Python / PyRoom Acoustics] Room acoustic simulation with Python
UnicodeEncodeError struggle with standard output of python3
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Drawing with Matrix-Reinventor of Python Image Processing-
Recommendation of Altair! Data visualization with Python
Comparison of matrix transpose speeds with Python
RaspberryPi L Chika with Python and C #
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
I tried to automatically send the literature of the new coronavirus to LINE with Python