概要
ブルーバックス「カラー図解 最新Raspberry Piで学ぶ電子工作」で使った秋月のADT7410使用センサモジュールを応用して 5分毎にクラウド上のGoogleスプレッドシートに温度を記録する仕組みを構築しました。
ハードウェア
Raspberry Pi 3 Model B
秋月ADT7410使用 I2C・16bit 温度センサモジュール
ブレッドボード・ジャンパーワイヤー
(接続方法、Raspberry Piの設定などは上記書籍を御参照ください)
データの流れ
Raspberry Pi側(node-redのプログラミング)
node-redで5分毎にトリガーをかけてデータ取得してIFTTTへデータを送る
↓
IFTTT側(あらかじめWebhooksとGoogle Spreadのセッティングはしておく)
Raspberry Piからデータを受け取ったら Google Spreadシート記録
node-redのプログラミング
ADT7410からのデータ読み出しで苦労しました。node-redで読み込んで御使用ください。
[
{
"id": "6d1c06ad.b64658",
"type": "inject",
"z": "93a77c4.18f3b8",
"name": "繰り返し",
"topic": "",
"payload": "",
"payloadType": "str",
"repeat": "",
"crontab": "*/5 0-23 * * *",
"once": false,
"onceDelay": 0.1,
"x": 110,
"y": 20,
"wires": [
[
"2a2fb99d.337236"
]
]
},
{
"id": "2a2fb99d.337236",
"type": "i2c in",
"z": "93a77c4.18f3b8",
"name": "i2C 温度センサー",
"address": "72",
"command": "0",
"count": "2",
"x": 150,
"y": 120,
"wires": [
[
"837fe248.35111",
"71a7f10a.a9b97",
"c028b1bd.24de2"
]
]
},
{
"id": "837fe248.35111",
"type": "debug",
"z": "93a77c4.18f3b8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload[1]",
"targetType": "msg",
"x": 760,
"y": 100,
"wires": []
},
{
"id": "71a7f10a.a9b97",
"type": "debug",
"z": "93a77c4.18f3b8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload[0]",
"targetType": "msg",
"x": 760,
"y": 140,
"wires": []
},
{
"id": "c028b1bd.24de2",
"type": "function",
"z": "93a77c4.18f3b8",
"name": "温度データ 計算(正負対応)",
"func": "//下位bit byte型\n\ndata1 = msg.payload[1];\ndata1 = data1 & 0xff;\n\n//上位bit byte型\ndata0 = msg.payload[0];\ndata0 = (data0 & 0xff) <<8;\n\n//上位(data0)+下位(data1) で並べる\ndata = data0 | data1;\n\n//13ビット(下3ビット捨てる)\ndata = data >>3;\n\n//正負判定して温度計算\nif ((data & 0x1000) === 0) { //かっこのかけ方ではまった\n temp = data *0.0625 ;\n }else{\n temp = ((~data & 0x1fff)+1)*-0.0625; //~しただけだと32bit分ある かっこかけかた\n }\n\nmsg.payload = temp;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 230,
"y": 220,
"wires": [
[
"ca9a44b.7779fb8",
"c988cf02.836b1",
"382e6da0.9a88c2",
"2bbd9de4.2f16f2"
]
]
},
{
"id": "c988cf02.836b1",
"type": "ui_gauge",
"z": "93a77c4.18f3b8",
"name": "温度メーター",
"group": "8a123253.0fbb5",
"order": 1,
"width": 0,
"height": 0,
"gtype": "gage",
"title": "センサー温度",
"label": "℃",
"format": "{{value}}",
"min": "10",
"max": "40",
"colors": [
"#00b500",
"#e6e600",
"#ca3838"
],
"seg1": "",
"seg2": "",
"x": 580,
"y": 260,
"wires": []
},
{
"id": "9a0f93b7.5a7bc",
"type": "http request",
"z": "93a77c4.18f3b8",
"name": "データをIFTTTへ送る",
"method": "POST",
"ret": "txt",
"paytoqs": false,
"url": "https://maker.ifttt.com/trigger/raspberrypi_send_data/with/key/[IFTTT Webhooksの指示通り記述]",
"tls": "",
"proxy": "",
"authType": "",
"x": 600,
"y": 360,
"wires": [
[]
]
},
{
"id": "2bbd9de4.2f16f2",
"type": "function",
"z": "93a77c4.18f3b8",
"name": "value1 を頭につけるだけ",
"func": "msg.payload = {'value1':msg.payload};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 230,
"y": 360,
"wires": [
[
"9a0f93b7.5a7bc"
]
]
},
{
"id": "382e6da0.9a88c2",
"type": "ui_chart",
"z": "93a77c4.18f3b8",
"name": "温度推移グラフ",
"group": "8a123253.0fbb5",
"order": 1,
"width": 0,
"height": 0,
"label": "温度推移グラフ",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "",
"ymax": "",
"removeOlder": "24",
"removeOlderPoints": "",
"removeOlderUnit": "3600",
"cutout": 0,
"useOneColor": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"useOldStyle": false,
"outputs": 1,
"x": 580,
"y": 300,
"wires": [
[]
]
},
{
"id": "ca9a44b.7779fb8",
"type": "debug",
"z": "93a77c4.18f3b8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 750,
"y": 220,
"wires": []
},
{
"id": "8a123253.0fbb5",
"type": "ui_group",
"z": "",
"name": "温度 ADT7410",
"tab": "c73c2bbe.f33908",
"disp": true,
"width": "6",
"collapse": false
},
{
"id": "c73c2bbe.f33908",
"type": "ui_tab",
"z": "",
"name": "Raspi Sensor",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
IFTTT側の設定
こちらの記事が参考になりました。
https://qiita.com/minatomirai21/items/603f32af283158e42989
Appletを組むために以下を設定します。
項目 | 説明 | サービス |
---|---|---|
If | Receive a web request | Webhooks |
Then | Add row to spreadsheet | Google Spreadsheet |
- Webhooks
詳細は割愛しますが以下の設定が必要になります。
項目 | 設定値 |
---|---|
Event Name | raspberrypi_send_data |
Key | IFTTTのMy Services → Webhooks → 右上のSettings とたどり、URL Status のURLをコピペして ブラウザで開くと記述の説明が出ます。 |
- Google Spreadsheet
例えばこんな感じで設定します。
項目 | 設定値 |
---|---|
Spreadsheet name | IFTTT_Maker_Webhooks_Events |
Drive folder path (optional) | IFTTT/MakerWebhooks/ |