- Sat Oct 03, 2015 1:26 pm
#30429
Hi!
Have you already created the database on the server? After that most of the work is done in PHP, the only think you have to do with Arduino and ESP8266 is sending the data.
This is the code I use to send the data:
Code: Select allString stringOne = "GET /garden/insertdata.php?hash=mysecretword&s=";
stringOne += SENSORID;
stringOne += "&h=";
stringOne += hum;
stringOne += "&t=";
stringOne += temp;
stringOne += "&p=";
stringOne += pressure;
stringOne += "&w=";
stringOne += w;
Serial.println(stringOne);
stringOne += " HTTP/1.0\r\nHost: www.myserver.net\r\nConnection: close\r\n\r\n";
char charBuf[200];
stringOne.toCharArray(charBuf, 200);
wifi.send(1, (const uint8_t*)charBuf, 200);
Then, on the server, this is the basic code:
Code: Select all<?php
// CREATE CONNECTION
$con = mysqli_connect("myserver.com", "user", "password", "database");
// CHECK CONNECTION
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// DATABASE QUERY
$SQL = "INSERT INTO data(`sensor`, `temp`, `hum`, `soiltemp`, `soilhum`, `press`, `weight`, `weightvalue`, `time`, `IP`) values (" . $_GET['s'] . ", " . $_GET['t'] . ", " . $_GET['h'] . ", " . $st . ", " . $sh . ", " . $press . ", " . $weight . ", " . $weightvalue . ", " . time() . ", '" . $_ENV["REMOTE_ADDR"] . "')";
print($SQL);
mysqli_query($con, $SQL);
// CLOSE THE CONNECTION
mysqli_close($con);
?>
Real code is a bit more complex: I check that the secret word exists and has the real value, and I also check the values received by the server. You can ask for more help if you need it.