A while back I posted some information about the ESP8266 Wi-Fi Module. You can view that post here. I mentioned that for testing it, an Arduino with multiple hardware serial ports would be ideal. Well, I just got an Arduino Leonardo which matches that criteria.
Here is how I hooked it up:
you can get the Fritzing file here. (The Fritzing part for the ESP8266 can be found here.) Notice that the TX and RX serial connections ‘crossover’. Not crossing over is a common mistake when first hooking up a serial communications device.
The Arduino sketch I used was chosen via
Examples->04.Communication->MultiSerialMega
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
void setup() { Serial.begin(9600); Serial1.begin(115200); } void loop() { if (Serial1.available()) { int inByte = Serial1.read(); Serial.write(inByte); } if (Serial.available()) { int inByte = Serial.read(); Serial1.write(inByte); } } |
You only need to change the serial BAUD rate for Serial1 (the ESP8266) from the default of 9600 to 115200. Once you upload the sketch to the Arduino, run the serial monitor and the commands you type will be sent to the ESP8266. Any response from the ESP8266 will be ‘echoed’ in the serial monitor.
Testing the ESP8266 on an Arduino with two hardware serial ports is much easier than my original way. Now I can add sensors to this circuit and send the information they gather through the ESP8266 to the WEB.
UPDATE: 12/29/2014
I ran into a common problem with the ESP8266. Sometimes the ESP8266 will return ‘busy’ when you enter a command. The ESP8266 is essentially locked up (i.e. frozen) at this point. To recover you can power off the device and power back on. However that is not a programatic solution. Fortunately there is a way to ‘RESET’ the ESP8266 from code if this situation occurs.
Here is how to wire the ESP8266:
You can download the Fritzing diagram source here.
Here is a sketch showing the Reset function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
/* Arduino Leonardo <--> ESP8266 Eamil: earl@microcontrollerelectonics.com */ #define SSID "your_SSID" #define PASS "your_password" #define DST_IP "192.168.0.100" #define CONNECT_ATTEMPTS 5 #define RST 8 #define CHP 9 #define LED 13 void setup() { pinMode(RST, OUTPUT); pinMode(LED, OUTPUT); pinMode(CHP, OUTPUT); reset(); Serial.begin(9600); Serial1.begin(115200); delay(5000); Serial1.println("AT+RST"); delay(1000); if (Serial1.find("ready")) Serial.println("Module is ready"); else { Serial.println("ESP8266 Module did not respond."); Serial.println("Enter Commands Manually."); while (1) chk_serial_io(); } delay(1000); boolean connected = false; for (int i = 0; i < CONNECT_ATTEMPTS; i++) { if (connectWiFi()) { connected = true; break; } } if (!connected) { Serial.println("Enter Commands Manually."); while (1) chk_serial_io(); } delay(5000); Serial1.println("AT+CIFSR"); Serial.println("ip address:"); while (Serial1.available()) Serial.write(Serial1.read()); Serial1.println("AT+CIPMUX=0"); } void reset() { digitalWrite(LED,HIGH); digitalWrite(CHP,HIGH); digitalWrite(RST,LOW); delay(100); digitalWrite(RST,HIGH); delay(1000); digitalWrite(LED,LOW); } void loop() { String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += DST_IP; cmd += "\",80"; Serial1.println(cmd); Serial.println(cmd); if (Serial1.find("Error")) return; cmd = "GET / HTTP/1.0\r\n\r\n"; Serial1.print("AT+CIPSEND="); Serial1.println(cmd.length()); if (Serial1.find(">")) { Serial.print(">"); } else { Serial1.println("AT+CIPCLOSE"); Serial.println("connect timeout"); delay(1000); return; } Serial1.print(cmd); delay(2000); while (Serial1.available()) { char c = Serial1.read(); Serial.write(c); if (c == '\r') Serial.print('\n'); } Serial.println("===="); delay(1000); } void chk_serial_io() { if (Serial1.available()) { // if (Serial1.find("busy")) { // Serial.println("Resetting..."); // reset(); // } int inByte = Serial1.read(); Serial.write(inByte); } if (Serial.available()) { int inByte = Serial.read(); Serial1.write(inByte); } } boolean connectWiFi() { Serial1.println("AT+CWMODE=1"); String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\""; Serial.println(cmd); Serial1.println(cmd); delay(2000); if (Serial1.find("OK")) { Serial.println("OK, WiFi Connected."); return true; } else { Serial.println("Can not connect to WiFi."); return false; } } |
Recent Comments