Currently, I use ESP8266-V12 connect with Arduino Uno by UART. I want send data to server by using GET HTTP request via calling senddata(). And I want to get information from a file txt on server via pingrequest().
But, I can't GET successfully. Could you please review my code?
If code has any problem, please teach me.
Thanks
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3);
void printResponse();
void setup_wifi();
char serialbuffer[100];//serial buffer for request url
void setup()
{
Serial.begin(9600);
while (!Serial) {
}
esp8266.begin(9600); // or 115200 if your ESP can only communicate at that speed
setup_wifi();
}
void printResponse() {
while (esp8266.available()) {
Serial.println(esp8266.readStringUntil('\n'));
}
}
boolean WaitForOK(long timeoutamount) {
return WaitForResponse("OK", timeoutamount);
}
boolean WaitForReady(long timeoutamount) {
return WaitForResponse("ready", timeoutamount);
}
boolean WaitForResponse(String response, long timeoutamount) {
unsigned long timeout = millis() + timeoutamount;
while (millis() <= timeout) {
while (esp8266.available() > 0) {
int len = esp8266.readBytesUntil('\n', serialbuffer, sizeof(serialbuffer));
String message = String(serialbuffer).substring(0, len - 1);
if (message == response) {
return true;
}
}
}
return false;
}
void setup_wifi()
{
Serial.println("setup wifi");
pinMode(12,OUTPUT);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(12, HIGH);
esp8266.println("AT+RST");
if (WaitForReady(2000))
{
Serial.println("\tAT+RST ......successfully");
}
esp8266.println("AT+CWQAP");
if (WaitForOK(1000))
{
Serial.println("\tAT+CWQAP ......successfully");
}
esp8266.println("AT+CWMODE=1");
if (WaitForOK(1000))
{
Serial.println("\tAT+CWMODE=1 ......successfully");
}
esp8266.println("AT+CIPMUX=1");
if (WaitForOK(1000))
{
Serial.println("\tAT+CIPMUX=1 ......successfully");
}
String WIFI_NAME = "Cuong868";
String PASS = "0938021868";
String cmd="AT+CWJAP=\""+ WIFI_NAME + "\",\""+ PASS + "\"";
esp8266.println(cmd);
if (WaitForOK(7000))
{
Serial.print("\t");
Serial.print(cmd);
Serial.println(" ......successfully");
}
esp8266.println("AT+CIFSR");
if (WaitForOK(1000))
{
Serial.println("\tAT+CIFSR ......successfully");
}
}
unsigned long lastTimeMillis = 0;
unsigned int ID = 000001;
float flat = 0;
float flon = 0;
void senddata()
{
while(1)
{
esp8266.println("AT+CIPSTART=4,\"TCP\",\"198.54.115.236\",80");
if (WaitForOK(5000))
{
Serial.println("AT+CIPSTART=\"TCP\",\"198.54.115.236\",80");
break;
}
}
String send_cmd="GET /hethongphancung/thaydoi.php?ma=";
send_cmd.concat(String(ID));
send_cmd.concat("&vido=");
send_cmd.concat(String(flat));
send_cmd.concat("&kinhdo=");
send_cmd.concat(String(flon));
send_cmd.concat("&trangthai=0");
send_cmd.concat(" HTTP/1.1\r\n");
send_cmd.concat("Host: www.thungrac.tk\r\n\r\n");
//send_cmd.concat("Connection: close\r\n\r\n");
Serial.println(send_cmd);
esp8266.print("AT+CIPSEND=4," + String(send_cmd.length() + 4));
delay(1000);
printResponse();
esp8266.print(send_cmd);
delay(1000);
printResponse();
delay(1000);
esp8266.println("AT+CIPCLOSE");
WaitForOK(1000);
}
String location= "/thung-rac/000001.txt";
void pingrequest()
{
while(1)
{
esp8266.println("AT+CIPSTART=4,\"TCP\",\"198.54.115.236\",80");
if (WaitForOK(5000))
{
Serial.println("AT+CIPSTART=\"TCP\",\"198.54.115.236\",80");
break;
}
}
String send_cmd="GET ";
send_cmd.concat(location);
send_cmd.concat(" HTTP/1.1\r\n");
send_cmd.concat("Host: www.thungrac.tk\r\n\r\n");
//send_cmd.concat("Connection: close\r\n\r\n");
Serial.println(send_cmd);
esp8266.print("AT+CIPSEND=4," + String(send_cmd.length() + 4));
delay(1000);
printResponse();
esp8266.print(send_cmd);
delay(1000);
printResponse();
delay(1000);
esp8266.println("AT+CIPCLOSE");
WaitForOK(1000);
}
int flag = 0;
void loop()
{
if(flag == 0 )
{
senddata();
pingrequest();
flag = 1;
}
}