Connection of ESP01 to the Arduino:
| ESP01 | Arduino |
| TX | Pin 3 |
| CH_PD | 3.3V |
| RST | No Connection |
| VCC | 3.3V |
| RX | Pin 4* |
| GPIO0 | No Connection |
| GPIO2 | No Connection |
| GND | GND |
* A resistor network to be used to drop the 5V signal from the Arduino to 3.3V for the ESP01.
A typical Arduino sketch to communicate with the ESP01:
#include <SoftwareSerial.h>
#define DEBUG true
// UNO's D3 connected to ESP's TX
// UNO's D4 connected to ESP's RX via resistor network
SoftwareSerial ESP01(3, 4); // RX, TX
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Starting...");
ESP01.begin(9600);
inputString.reserve(200); // reserve 200 bytes for the inputString:
}
void loop() {
if (stringComplete) {
sendData(inputString,2000,DEBUG); // print the string when a newline arrives:
inputString = ""; // clear the string:
stringComplete = false;
}
delay(100);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read(); // get the new byte:
inputString += inChar; // add it to the inputString:
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
ESP01.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(ESP01.available())
{
// "Construct" response from ESP01 as follows - this is to be displayed on Serial Monitor.
char c = ESP01.read(); // read the next character.
response+=c;
}
}
if(debug) Serial.print(response);
return (response);
}


No comments:
Post a Comment