0

I wrote a programm in c++ for Arduino Uno which is connected to a wifi module (ESP8266) and sends the calculated data to a server through wifi-Module with a http post request.

The problem is that the programm sends the data only 2 times throuth http and then it stops. I mean it goes to the loop but the http-send-request is not successful.

here is my program:

#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; }

the output is:

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

Hello Navid and welcome to the community. Buddy your code is extremely hard to read.

Now let me see if I can help you archiving your goals. First of all try not to use AT commands at all, you have an ESP8266, that's not just a Wi-Fi module but also a really good micro controller you can actually use, code for it (even using Arduino IDE). I would HIGHLY suggest you to use it if you wanna do POST and GET methods. AT commands do work, but they are not as reliable as they should be, and with your code written in that format, its going to be difficult to help you buddy.

Now if you are calling it a Wi-Fi "Module" I can presume it's a ESP01 you are talking about (those ESP8266 with no usb connection), you can find on the internet a few tutorials on how to upload code to them using your arduino uno's FTDI chip (basicly grounding the reset pin of your Arduino).

With that been said, hopefully you can split your code into those 2 micro controllers if you really need to use the Arduino uno, and do a few functions to trigger based on Serial communication on both sides. I know it sounds a lot of work, but trust me, that's by far the easiest and more reliable solution you will find.

And let me give you an bonus tip, next time you wanna work with Wi-Fi applications, get yourself a Wemos D1, they are small easy to use, reliable, have GPIO's as well as ADC and PWM pins, you can pretty much put your Arduino on a shelf for 4 dollars.

And please remember Navid, code syntax is really important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.