Hello!
I am using an ESP8266 with arduino uno/nano to send data to a mysql server, using AT commands and software serial, with no problem at all. Everything works fine.
Now, I want to be able also to extract data from the server response.
The only problem is, that I can't write a code to actually parse this serial response, and extract from there, the data I am interested. It is difficult to do this?
Here is the code:
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
String AP = "xxxxxxx"; // CHANGE ME
String PASS = "xxxxx"; // CHANGE ME
String HOST = "home.adrianc.ro";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
SoftwareSerial ESP8266 (rxPin, txPin);
unsigned long lastTimeMillis = 0;
void setup() {
Serial.begin(9600);
ESP8266.begin(9600);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
delay(1000);
}
void printResponse() {
while (ESP8266.available()) {
Serial.println(ESP8266.readStringUntil('\n'));
}
}
void loop() {
if (millis() - lastTimeMillis > 30000) {
lastTimeMillis = millis();
ESP8266.println("AT+CIPMUX=1");
delay(1000);
printResponse();
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"home.adrianc.ro\",80");
delay(1000);
printResponse();
String cmd = "GET /weatherstation/readtemp.php?";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
ESP8266.println(cmd);
delay(1000);
ESP8266.println("");
}
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
ESP8266.println(command);//at+cipsend
if(ESP8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
The response from server is:
09:20:35.183 -> AT+CIPMUX=1
09:20:35.183 ->
09:20:35.183 -> OK
09:20:36.214 -> AT+CIPSTART=4,"TCP","home.adrianc.ro",80
09:20:36.261 -> 4,CONNECT
09:20:36.308 ->
09:20:36.308 -> OK
09:20:38.288 -> AT+CIPSEND=4,37
09:20:38.288 ->
09:20:38.322 -> OK
09:20:38.322 -> >
09:20:38.322 -> Recv 37 bytes
09:20:38.322 ->
09:20:38.322 -> SEND OK
09:20:38.459 ->
09:20:38.459 -> +IPD,4,8:
09:20:38.459 -> 5.00
09:20:38.459 -> 4,CLOSED
So, the part that I am interested in, is 5.00, right after: "+IPD,4,8:" and before "4,CLOSED" wich is the server response. how can I parse that, and transform it to a string / and eventually to a float / or int.
If I must put some delimiter, special character or something, for the parsing process to be easier, before and after 5.00, I can do this with no problem.
Thank you very much!
Adrian.