使用POST方法将数据从Arduino发送到PHP
我正试图向服务器发送一些数据。当我单独使用此代码时,它可以正常工作。但是当我将我的其他程序代码添加到Arduino时;变量使用POST方法将数据从Arduino发送到PHP,php,sockets,post,arduino,wifi,Php,Sockets,Post,Arduino,Wifi,我正试图向服务器发送一些数据。当我单独使用此代码时,它可以正常工作。但是当我将我的其他程序代码添加到Arduino时;变量postRequest没有获得正确的值(如图所示) 正确结果 当添加我的其他程序代码时,结果是: 数据不正确 arduino代码: 发送数据的功能: boolean httpPOST(String data, String server, int port, String uri, String* response) { // initiate TCP connec
postRequest
没有获得正确的值(如图所示)
正确结果
当添加我的其他程序代码时,结果是:
数据不正确
arduino代码:
发送数据的功能:
boolean httpPOST(String data, String server, int port, String uri, String* response) {
// initiate TCP connection
String tcpStart = "AT+CIPSTART=0,\"TCP\",\"" + server + "\"," + port;
Serial.println(tcpStart);
// prepare the data to be posted
String postRequest =
"POST " + uri + " HTTP/1.1\r\n" +
"Host: " + server + ":" + port + "\r\n" +
"Accept: *" + "/" + "*\r\n" +
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n"
"\r\n" +
"record=123456789876543212345678765";
// notify ESP8266 about the lenght of TCP packet
String sendCmd = "AT+CIPSEND=0," + postRequest.length();
esp8266.println(tcpStart);
if (esp8266.find("OK")) {
Serial.println("TCP connection OK");
Serial.println(postRequest);
Serial.println(postRequest.length());
jj=1;
esp8266.print("AT+CIPSEND=0,");
esp8266.println(postRequest.length());
delay(500);
if (esp8266.find(">")) {
Serial.println("Sending packet...");
esp8266.println(postRequest);
if (esp8266.find("SEND OK")) {
Serial.println("Packet sent");
while (esp8266.available()) {
String tmpResp = esp8266.readString();
response = &tmpResp;
}
// close the connection
esp8266.println("AT+CIPCLOSE=0");
Serial.println("close");
return true;
}
else {
Serial.println("An error occured while sending packet");
return false;
}
}
else {
Serial.println("ESP8266 is not listening for incoming data");
return false;
}
}
else {
Serial.println("Cannot initiate TCP connection");
return false;
}
}
循环中调用的函数:
void loop(){
//some codes ..
if (check()) { //check function for checking connected esp8266 Module to arduino
resetESP8266();
if (connectWifi("apwifi", "password")) {
delay(5000);
String response="";
boolean ok = httpPOST("record="+record, "192.168.1.109", 80, "/lab/include/reciv.php", &response);
if (ok) {
Serial.println(response);
}
else {
Serial.println("Something went wrong while posting data");
}
}
}
//some codes..
}
这意味着您的postRequest.length()为0。您没有从设备获得输出。您能否向arduino提供连接设备的更多详细信息以及您项目的任何草图?是的,它是空的。除了WiFi模块esp8266外,时钟模块(ds3231)和NRF rc522还连接到Arduino。其他代码来操作它们。例如,库(此库允许您与I2C/TWI设备通信)当库添加到程序中时,其值不可变@AmitRayit很有趣!!当我们减少发送的数据量时(String postRequest=“POST”+uri+”HTTP/1.1\r\n“+”主机:“+server+”:“+port+”\r\n“+”接受:“+”/“+”*\r\n“+”记录=12345678876543212345678765”),postRequest量是可变的,并且发送正确。您如何为ESP供电?如果它来自Arduino,这种行为是正常的。只有在添加其他代码时,程序才能正常工作。你自己也遇到过同样的问题吗?解决办法是什么@驱逐4