Created
July 6, 2019 20:48
-
-
Save kenichimiki/9d9dece8cb11b20aaa4cbcedbc8a94bb to your computer and use it in GitHub Desktop.
Store the senser histories of Nature Remo to SQL DB( Room Temperature, Humidity and Illuminance)
This file contains hidden or 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 | |
| /************************************************************************* | |
| * home-api (History of Nature Remo Senser) | |
| * Home Tools for private. Using IFTTT and Google Home etc | |
| * | |
| * PHP 5 or later | |
| * | |
| * @category Home IoT | |
| * @author Miki | |
| * @url https://www.miki-ie.com/ | |
| * @copyright 2019 (c) MIKI-IE All rights Reserved. | |
| * @license https://opensource.org/licenses/mit-license.html MIT License | |
| * @version 1.0 | |
| *************************************************************************/ | |
| //Nature Remo API情報 | |
| $token = '@NatureRemoApiToken@'; // tokenを設定 | |
| $header = [ | |
| 'Authorization: Bearer '.$token, // 前準備で取得したtokenをヘッダに含める | |
| 'Content-Type: application/json', | |
| ]; | |
| $base_url = 'https://api.nature.global/1/'; | |
| //DB情報 | |
| $host_name = '127.0.0.1:3307'; //DB | |
| $user_name = '@DB_USER@'; | |
| $password = '@DB_PASS@'; | |
| $database_name ='@DB_NAME@'; | |
| $table_name = '@DB_TABLE@'; | |
| $mysqli = new mysqli($host_name, $user_name, $password, $database_name); | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, $base_url.'devices'); | |
| curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); | |
| curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 証明書の検証を行わない | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, $header); | |
| curl_setopt($curl, CURLOPT_HEADER, true); | |
| $response = curl_exec($curl); | |
| // ステータスコード取得 | |
| $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| echo 'HTTP_CODE:'.$code."\n"; | |
| // header & body 取得 | |
| $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); | |
| echo 'HEADER_SIZE:'.$header_size."\n"; | |
| $header = substr($response, 0, $header_size); | |
| $body = substr($response, $header_size); | |
| $result = json_decode($body, true); | |
| echo 'te: '.$result[0]["newest_events"]["te"]["val"]."\n"; | |
| echo 'hu: '.$result[0]["newest_events"]["hu"]["val"]."\n"; | |
| echo 'il: '.$result[0]["newest_events"]["il"]["val"]."\n"; | |
| $te = $result[0]["newest_events"]["te"]["val"]."\n"; | |
| $hu = $result[0]["newest_events"]["hu"]["val"]."\n"; | |
| $il = $result[0]["newest_events"]["il"]["val"]."\n"; | |
| $datetime = date("Y/m/d H:i:s"); | |
| curl_close($curl); | |
| // SQL(INSERT)を作成 | |
| $sql = "INSERT INTO $table_name ( | |
| DATETIME, TE, HU, IL | |
| ) VALUES ( | |
| '$datetime', $te, $hu, $il | |
| )"; | |
| echo $sql."\n"; | |
| $mysqli->query($sql); | |
| $mysqli->close(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment