Hello guys,
I have recently bought a ESP8266 WiFi module, and I’m trying to configure with my arduino board.
I providing 3.3V and GND from arduino itself, Also TX and RX pins are connected directly on digital pins 5 and 6 (SoftwareSerial). Whenever I’m trying to read the response from module (which I store in a String buffer), it shows some junk data. Here is my code. Also I have attached the responses I’ve received from module. Please help…
#include <SoftwareSerial.h>
void send_data(void);
SoftwareSerial mySerial(5,6); // RX, TX
String buff="";
String data = "4";
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
unsigned long lastConnectionTime = 0;
void setup()
{
mySerial.begin(115200);
while(!mySerial){
//Wait
}
Serial.begin(9600);
while(!Serial){
//Wait
}
mySerial.println("AT");
if(mySerial.available()){
buff += mySerial.read();
}
if(buff="OK"){
Serial.println("AT OK");
//Serial.println(buff);
buff="";
}
else{
Serial.println("System failed");
exit(0);
}
mySerial.println("AT+RST");
delay(100);
if(mySerial.available()){
buff += mySerial.read();
}
Serial.println("AT+RST data rx is:");
Serial.println(buff);
buff="";
mySerial.println("AT+CIPMUX=1");
if(mySerial.available()){
buff += mySerial.read();
}
if(buff="OK"){
Serial.println("AT+CIPMUX=1 OK");
//Serial.println(buff);
buff="";
}else{
Serial.println("AT+CIPMUX = 1 Error!");
exit(0);
}
delay(100);
}
void loop() { // run over and over
if (millis() - lastConnectionTime > postingInterval){
send_data();
}
}
void send_data(){
Serial.println("Opening TCP connection: rohirto.ddns.net,8080, CH4");
mySerial.println("AT+CIPSTART=4,\"TCP\",\"rohirto.ddns.net\",8080");
if(mySerial.available()){
buff += mySerial.read();
}
Serial.println("AT+CIPSTART Data recieved is:");
Serial.println(buff);
buff="";
delay(100);
mySerial.println("AT+CIPSEND=4,139");
if(mySerial.available()){
buff += mySerial.read();
}
Serial.println("AT+CIPSEND Data recieved is:");
Serial.println(buff);
buff="";
delay(100);
http_post();
if(mySerial.available()){
buff += mySerial.read();
}
Serial.println("POST REQUEST Data recieved is:");
Serial.println(buff);
buff="";
delay(100);
lastConnectionTime = millis();
}
void http_post(){
Serial.println("POST request");
mySerial.println("POST /process_post HTTP/1.1");
mySerial.println("Host: rohirto.ddns.net:8080");
mySerial.println("Content-Type: application/x-www-form-urlencoded");
mySerial.print("Content-Length: ");
mySerial.println(data.length());
mySerial.println("Connection: Keep-Alive");
mySerial.println();
mySerial.println(data);
mySerial.println();
}