Created
December 16, 2017 09:21
-
-
Save Synashida/c98e2e1ca970bcda34ebf0011f79c45d to your computer and use it in GitHub Desktop.
ElectricImpで部屋の温度と湿度を常時計測する ref: https://qiita.com/Syn256/items/b8d03f28bdbed28f5b74
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendTempHumid(tempHumid) { | |
// Send the device a 'pong' message immediately | |
server.log("デバイス = " + tempHumid[0] + "度 / " + tempHumid[1] + "%"); | |
local url = "センサーデータ受信用サーバのAPIパス"; | |
// Prepare the request with a JSON payload | |
local body = http.jsonencode({ "temp": tempHumid[0], "humid" : tempHumid[1] }); | |
local extraHeaders = {}; | |
local request = http.post(url, extraHeaders, body); | |
// Send the message | |
local incomingDataTable = request.sendsync(); | |
// Display the received data | |
server.log("Code: " + incomingDataTable.statuscode + ". Message: " + incomingDataTable.body); | |
} | |
device.send("getTempHumid", null); | |
device.on("sendTempHumid", sendTempHumid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#require "HTS221.device.lib.nut:2.0.1" | |
// 温度湿度センサーの接続設定 | |
hardware.i2cNM.configure(CLOCK_SPEED_400_KHZ); | |
tempHumid <- HTS221(hardware.i2cNM); | |
// 最終データを記録(agentに飛ばすため) | |
lastTemp <- ""; | |
lastHumid <- ""; | |
// agentにセンサー情報を送信 | |
function sendTempHumid() { | |
agent.send("sendTempHumid", [lastTemp, lastHumid]); | |
} | |
// センサーから温度湿度の情報を読み取る | |
function readTempHumid() { | |
tempHumid.setMode(HTS221_MODE.CONTINUOUS, 7); | |
local result = tempHumid.read(); | |
if ("error" in result) { | |
server.error("An Error Occurred: " + result.error); | |
lastTemp = "error"; | |
lastHumid = "error"; | |
} else { | |
server.log(format("Current Humidity: %0.2f %s, Current Temperature: %0.2f °C", result.humidity, "%", result.temperature)); | |
lastTemp = result.temperature.tostring(); | |
lastHumid = result.humidity.tostring(); | |
} | |
// agentに送信 | |
sendTempHumid(); | |
// 30秒後に自分をコールバック | |
imp.wakeup(30.0, readTempHumid); | |
} | |
function getTempHumid(data) { | |
readTempHumid(); | |
} | |
// agent側から温度湿度の読み出しリクエストをデバイス内部関数呼び出しに変換 | |
agent.on("getTempHumid", getTempHumid); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$json_data = file_get_contents('php://input'); | |
try { | |
$json_data = json_decode($json_data); | |
$db = pg_connect("******"); | |
$sql = sprintf( | |
"INSERT INTO temphumids (temp, humid) VALUES(%s, %s);" | |
, is_numeric($json_data->temp) ? $json_data->temp : 'null' | |
, is_numeric($json_data->humid) ? $json_data->humid : 'null' | |
); | |
pg_query($db, $sql); | |
echo "OK"; | |
} catch(Exception $e) { | |
echo "NG"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment