ESP8266 Arduino Programming Tutorial

ESP8266 Arduino Programming Tutorial

ESP8266 Arduino Programming Tutorial - Easy to learn, step by step.

  • 8,430 views
  • 0 comments
  • 10 respects

Components and supplies

Adafruit industries ada260 image 90px
USB-A to Mini-USB Cable
×1
Ard nano
Arduino Nano R3
×1
Xbee USB adapter (FTDI ready)
×1
IO Expansion Shield for Arduino (V6)
×1

Apps and online services

ESP_Flasher
Ide web
Arduino IDE
CoolTerm

About this project

What is ESP8266 arduino?

The Arduino ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and MCU (Micro Controller Unit) capability produced by Shanghai-based Chinese manufacturer, Espressif Systems.

The chip first came to the attention of western makers in August 2014 with the ESP-01 module, made by a third-party manufacturer, AI-Thinker. This small module allows microcontrollers to connect to a Wi-Fi network and make simple TCP/IP connections using Hayes-style commands. However, at the time there was almost no English-language documentation on the chip and the commands it accepted. The very low price and the fact that there were very few external components on the module which suggests that it could eventually be very inexpensive in volume, attracted many hackers to explore the module, chip, and the software on it, as well as to translate the Chinese documentation.

ESP8266 Arduino Tutorial

These stuffs are needed:

Software

ESP_Flasher

Arduino IDE 1.0.6

CoolTerm

NetAssist

Hardware

DFRduino UNO R3

Xbee USB adapter (FTDI ready)

IO Expansion Shield for Arduino (V6)

USB Cable A-B for Arduino

Mini USB cable

How to Use ESP8266 arduino?

1. Connect AP

2. Download the ESP8266 library, and unzip it to "C:\Users\***\Documents\Arduino\libraries"

3. Insert the expansion shield on UNO, and plug ESP8266 in the socket on the expansion shield.

4. Note the switch: “RUN/Prog” at “Prog” side; “BOOT/UART” at "BOOT" side;

5. Wire adapter to the expansion shield: TX- PIN10, RX- PIN11, GND - GND;

How to Use ESP8266 Arduino?

1. Connect AP

2. Download the ESP8266 library, and unzip it to "C:\Users\***\Documents\Arduino\libraries"

3. Insert the expansion shield on UNO, and plug ESP8266 in the socket on the expansion shield.

4. Note the switch: “RUN/Prog” at “Prog” side; “BOOT/UART” at "BOOT" side;

5. Wire adapter to the expansion shield: TX- PIN10, RX- PIN11, GND - GND;

6. Open the sketch "Connect AP", and modify the wifi AP ssid and password of yours;

7. Upload the sketch;

8. Turn the switch of the expansion shield “RUN/Prog” to “Run” side;

9. Use software "CoolTerm" to monitor if the AP connection is done.(Configure as the follow picture)

2 TCP_Client_Single

1. Pull the switch"RUN/Prog" to "Prog";

2. Check the Ip address of your computer, we will use it as the Server Ip address;

3. Open sample sketch "TCP_Client_Single", write your wifi's ssid, password , Server Ip address, port;

4. Open the software"TCP/IP Net Assistant V3.8", configure as follow, and click Connect to monitor the PC doing as Server;

5. Upload the modified sketch, and then pull the switch"RUN/Prog" to "RUN";

6. Open "CoolTerm" and monitor the if the AP connection was good;

7. Send a message from "TCP/IP Net Assistant V3.8", you could see "New message" appear on "CoolTerm" .

Note: ESP8266 can connect to 5 servers simultaneously.

4 TCP_Pure_Data_Mode

1. Pull the switch"RUN/Prog" to "Prog";

2. Open sample sketch "TCP_Pure_Data_Mode", write your wifi's ssid, password , Server Ip address, port;

3. Open the software"TCP/IP Net Assistant V3.8", configure as follow, and click Connect to monitor the PC doing as Server;

4. Upload the modified sketch, and then pull the switch"RUN/Prog" to "RUN";

5. Open "CoolTerm" and monitor the if the AP connection was good;

6. Send a message from "TCP/IP Net Assistant V3.8", you could see New message appear on "CoolTerm".

Note: When the symbol">" appeared, it means ESP8266 entered into Transparent Mode which transfer data faster than normal mode.

5 Server mode

Note:Since now, the STA mode which support the Server mode is not stable, we are working on that you could refer to the ESP8266 manual book to try.

* Problem Shooting

If the monitor print"Connect failed!" Please try/check these steps:

Unplug the USB cable from Arduino to power off, and plug it again to restart module.

The wifi you are using is good, and the code ssid and password is correct.

Whether the button on expansion shield was push to the left side"RUN" but not"PROG".

Whether the button on the ESP8266 is also on the left side"BOOT".

It's might for the wrong wire.

The wifiBee socket on expansion shield has a loose connection to the wifi Bee.

If everything is ok, but still failed connection. You may have a try "Update Firmware"

Update Firmware

Please download the Firmware and Tools first. then Like in AT mode, but pull the swith of ESP8266 to "UART" side.Don't forget to pull it back to "BOOT" after updating firmware.

1. Open software "ESP_Flasher";

2. Choose Firmware;

3. Choose Serial Port of ESP8266, click Flash to burn firmware;

4. Wait for done.

Application

This is a little application which is built on the 4.1.2 2 TCP_Client_Single trail, if you have finished that part, you could upload the sketch below, and send commands "H","L" to open up or turn off it on your computer. And since that almost every Arduino card has a LED built on board, connected with D13, so in the sketch, I will use it as the target LED.

// this example use esp8266 to connect to an Access Point and connect to SINGLE TCP Server which is at the same subnet 
2 // such as the esp8266 is is 192.168.1.3, and the server ip is 192.168.1.1 ,then esp8266 can connect to the server 
3  
4 //Then connect a LED on Digital pin13, and open the software on PC_TCP server, send command to control the LED state: 
5 //send "H" to turn ON LED; send "L" to turn OFF LED 
6  
7 #include "esp8266.h" 
8 #include "SoftwareSerial.h" 
9  
10 #define ssid		"test"		// you need to change it  
11 #define password	"12345678" 
12  
13 #define serverIP	"192.168.1.1" 
14 #define	serverPort	"8081" 
15 int ledPin = 13; 
16 String incomingData = ""; 
17  
18 Esp8266 wifi; 
19 SoftwareSerial mySerial(10, 11); // RX, TX 
20  
21 void setup() { 
22   pinMode(ledPin, OUTPUT); 
23   delay(2000);				// it will be better to delay 2s to wait esp8266 module OK 
24   Serial.begin(115200); 
25   mySerial.begin(115200); 
26   wifi.begin(&Serial, &mySerial);		//Serial is used to communicate with esp8266 module, mySerial is used to debug 
27   if (wifi.connectAP(ssid, password)) { 
28     wifi.debugPrintln("connect ap sucessful !"); 
29   } else { 
30     while (true); 
31   } 
32   wifi.setSingleConnect(); 
33   if (wifi.connectTCPServer(serverIP, serverPort)) { 
34     wifi.debugPrintln("connect to TCP server successful !"); 
35   } 
36   String ip_addr; 
37   ip_addr = wifi.getIP(); 
38   wifi.debugPrintln("esp8266 ip:" + ip_addr); 
39 } 
40  
41 void loop() { 
42   int state = wifi.getState(); 
43   switch (state) { 
44     case WIFI_NEW_MESSAGE: 
45       wifi.debugPrintln("new message!"); 
46       incomingData = wifi.getMessage(); 
47       wifi.sendMessage(incomingData);		//send the message to TCP server what it has received 
48       wifi.setState(WIFI_IDLE); 
49       break; 
50     case WIFI_CLOSED :							//reconnet to the TCP server 
51       wifi.debugPrintln("server is closed! and trying to reconnect it!"); 
52       if (wifi.connectTCPServer(serverIP, serverPort)) { 
53         wifi.debugPrintln("reconnect OK!"); 
54         wifi.setState(WIFI_IDLE); 
55       } 
56       else { 
57         wifi.debugPrintln("reconnect fail"); 
58         wifi.setState(WIFI_CLOSED); 
59       } 
60       break; 
61     case WIFI_IDLE : 
62       int sta = wifi.checkMessage(); 
63       wifi.setState(sta); 
64       break; 
65   } 
66   if (incomingData == "H")  { 
67     digitalWrite(13, HIGH); 
68     incomingData = ""; 
69   } 
70   else if (incomingData == "L")  { 
71     digitalWrite(13, LOW); 
72     incomingData = ""; 
73   } 
74 } 

AT command

How to enter AT mode

You can setup and control the module completely with AT command through Serial.

1. Insert ESP8266 onto the USB-Serial adapter

2. Open the Monitor in Arduino IDE. Choosing "Both NL & CR" "115200"

3. Send "AT" to enter into the AT mode once received OK.

Read the original ESP8266 Arduino Programming Tutorial

Comments

Similar projects you might like

ESP8266 - Beginner Tutorial + Project

by Inventor Niv

  • 21,426 views
  • 7 comments
  • 50 respects

Arduino Bluetooth Basic Tutorial

by Mayoogh Girish

  • 616,779 views
  • 59 comments
  • 361 respects

ESP8266 ESP-01 Webserver

by Robin Kanattu Thomas

  • 119,238 views
  • 22 comments
  • 160 respects

How to Program ESP8266 with Arduino UNO

by Harsh Mangukiya

  • 55,095 views
  • 14 comments
  • 22 respects

PIR Sensor Tutorial - With Or Out Arduino

by Tarantula3

  • 8,427 views
  • 3 comments
  • 31 respects
Add projectSign up / Login