I have problem with my esp8266 it connects to my site and sends 89 bytes of data but does return a confirmation status all i get is SEND OK but i don't get any confirmation from my server
This is my code below
#include <SoftwareSerial.h>
#include<time.h>
SoftwareSerial ESP8266(3, 2); // RX, TX
//DEFINE ALL GLOBAL VAARIABLES HERE
unsigned int timeout_start_val;
char scratch_data_from_ESP[20];//first byte is the length of bytes
char payload[150];
byte payload_size=0, counter=0;
char ip_address[16];
char youtube_subs[10];
char youtube_views[13];
char current_temp[5];
char hi_temp[5];
char lo_temp[5];
//MODES
const char CWMODE = '3';//CWMODE 1=STATION, 2=APMODE, 3=BOTH
const char CIPMUX = '1';//CIPMUX 0=Single Connection, 1=Multiple Connections
//PASS word
const char SSID_ESP[]="Xamel";//"AOTEA TZ";//from constant
const char SSID_KEY[]="cewu5103";//"wyrnC592";
//DEFINE KEYWORDS HERE
const char keyword_OK[]="OK";
const char keyword_FAIL[]="FAIL";
const char keyword_Ready[] = "Ready";
const char keyword_no_change[] = "no change";
const char keyword_blank[] = "#&";
const char keyword_ip[] = "192.";
const char keyword_rn[] = "\r\n";
const char keyword_quote[] = "\"";
const char keyword_carrot[] = ">";
const char keyword_sendok[] = "SEND OK";
const char keyword_linkdisc[] = "Unlink";
const char keyword_CHECKCWMODE[] = "+CWMODE:3";
const char keyword_URL_CONNECTION_ERROR[]="Link typ ERROR";
const char TEST_URL[]="";
const char GET_URL[]="GET /ESP8266_BackEnd/SaveDetail.php?Name=";
int str1=strlen(GET_URL);
const char Name[]="Alex ";
int str2=strlen(Name);
const char TRAILING_URL[]="HTTP/1.1 \\r\\nHost: www.pick2get.com\\r\\n\\r\\n";
int str3=strlen(TRAILING_URL);
int strTotal=str1+str2+str3;
const char AT_CIPSTART[]="AT+CIPSTART=4,\"TCP\",\"pick2get.com\",80 \r\n";
const char AT_CIPSEND[]="AT+CIPSEND=4,";
//function prototype
boolean read_until_ESP(const char keyword1[], int key_size, int timeout_val, byte mode);
void serial_dump_ESP();
boolean setup_ESP(const char SSID_ESP[],const char SSID_KEY);
String url="&School=4D616B6F6E676F";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//.print("Total string length");Serial.println(strTotal);
// set the data rate for the SoftwareSerial port
ESP8266.begin(9600);
//
//Serial.println(read_until_ESP(keyword_URL_CONNECTION_ERROR,sizeof(keyword_URL_CONNECTION_ERROR),5000,0));
setup_ESP(SSID_ESP,SSID_KEY);
ESP8266.print(AT_CIPSTART);
ESP8266.print("");
delay(1000);
ESP8266.print(AT_CIPSEND);
ESP8266.print(strTotal);
ESP8266.print("\r\n");
delay(1000);
ESP8266.print("");
ESP8266.print(GET_URL);
ESP8266.print(Name);
ESP8266.print(TRAILING_URL);
delay(1000);
ESP8266.print("");
//ESP8266.println("&Grade=466F726D204F6E65");
//ESP8266.println("&DeviceIP=3139322E3136382E302E31");
//ESP8266.println(TRAILING_URL);
}
void loop() { // run over and over
byte buffer[300];
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
if (Serial.available()) {
ESP8266.write(Serial.read());
}
}
This is the output generated on the serial monitor
ESP CHECK OK
ESP RESET OK
ESP RESET OK
timeout
ESP CWMODE SET FAILED
CWMODE ALREADY SET
SETTING SSID_ESP: Xamel
SETTING SSID_KEY: cewu5103
ESP SSID SET OK DEVICE CONNECTED
CHECKING FOR AN IP ADDRESS
IP ADDRESS = 192.168.4.1
ESP CIPMUX SET
AT+CIPSTART=4,"TCP","pick2get.com",80
OK
Linked
AT+CIPSEND=4,89
> GET /ESP8266_BackEnd/SaveDetail.php?Name=Alex HTTP/1.1 \r\nHost: www.pick2get.com\r\n\r\n
SEND OK
OK
Unlink
Can any one please help here.
When adding ESP8266.print("+IPD\r\n"); this is the output i get
ESP CHECK OK
ESP RESET OK
ESP RESET OK
timeout
ESP CWMODE SET FAILED
CWMODE ALREADY SET
SETTING SSID_ESP: Xamel
SETTING SSID_KEY: cewu5103
ESP SSID SET OK DEVICE CONNECTED
CHECKING FOR AN IP ADDRESS
IP ADDRESS = 192.168.4.1
ESP CIPMUX SET
AT+CIPSTART=4,"TCP","pick2get.com",80
AT+CIPSEND=4,89
busy p...
OK
Linked
GET /ESP8266_BackEnd/SaveDetail.php?Name=Alex HTTP/1.1 \r\nHost: www.pick2get.com\r\n\r\n
wrong syntax
ERROR
AT+IPD
ERROR
I have managed to fix the lack of server response by removing the \r\n \nd replacing them with regular \r\n\r\n
enter code here
const char TRAILING_URL[]="HTTP/1.1 \r\nHost: www.pick2get.com \r\n\r\n";
int str3=strlen(TRAILING_URL);
int strTotal=str1+str2+str3;
const char AT_CIPSTART[]="AT+CIPSTART=4,\"TCP\",\"pick2get.com\",80 \r\n";
const char AT_CIPSEND[]="AT+CIPSEND=4,";
At this lines and was able to get a server response
+IPD,4,392:HTTP/1.1 400 Bad Request Date: Tue, 15 May 2018 12:07:08 GMT Server: Apache Content-Length: 226 Connection: close Content-Type: text/html; charset=iso-8859-1
400 Bad Request Bad Request
Your browser sent a request that this server could not understand.
finally i have debugged and fixed all the errors and the code works just fine This and server returns a status 200
turns out it was just me pointing GET request in the wrong folder path with this line code at in the GET_URL variable
const char GET_URL[]="GET pick2get.com/ESP8266_BackEnd/SaveDetail.php?Name=";
I replaced with this
const char GET_URL[]="GET /ESP8266_BackEnd/SaveDetail.php?Name=";
And got the desired result
+IPD,4,166:HTTP/1.1 200 OK Date: Tue, 15 May 2018 12:2249 GMT Server: Apache Upgrade: h2 Connection: Upgrade Transfer-Encoding: chunked Content-Type: text/html
2
OK
+IPD,4,191:16
Connected successfully
5c
DATA TRANSMITTED SUCSSEFULLYSTUDENT NAME RECIVED: Alex
STUDENT SCHOOL NAME RECIVED:
3b
STUDENT GRADE RECIVED:
DEVICE IP ADDREESS RECIVED:
OK
+IPD,4,42:1f New record created successfully 0 Thanks a lot guys below is the complete working code
/*
Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)
Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Not all pins on the Leonardo and Micro support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
created back in the mists of time
modified 25 May 2012
by Nolawi solomon
*/
#include <SoftwareSerial.h>
#include<time.h>
SoftwareSerial ESP8266(3, 2); // RX, TX
//DEFINE ALL GLOBAL VAARIABLES HERE
unsigned int timeout_start_val;
char scratch_data_from_ESP[20];//first byte is the length of bytes
char payload[150];
byte payload_size=0, counter=0;
char ip_address[16];
//MODES
const char CWMODE = '3';//CWMODE 1=STATION, 2=APMODE, 3=BOTH
const char CIPMUX = '1';//CIPMUX 0=Single Connection, 1=Multiple Connections
//PASS word
const char SSID_ESP[]="Xamel";//"AOTEA TZ";//from constant
const char SSID_KEY[]="cewu5103";//"wyrnC592";
//DEFINE KEYWORDS HERE
const char keyword_OK[]="OK";
const char keyword_FAIL[]="FAIL";
const char keyword_Ready[] = "Ready";
const char keyword_no_change[] = "no change";
const char keyword_blank[] = "#&";
const char keyword_ip[] = "192.";
const char keyword_rn[] = "\r\n";
const char keyword_quote[] = "\"";
const char keyword_carrot[] = ">";
const char keyword_sendok[] = "SEND OK";
const char keyword_linkdisc[] = "Unlink";
const char keyword_CHECKCWMODE[] = "+CWMODE:3";
const char keyword_URL_CONNECTION_ERROR[]="Link typ ERROR";
//const char TEST_URL[]="";
const char GET_URL[]="GET /ESP8266_BackEnd/SaveDetail.php?Name=";
int str1=strlen(GET_URL);
const char Name[]="Alex ";
int str2=strlen(Name);
const char TRAILING_URL[]="HTTP/1.1\r\nHost: www.pick2get.com \r\n\r\n";
int str3=strlen(TRAILING_URL);
int strTotal=str1+str2+str3;
const char AT_CIPSTART[]="AT+CIPSTART=4,\"TCP\",\"pick2get.com\",80 \r\n";
const char AT_CIPSEND[]="AT+CIPSEND=4,";
//function prototype
boolean read_until_ESP(const char keyword1[], int key_size, int timeout_val, byte mode);
void serial_dump_ESP();
boolean setup_ESP(const char SSID_ESP[],const char SSID_KEY);
//String url="&School=4D616B6F6E676F";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//.print("Total string length");Serial.println(strTotal);
// set the data rate for the SoftwareSerial port
ESP8266.begin(9600);
//
//Serial.println(read_until_ESP(keyword_URL_CONNECTION_ERROR,sizeof(keyword_URL_CONNECTION_ERROR),5000,0));
setup_ESP(SSID_ESP,SSID_KEY);
ESP8266.print(AT_CIPSTART);
ESP8266.print("");
delay(1000);
ESP8266.print(AT_CIPSEND);
ESP8266.print(strTotal);
ESP8266.print("\r\n");
delay(1000);
ESP8266.print("");
ESP8266.print(GET_URL);
ESP8266.print(Name);
ESP8266.print(TRAILING_URL);
//delay(3000);
//ESP8266.print("");
//delay(2000);
//ESP8266.print("\r\n");
//delay(2000);
//ESP8266.print("");
//ESP8266.print("AT+IPD\r\n");
//ESP8266.println("&Grade=466F726D204F6E65");
//ESP8266.println("&DeviceIP=3139322E3136382E302E31");
//ESP8266.println(TRAILING_URL);
}
void loop() { // run over and over
byte buffer[300];
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
if (Serial.available()) {
ESP8266.write(Serial.read());
}
}
boolean read_until_ESP(const char keyword1[], int key_size, int timeout_val, byte mode){
timeout_start_val=millis();//for the timeout
char data_in[20];//this is the buffer - if keyword is longer than 20, then increase this
int scratch_length=1;//the length of the scratch data array
key_size--;//since we're going to get an extra charachter from the sizeof()
//FILL UP THE BUFFER
for(byte i=0; i<key_size; i++){//we only need a buffer as long as the keyword
//timing control
while(!ESP8266.available()){//wait until a new byte is sent down from the ESP - good way to keep in lock-step with the serial port
if((millis()-timeout_start_val)>timeout_val){//if nothing happens within the timeout period, get out of here
Serial.println("timeout");
return 0;//this will end the function
}//timeout
}// while !avail
data_in[i]=ESP8266.read();// save the byte to the buffer 'data_in[]
if(mode==1){//this will save all of the data to the scratch_data_from
scratch_data_from_ESP[scratch_length]=data_in[i];//starts at 1
scratch_data_from_ESP[0]=scratch_length;// [0] is used to hold the length of the array
scratch_length++;//increment the length
}//mode 1
}//for i
//THE BUFFER IS FULL, SO START ROLLING NEW DATA IN AND OLD DATA OUT
while(1){//stay in here until the keyword found or a timeout occurs
//run through the entire buffer and look for the keyword
//this check is here, just in case the first thing out of the ESP was the keyword, meaning the buffer was actually filled with the keyword
for(byte i=0; i<key_size; i++){
if(keyword1[i]!=data_in[i])//if it doesn't match, break out of the search now
break;//get outta here
if(i==(key_size-1)){//we got all the way through the keyword without breaking, must be a match!
return 1; //return a 1 and get outta here!
}//if
}//for byte i
//start rolling the buffer
for(byte i=0; i<(key_size-1); i++){// keysize-1 because everthing is shifted over - see next line
data_in[i]=data_in[i+1];// so the data at 0 becomes the data at 1, and so on.... the last value is where we'll put the new data
}//for
//timing control
while(!ESP8266.available()){// same thing as done in the buffer
if((millis()-timeout_start_val)>timeout_val){
Serial.println("timeout");
return 0;
}//timeout
}// while !avail
data_in[key_size-1]=ESP8266.read();//save the new data in the last position in the buffer
if(mode==1){//continue to save everything if thsi is set
scratch_data_from_ESP[scratch_length]=data_in[key_size-1];
scratch_data_from_ESP[0]=scratch_length;
scratch_length++;
}//mode 1
/* JUST FOR DEBUGGING
if(ESP8266.overflow())
Serial.println("*OVER");
*/
}//while 1
}//read until ESP
boolean setup_ESP(const char SSID_ESP[],const char SSID_KEY[]){//returns a '1' if successful
ESP8266.print("AT\r\n");// Send just 'AT' to make sure the ESP is responding
//this read_until_... function is used to find a keyword in the ESP response - more on this later and in the function itself
if(read_until_ESP(keyword_OK,sizeof(keyword_OK),5000,0))//go look for keyword "OK" with a 5sec timeout
Serial.println("ESP CHECK OK");
else
Serial.println("ESP CHECK FAILED");
serial_dump_ESP();//this just reads everything in the buffer and what's still coming from the ESP
ESP8266.print("AT+RST\r\n");// Give it a reset - who knows what condition it was in, better to start fresh
if(read_until_ESP(keyword_Ready,sizeof(keyword_Ready),5000,0))//go look for keyword "Ready" - takes a few seconds longer to complete
Serial.println("ESP RESET OK");//depneding on the FW version on the ESP, sometimes the Ready is with a lowercase r - ready
else
Serial.println("ESP RESET FAILED");
serial_dump_ESP();
ESP8266.print("AT+RST\r\n");// Give it a reset - who knows what condition it was in, better to start fresh
if(read_until_ESP(keyword_Ready,sizeof(keyword_Ready),5000,0))//go look for keyword "Ready" - takes a few seconds longer to complete
Serial.println("ESP RESET OK");//depneding on the FW version on the ESP, sometimes the Ready is with a lowercase r - ready
else
Serial.println("ESP RESET FAILED");
serial_dump_ESP();
ESP8266.print("AT+CWMODE=");// set the CWMODE
ESP8266.print(CWMODE);//just send what is set in the constant
ESP8266.print("\r\n");
if(read_until_ESP(keyword_OK,sizeof(keyword_OK),1000,0))//go look for keyword "OK"
Serial.println("ESP CWMODE SET");
else
Serial.println("ESP CWMODE SET FAILED");//probably going to fail, since a 'no change' is returned if already set - would be nice to check for two words
ESP8266.println("AT+CWMODE?");
if(read_until_ESP(keyword_CHECKCWMODE,sizeof(keyword_CHECKCWMODE),1000,0))
{
Serial.println("CWMODE ALREADY SET");
serial_dump_ESP();
}
serial_dump_ESP();
//Here's where the SSID and PW are set
Serial.print("SETTING SSID_ESP: ");
Serial.println(SSID_ESP);
Serial.print("SETTING SSID_KEY: ");
Serial.println(SSID_KEY);
//DEbug poin
ESP8266.print("AT+CWJAP=");//
ESP8266.print("\"");
ESP8266.print(SSID_ESP);//
ESP8266.print("\",\"");
ESP8266.print(SSID_KEY);//
ESP8266.print("\"\r\n");
if(read_until_ESP(keyword_OK,sizeof(keyword_OK),5000,0)==1)//go look for keyword "OK"
Serial.println("ESP SSID SET OK DEVICE CONNECTED");
else if(read_until_ESP(keyword_FAIL,sizeof(keyword_FAIL),5000,0)==1)
Serial.println("ESP SSID SET FAILED");
serial_dump_ESP();
//This checks for and stores the IP address
Serial.println("CHECKING FOR AN IP ADDRESS");
ESP8266.print("AT+CIFSR\r\n");//command to retrieve IP address from ESP
if(read_until_ESP(keyword_rn,sizeof(keyword_rn),10000,0)){//look for first \r\n after AT+CIFSR echo - note mode is '0', the ip address is right after this
if(read_until_ESP(keyword_rn,sizeof(keyword_rn),1000,1)){//look for second \r\n, and store everything it receives, mode='1'
//store the ip adress in its variable, ip_address[]
for(int i=1; i<=(scratch_data_from_ESP[0]-sizeof(keyword_rn)+1); i++)//that i<=... is going to take some explaining, see next lines
ip_address[i] = scratch_data_from_ESP[i];//fill up ip_address with the scratch data received
//i=1 because i=0 is the length of the data found between the two keywords, BUT this includes the length of the second keyword, so i<= to the length minus
//size of teh keyword, but remember, sizeof() will return one extra, which is going to be subtracted, so I just added it back in +1
ip_address[0] = (scratch_data_from_ESP[0]-sizeof(keyword_rn)+1);//store the length of ip_address in [0], same thing as before
Serial.print("IP ADDRESS = ");//print it off to verify
for(int i=1; i<=ip_address[0]; i++)//send out the ip address
Serial.print(ip_address[i]);
Serial.println("");
}}//if first \r\n
else
Serial.print("IP ADDRESS FAIL");
serial_dump_ESP();
ESP8266.print("AT+CIPMUX=");// set the CIPMUX
ESP8266.print(CIPMUX);//from constant
ESP8266.print("\r\n");
if(read_until_ESP(keyword_OK,sizeof(keyword_OK),5000,0))//go look for keyword "OK" or "no change
Serial.println("ESP CIPMUX SET");
else
Serial.println("ESP CIPMUX SET FAILED");
serial_dump_ESP();
//that's it! Could be done by nesting everything together, so if one thing fails, it returns '0', and if it gets all the way through it returns '1'...oh well
}//setup ESP
//pretty simple function - read everything out of the serial buffer and whats coming and get rid of it
void serial_dump_ESP(){
char temp;
while(ESP8266.available()){
temp =ESP8266.read();
delay(1);//could play around with this value if buffer overflows are occuring
}//while
}//serial dump