Search code examples
arduinoethernet

Arduino load data into database from ethernet shield


I have one problem on programming harduino. I have two arduino. In the first there are Sensor Tmperature DHT22 and Transmitter 433 mhz. In the second there are reciever and ethernet shield. The first must send temperature and Humidity at the second arduino that it must load into a database located in a one server(altervista).

All works fine, the read of parameters and sending the data on second arduino, but the data not load into database.

This is the code that I done for the receiver arduino(rx):

#include <DataCoder.h>
#include <VirtualWire.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90, 0x90, 0x90, 0x90, 0x90, 0x90  };
IPAddress ip(192,168,1,127); //indirizzo IP disponibile sulla rete
IPAddress myDns(8,8,8,8); //tuo DNS
char server[] = "www.ilmeteomolfetta.altervista.org"; //sito web
EthernetClient client;
String strURL = ""; //Stringa per caricare dati sul web
const int rx_pin=4;
const int led_pin=13;

void setup() 
{
delay(1000);
Serial.begin(9600);
SetupRFDataRxnLink(rx_pin, 800);
Ethernet.begin(mac, ip, myDns);
//Invio al PC il mio IP
Serial.print("Il mio IP: ");
Serial.println(Ethernet.localIP());
}

void loop() 
{
if(client.connect(server,80))
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
union RFData inDataSeq;//To store incoming data
float inArray[2];//To store decoded information
if(RFLinkDataAvailable(buf, &buflen))
{
    for(int i =0; i< buflen; i++)
    {
        inDataSeq.s[i] = buf[i];
    }
 DecodeRFData(inArray, inDataSeq);
 Serial.print("Umidita': ");
 Serial.print(inArray[1]);
 Serial.print("\t");
 Serial.print("Temperatura: ");
 Serial.print(inArray[0]);
 Serial.println();
}
    Serial.println("Connessione...");
    //Creo URL
    strURL="GET /add.php?temp="; //URL
    strURL+=inArray[0];
    strURL+="&umi=";
    strURL+=inArray[1];
    strURL+=" HTTP/1.1";
    //Invio richiesta al server
    client.println(strURL);
    client.println("Host: www.ilmeteomolfetta.altervista.org");
    client.println("Connection: Close");
    client.println();
    client.stop();
    Serial.println(strURL);
    delay(5000);
}
else
{
    Serial.println("Errore di connessione...");
    Serial.println("Disconnessione in corso...");
    client.stop();
}
}

and this is the code that writes into database from ethernet shild:

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

The problem is that when I view the data it show 0 to temperature and Humidity because in $temp1=$_POST["temp1"]; $hum1=$_POST["hum1"];

I think that the problem is the array used for create the string to send request to database from arduino because in temp1 and hum1 the values are 0


Solution

  • You're using a GET request on your Arduino code but trying to receive a POST request on the PHP code. You have to use the same on both.

    Either you use GET also on the PHP file AND use the variable names you set on your arduino code:

    <?php
    include("connect.php");
    
    $link=Connection();
    
    $temp1=$_GET["temp"]; // The "temp" should match the parameter on the URL
    $hum1=$_GET["umi"]; // "umi" is the other parameter.
    
    $query = "INSERT INTO `tempLog` (`temperature`, `humidity`) 
        VALUES ('".$temp1."','".$hum1."')"; 
    
    mysql_query($query,$link);
    mysql_close($link);
    
    header("Location: index.php");
    ?>
    

    Since you seem to be learning, I suggest that you use the option above. If you want to learn some more, go ahead and do the following, with a POST Request:

    paramURL="temp="; //URL
    paramURL+=inArray[0];
    paramURL+="&umi=";
    paramURL+=inArray[1];
    //Invio richiesta al server
    client.println("POST /add.php HTTP/1.0");
    client.println("Host: www.ilmeteomolfetta.altervista.org");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(paramURL.length());
    client.println();
    client.println(paramURL);
    
    client.stop();
    Serial.println(paramURL);
    delay(5000);