-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
98 lines (89 loc) · 3.4 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
// read sensor ID ('esp8266-'+ChipID)
$headers = array();
if (isset($_SERVER['HTTP_SENSOR']))
$headers['Sensor'] = $_SERVER['HTTP_SENSOR'];
if (isset($_SERVER['HTTP_X_SENSOR']))
$headers['Sensor'] = $_SERVER['HTTP_X_SENSOR'];
// stop if no sensor name is detected
if (! isset($headers['Sensor'])) {
die("no sensor id sended!");
}
// set the database parameters
$database_name = "luftdaten";
$database_host = "127.0.0.1";
$database_user = "change_it";
$database_password = "change_it";
$table_name = "sensor_data";
// establish the database connection
$pdo = new PDO('mysql:host=' . $database_host . ';dbname=' . $database_name, $database_user, $database_password);
// read the content sended to the API file
$json = file_get_contents('php://input');
// decode the encoded data into the results array
$results = json_decode($json, true);
// declare possible field names
$possible_fields = array(
"durP1",
"ratioP1",
"P1",
"durP2",
"ratioP2",
"P2",
"SDS_P1",
"SDS_P2",
"temperature",
"humidity",
"BMP280_temperature",
"BMP280_pressure",
"BME280_temperature",
"BME280_humidity",
"BME280_pressure",
"samples",
"min_micro",
"max_micro",
"signal"
);
// copy sensor data values to values array
foreach ($results["sensordatavalues"] as $sensordatavalues) {
$values[$sensordatavalues["value_type"]] = $sensordatavalues["value"];
}
if(!isset($values["temperature"]) && isset($values["BME280_temperature"])){
$values["temperature"] = $values["BME280_temperature"];
}
if(!isset($values["temperature"]) && isset($values["BMP280_temperature"])){
$values["temperature"] = $values["BMP280_temperature"];
}
if(!isset($values["humidity"]) && isset($values["BME280_humidity"])){
$values["humidity"] = $values["BME280_humidity"];
}
// set missing fields to ensure their presence
foreach ($possible_fields as $possible_field) {
if (! isset($values[$possible_field])) {
$values[$possible_field] = NULL;
}
}
// prepare the database query
$insert = $pdo->prepare("REPLACE INTO `" . $database_name . "`.`" . $table_name . "`(`Time`,`SensorID`,`durP1`,`ratioP1`,`P1`,`durP2`,`ratioP2`,`P2`,`SDS_P1`,`SDS_P2`,`Temp`,`Humidity`,`BMP280_temperature`,`BMP280_pressure`,`BME280_temperature`,`BME280_humidity`,`BME280_pressure`,`Samples`,`Min_cycle`,`Max_cycle`,`Signal`) VALUES (NOW(),:SensorID,:durP1,:ratioP1,:P1,:durP2,:ratioP2,:P2,:SDS_P1,:SDS_P2,:Temp,:Humidity,:BMP280_temperature,:BMP280_pressure,:BME280_temperature,:BME280_humidity,:BME280_pressure,:Samples,:Min_cycle,:Max_cycle,:Signal)");
// execute the database query
$insert->execute(array(
':SensorID' => $headers['Sensor'],
':durP1' => $values["durP1"],
':ratioP1' => $values["ratioP1"],
':P1' => $values["P1"],
':durP2' => $values["durP2"],
':ratioP2' => $values["ratioP2"],
':P2' => $values["P2"],
':SDS_P1' => $values["SDS_P1"],
':SDS_P2' => $values["SDS_P2"],
':Temp' => $values["temperature"],
':Humidity' => $values["humidity"],
':BMP280_temperature' => $values["BMP280_temperature"],
':BMP280_pressure' => $values["BMP280_pressure"],
':BME280_temperature' => $values["BME280_temperature"],
':BME280_humidity' => $values["BME280_humidity"],
':BME280_pressure' => $values["BME280_pressure"],
':Samples' => $values["samples"],
':Min_cycle' => $values["min_micro"],
':Max_cycle' => $values["max_micro"],
':Signal' => $values["signal"]
));