read value from mysql

hello

i want read value from database mysql localhost

and get this value to use in arduino

i use ethernet shield

Have you allready tried some code ?
Did you check the examples of the ethernet library?

How does this relate to your other post ?

robtillaart:
Have you allready tried some code ?
Did you check the examples of the ethernet library?

yes .. and i installed another library
but to no avail

robtillaart:
How does this relate to your other post ?

Here I explained it better

thank you

You have not explained it well enough BUT i have made you a script and test_user account on my database and ran the code on my ethernet shield and tested it fully… you own me lol

here’s the code, you only have select privileges so no funny stuff…
I assume you know how to setup a database, just make sure you atleast have a unique ID which is auto incrementing as a minimum, but for now you can have access to my database for testing this script. All you need to do is just flash the code to your Arduino and run it, I have already made the PHP script and database for you…
The Arduino code used the default Ethernet library and a mixture of the DHCP and webClientRepeting examples along with some custom code written by myself which handles the incoming data and assigns it to variables, which auto syncs and can be repeated continuously.

PHP server code

<?php
$buffer1 = array();
$buffer2 = array();
$v = 0;
//setting header to json
header('Content-Type:text/html');
$op = htmlentities ($_GET["op"]);
//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'south609_Test_User');
define('DB_PASSWORD', 'TestUser');
define('DB_NAME', 'south609_ESP8266');
//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
    die("Connection failed: " . $mysqli->error);
}
// query 
$sql = "SELECT temp,hum FROM ESP8266 WHERE ID = '$op'"; // we will only retrieve one row as we are getting it by a unique ID
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $buffer1[$v] = $row["temp"];
        $buffer2[$v] = $row["hum"];
        $v++;
    }
}
if ($buffer1) {
    echo "#" . $buffer1[0] . "$" . $buffer2[0] . "$";
}
else {
    echo "Failed";
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
?>

Arduino code to run the Ethernet Shield

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
char server[] = "www.southamptonstudentrooms.com";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
int option = 1;
int arrayPosition = 0;
int valueCount = 0;
char receiveBuffer[16];
char receiveBufferTwo[16];
bool receiveLock = false;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }
  printIPAddress();
}

void loop() {
  switch (Ethernet.maintain())
  {
    case 1:
      //renewed fail
      Serial.println("Error: renewed fail");
      break;
    case 2:
      //renewed success
      Serial.println("Renewed success");

      //print your local IP address:
      printIPAddress();
      break;
    case 3:
      //rebind fail
      Serial.println("Error: rebind fail");
      break;
    case 4:
      //rebind success
      Serial.println("Rebind success");
      //print your local IP address:
      printIPAddress();
      break;
    default:
      //nothing happened
      break;
  }
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    //Serial.write(c);
    if (c == '#') {
      arrayPosition = 0;
      valueCount = 0;
      receiveLock = true;
    }
    if (receiveLock) {
      if (c == '

) {
        valueCount++;
        arrayPosition = 0;
        if (valueCount >= 2) {
          Serial.println(“We have recived two variables”);
          Serial.print("buffer one: "); Serial.println(receiveBuffer);
          Serial.print("buffer two: "); Serial.println(receiveBufferTwo);
          valueCount = 0;
          receiveLock = false;
        }
      }
      else if (c != ‘#’) {
        if (valueCount == 0) {
          receiveBuffer[arrayPosition] = c;
        }
        else if (valueCount == 1) {
          receiveBufferTwo[arrayPosition] = c;
        }
        arrayPosition++;
      }
    }
  }
  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }
}

void printIPAddress()
{
  Serial.print(“My IP address: “);
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(”.”);
  }
  Serial.println();
}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();
  // if there’s a successful connection:
  if (client.connect(server, 80)) {
    Serial.println(F(“Connecting…”));
    //standard url = http://southamptonstudentrooms.com/IoT/basicGetInfo.php?op=1
    // send the HTTP GET request
    client.print(F(“GET /IoT/basicGetInfo.php?op=”)); // add print F
    client.print(option);
    client.println(F(" HTTP/1.1"));
    client.println(F(“Host: southamptonstudentrooms.com”));
    client.println(F(“Connection: close”));
    client.println();
    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn’t make a connection:
    Serial.println(“connection failed”);
  }
}