| API For Arduino developers

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

- File List
- File Members

TCPClientMultiple.ino
1
21 #include "ESP8266.h"
22
23 #define SSID "ITEAD"
24 #define PASSWORD "12345678"
25 #define HOST_NAME "192.168.1.1"
26 #define HOST_PORT (8090)
27
28 ESP8266 wifi(Serial1);
29
30 void setup(void)
31 {
32 Serial.begin(9600);
33 Serial.print("setup begin\r\n");
34
35 Serial.print("FW Version: ");
36 Serial.println(wifi.getVersion().c_str());
37
38
39 if (wifi.setOprToStationSoftAP()) {
40 Serial.print("to station + softap ok\r\n");
41 } else {
42 Serial.print("to station + softap err\r\n");
43 }
44
45 if (wifi.joinAP(SSID, PASSWORD)) {
46 Serial.print("Join AP success\r\n");
47 Serial.print("IP: ");
48 Serial.println(wifi.getLocalIP().c_str());
49 } else {
50 Serial.print("Join AP failure\r\n");
51 }
52
53 if (wifi.enableMUX()) {
54 Serial.print("multiple ok\r\n");
55 } else {
56 Serial.print("multiple err\r\n");
57 }
58
59 Serial.print("setup end\r\n");
60 }
61
62 void loop(void)
63 {
64 uint8_t buffer[128] = {0};
65 static uint8_t mux_id = 0;
66
67 if (wifi.createTCP(mux_id, HOST_NAME, HOST_PORT)) {
68 Serial.print("create tcp ");
69 Serial.print(mux_id);
70 Serial.println(" ok");
71 } else {
72 Serial.print("create tcp ");
73 Serial.print(mux_id);
74 Serial.println(" err");
75 }
76
77
78 char *hello = "Hello, this is client!";
79 if (wifi.send(mux_id, (const uint8_t*)hello, strlen(hello))) {
80 Serial.println("send ok");
81 } else {
82 Serial.println("send err");
83 }
84
85 uint32_t len = wifi.recv(mux_id, buffer, sizeof(buffer), 10000);
86 if (len > 0) {
87 Serial.print("Received:[");
88 for(uint32_t i = 0; i < len; i++) {
89 Serial.print((char)buffer[i]);
90 }
91 Serial.print("]\r\n");
92 }
93
94 if (wifi.releaseTCP(mux_id)) {
95 Serial.print("release tcp ");
96 Serial.print(mux_id);
97 Serial.println(" ok");
98 } else {
99 Serial.print("release tcp ");
100 Serial.print(mux_id);
101 Serial.println(" err");
102 }
103
104 delay(3000);
105 mux_id++;
106 if (mux_id >= 5) {
107 mux_id = 0;
108 }
109 }
110
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:386
ESP8266::joinAP
bool joinAP(String ssid, String pwd, uint8_t pattern=3)
Join in AP.
Definition: ESP8266.cpp:178
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:303
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:396
ESP8266::enableMUX
bool enableMUX(void)
Enable IP MUX(multiple connection mode).
Definition: ESP8266.cpp:288
ESP8266::getLocalIP
String getLocalIP(void)
Get the IP address of ESP8266.
Definition: ESP8266.cpp:281
ESP8266.h
The definition of class ESP8266.
ESP8266::createTCP
bool createTCP(String addr, uint32_t port)
Create TCP connection in single mode.
Definition: ESP8266.cpp:298
ESP8266::setOprToStationSoftAP
bool setOprToStationSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station + softap.
Definition: ESP8266.cpp:146
ESP8266
Provide an easy-to-use way to manipulate ESP8266.
Definition: ESP8266.h:42