30A-ESP8266 Mesh User Guide EN V1.2 20160402
30A-ESP8266 Mesh User Guide EN V1.2 20160402
Version 1.2
Copyright 2016
Title
Subject
Chapter 1
Overview
Chapter 2
Mesh Header
Introduces the mesh header format and details about the fields
and codes.
Chapter 3
API Reference
Chapter 4
Sample Code
Release Notes
Date
Version
Release notes
2015.07
V1.0
First release.
2015.09
V1.1
Updated Chapter 3.
2016.01
V1.2
Note:
This current version is an early release to support initial product developers. The content is
subject to change without advance notice.
Table of Contents
1. Overview .......................................................................................................................... 1
1.2.1.
1.2.2.
1.2.3.
2.2.1.
Structure .................................................................................................................7
2.2.2.
Example ..................................................................................................................8
3.1.1.
3.1.2.
3.1.3.
3.1.4.
3.1.5.
3.1.6.
4.1.
4.2.
4.3.
4.4.
4.5.
Device ...............................................................................................................................12
Dev-App ............................................................................................................................15
1. Overview
1.
Overview
The development of the Internet of Things (IoT) requires an increasing number of nodes to
connect to the internet. However, only limited number (usually fewer than 32) of nodes can
directly connect to the same router. There are two solutions currently available for this
problem.
Super router: the higher capacity router allows more nodes to directly connect to it.
Mesh network: the nodes can establish a network and forward packets.
ESP8266 uses mesh network as shown in Figure 1-1. As a result, a large number of nodes
can connect to the internet without any improvements of the current router.
Up to 87 Wi-Fi devices.
"
1.1.
Concepts
1/16
2016.04
1. Overview
Local Device
As shown in Figure 1-2, if users configure a device to connect to the router via ESP-Touch
but not activate it on the server-side, then the device is a local device.
Device
Router
Mobile App
Local Network
"
Cloud Device
As shown in Figure 1-3, if users configure a device to connect to the router via ESP-Touch
and activate it on the server-side, then the device is a cloud device.
Office
Room
Device
Mobile App
Router A
Router B
Phone
Server
Cloud Network
"
App.
Espressif
2/16
2016.04
1. Overview
Online status: The device is a local device or cloud device; the device and IOT App
Cloud status
Online status
Oine status
Cloud device
Local device
1.2.
Network Structure
1.2.1.
Networking Principle
Mesh network supports auto-networking. When users set up a mesh network via ESPTouch, the device automatically scans the Wi-Fi APs nearby.
1.2.2.
Networking Diagram
Figure 1-4 shows the mesh network diagram.
Internet
Online-Mesh
Local-Mesh
Router
Root node
Dev1-1
"
Dev2-1
Dev 2-2
Dev 2-n
Dev-3-1
Dev-3-2
Dev-3-n
Dev-m-1
Dev-m-2
Dev-m-n
Non-root nodes
3/16
2016.04
1. Overview
The node that directly connects to the router is the root node and others are non-root
1.2.3.
Network Node
According to the location in a mesh network, a node can be:
A Root Node
It receives and sends packets.
It forwards the packets from server, mobile apps and its child nodes.
Or,
A Non-root Node
Espressif
Non-leaf node: It receives and sends packets, as well as forwards the packets from its
parent node and child nodes.
Leaf node: It only receives and sends packets, but does not forward packets.
4/16
2016.04
2. Mesh Header
2.
2.1.
Mesh Header
Mesh Header Format
Figure 2-1 shows the mesh header format.
0
ver
1
o
flags
proto
len
dst_addr
src_addr
ot_len
option_list
Packet Body
"
Figure 2-1. Mesh Header Format
Length
Description
ver
2 bits
Mesh version.
1 bit
Option flag.
bit
5 bits
flags
Espressif
CP
CR
resv
FP
FR
resv
Reserved.
5/16
2016.04
2. Mesh Header
Field Name
Length
Description
bit
8 bits
P2P
protocol
proto
0: downwards
1: upwards
P2P
protocol
enum mesh_usr_proto_type {
M_PROTO_NONE = 0,
M_PROTO_HTTP,
M_PROTO_JSON,
M_PROTO_MQTT,
M_PROTO_BIN,
//
//
//
//
//
used
user
user
user
user
};
len
2 Bytes
dst_addr
6 Bytes
Source address
src_addr
6 Bytes
ot_len
option-1
option-2
option-n
option_list
otype
otype
Espressif
1 Byte
olen
ovalue
Option type.
6/16
2016.04
2. Mesh Header
Field Name
olen
ovlaue
2.2.
Mesh Option
2.2.1.
Structure
Length
Description
1 Byte
User defined
M_O_ROUTER_SPREAD,
M_O_ROUTE_ADD,
M_O_ROUTE_DEL,
M_O_TOPO_REQ,
M_O_TOPO_RESP,
M_O_MCAST_GRP,
M_O_MESH_FRAG,
M_O_USR_FRAG,
M_O_USR_OPTION,
//user option
};
Table 2-2. Mesh Header Type
Field Name
Description
M_O_FLOW_REQ
2 Bytes
M_O_FLOW_RESP
6 Bytes
106 Bytes
M_O_ROUTE_ADD
6*n+2
Bytes
M_O_ROUTE_DEL
6*n+2
Bytes
M_O_ROUTER_SPRE
AD
Espressif
Length
7/16
Format
otype
olen
0x00
0x02
ovalue
otype
olen
ovalue
0x01
0x06
congest capacity
otype
olen
ovalue
0x02
0x6A
Router information
otype
olen
ovalue
0x03
length
otype
olen
ovalue
0x04
length
2016.04
2. Mesh Header
Field Name
M_O_TOPO_REQ
M_O_TOPO_RESP
2.2.2.
Length
Description
Format
8 Bytes
otype
olen
ovalue
0x05
0x06
6*n+2
Bytes
otype
olen
0x06
length
ovalue
MAC address list
Example
"
Table 2-3. Flow Request Packet
Field Name
Value
Description
head.ver
00
head.O
head.flags.FP
head.flags.FR
000
Reserved.
head.proto.D
Upwards.
head.proto.P2P
head.proto.protocol
000000
head.len
0x0014
head.dst_addr
18 FE 34 A5 3B AD
head.src_addr
18 FE 34 A2 C7 76
0x0004
head.option_list[0].otype
0x00
M_FLOW_REQ.
head.option_list[0].olen
0x02
head.flags.resv
head.ot_len
"
Espressif
8/16
2016.04
2. Mesh Header
Description
head.ver
00
head.O
head.flags.FP
head.flags.FR
000
Reserved.
head.proto.D
Downwards.
head.proto.P2P
head.proto.protocol
000000
head.len
0x0015
head.dst_addr
18 FE 34 A2 C7 76
head.src_addr
18 FE 34 A5 3B AD
0x0008
head.option_list[0].otype
0x01
M_FLOW_RESP.
head.option_list[0].olen
0x06
head.option_list[0].ovalue
0x01
head.flags.resv
head.ot_len
Espressif
Value
9/16
2016.04
3. API Reference
3.
API Reference
3.1.
Data Structure
3.1.1.
// version of mesh
uint8_t oe: 1;
// option flag
uint8_t fp: 1;
uint8_t fr: 1;
uint8_t rsv:3;
// reserved
struct {
uint8_t d:
1;
uint8_t p2p:1;
uint8_t protocol:6;
} proto;
uint16_t len;
uint8_t dst_addr[ESP_MESH_ADDR_LEN];
// destination address
uint8_t src_addr[ESP_MESH_ADDR_LEN];
// source address
// mesh option
} __packed;
3.1.2.
3.1.3.
Espressif
uint8_t otype;
// option type
uint8_t olen;
2016.04
3. API Reference
uint8_t ovalue[0];
// option value
} __packed;
3.1.4.
// identity of fragmentation
struct {
uint16_t resv:1;
// reserved
uint16_t mf:1;
// more fragmentation
uint16_t idx:14;
// fragmentation offset
} offset;
} __packed;
3.1.5.
3.1.6.
3.1.7.
3.2.
Packet APIs
Note:
For the packet APIs, please refer to 2C-ESP8266__SDK__Programming Guide.
Espressif
11/16
2016.04
4. Sample Code
4.
4.1.
Sample Code
Device
For details, please refer to ESP8266_MESH_DEMO/blob/master/mesh_demo/demo/
mesh_demo.c.
4.2.
Mobile or Server
void controller_entrance(Parameter list)
{
/*Add your codes to check status*/
/*Add your codes to build control packet*/
uint8_t json_control_data[] = {/*Add your codes*/};
uint16_t control_data_len = sizeof(json_control_data)
struct mesh_header_format *mesh_header = NULL;
/* src_addr should be the combination of IP and port of
Mobile or Server. You can set the address to zero, then the
root device will fill in the section. If you fill in the
section by yourself, please make sure the value is right.*/
uint8_t src_addr[] = {0,0,0,0,0,0},
dst_addr[] = {xx,xx,xx,xx,xx,xx};
mesh_header = (struct mesh_header_format
*)espconn_mesh_create_packet(dst_addr, src_addr, false, true,
M_PROTO_JSON, control_data_len,
false, 0, false, 0, false, 0, 0);
if (!mesh_header)
{
printf(alloc resp packet fail\n);
return;
}
if (espconn_mesh_set_usr_data(mesh_header,
resp_json_packet_body, resp_data_len))
{
printf(set user data fail\n);
Espressif
12/16
2016.04
4. Sample Code
free(mesh_header);
return;
}
// sent control packet
espconn_mesh_sent(esp, mesh_header, mesh_header->len);
free(mesh_header);
}
4.3.
Getting Topology
void topology_entrance(Parameter list)
{
/*Add your codes to check status*/
/*Add your codes to build getting topology packet*/
bool res;
struct mesh_header_format *mesh_header = NULL;
struct mesh_header_option_format *topo_option = NULL;
uint8_t src_addr[] = {0,0,0,0,0,0};
uint8_t dst_addr[] = {xx,xx,xx,xx,xx,xx}; // MAC address of root
device
uint8_t dev_mac[6] = {xx,xx,xx,xx,xx,xx}; // zero represents
topology of all devices
uint16_t ot_len = sizeof(*topo_option) + sizeof(struct
mesh_header_option_header_type) + sizeof(dev_mac);
mesh_header = (struct mesh_header_format
*)espconn_mesh_create_packet(
dst_addr, src_addr, false, true, M_PROTO_NONE, 0,
true, ot_len, false, 0, false, 0, 0);
if (!mesh_header) {
printf(alloc resp packet fail\n);
return;
}
topo_option = (struct mesh_header_option_format
*)espconn_mesh_create_option(
M_O_TOPO_REQ, dev_mac, sizeof(dev_mac));
Espressif
13/16
2016.04
4. Sample Code
if (!topo_option) {
printf(alloc topo option fail\n);
free(mesh_header);
return;
}
res = espconn_mesh_add_option(mesh_header, topo_option);
free(topo_option);
if (res) {
printf(add topo option fail\n);
free(mesh_header);
return;
}
// send packet of getting topology
espconn_mesh_sent(esp, mesh_header, mesh_header->len);
free(mesh_header);
}
4.4.
Espressif
14/16
2016.04
4. Sample Code
4.5.
Dev-App
For details of the example codes, please refer to:
ESP8266_MESH_DEMO/blob/master/mesh_demo/include/user_config.h
ESP8266_MESH_DEMO/blob/master/mesh_demo/demo/mesh_demo.c
Espressif
15/16
2016.04
All trade names, trademarks and registered trademarks mentioned in this document are
property of their respective owners, and are hereby acknowledged.
www.espressif.com