Hola amigos,
Quienes han visto mis post saben que investigo y pruebo, busco ejemplos, tutoriales, etc. Pero no encuentro ya solución a mi proyecto.
Tengo un arduino UNO y una Shield Ethernet W5100, y una base de datos XAMPP con mysql con archivos php.
Intento enviar datos a MySQL ya sea directamente o vía web service. Cuando empiezo desde 0 no tengo problema en conectarme a Google.com, encontrar la IP de la tarjeta, pero cuando intento conectar al servidor web para enviar peticiones GET ahí se acaba la historia.
He copiado literalmente ejemplos como los de sensores de humedad y temperatura, y me da errores como DHT not defined, o que que no puede subir el sketch, lo mas que he logrado es conectar el web server pero no reconoce la petición GET indicando error 400.
Puedo copiar el link http://localhost:80/ethernet/data.php?temperature=15&humidity=14&heat_index=13 en la url y envía datos a la base de datos, pero no desde el arduino.
En resumen, no logro enviar datos al web service, si alguien puede orientarme se los agradeceré.
Acompaño y código y los archivos php.
<
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <Ethernet.h>
#include <DHTesp.h>
#define DHTPIN 9;
#define DHTTYPE DHT22;
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 0, 14 }; //Enter the IP of ethernet shield
byte serv[] = {192, 168, 0, 13} ; //Enter the IPv4 address
EthernetClient cliente;
void setup() {
Serial.begin(9600); //setting the baud rate at 9600
Ethernet.begin(mac, ip);
dht.begin();
}
void loop() {
float hum = dht.readHumidity(); //Reading the humidity and storing in hum
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
float fah = dht.readTemperature(true); //reading the temperature in Fahrenheit
float heat_index = dht.computeHeatIndex(fah, hum); //Reading the heat index in Fahrenheit
float heat_indexC = dht.convertFtoC(heat_index); //Converting the heat index in Celsius
if (cliente.connect(serv, 80)) { //Connecting at the IP address and port we saved before
Serial.println("connected");
cliente.print("GET /ethernet/data.php?"); //Connecting and Sending values to database
cliente.print("temperature=");
cliente.print(temp);
cliente.print("&humidity=");
cliente.print(hum);
cliente.print("&heat_index=");
cliente.println(heat_indexC);
//Printing the values on the serial monitor
Serial.print("Temperature= ");
Serial.println(temp);
Serial.print("Humidity= ");
Serial.println(hum);
Serial.print("Heat Index= ");
Serial.println(heat_indexC);
cliente.stop(); //Closing the connection
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(5000);
}
ARCHIVO PHP DATA
<?php
include ('connection.php');
$sql_insert = "INSERT INTO data (temperature, humidity, heat_index) VALUES ('".$_GET["temperature"]."', '".$_GET["humidity"]."', '".$_GET["heat_index"]."')";
if(mysqli_query($con,$sql_insert))
{
echo "Done";
mysqli_close($con);
}
else
{
echo "error is ".mysqli_error($con );
}
?>
ARCHIVO PHP CONNECTION
<?php
$username = "root"; // Usuario DB
$pass = ""; //Contraseña DB
$host = "localhost"; //IP de la Base de Datos
$db_name = "ethernet"; //Nombre de la Base de Datos
$con = mysqli_connect ($host, $username, $pass);
$db = mysqli_select_db ( $con, $db_name );
?
>