# WiFi module ESP8266

## Introduction

This module uses the ESP8266EX's official model ESP-WROOM-02D, which has 4MB of QSPI Flash. It is certified by the 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 upload circuit and a communication module. The automatic upload circuit recommends using 2 S8050 transistors to receive the RTS and DTR signals from the [USB uploader](https://docs.petoi.com/communication-modules/usb-downloader-ch340c) (CH340C or CH343G) and trigger the upload sequence.

## Hardware setup

### 1. Connection

Plug the WiFi module into the communication module debugging socket of the USB uploader, then connect the USB uploader to the computer via the USB data cable as follows, and be ready to upload the sketch for the WiFi module：

![](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MX5in6n7A0W1Y5xGIQc%2F-MX5irvjW_RDxOjLCyP5%2F1.jpeg?generation=1617168141596850\&alt=media)

## Development Environment Settings

We use the Arduino IDE as the development environment.

{% hint style="info" %}
You'll need to download and install the [Arduino IDE](https://www.arduino.cc/en/software) first.&#x20;
{% endhint %}

### 2.1 Add ESP8266 source to the board manager

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F0DzzxZcEsWxc6sosCuUH%2Fimage.png?alt=media&#x26;token=88086062-23b4-42bb-8b1c-848c0d279141" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FdVzLaG8zg8t12XThvJYc%2Fimage.png?alt=media&#x26;token=786f3b22-c0b7-4dd7-ae9f-55f339960719" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FVtZIQ8blKvfV2pia4Ay0%2Fimage.png?alt=media&#x26;token=0f7784a1-6a0a-4615-a7bd-d2b36b15431c" alt=""><figcaption></figcaption></figure>

URL：<http://arduino.esp8266.com/stable/package_esp8266com_index.json>. Paste it into the Additional Boards Manager URLs in the Arduino IDE preferences.

Then open the **Board Manager** and input *ESP8266* or *8266* to search for the board support package:

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F4G8fefW8n4Rca7FIIdTU%2Fimage.png?alt=media&#x26;token=b1eafc71-0d10-4089-a815-77bc5f0cbf86" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FE921xhZzEFiy036Qq9i1%2Fimage.png?alt=media&#x26;token=0e828764-46e0-4543-83a8-77499e304da7" alt=""><figcaption></figcaption></figure>

Download the latest version of ESP8266 from ESP8266 Community.

### 2.2 Configuration of the Module

After downloading the board support package, we select Board -> ESP8266 -> Generic ESP8266 Module.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FnSHrx1a2F9YBdLZ69aXQ%2Fimage.png?alt=media&#x26;token=fbd9ed1d-cc49-49e5-b224-7da35d546e61" alt=""><figcaption></figcaption></figure>

Then we set the parameters：

| Parameters    | Settings                                                      |
| ------------- | ------------------------------------------------------------- |
| Builtin Led   | 2                                                             |
| Upload Speed  | 921600（Auto-negotiation during uploading, 115200 is too slow） |
| CPU Frequency | 160MHz                                                        |
| Flash Size    | 4MB                                                           |
| Reset Method  | DTR reset                                                     |
| lwIP variant  | V2 Lower memory                                               |
| Erase Flash   | Only sketch                                                   |

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FfkznHPbaJor6a9IUQ3WD%2Fimage.png?alt=media&#x26;token=fdc55c1e-24d0-4724-a364-86f2217ec804" alt=""><figcaption></figcaption></figure>

### 2.3 Upload the test program

After configuration, we used the Arduino classic "Blink" program to test the ESP8266 development board.&#x20;

Open the Blink project, configure the development board, plug the WiFi module into the communication module debugging socket of the USB uploader (USB Adapter), then connect the USB uploader to the computer via the USB data cable, and upload the **Blink** sketch to the WiFi module.&#x20;

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FeQHTEHzAH4RUA6ascnU1%2Fimage.png?alt=media&#x26;token=c780d3e3-5365-428b-b462-6c69eacf8321" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FPBLbluqwHtI7jEoijTHm%2Fimage.png?alt=media&#x26;token=2473b169-8a66-41c9-86f8-687c23adea1e" alt=""><figcaption></figcaption></figure>

Compared with the Arduino UNO, the compilation time is slightly longer. After Linking, the uploading progress will be displayed as follows:

````
```
. Variables and constants in RAM (global, static), used 28104 / 80192 bytes (35%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ DATA     1496     initialized variables
╠══ RODATA   920      constants       
╚══ BSS      25688    zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 59667 / 65536 bytes (91%)
║   SEGMENT  BYTES    DESCRIPTION
╠══ ICACHE   32768    reserved space for flash instruction cache
╚══ IRAM     26899    code in IRAM    
. Code in flash (default, ICACHE_FLASH_ATTR), used 232148 / 1048576 bytes (22%)
║   SEGMENT  BYTES    DESCRIPTION
╚══ IROM     232148   code in flash   

"C:\Users\wjf-1\AppData\Local\Arduino15\packages\esp8266\tools\python3\3.7.2-post1/python3" -I "C:\Users\wjf-1\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2/tools/upload.py" --chip esp8266 --port "COM51" --baud "921600" ""  --before default_reset --after hard_reset write_flash 0x0 "C:\Users\wjf-1\AppData\Local\arduino\sketches\E155A600E863F22636B5717F21F23873/Blink.ino.bin"
esptool.py v3.0
Serial port COM51
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: e8:db:84:fc:63:f0
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 265616 bytes to 195731...
Writing at 0x00000000... (8 %)
Writing at 0x00004000... (16 %)
Writing at 0x00008000... (25 %)
Writing at 0x0000c000... (33 %)
Writing at 0x00010000... (41 %)
Writing at 0x00014000... (50 %)
Writing at 0x00018000... (58 %)
Writing at 0x0001c000... (66 %)
Writing at 0x00020000... (75 %)
Writing at 0x00024000... (83 %)
Writing at 0x00028000... (91 %)
Writing at 0x0002c000... (100 %)
Wrote 265616 bytes (195731 compressed) at 0x00000000 in 4.4 seconds (effective 482.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

```
````

## Upload the WiFi Firmware to the WiFi module

Project URL：<https://github.com/PetoiCamp/OpenCat/tree/main/ModuleTests/ESP8266WiFiController>

There are seven 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.
* actions.h
* actions.json
* calibrationpage.h
* commons.h

Please open the **ESP8266WiFiController.ino** in the Arduino IDE and install the library **WiFiManager** and **Dictionary** in the Library Manager:

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FU2Lfx4pFMSmyUqUyCpY3%2Fimage.png?alt=media&#x26;token=87ff4546-3352-4698-a475-ea0de8b304cd" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FIfRKuKvxGMRenuWqXMwc%2Fimage.png?alt=media&#x26;token=12694520-5de9-48b6-b8ab-4c982d9f492e" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FZaFFvCiqWRp6vlQs4hsx%2Fimage.png?alt=media&#x26;token=14439b33-40be-40fc-810e-df404871108e" alt=""><figcaption></figcaption></figure>

Then, upload the sketch to the ESP8266 WiFi module.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FhhbZLcZwap4BXBi0XNIO%2Fimage.png?alt=media&#x26;token=76122889-f0cf-460e-9ae6-98813e11f1f1" alt=""><figcaption></figcaption></figure>

## How to Use

After the sketch is uploaded to the WiFi module, we strongly recommend running it with your USB uploader (USB Adapter) so you can get the serial output in the serial monitor of Arduino IDE.

Open the serial monitor in Arduino IDE as follows. Then, unplug and re-plug the WiFi module into the USB uploader.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FJCRXhKcpYMwBCuLnTWGw%2Fimage.png?alt=media&#x26;token=7edc67d2-9b62-4a26-b45f-5eb8f6af4a20" alt=""><figcaption><p>Open the serial monitor</p></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2Ff18XQmcScgttZ4J3YjGD%2Fimage.png?alt=media&#x26;token=bb4bf964-7bfc-4fbd-a0b7-51ff4c962d6c" alt=""><figcaption></figcaption></figure>

Open your smartphone's WiFi scanner and find an unencrypted access point named "Bittle-AP." Connect it.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F93CNKJZe5W46YvXiUHH7%2FMobile.jpg?alt=media&#x26;token=b3bbab43-3c1a-46b8-a958-52bffd6982e8" alt=""><figcaption><p>Mobile Phone</p></figcaption></figure>

![Computer](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-MYsGrlObZCu2Rq_KPG-%2F-MYsH0LHNxDN2eFNxVzR%2Fimage.png?alt=media\&token=ea832895-e7f8-44ad-8c3e-ca33d70a5c8f)

{% hint style="info" %}
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. Please turn off your smartphone's network connection optimization function and force it to connect to the "**Bittle-AP**" hotspot.
{% endhint %}

Your smartphone may auto-jump to the "WiFiManager" page when connecting to "**Bittle-AP**."&#x20;

![](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-MYsGrlObZCu2Rq_KPG-%2F-MYsHKUHaeNZD3QmAJHW%2Fimage.png?alt=media\&token=3041aed3-975a-4edd-92b7-1964591eaeb1)

If not, please open your browser and enter **192.168.4.1** to enter the WiFi connection configuration page manually.

![](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-MYsGrlObZCu2Rq_KPG-%2F-MYsH6pfYGtJbdNy5suF%2Fimage.png?alt=media\&token=d4904c78-832c-49e3-8735-69e5e30349e7)

On the WiFiManager page, Please click the **Configure WiFi** button. The WiFi module will automatically search for and display all nearby WiFi SSIDs. After you click on your WiFi SSID and enter the password, the WiFi module will connect to this network first.

![](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQ6a951Q6Jn1Zzt5Ajr%2F-MYsGrlObZCu2Rq_KPG-%2F-MYsHBHbR-VlnnJKV4J_%2Fimage.png?alt=media\&token=fc802926-fcb3-4759-a739-67a9b76bdccd)

After successfully connecting to the network, the WiFi module will print out the IP address assigned by DHCP through the serial port on the serial monitor.&#x20;

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F9dQVjcvMriaodQ5cJhm8%2Fimage.png?alt=media&#x26;token=9a36f766-8068-4d48-b58c-d8b1941fd8d3" alt=""><figcaption></figcaption></figure>

Now, please unplug the WiFi module from the USB uploader and plug it into the NyBoard as follows：

![](https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Fdocs%2F-MX5in6n7A0W1Y5xGIQc%2F-MX5irviyPdr2PsEbkor%2F0.jpeg?generation=1617168141572643\&alt=media)

Power on the robot and enter the WiFi module's IP address in the web browser, and you can now control the robot through WiFi! &#x20;

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FetJYTz8N0Ga75NGJz5yx%2Fhome.jpeg?alt=media&#x26;token=66159e7a-eead-4e96-841c-9a171c8ded06" alt=""><figcaption><p>Home page</p></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FtlldudHNiq8u22r8lqhL%2FActions02.jpeg?alt=media&#x26;token=bac1aafd-6659-4412-98ca-647061c13a58" alt=""><figcaption><p>Actions page</p></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F8uMAHfJHIR382CdmqfCO%2FJointCalibration02.jpeg?alt=media&#x26;token=c89a7635-e3aa-4752-8b33-d7c25677a8a7" alt=""><figcaption><p>Calibration page</p></figcaption></figure>

## About the Sample Code&#x20;

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 when we need to change the network environment.

We use the WiFi manager library to configure the WiFi information online.&#x20;

```
  // WiFiManager
  WiFiManager wifiManager;

  // Start WiFi manager, default gateway IP is 192.168.4.1
  wifiManager.autoConnect("Bittle-AP");
```

### 3.2 Web server

Create a new web server and configure port 80 (commonly used HTTP server port)

```
ESP8266WebServer server(80);
```

### 3.3 Configure 3 HTTP service handler

The HTTP response function is to handle the incoming HTTP requests.

```
void handleMainPage() {
 //Serial.println("GET /");
 server.send(200, "text/html", mainpage);
}
void handleActionPage() {
 //Serial.println("GET /actionpage");
 server.send(200, "text/html", actionpage);
}
```

The handleMainPage and handleActionPage response 200 (OK) and corresponding web HTML code for your web browser (client).

```
void handleAction(){
  String argname = server.arg("name");

  if(argname == "gyro"){              // gyro switch
    Serial.print("g");
  }
…
```

The HandleAction function is slightly different. This is an HTTP request processing function with parameter passing. When the parameter is "gyro," the WiFi module's serial port 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:

```
http：//IP address or DomainName/action?name=gyro
```

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.&#x20;

The complete walk button configuration is as follows:

```
<button style="width: 25%" onclick="location.href='/action?name=gyro'">GyroOn/Off</button>
```

We will send the action page again after parsing the "name" parameter.

```
server.send(200, "text/html", actionpage);
```

We bond the handler method with the corresponding URLs.

```
server.on("/", handleMainPage);
server.on("/actionpage", handleActionPage);
server.on("/action", handleAction);
```

### 3.4 Start the Web Server

```
server.begin();
Serial.println("HTTP server started");
```

### 3.5 Handle Client Requests

```
void loop(void){
 server.handleClient();
}
```

## More Ways Playing the WiFi Module

Compared to the ATMega328P on the NyBoard, the ESP8266 has more hardware and software resources, so you can do more experiments with it.&#x20;

Connect your Bittle to IoT platforms with HTTP restful APIs.&#x20;

MQTT and node-red.&#x20;

OTA with WiFi.&#x20;

Make ESP8266 a strong co-processor for NyBoard for motion data fusion.
