-->
Page 1 of 1

Filling a database using Arduino UNO and ESP8266

PostPosted: Thu Oct 01, 2015 8:54 pm
by dgo_perez
Hi!

I'm trying to send data from an Arduino UNO to a database through the ESP8266, but I don' understand at all the syntax of the GET request. I just wanna fill the database every certain time using AT commands. I'm using XAMPP btw. I really appreciate any help from u guys. Thanks.

Re: Filling a database using Arduino UNO and ESP8266

PostPosted: Thu Oct 01, 2015 9:44 pm
by EspFan
If you are using CHERTS SDK you can look at the DHT-22 ThingSpeak example.

In there the data is sent via HTTP GET request.

Then you need a simple PHP script to log that data to a database.

You can put the data into a CSV log file very simply like this:
Code: Select all<?php
$logData = array(
   date('Y/m/d H:i:s'),
   $_GET['temp']
);
$fp = fopen('dataLog.csv','a');
fwrite($fp, implode(',', $logData) . "\r\n");
?>

Re: Filling a database using Arduino UNO and ESP8266

PostPosted: Sat Oct 03, 2015 1:26 pm
by Spafin
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.