Go Down

Topic: arduino + ethernet shield + dht22 AND apache + php + mysql (Read 215 times) previous topic - next topic

migue2707

Hi, I am capturing humidity and temperature with the dht22 sensor, I managed to display them on the serial monitor that has the arduino platform, but when I try to send this data to another PC that serves as the server in which Xammp runs, Connection message set but can not store the data in Mysql, I do not know what my error. Please help me by taking the trouble to check my code, and tell me that I am wrong thanks for your time.

Code: [Select]


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

#define DHTPIN 2                
#define DHTTYPE DHT22      

DHT dht(DHTPIN, DHTTYPE);                  

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x4E, 0xD7 };      
byte ip[] = { 192,168,1,87 };                            
byte server[] = { 192,168,1,117 };                        
EthernetClient client;


void setup() {
  Serial.begin(9600);
  Serial.println("newexample");
  Serial.print("dht22");
  delay(1000);
  Ethernet.begin(mac, ip);

  
  Serial.print("address IP: ");
  Serial.println(Ethernet.localIP());
  Serial.println();
  
  
  Serial.print("run Sensor DHT");
  Serial.println(DHTTYPE);
  Serial.println();
  dht.begin();
 
  delay(2000);  
}

void loop() {
  delay(1000);  
  float h = dht.readHumidity();
  float t = dht.readTemperature();  
  
 
  if (isnan(h) || isnan(t)) {
    Serial.println("Wrong sensor DHT!");
    return;
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");

  if (client.connect(server, 80)) {  
    client.print("GET /test/iot.php?valorh=");
    client.print(h);
    client.print("GET /test/iot.php?valort=");
    client.print(t);
    client.println(" HTTP/1.0");
    client.println("User-Agent: Arduino 1.0");
    client.println();
    Serial.println("Connected");
  } else {
    Serial.println("Wrong conexion");
  }
  if (!client.connected()) {
    Serial.println("Disconnected!");
  }
  client.stop();
  client.flush();
  delay(60000);
}





I have a folder on my server called test and inside these two files config.php and iot.php. The config.php file contains the information to establish a connection to the database. The iot.php file uploads the received data to the sensor database.


config.php
Code: [Select]

<?php
// config.php
   
    
$dbhost "localhost";
    
$dbuser "root";
    
$dbpass "";
    
$dbname "sensores";
  
    
$con mysqli_connect($dbhost$dbuser$dbpass$dbname);
?>




iot.php
Code: [Select]

<?php
    
// iot.php
    
    
require("config.php");
  
    
$valorh mysqli_real_escape_string($con$_GET['valorh']);
    
$query "INSERT INTO valores(valorh) VALUES('".$valorh."')";
    
$valort mysqli_real_escape_string($con$_GET['valort']);
    
$query "INSERT INTO valores(valort) VALUES('".$valort."')";
    
    
mysqli_query($con$query);
    
mysqli_close($con);
?>




Also in Mysql I have created the database sensores, inside I have the table valores:
id(int(autoincrementable)
valorh(double)
valort(double)
tiempo(timestamp))



thx for helping me.

rw950431

You can check your PHP code by visiting the iot.php URL in your browser with values in the query string something like

http://<your XAMPP ip address>/test/iot.php?valort=20&valorh=50


Rather than do two seperate GETs you can combine them like I did above..  try something like

Code: [Select]
    client.print("GET /test/iot.php?valorh=");
    client.print(h);
    client.print("&valort=");
    client.println(t);
    client.println("Host: 192.168.1.1");  // change this to your IP address
    client.println("Connection: close");
    client.println();


And find the location of the XAMPP log files so you can monitor whats being sent from the arduino.

PaulS

Quote
Rather than do two seperate GETs you can combine them like I did above
You can do two separate GET requests, but each one must be completely separate. Make one, read the response, and then make the second one.

What is being sent, by your code, to the server is something like:
"GET /test/iot.php?valorh=33 GET /test/iot.php?valort=14 HTTP/1.0"
which is NOT a valid GET request.
The art of getting good answers lies in asking good questions.

Go Up