source: rtos_arduino/trunk/arduino_lib/libraries/ESP8266/examples/TCPServer/TCPServer.ino@ 141

Last change on this file since 141 was 141, checked in by ertl-honda, 7 years ago

サンプルが動作するよう修正.

File size: 3.2 KB
Line 
1/**
2 * @example TCPServer.ino
3 * @brief The TCPServer demo of library WeeESP8266.
4 * @author Wu Pengfei<pengfei.wu@itead.cc>
5 * @date 2015.02
6 *
7 * @par Copyright:
8 * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version. \n\n
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#include "ESP8266.h"
23
24#define SSID "ITEAD"
25#define PASSWORD "12345678"
26
27ESP8266 wifi;
28
29void 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 wifi.begin(Serial5);
38 if (wifi.setOprToStationSoftAP()) {
39 Serial.print("to station + softap ok\r\n");
40 } else {
41 Serial.print("to station + softap err\r\n");
42 }
43
44 if (wifi.joinAP(SSID, PASSWORD)) {
45 Serial.print("Join AP success\r\n");
46 Serial.print("IP: ");
47 Serial.println(wifi.getLocalIP().c_str());
48 } else {
49 Serial.print("Join AP failure\r\n");
50 }
51
52 if (wifi.enableMUX()) {
53 Serial.print("multiple ok\r\n");
54 } else {
55 Serial.print("multiple err\r\n");
56 }
57
58 if (wifi.startTCPServer(8090)) {
59 Serial.print("start tcp server ok\r\n");
60 } else {
61 Serial.print("start tcp server err\r\n");
62 }
63
64 if (wifi.setTCPServerTimeout(10)) {
65 Serial.print("set tcp server timout 10 seconds\r\n");
66 } else {
67 Serial.print("set tcp server timout err\r\n");
68 }
69
70 Serial.print("setup end\r\n");
71}
72
73void loop(void)
74{
75 uint8_t buffer[128] = {0};
76 uint8_t mux_id;
77 uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
78 if (len > 0) {
79 Serial.print("Status:[");
80 Serial.print(wifi.getIPStatus().c_str());
81 Serial.println("]");
82
83 Serial.print("Received from :");
84 Serial.print(mux_id);
85 Serial.print("[");
86 for(uint32_t i = 0; i < len; i++) {
87 Serial.print((char)buffer[i]);
88 }
89 Serial.print("]\r\n");
90
91 if(wifi.send(mux_id, buffer, len)) {
92 Serial.print("send back ok\r\n");
93 } else {
94 Serial.print("send back err\r\n");
95 }
96
97 if (wifi.releaseTCP(mux_id)) {
98 Serial.print("release tcp ");
99 Serial.print(mux_id);
100 Serial.println(" ok");
101 } else {
102 Serial.print("release tcp");
103 Serial.print(mux_id);
104 Serial.println(" err");
105 }
106
107 Serial.print("Status:[");
108 Serial.print(wifi.getIPStatus().c_str());
109 Serial.println("]");
110 }
111}
112
Note: See TracBrowser for help on using the repository browser.