Retrieve Data from database using Arduino

I am trying to write and read mysql database from arduino mega.. Already I wrote the DHT22 sensor's details on database and preview it using webpage.. Also in this webpage have some ON/OFF switch.. when i click this switch the database change according to switch position.. Now I want change the Arduino output pin according to database value.. how can i do it?

Whatever program / communication you used (and forgot to mention) to send data between Arduino and database, use it backwards.

blimpyway:
Whatever program / communication you used (and forgot to mention) to send data between Arduino and database, use it backwards.

add.php

<?php include("connect.php"); $link=Connection(); $temp1=$_POST["temp1"]; $hum1=$_POST["hum1"]; $query = "INSERT INTO `tempLog` (`temperature`, `humidity`) VALUES ('".$temp1."','".$hum1."')"; mysql_query($query,$link); mysql_close($link); header("Location: index.php"); ?>

index.php

<?php include("connect.php"); $link=Connection(); $result=mysql_query("SELECT * FROM `tempLog` ORDER BY `timeStamp` DESC",$link); ?> Sensor Data

Temperature / moisture sensor readings

<?php if($result!==FALSE){ while($row = mysql_fetch_array($result)) { printf("", $row["timeStamp"], $row["temperature"], $row["humidity"]); } mysql_free_result($result); mysql_close(); } ?>
 Timestamp   Temperature 1   Moisture 1 
 %s  %s   %s 

arduino code

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

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

#define DHTPIN 8 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

String data;

void setup() {

Serial.begin(9600);
dht.begin();

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}

}

void loop() {

float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

data = "temp1=" + String(t,2) + "&hum1=" + String(h,2);

if (client.connect("www.automation.sldroid.com",80)) { // REPLACE WITH YOUR SERVER ADDRESS
client.println("POST /add.php HTTP/1.1");
client.println("Host: automation.sldroid.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
}

if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}

delay(1000);

}

I'm not into php but I see you use HTTP POST in arduino to send some data to a PHP script, I don't see why cant you have another PHP script that retrieves data from database and send it back as plain text.

Then all you have to do in arduino is to GET /script_orl and read whatever the second PHP script sends.

Hi, I'm somewhat trying to do the same thing. I need the arduino to read data from database, modify it and upload back on the database but all it is doing right now is modifying and uploading but not retrieving. Please help.

I don't see why cant you have another PHP script that retrieves data from database and send it back as plain text.

I have used two PHP scripts one for uploading and one for retrieving. Both are working. Everything is compiling well. I guess the problem is somewhere in my logic of linking the arduino to the PHP scripts. Kindly have a look:

https://forum.arduino.cc/index.php?topic=528588.0

Please reply on the above referred post. I need your help.

If you want to retrieve data from an http host check this tutorial: https://www.arduino.cc/en/Tutorial/WebClient

If you want to retrieve data from an http host check this tutorial: https://www.arduino.cc/en/Tutorial/WebClient

I already did.
Please check it on Sending data from phpMyAdmin database to Arduino [Part 2] - Project Guidance - Arduino Forum

I solved the issue. Now I'm able to send and receive the data simultaneously from within my Arduino IDE. I used JSON. I've even made a video tutorial for anyone else who needs help.

Thanks to everyone who helped.

I am using ESP2866 and a keypad and a gsm module. I want to make a web page using esp8266 and send a otp to a phone number using gsm.
Web page can be used to change Or enter a phone number of the user on which otp will come.
But i have no idea how this can be done. Please help

Sir tomorrow is my project and there is a problem in my codes of exit 1 and. ')'before. ';'.
The codes are:-
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 13
#define echoPin 12
// Find LCD address for I2C in this tutorial it is 0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2);
int counter = 0;
int currentState1 = 0;
int previousState1 = 0;
int currentState2 = 0;
int previousState2 = 0;
int inside = 0;
int outside = 0;

void setup()
{
// initialize the LCD
lcd.begin();
//Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.setCursor(8, 0);
lcd.print("OUT: ");
lcd.setCursor(0, 1);
lcd.print("Total Inside: ");
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 9){
currentState1 = 1;
}
else {
currentState1 = 0;
}
delay(100);
if(currentState1 != previousState1){
if(currentState1 == 1){
counter = counter + 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
inside = inside +1;}
lcd.setCursor(4, 0);
lcd.print(inside);
if (distance > 9 && distance <= 18){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1){
counter = counter - 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
outside = outside +1;}
lcd.setCursor(13, 0);
lcd.print(outside);
lcd.setCursor(14, 1);
lcd.print(counter);
if (counter > 9 || counter < 0){
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
}
}

Please tell me fast the problem as i have short time.
I cookies this code from the video "Bi-Directional Visitor Counter using single ultrasonic sensor with LCD - YouTube"