| API For Arduino developers

- Main Page
- Related Pages
- Classes
- Files
- Examples

- File List

TCPServer.ino
1
22 #include "ESP8266.h"
23
24 #define SSID "ITEAD"
25 #define PASSWORD "12345678"
26
27 ESP8266 wifi(Serial1);
28
29 void setup(void)
30 {
31 Serial.begin(9600);
32 Serial.print("setup begin\r\n");
33
34 Serial.print("FW Version:");
35 Serial.println(wifi.getVersion().c_str());
36
37 if (wifi.setOprToStationSoftAP()) {
38 Serial.print("to station + softap ok\r\n");
39 } else {
40 Serial.print("to station + softap err\r\n");
41 }
42
43 if (wifi.joinAP(SSID, PASSWORD)) {
44 Serial.print("Join AP success\r\n");
45 Serial.print("IP: ");
46 Serial.println(wifi.getLocalIP().c_str());
47 } else {
48 Serial.print("Join AP failure\r\n");
49 }
50
51 if (wifi.enableMUX()) {
52 Serial.print("multiple ok\r\n");
53 } else {
54 Serial.print("multiple err\r\n");
55 }
56
57 if (wifi.startTCPServer(8090)) {
58 Serial.print("start tcp server ok\r\n");
59 } else {
60 Serial.print("start tcp server err\r\n");
61 }
62
63 if (wifi.setTCPServerTimeout(10)) {
64 Serial.print("set tcp server timout 10 seconds\r\n");
65 } else {
66 Serial.print("set tcp server timout err\r\n");
67 }
68
69 Serial.print("setup end\r\n");
70 }
71
72 void loop(void)
73 {
74 uint8_t buffer[128] = {0};
75 uint8_t mux_id;
76 uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
77 if (len > 0) {
78 Serial.print("Status:[");
79 Serial.print(wifi.getIPStatus().c_str());
80 Serial.println("]");
81
82 Serial.print("Received from :");
83 Serial.print(mux_id);
84 Serial.print("[");
85 for(uint32_t i = 0; i < len; i++) {
86 Serial.print((char)buffer[i]);
87 }
88 Serial.print("]\r\n");
89
90 if(wifi.send(mux_id, buffer, len)) {
91 Serial.print("send back ok\r\n");
92 } else {
93 Serial.print("send back err\r\n");
94 }
95
96 if (wifi.releaseTCP(mux_id)) {
97 Serial.print("release tcp ");
98 Serial.print(mux_id);
99 Serial.println(" ok");
100 } else {
101 Serial.print("release tcp");
102 Serial.print(mux_id);
103 Serial.println(" err");
104 }
105
106 Serial.print("Status:[");
107 Serial.print(wifi.getIPStatus().c_str());
108 Serial.println("]");
109 }
110 }
111
ESP8266::send
bool send(const uint8_t *buffer, uint32_t len)
Send data based on TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:261
ESP8266::getVersion
String getVersion(void)
Get the version of AT Command Set.
Definition: ESP8266.cpp:80
ESP8266::releaseTCP
bool releaseTCP(void)
Release TCP connection in single mode.
Definition: ESP8266.cpp:196
ESP8266::recv
uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout=1000)
Receive data from TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:271
ESP8266::enableMUX
bool enableMUX(void)
Enable IP MUX(multiple connection mode).
Definition: ESP8266.cpp:181
ESP8266::getLocalIP
String getLocalIP(void)
Get the IP address of ESP8266.
Definition: ESP8266.cpp:174
ESP8266.h
The definition of class ESP8266.
ESP8266::setOprToStationSoftAP
bool setOprToStationSoftAP(void)
Set operation mode to station + softap.
Definition: ESP8266.cpp:121
ESP8266::joinAP
bool joinAP(String ssid, String pwd)
Join in AP.
Definition: ESP8266.cpp:145
ESP8266::setTCPServerTimeout
bool setTCPServerTimeout(uint32_t timeout=180)
Set the timeout of TCP Server.
Definition: ESP8266.cpp:231
ESP8266
Provide an easy-to-use way to manipulate ESP8266.
Definition: ESP8266.h:38
ESP8266::startTCPServer
bool startTCPServer(uint32_t port=333)
Start TCP Server(Only in multiple mode).
Definition: ESP8266.cpp:236
ESP8266::getIPStatus
String getIPStatus(void)
Get the current status of connection(UDP and TCP).
Definition: ESP8266.cpp:167