This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTCPServer.ino
160 lines (131 loc) · 3.78 KB
/
TCPServer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/****************************************************************************************************************************
TCPServer.ino
For WizFi360/ESP8266/ESP32-AT-command running shields
ESP_AT_Lib is a wrapper library for the WizFi360/ESP8266/ESP32 AT-command shields
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_Lib
Licensed under MIT license
@example TCPServer.ino
@brief The TCPServer demo of library WeeESP8266.
@author Wu Pengfei<[email protected]>
@date 2015.02
@par Copyright:
Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version. \n\n
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************************************************************************************/
#include "defines.h"
#include "ESP_AT_Lib.h"
ESP8266 wifi(&EspSerial);
// Your board <-> ESP_AT baud rate:
#define ESP_AT_BAUD 115200
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(1000);
#if defined(BOARD_NAME)
Serial.println("\nStart TCPServer on " + String(BOARD_NAME));
#else
Serial.println("\nStart TCPServer");
#endif
// initialize serial for ESP module
EspSerial.begin(ESP_AT_BAUD);
Serial.print("FW Version:");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStationSoftAP())
{
Serial.println("Set AP/STA Mode OK");
}
else
{
Serial.println("Set AP/STA Mode failed");
}
if (wifi.joinAP(SSID, PASSWORD))
{
Serial.println("Connect to WiFi OK");
Serial.print("IP: ");
Serial.println(wifi.getLocalIP().c_str());
}
else
{
Serial.println("Connect to WiFi failed");
}
if (wifi.enableMUX())
{
Serial.println("enableMUX OK");
}
else
{
Serial.println("enableMUX failed");
}
if (wifi.startTCPServer(8090))
{
Serial.println("Start TCP server OK");
}
else
{
Serial.println("start TCP server failed");
}
if (wifi.setTCPServerTimeout(10))
{
Serial.println("Set TCP server timeout 10 seconds");
}
else
{
Serial.println("Set TCP server timeout failed");
}
Serial.println("Done");
}
void loop()
{
uint8_t buffer[128] = {0};
uint8_t mux_id;
uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
if (len > 0)
{
Serial.print("Status:[");
Serial.print(wifi.getIPStatus().c_str());
Serial.println("]");
Serial.print("Received from :");
Serial.println(mux_id);
Serial.print("[");
for (uint32_t i = 0; i < len; i++)
{
Serial.print((char)buffer[i]);
}
Serial.println("]");
if (wifi.send(mux_id, buffer, len))
{
Serial.println("Send back OK");
}
else
{
Serial.println("Send back failed");
}
if (wifi.releaseTCP(mux_id))
{
Serial.print("Release TCP ");
Serial.print(mux_id);
Serial.println(" OK");
}
else
{
Serial.print("Release TCP ");
Serial.print(mux_id);
Serial.println(" failed");
}
Serial.print("Status:[");
Serial.print(wifi.getIPStatus().c_str());
Serial.println("]");
}
}