Olen tehnyt seuraavan scriptin ja nyt pitäisi vielä kaivaa anturin nimi esiin. Mistäs kohtaa raw-dataa se oikein löytyisi?
Scripti on seuraava
Teksti ilman muotoiluja
<?php
$db = $mysqli = new mysqli(“xxxxx”, “xxxxx”, “xxxxxx”, “xxxxxx”);
mysqli_set_charset($db,“utf8mb4”);
Define Type Variables
date_default_timezone_set(‘Europe/Helsinki’);
$webhook = file_get_contents(‘php://input’);
$json = json_decode($webhook, true);
$tags = array(“C6:BD:75:6D:37:3B”, “D0:19:F5:0F:7D:D0”, “EB:F2:42:75:B8:AE”, “DE:D8:0B:89:EF:2F”, “E6:7E:6F:39:A4:5E”, “E3:09:1D:AE:7A:90”, “E5:04:15:90:F0:09”, “D5:6D:A1:23:73:C7”);
$current_time = date(‘H:i’);
$inserted_mac_addresses = array();
foreach($tags as $tag) {
$data = $json[“data”][“tags”][$tag][“data”];
$temperature_bytes = substr($data, 8, 12);
$temperature_int = hexdec($temperature_bytes);
$temperature_int = unpack(“s”, pack(“S”, $temperature_int))[1];
$temperature_offset = hexdec(substr($data, 2, 2));
$temperature = ($temperature_int + $temperature_offset) * 0.005;
$temperature = round($temperature,1);
if ($current_time == “00:00” || $current_time == “02:00” || $current_time == “04:00” || $current_time == “06:00” || $current_time == “08:00” || $current_time == “10:00” || $current_time == “12:00” || $current_time == “14:00” || $current_time == “16:00” || $current_time == “18:00” || $current_time == “20:00” || $current_time == “22:00” && !in_array($tag, $inserted_mac_addresses)) {
$inserted_mac_addresses = $tag;
$now = date(“Y-m-d H:i”);
$sqlcount = “SELECT * FROM lampotilat WHERE pakastinarkkumac=‘$tag’ and paivamaara = ‘$now’ limit 1”;
$num_rows = mysqli_query($db,$sqlcount);
$rowcount=mysqli_num_rows($num_rows);
$nnow1= strtotime(date(“Y-m-d H:i:s”)) . ‘000’;
if ($rowcount == 0) {
$sql = “INSERT INTO lampotilat (lampotila, pakastinarkkumac, paivamaara, unixtime) VALUES (‘$temperature’, ‘$tag’,‘$now’,$nnow1)”;
mysqli_query($db, $sql);
file_put_contents(‘ruuvi.log’, “LISÄTÄÄN KANTAAN. MAC: $tag - $temperature\r\n”, FILE_APPEND);
}else{
file_put_contents(‘ruuvi.log’, “EI LISTÄTÄ KANTAAN ($now). MAC: $tag - $temperature\r\n”, FILE_APPEND);
}
}
}
?>