Hi...
I'm working on esp8266 with Arduino Uno to get data from the server (from SQL database through PHP) and print on the serial monitor. I'm using AT commands, and everything is working fine. But it doesn't give correct output data all the time.
Suppose I've echoed '135' from the PHP code to print on the serial monitor, then the output on serial comes like this: (if the code is running in an infinite loop)
135
135
035
13?
135
1?^
1q5
135
135...... like this.
I've tried Serial.flush() as well, in the hope that the problem may vanish. But I didn't work.
Please help as I need 100% accuracy in the serial output.
Here is the code on Arduino:
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
SoftwareSerial esp8266(RX, TX);
String wifi = "wifi_name;
String pass = "password";
//String API = ""; //used for security purposes
String server_host = "localhost";
String port = "80";
int countTrueCommand;
String RFID_value = "";
void setup() {
Serial.begin(115200);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + wifi + "\",\"" + pass + "\"", 20, "OK");
}
void loop() {
RFID_value = getRFID_data();
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + server_host + "\"," + port, 15, "OK");
String getData = "GET /Test_site/RFID.php?tagID="+RFID_value; //This is local site that gives the output data.
sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
esp8266.println(getData);
delay(1000);
countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 10, "OK");
if (esp8266.available() > 0) {
String message = readWifiSerialMessage();
Serial.println(message);
}
}
String getRFID_data() {
return "498191616678"; //Replace with RFID Functionality.
}
String readWifiSerialMessage() {
char value[20];
int index_count = 0;
while (esp8266.available() > 0) {
//esp8266.readStringUntil('\0');
//Serial.flush();
value[index_count] = esp8266.read();
index_count++;
value[index_count] = '\0'; // Null terminate the string
}
String str(value);
str.trim();
return str;
}
void sendCommand(String command, int maxTime, char readReplay[]) {
int countTimeCommand;
boolean found = false;
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("Success");
countTrueCommand++;
countTimeCommand = 0;
}
if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}