I want to get data from ESP to my Arduino. I added firmware v2 to my ESP and it works correctly in my serial USB to TTL and shows the JSON data that I send to it.
But when I connect to my Uno serial it shows some unknown chars.
#include <SoftwareSerial.h> //Including the software serial library
#define DEBUG true
SoftwareSerial esp8266(2, 3); // This will make the Arduino pin 2 as the RX pin and Arduino pin 3 as the TX. Software UART
/* So you have to connect the TX of the esp8266 to the pin 2 of the Arduino and the TX of the esp8266 to the pin 3 of the Arduino. This means that you need to connect the TX line from the esp to the Arduino's pin 2 */
void setup() {
Serial.begin(115200); // Setting the baudrate to 9600
esp8266.begin(115200); // Set it according to your esp’s baudrate. Different esp’s have different baud rates.
pinMode(11, OUTPUT); // Setting the pin 11 as the output pin.
digitalWrite(11, LOW); // Making it low.
pinMode(12, OUTPUT); // Setting the pin 12 as the output pin..
digitalWrite(12, LOW); // Making pin 12 low.
pinMode(13, OUTPUT); // Setting the pin 13 as the output pin.
digitalWrite(13, LOW); // Making pin 13 low.
sendData("AT+RST\r\n", 2000, DEBUG); //This command will reset module to default
sendData("AT+CWMODE=2\r\n", 1000, DEBUG); // This will configure the mode as access point
sendData("AT+CIFSR\r\n", 1000, DEBUG); // This will get ip address and will show it
sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // This will configure the ESP8266 for multiple connections
sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // This will set the server on port 80
}
void loop() {
if (esp8266.available()) {
// Checking that whether the esp8266 is sending a message or not (Software UART Data)
while (esp8266.available()) {
// Checking whether ESP8266 has received the data or not
char c = esp8266.read(); // Read the next character.
Serial.print(c);
// Storing the response from the ESP8266
}
}
}
String sendData(String command, const int timeout, boolean debug) {
// Function to send the data to the esp8266
String response = "";
esp8266.print(command); // Send the command to the ESP8266
long int time = millis();
while ((time + timeout) > millis()) {
// ESP8266 will wait for some time for the data to receive
while (esp8266.available()) {
// Checking whether ESP8266 has received the data or not
char c = esp8266.read(); // Read the next character.
response += c; // Storing the response from the ESP8266
}
}
if (debug) {
Serial.print(response);
// Printing the response of the ESP8266 on the serial monitor.
}
return response;
}
Prints this for me:
???AR)?WS?TZ?eO?C?AJ+CIFSR
+CIFSR:APHP,"192.168.4.1"
+CIFSQ:APMAC,"62:01:94:22:96:7e"
OK
AZ??R????jC?u?H?AT+CIPSERVDR=1,80
OK
0,CONNECT
+IPD,0,385:POST / HTTP/1.1
cache-control: no-cache
Postman-Token; 45494ff7-b3e1-400f-8afc-fcda26514705
Content,Type: application/jsonUser-Agent: PostmanRunthme/6.1.6
@ccept: */*Host: 192.178.4.1
accept-encoding: gzip, defl`te
content,length: 14Connection: keep-alive
{"name":"`li"}0,CLOSED
My correct JSON is: {"name":"ali"}
. But it shows incorrectly. And I set the mt serial library to #define _SS_MAX_RX_BUFF 1024
.
How can I fix this?
My connection is:
3 Answers 3
i used AT+UART_DEF=9600,8,1,0,0 and it works correctly now
Software serial doesn't work reliably at 115200. You need to use a lower baud rate:
- Connect the ESP8266 back to your serial USB to TTL setup.
- Send the following command to reconfigure the ESP8266 to do AT communication at 9600 baud:
AT+UART_DEF=9600,8,1,0,3
- Change line 9 of your sketch to
esp8266.begin(9600);
. This will cause the Uno to communicate with the ESP8266 at 9600 baud. - Upload the revised sketch to your Uno and connect it back up to the ESP8266.
-
when i use this command AT+UART_DEF=9600,8,1,0,3 and i change to 9600 do not show any thing or command AT no response from esp why this happenaliyousefian– aliyousefian2017年08月13日 05:54:17 +00:00Commented Aug 13, 2017 at 5:54
Try connecting arduino's TX and RX pins (0 and 1) with esp8266's RX and TX and see if it works. Upload a blank sketch. Whatever you type in serial box will be sent to esp. Instead of using voltage divider you should use logic level converters with LM2596.
-
i used pin 0 and 1 but nothing changed why some characters shows wrongaliyousefian– aliyousefian2017年08月12日 16:57:27 +00:00Commented Aug 12, 2017 at 16:57