Esp-8266 (esp-01) communication with PC

Hello,

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.

Thank you for reading this.

From where would the data into Software serial come? Whata data is sent and how?
Maybe Arduino/reference/serial gives some help.

First of all. A leonardo has native USB (uart0) and Serial pins exposed for Serial1. So there is no need to use swSerial.

What are you trying to do ?

If it is just simply communicating with the ESP-01 AT-commands. In that case

The esp8266 communicates at 115200 by default. No chance of really doing that on swSerial, but as i said, there is no need for that.

If you find a tutorial for doing this on an UNO, It will be more or less the same.

Always use a voltage divider on the esp rx-line to drop the logic level voltage from 5v down to the 3.3v of the esp-01.

Please show us the schematic of how you have connected it at the moment.

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
  }
}

I think the OP has posted the sketch for the leonardo (not the esp-01)

think you are correct there !
the use of SoftwareSerial when the leonardo has the Serial1 hardware port made me think it was the ESP-01

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.

Thank you for your answer anyways.

So I should just not include SoftwareSerial? I am currently connected to the TX and RX pin so does SoftwareSerial kinda blocks it out or something?

Thank you for answering. :smiley:

Anything else you uploaded to the ESP ?

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.

So connect

ESP-TX to Leonardo RX (pin 0)
and
Leonardo Tx (pin 1) -> 1K -> ESP Rx -> 1K -> 1K -> GND

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.