Arduino 从以太网盾将数据加载到数据库中

Arduino load data into database from ethernet shield

我有一个关于编程 harduino 的问题。 我有两个阿杜伊诺。第一个是传感器温度 DHT22 和发射器 433 mhz。第二个是接收器和以太网屏蔽。 第一个必须在第二个 arduino 发送温度和湿度,它必须加载到位于一个服务器(altervista)中的数据库中。

一切正常,读取参数并在第二个arduino上发送数据,但数据没有加载到数据库中。

这是我为接收器 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();
}
}

这是从以太网盾写入数据库的代码:

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

问题是,当我查看数据时,温度和湿度显示为 0,因为在 $temp1=$_POST["temp1"]; $hum1=$_POST["hum1"];

我认为问题出在用于创建字符串以从 arduino 向数据库发送请求的数组,因为在 temp1 和 hum1 中,值是 0

您在 Arduino 代码上使用 GET 请求,但试图在 PHP 代码上接收 POST 请求。你必须在两者上使用相同的东西。

你也可以在 PHP 文件上使用 GET 并使用你在 arduino 代码中设置的变量名:

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

既然你好像在学习,我建议你使用上面的选项。如果您想了解更多,请继续执行以下操作,并提出 POST 请求:

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