WiFi ESP8266

WiFi module ESP8266

Introduction

This module uses ESP8266EX's official model ESP-WROOM-02D, 4MB QSPI Flash. The module is certified by FCC in the United States, CE-RED in Europe, TELEC in Japan, and KC in South Korea.
The module is fully opened, you can program it separately. This is not a simple transparent transmission module.

Module Functions

The module includes an automatic download circuit and a communication module. The automatic download circuit refers to the official recommendation to use 2 S8050 transistors to receive the RTS and DTR signals from the CH340C downloader and trigger the download sequence.

Assembly

Connect to the NyBoard:
Update sketches through USB downloader:

Development Environment Settings

We use the Arduino as the development environment.

2.1 Add ESP8266 source to the board manager

URL:http://arduino.esp8266.com/stable/package_esp8266com_index.json. Paste it into the URL of the additional development board in the Arduino preferences.
Then open the "Board Manager" and enter ESP8266 or 8266 to search for the board support package:
Download the package ESP8266 by ESP8266 Community.

Configuration of the Module

After the board support package downloaded, we select ESP8266 Board (current version: 2.74) -> Generic ESP8266 Module.
Then we set the parameters:
Parameters
Settings
Builtin Led
2
Upload Speed
921600(Auto-negotiation during downloading, 115200 is too slow)
CPU Frequency
160MHz
Flash Size
4MB
Reset Method
DTR reset
lwIP variant
V2 Lower memory
Erase Flash
Only sketch

Download Test

After configuration, we use the Arduino classic "Blink" program to test the ESP8266 development board.
Open the Blink project, configure the development board, plug the module into the communication module debugging interface of the USB downloader, and download the Blink example.
Compared with the Arduino UNO, the compilation time is slightly longer, after Linking, the download progress will be displayed as a percentage:
The "Blink sketch" uses 257KB of flash and 26.8KB of the SRAM.

Download WiFi Firmware

There're 3 files in the project:
  • ESP8266WiFiController.ino: Arduino sketch with server core code.
  • mainpage.h:welcome page (html) in a char array.
  • actionpage.h:action controller page (html) in a char array.
Please put them in the folder named "ESP8266WiFiController", then open the ino file and download it to the ESP8266 WiFi module.

How to Use

After the sketch downloader to the WiFi module, we strongly recommend run it with your USB downloader so you can get the serial output in the Arduino serial monitor.
Open your smartphone WiFi scanner and find an access point named "Bittle-AP" that is not encrypted. Connect it.
If your smartphone automatically optimizes your network connection, it will cut the connection of "Bittle-AP" because there is no Internet connection. Your smartphone may connect to the WiFi with an Internet connection or even use your cellular data.
Your smartphone may auto jump to the "WiFiManager" page when connecting to "Bittle-AP".
If not, please open your browser and enter 192.168.4.1 to enter the WiFi connection configuration page manually.
On the WiFiManager page, Bittle's wireless module will automatically search for all nearby WiFi SSIDs and display them. After you click on your own WiFi SSID and enter the password, Bittle will connect to this network first.
Bittle will print out the IP address assigned by DHCP through the serial port after connecting to the WiFi successfully. You can also configure a fixed IP address in Arduino.
Enter the IP address of the WiFi module, now you can control the Bittle through WiFi!

About the Sample Code

The sample code is a simple web server example, including 2 HTML pages. The two pages are stored in two header files in the form of string constants. The advantage is to avoid calling the client.print function constantly.

3.1 Set Up the WiFi Networks

Before we start our web server, we should configure the WiFi to connect to your local area network(LAN). We used to enter the WiFi SSID and password in the program, but it is very inconvenient while we need to change the network environment.
We use the WiFi manager library to configure the WiFi information through web.
1
// WiFiManager
2
WiFiManager wifiManager;
3
4
// Start WiFi manager, default gateway IP is 192.168.4.1
5
wifiManager.autoConnect("Bittle-AP");
Copied!

3.2 Web server

Create a new web server and configure port 80 (commonly used HTTP server port)
1
ESP8266WebServer server(80);
Copied!

3.3 Configure 3 HTTP service handler

The HTTP response function is to handle the incoming HTTP requests.
1
void handleMainPage() {
2
//Serial.println("GET /");
3
server.send(200, "text/html", mainpage);
4
}
5
void handleActionPage() {
6
//Serial.println("GET /actionpage");
7
server.send(200, "text/html", actionpage);
8
}
Copied!
The handleMainPage and handleActionPage response 200 (OK) and corresponding web HTML code for your web browser (client).
1
void handleAction(){
2
String argname = server.arg("name");
3
4
if(argname == "gyro"){ // gyro switch
5
Serial.print("g");
6
}
7
Copied!
The HandleAction function is slightly different. This is an HTTP request processing function with parameter passing. When the parameter is "gyro", the serial port of the WiFi module sends out the command ("g", switch IMU), so that our Bittle will execute the command.
So how is this "gyro" parameter generated and passed? Because we sent such an HTTP request with a value to the server:
1
http://IP address or DomainName/action?name=gyro
Copied!
The server parses the action parameter by the function and resolves that the name is "gyro".
We can directly enter this URL in the browser and execute it with the keyboard. The more common method is to add a link to the "Walk" button on the ActionPage web page. When the gyro button is pressed, the above URL will be sent to the host.
The complete walk button configuration is as follows:
1
<button style="width: 25%" onclick="location.href='/action?name=gyro'">GyroOn/Off</button>
Copied!
After parsing the "name" parameter, we send the actionpage again.
1
server.send(200, "text/html", actionpage);
Copied!
We bond the handler method with the corresponding URLs.
1
server.on("/", handleMainPage);
2
server.on("/actionpage", handleActionPage);
3
server.on("/action", handleAction);
Copied!

3.4 Start the Web Server

1
server.begin();
2
Serial.println("HTTP server started");
Copied!

3.5 Handle Client Requests

1
void loop(void){
2
server.handleClient();
3
}
Copied!

More Ways Playing the WiFi Module

Compared to the ATMega328P on the NyBoard, there’re more hardware and software resources on the ESP8266, you can do more experiments with it.
Connect your Bittle to IoT platforms with HTTP restful APIs.
MQTT and node-red.
OTA with WiFi.
Make ESP8266 a strong co-processor for NyBoard for motion data fusion.
Last modified 7mo ago