Go Down

Topic: Retrieve Data from database using Arduino (Read 1 time) previous topic - next topic

mecduino

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?

blimpyway

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

mecduino

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);
?>

<html>
   <head>
      <title>Sensor Data</title>
   </head>
<body>
   <h1>Temperature / moisture sensor readings</h1>

   <table border="1" cellspacing="1" cellpadding="1">
      <tr>
         <td>&nbsp;Timestamp&nbsp;</td>
         <td>&nbsp;Temperature 1&nbsp;</td>
         <td>&nbsp;Moisture 1&nbsp;</td>
      </tr>

      <?php
        if($result!==FALSE){
           while($row = mysql_fetch_array($result)) {
              printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>",
                 $row["timeStamp"], $row["temperature"], $row["humidity"]);
           }
           mysql_free_result($result);
           mysql_close();
        }
      ?>

   </table>
</body>
</html>

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);
 
}

blimpyway

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.

udbhav95

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.

Quote
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:

Code: [Select]
https://forum.arduino.cc/index.php?topic=528588.0

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

foobar23

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

udbhav95

Quote
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 https://forum.arduino.cc/index.php?topic=528588.0

udbhav95

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.

https://youtu.be/6hi9Wf99hfg

Thanks to everyone who helped.

Go Up