-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbwrite.php
More file actions
130 lines (98 loc) · 4.3 KB
/
dbwrite.php
File metadata and controls
130 lines (98 loc) · 4.3 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
// Change port accordingly
$servername = "eu-cdbr-west-03.cleardb.net";
// REPLACE with your Database name
$dbname = "heroku_2db5e38e1987785";
// REPLACE with Database user
$username = "be4b887cdc6b20";
// REPLACE with Database user password
$password = "3ffda6fa";
// Keep this API Key value to be compatible with the ESP32 code provided in the project page.
// If you change this value, the ESP32 sketch needs to match
$api_key_value = 'tPmAT5Ab3j7F9';
// keep the respnse payload
$response = ['success' => false, 'massage' => 'Internal server error' ];
// set the http response code
$responseCode = 500;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// get input json payload
$json = file_get_contents('php://input');
$json = json_decode($json, true);
// get api key
$apiKey = $json['X-API-KEY'] ?? null; // shorthand operator IF X-API-KEY exist in header assing X-API-KEY value ELSE assign null
if($apiKey == $api_key_value) {
// var_dump($json); return;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
$response['massage'] = 'Connection failed'.$conn->connect_error;
$responseCode = 500; // internal server error
} else {
// prepare sql connection
// ref -> https://www.w3schools.com/php/php_mysql_prepared_statements.asp
$stmt1 = $conn->prepare("INSERT INTO data (dateTimeStamp,gasLeakageDetected,flameDetected,temperatureValue,window1Status) VALUES (?,?,?,?,?);");
// get values form pay load
$serialPayload=$json['serialPayload'];
// // Remove the starting "*" character from the payload
// $payload = substr($httpPayload, 1);
// Split the payload into an array of values using "&" and "$" as delimiters
//$values = explode('&', substr($payload, 0, -1));
$values = explode('&',$serialPayload);
// Access each value in the array
$dateTime = $values[0];
$gasLeakageDetected = $values[1] == "1";
$flameDetected = $values[2] == "1";
$temperatureValue = floatval($values[3]);
$window1Status = $values[4];
$gasWeight = floatval($values[5]);
// bind paramiters to sql statement
$stmt1->bind_param(
'siidi',
$dateTime,
$gasLeakageDetected,
$flameDetected,
$temperatureValue,
$window1Status,
//$window2Status,
);
if ($stmt1->execute() === true) {
$response['success'] = true;
$response['massage'] = 'Data inserted to Main Table successfully';
$responseCode = 200; // response success
} else {
$response['massage'] = 'Error: ' . $sql .' '. $conn->error;
$responseCode = 500; // internal server error
}
$stmt1->close();
$stmt2 = $conn->prepare("INSERT INTO weight_data (gasWeight,dateTimeStamp) VALUES (?,?);");
$stmt2->bind_param(
'ds',
$gasWeight,
$dateTime,
);
if ($stmt2->execute() === true) {
$response['success'] = true;
$response['massage'] = 'Data inserted to Weight Table successfully';
$responseCode = 200; // response success
} else {
$response['massage'] = 'Error: ' . $sql .' '. $conn->error;
$responseCode = 500; // internal server error
}
$stmt2->close();
}
// Close the connection
$conn->close();
} else {
$response['massage'] = 'Wrong API Key provided!';
$responseCode = 401; // unauthorized request
}
} else {
$response['massage'] = 'Method Not Allowed';
$responseCode = 405; // Method Not Allowed
}
// send response to client
http_response_code($responseCode);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
?>