我正在尝试将一些数据发送到服务器。当我单独使用此代码时,正常工作。但是当我将其他程序代码添加到Arduino时;变量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..
}