Wifi模块未正确发送数据

时间:2019-08-11 14:46:56

标签: c++ arduino http-post arduino-esp8266

我用Arduino Uno的c ++编写了一个程序,该程序已连接到wifi模块(ESP8266),并通过带有http发布请求的wifi模块将计算出的数据发送到服务器。

问题是程序仅通过http发送2次数据,然后停止。我的意思是说它进入了循环,但是http-send-request不成功。

这是我的程序:

#include <MySignals.h>
#include "Wire.h"
#include "SPI.h"

unsigned long currentMillis;

const unsigned long period = 10000;

int a=1;

void setup()  {

Serial.begin(115200);

MySignals.begin();
 void httpBase(int); //Enable WiFi ESP8266 Power -> bit1:1           >      bitSet(MySignals.expanderState, EXP_ESP8266_POWER);  
 MySignals.expanderWrite(MySignals.expanderState);

 MySignals.initSensorUART();

 MySignals.enableSensorUART(WIFI_ESP8266);   delay(1000);

// Checks if the WiFi module is started   int8_t answer =
sendATcommand("AT", "OK", 6000);   if (answer == 0)   {
MySignals.println("Error");
// waits for an answer from the module
while (answer == 0)
{
  // Send AT every two seconds and wait for the answer
  answer = sendATcommand("AT", "OK", 6000);
}   }   else if (answer == 1)   {

MySignals.println("WiFi succesfully working!");


if (sendATcommand("AT+CWMODE=1", "OK", 6000))
{
  MySignals.println("CWMODE OK");
}
else
{
  MySignals.println("CWMODE Error");

}


//Change here your WIFI_SSID and WIFI_PASSWORD
if (sendATcommand("AT+CWJAP=\"********\",\"****\"",
"OK", 20000))
{
  MySignals.println("Connected!");
}
else
{
  MySignals.println("Error");

}   }


 }


void loop()  {




delay(10);  // wait for a second 

a=a+1;

httpBase(a);


 }




 void httpBase(int gsr) {








 String data="value1="+String(gsr); //String 
 data="a="+String(gsr);
 String postString="POST /ardu.php HTTP/1.1\r\nHost:
 192.168.178.36\r\nContent-Type: application/x-www-form-  
 urlencoded\r\nContent-Length:
 "+String(data.length())+"\r\n\r\n"+data+"\r\n"; char* post = new
 char[postString.length()+1]; strcpy(post,postString.c_str());

 String inf = "AT+CIPSEND="+String(strlen(post)); char *info = new
 char[inf.length() + 1]; strcpy(info, inf.c_str());

 delay(1500); sendATcommand("AT+CIPMUX=0", "OK", 3000);

 //sendATcommand("AT+CIPSERVER=1","OK",1000);


 sendATcommand("AT+CIPSTART=\"TCP\",\"192.168.178.36\",80", "OK",
 2000);

 sendATcommand(info, "OK", 2000); sendATcommand(post, "OK", 2000);

 sendATcommand("AT+CIPCLOSE", "OK", 3000);



 delay(10000); }






int8_t sendATcommand(char* ATcommand, char* expected_answer1, 
unsigned
int timeout) {


uint8_t x = 0,  answer = 0;   char response[500];   unsigned long
previous;

memset(response, '\0', sizeof(response));    // Initialize the
string

delay(100);

while ( Serial.available() > 0) Serial.read();   // Clean the 
input
buffer

delay(1000);   Serial.println(ATcommand);    // Send the AT   command

 x = 0;   previous = millis();

 // this loop waits for the answer   do   {

 if (Serial.available() != 0)
 {
  response[x] = Serial.read();
  x++;
  // check if the desired answer is in the response of the module
  if (strstr(response, expected_answer1) != NULL)
  {
    answer = 1;
    //MySignals.println(response);

  }
}
// Waits for the asnwer with time out   }   while ((answer == 0) && ((millis() - previous) < timeout));

 return answer; }

这是我的输出:

AT
WiFi succesfully working!
AT+CWMODE=1
CWMODE OK
AT+CWJAP="*******","******"
Connected!
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=127
POST /ardu.php HTTP/1.1
Host: 192.168.178.36
Content-Type: application/x-www-form-urlencoded
Content-Length: 8

value1=2

AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=127
POST /ardu.php HTTP/1.1
Host: 192.168.178.36
Content-Type: application/x-www-form-urlencoded
Content-Length: 8

value1=3

AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=127
POST /ardu.php HTTP/1.1
Host: 192.168.1
⸮⸮6⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=1
⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=1
⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=1
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
AT+CIPSEND=1
⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80

⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
2


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
2


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80
⸮
⸮
AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE
AT+CIPMUX=0
AT+CIPSTART="TCP","192.168.178.36",80


AT+CIPCLOSE

....

0 个答案:

没有答案