Raspberry Pi でREST API利用。IFTTTで自宅の気圧などをGoogle Driveへ記録

安価な気圧センサー(BME280利用)を入手したのでRaspberry Pi で記録中。以下備忘録。

気圧センサー BME280
気圧、気温、湿度を取得できる。3.3V動作。
姉妹チップ(?)のBMP280は湿度が取得できない。気圧、気温のみ。
BME280はチップが正方形、BMP280は長方形。
I2CとSPI通信どちらかが利用可能。今回はI2C利用。
10分に1回記録。

Raspberry Pi
参考:http://www.raspberrypi-spy.co.uk/2016/07/using-bme280-i2c-temperature-pressure-sensor-in-python/

まず下記bme280.py をダウンロードして保存。

https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bme280.py

REST APIで記録(IFTTT Maker Webhooks)
汎用性を考えてREST APIを使い、IFTTTにトリガを送付。
IFTTT のトリガー”Maker Webhooks”(旧名 Maker)で を作成できる。

import RPi.GPIO as GPIO
import time
import datetime
import requests
import bme280
def upIfttt(tem,press,hum):
 url = 'https://maker.ifttt.com/trigger/env/with/key/(xxxxxx)'
 head = {'Content-Type': 'application/json'}
 body = '{"value1":"%(temperature)d","value2":"%(pressure)d","value3":"%(humidity)d"}' % {'temperature':tem,'pressure':press,'humidity':hum}
 r = requests.post(url,headers =head, data=body)
 return r.text
#main
while True:
 temperature,pressure,humidity = bme280.readBME280All()
 d=datetime.datetime.today()
 b = 0
 c = int(d.minute)
 if ((c - (c/10)*10) == 0):
  if b == 0:
   b = 1
   print upIfttt(temperature,pressure,humidity)
  else:
   b=0
 time.sleep(1)

IFTTT のアクション設定
IFTTTでMaker Webhooks にトリガが発生したら、アクションで”Google Drive”のうち”SpreadSheet”を利用と指定。
Excelのように直接分析もできて便利。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA