I am trying to use a esp-8266 (esp-01) to transfer the "Serial.println()" data on my leonardo to my pc wirelessly, basically just replaces the usb cable, nothing fancy. I can't really find a good tutorial regarding the subject so I am asking for your help. I am very new to Arduino so any help will be greatly appreciated.
Here is my current code. The Serial.available keeps returning 0 for some reason so it keeps skipping if() every loop, I have no idea why:
#include <SoftwareSerial.h>
SoftwareSerial MyEsp01(2, 3);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
MyEsp01.begin(9600);
}
void loop() {
if (Serial.available()) {
MyEsp01.println("this is the data");
Serial.println("data transfered");
}
Serial.println(Serial.available());
delay(2000);
}
I haven't do anything on the esp-01, I have no idea what to do there.
on the ESP01 GPIO3 (U0RXD) and GPIO1 (U0TXD) are used for Serial
for SoftwareSerial I tend to use GPIO2 and GOIO0, e.g.
// ESP-01 Software serial test Tx GPIO2 and Rx GPIO0
// using ESP8266 SoftwareSerial
// https://github.com/plerup/espsoftwareserial/
#include <SoftwareSerial.h>
// pins Rx GPIO0 and Tx GPIO 2
SoftwareSerial swSer(0, 2); // Tx GPIO2 and Rx GPIO0
void setup() {
delay(2000);
Serial.begin(115200); //Initialize hardware serial with baudrate of 115200
swSer.begin(9600); //Initialize software serial with baudrate of 115200
Serial.println("\nESP-01 Software serial test started\n");
Serial.println(" for loopback connect Tx GPIO2 to Rx GPIO0\n");
}
void loop() {
while (swSer.available() > 0) { //wait for data at software serial
Serial.write(swSer.read()); //Send data recived from software serial to hardware serial
}
while (Serial.available() > 0) { //wait for data at hardware serial
swSer.write(Serial.read()); //send data recived from hardware serial to software serial
}
}
Here is the schematic. I have another device that is used for uploading codes to the esp-01, I forgot to mention that I burned "Serial.begin(9600)" onto it, I apologize for any confusion.
All the tutorials I watched are on UNO, I didn't think it'd make a difference. So what should I do? Sorry if this is a dumb question I'm just really not familiar with arduino.
OK well what i recommend you to do is put a voltage divider between the ESP rx line to prevent damage.
Actually since you have an upload tool, the best would be to just get all the connections right.
So.
Upload the serialPassThrough example onto the Leonardo.
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
if (Serial.available()) { // If anything comes in Serial (USB),
Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1)
}
if (Serial1.available()) { // If anything comes in Serial1 (pins 0 & 1)
Serial.write(Serial1.read()); // read it and send it out Serial (USB)
}
}
And upload to the ESP-01 something that echo back whatever it receives, and also sends out a message every second.
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) { // If anything comes in Serial
Serial.print("Echo from ESP-01 : ");
while (Serial.available()) {
Serial.write(Serial.read());
}
Serial.println();
}
delay(1000);
Serial.println("Hi from the ESP-01");
}
Now you will have to modify your connections a little since this is using hwSerial1 on pins 0 & 1 instead of swSerial on 2 and 3.
This is the voltage divider. It is really quite important. Omitting it may not break you ESP straight away, but the ESP-01 GPIO pins are not 5v tolerant.
Anyway, try this, open the Serial monitor and set the baud-rate to 9600,
note: Again i am making this all up as i go along, i really should devote some time to making a tutorial instead of doing it like this.