Hi,
I have esp8266-01 module connected to my Arduino Uno where ESP8266-01 TX is connected to pin 10 in Arduino and ESP8266-01 RX is connected to pin 11 in Arduino .
I am running the below code but ESP8266-01 still not connected to my AP. When I run those AT commands via Serial Monitor the ESP8266-01 is connected to my AP and set server.
Any idea what I am doing wrong?
Here is my code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266SerialPort(10,11);
String receivedString = "";
boolean newData = false;
char prevChar = 'A';
boolean getMessage = false;
void setup() {
Serial.begin(115200);
Serial.println("Inside setup");
// 10 sec delay, let esp8266 do its own reset
delay(3000);
esp8266SerialPort.begin(9600);
// esp8266SerialPort.setTimeout(TIMEOUT);
// delay(2000);
// sendCommand("AT", "OK", CONTINUE);
Serial.println("sending reset command");
esp8266SerialPort.println("AT+RST");
// recvString();
// showNewData();
delay(8000);
String cmd = "AT+CWJAP="TP-LINK_8E7A1A"; cmd += "",""; cmd += ""; cmd += """;
Serial.println("Connecting to AP");
esp8266SerialPort.println(cmd);
delay(4000);
Serial.println("setting mode to 1");
esp8266SerialPort.println("AT+CWMODE=1");
// recvString();
// showNewData();
delay(2000);
Serial.println("Enabling more than one connection");
esp8266SerialPort.println("AT+CIPMUX=1");
// recvString();
// showNewData();
delay(2000);
Serial.println("Get IP address");
esp8266SerialPort.println("AT+CIFSR");
// recvString();
// showNewData();
delay(2000);
Serial.println("start server on prot 1001");
esp8266SerialPort.println("AT+CIPSERVER=1,1001");
// recvString();
// showNewData();
delay(2000);
// Serial.println("Preparing to send data.. ");
// delay(5000);
// esp8266SerialPort.println("AT+CIPSEND=0,5");
// Serial.println("Sending data");
// esp8266SerialPort.println("DUDY");
// delay(2000);
}
void loop() {
// Wifi.begin(ssid);
//esp8266SerialPort.println("AT+QAP");
delay(1000);
// Serial.println("inside loop, iteration done");
recvString();
showNewData();
delay(2000);
// Serial.println("Preparing to send data.. ");
// delay(5000);
// esp8266SerialPort.println("AT+CIPSEND=0,5");
// Serial.println("Sending data");
// esp8266SerialPort.println("DUDY");
// delay(2000);
Serial.println("Preparing to send data to application.. ");
sendData();
delay(2000);
}
void sendData(){
//esp8266SerialPort.println("AT+CIPSERVER=1,1001");
esp8266SerialPort.println("AT+CIPSEND=0,21");
esp8266SerialPort.println("DUDY");
delay(3000);
// esp8266SerialPort.println("AT+CIPCLOSE=0");
}
void recvString() {
if (esp8266SerialPort.available() > 0) {
// Serial.println("recieved new char ");
}
while (esp8266SerialPort.available() > 0) {
char c ;
c = (char)(esp8266SerialPort.read());
receivedString = c;
// Serial.println("recieved new char " + receivedString );
// In case we saw '^XX' and have not started to read the message (e.g. getMessage == false)
// it meands we started to read the message
//
if (prevChar == 'X' && c == 'X' && !getMessage) {
//Serial.println(c);
Serial.print("recieved new string: ");
getMessage = true;
continue;
};
// In case we finished to read the message, 'XX$'
//
if (c == 'X' && getMessage && prevChar == 'X' ) {
//Serial.println(c);
getMessage = false;
continue;
};
// Recieved first 'X' that closes the 'XX$' so lets stop printing
//
if (getMessage && c == 'X') {
getMessage = false;
Serial.println("");
continue;
}
// We are in the middle of reading a char
if (getMessage) {
Serial.print(c);
prevChar = c;
continue;
}
prevChar = c;
newData = true;
}
}
void showNewData() {
if (newData == true) {
//Serial.print("This just in ... ");
//Serial.println(receivedString);
newData = false;
}
}
Thanks.