I am struggling since quite some time now, using the ESP8266-01 Module for WiFi connection in my Arduino IDE Project, where I use the MEGA 2560.
Before I am really giving up on it, I thought maybe someone in this forum can guide me to the mistake I am apparently making.
I connected the ESP8266 module with my Arduino via the attached it the wiring layout (see attached image ESP8266_basic_bb.png), so essentially it is
ESP <-> MEGA
GND --- GND
TXD --- PIN 52
CH_PD --- V3.3
VCC --- V3.3
RXD -- PIN 51 via Voltage Reducer from 5v to 3.3v
'ESP8266_basic_bb.png'
My Arduino IDE code for trying the basics of the connection is this:
#include <SoftwareSerial.h>
const byte rxPin = 51; // Wire this to Tx Pin of ESP8266
const byte txPin = 52; // Wire this to Rx Pin of ESP8266
SoftwareSerial ESP8266 (rxPin, txPin); // RX, TX
void setup() {
Serial.begin(9600);
ESP8266.begin(9600);
Serial.println("Ready");
}
bool okReceived = false;
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
Serial.println("Command Sent: " + command);
ESP8266.println(command);
}
int responseCounter = 0;
if (ESP8266.available() > 0) {
while (ESP8266.available() > 0) {
if (responseCounter == 0) {
Serial.println("Response Received:");
}
String response = ESP8266.readStringUntil('\n');
Serial.println(response);
responseCounter++;
}
Serial.println("============");
}
}
My goal is (for now) to communicate via AT commands with the ESP8266 Module on the MEGA.
I've tested with the USB programming board, and that worked fine, just I cannot get it to work on the MEGA. I was also able to flash it with the most recent firmware through that USB programming Board. But although I tried many variants of PIN connections (e.g. Combinations of 18/19) I cannot get responses from the Module, when I issue AT commands via the Serial Monitor of the Arduino IDE.
Anyone can point me to the one (or many) thing(s), that I am doing wrong?
Something in the wiring must be wrong, but that is like a summary of what I found via google....
use hardware Serial1 on pins TX1,RX1. and the esp-01 will not burn if you don't use a voltage divider. for permanent connection you can later use a proper level shifter.
wire RX to TX
use the SerialPassthrough sketch from Communication examples at 115200 baud to communicate with AT firmware from Serial Monitor
That is MRC1708 (I draw wrong PIN connection and fixed that - it is supposed to be 5V IN from MEGA in the middle PIN and 3.3V OUT on the right PIN to ESP-01).
One very common mistake is that people do not use a dedicated 3.3V power supply for the ESP-01 module. Using the Mega's 3.3V can be unreliable and inconsistent.
No need to use SoftwareSerial since the Mega has 3 hardware serial ports in addition to the main one! The default baud rate of the ESP-01 AT firmware is 115200.
Juraj:
use hardware Serial1 on pins TX1,RX1. and the esp-01 will not burn if you don't use a voltage divider. for permanent connection you can later use a proper level shifter.
wire RX to TX
use the SerialPassthrough sketch from Communication examples at 115200 baud to communicate with AT firmware from Serial Monitor
Then I recommend my WiFiEspAT library
Thanks for the reply!
I removed the voltage devider and connected RX from ESP to TX1 on MEGA PIN 18 and TX from ESP to RX1 on PIN 19.
Using the basic example, I cannot get any response from the ESP.
The ESP itself is working as I checked that with the ESP USB Board.
I seem to be missing some very basic level stuff.
Can anyone point me to possible things I could be doing wrong?
One very common mistake is that people do not use a dedicated 3.3V power supply for the ESP-01 module. Using the Mega's 3.3V can be unreliable and inconsistent.
No need to use SoftwareSerial since the Mega has 3 hardware serial ports in addition to the main one! The default baud rate of the ESP-01 AT firmware is 115200.
.
Thanks ieee488.
Maybe the use of MEGA 3.3V as source is the issue. I will check with other supplies.
Here is a sketch for an Arduino Mega2560, with an ESP-01 connected to Serial 1.
I recently used this to add WiFi to a Mega. It uses the WiFiEsp library, so make sure your IDE has it available.
#include "WiFiEsp.h"
// ********************************************************* DEFINITIONS *********************************************************
char ssid[] = "********"; // your network SSID (name)
char pass[] = "********"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// ********************************************************* INITIALIZE *********************************************************
void setup() {
Serial.begin(115200); // initialize serial 0 for debugging
Serial1.begin(115200); // initialize serial 1 for ESP module
WiFi.init(&Serial1); // initialize ESP module
if (WiFi.status() == WL_NO_SHIELD) { // check for the presence of the shield
Serial.println("WiFi shield not present"); // If shield not present, don't continue
while (true);
}
while ( status != WL_CONNECTED) { // attempt to connect to WiFi network
Serial.print("Attempting to connect to WPA SSID: "); // Print message to serial monitor
Serial.println(ssid); // Print SSID to serial monitor
status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
}
Serial.println("You're connected to the network"); // Print success message to serial monitor
}
// ********************************************************* MAIN LOOP *********************************************************
void loop()
{
// check the network connection once every 10 seconds
Serial.println();
printCurrentNet();
printWifiData();
delay(10000);
}
// ********************************************************* FUNCTION DEFINITIONS *********************************************************
// Print WiFi data to serial Monitor --------------------------------------------------------
void printWifiData()
{
// *** print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// *** print your MAC address
byte mac[6];
WiFi.macAddress(mac);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
Serial.print("MAC address: ");
Serial.println(buf);
}
// Print WiFi connection details to serial Monitor --------------------------------------------------------
void printCurrentNet()
{
// *** print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// *** print the MAC address of the router you're attached to
byte bssid[6];
WiFi.BSSID(bssid);
char buf[20];
sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
Serial.print("BSSID: ");
Serial.println(buf);
// *** print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
}
ESP TX -----To Mega RX (Serial 1)
ESP CH_PD - To 3.3v power supply's +
ESP RST --- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP VCC --- To 3.3v power supply's +
ESP GND --- Connect it to both the Mega's GND, and also to the 3.3v power supply's GND
GPIO2 ----- Can leave open
GPIO0 ----- Can leave open (although I tie it to the 3.3v power supply's + with a 10kOhm resistor)
ESP RX -----To Mega TX (Serial 1)
I connect the RST and GPIO0 pins to the 3.3v power supply's + with the 10kOhm resistors, so those inputs aren't floating. Since one is the Reset, and the other is the Program pin.
For initial testing, I see you are using the Arduino Mega's 3.3v pin as the power supply. This really isn't a great idea, as the ESP8266 chip can pull a bit of power when transmitting. So while it may be ok during your learning phase of just trying to the two talking to each other, you really need a separate 3.3v power supply for the ESP chip.
And as more FYI, here is the wiring diagram I refer to when I'm integrating one into my projects. I setup a push button for the RST pin, and I also setup a slide switch for GPIO0 ... in case I want to program after it's already integrated.
jijaji:
That is MRC1708 (I draw wrong PIN connection and fixed that - it is supposed to be 5V IN from MEGA in the middle PIN and 3.3V
I can find on Duck2Go no such thing as an MRC1708. I strongly suspect it does not exist.
Juraj:
the esp-01 will not burn if you don't use a voltage divider.
That is s very contentious assertion.
The simplest level shifter is a diode with cathode to the 5 V device and anode to the ESP8266. You may or may not need a pull-up to 3.3 V on the ESP8266; Adafruit suggests you don't (but the diode should always be adjacent to the ESP8266).