我在编程方面有一个问题。我有两个阿迪诺。第一种是传感器、温度、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();
}
}这是从以太网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");
?>问题是,当我查看数据时,它显示出0到温湿度,因为在$temp1=$_POST“temp1”中,$hum1=$_POST"hum1";
我认为问题在于创建用于从arduino向数据库发送请求的字符串的数组,因为在temp1和hum1中值为0。
发布于 2016-02-09 18:07:19
您正在对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");
?>既然你似乎在学习,我建议你使用上面的选项。如果您想了解更多内容,请按以下方式进行,并提供一个帖子请求:
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);https://stackoverflow.com/questions/35297754
复制相似问题