ITEAD STUDIO
/
WeeESP8266_TCPServer
A program of WeeESP8266 library.
TCPServer.cpp@1:2560c001b1c9, 2015-02-11 (annotated)
- Committer:
- itead
- Date:
- Wed Feb 11 06:13:53 2015 +0000
- Revision:
- 1:2560c001b1c9
- Parent:
- 0:724d74f5a99a
Change buffer size
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
itead | 0:724d74f5a99a | 1 | /** |
itead | 0:724d74f5a99a | 2 | * @file TCPServer.cpp |
itead | 0:724d74f5a99a | 3 | * @brief The TCPServer demo of library WeeESP8266. |
itead | 0:724d74f5a99a | 4 | * @author Wu Pengfei<pengfei.wu@itead.cc> |
itead | 0:724d74f5a99a | 5 | * @date 2015.02 |
itead | 0:724d74f5a99a | 6 | * |
itead | 0:724d74f5a99a | 7 | * @par Copyright: |
itead | 0:724d74f5a99a | 8 | * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n |
itead | 0:724d74f5a99a | 9 | * This program is free software; you can redistribute it and/or |
itead | 0:724d74f5a99a | 10 | * modify it under the terms of the GNU General Public License as |
itead | 0:724d74f5a99a | 11 | * published by the Free Software Foundation; either version 2 of |
itead | 0:724d74f5a99a | 12 | * the License, or (at your option) any later version. \n\n |
itead | 0:724d74f5a99a | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
itead | 0:724d74f5a99a | 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
itead | 0:724d74f5a99a | 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
itead | 0:724d74f5a99a | 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
itead | 0:724d74f5a99a | 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
itead | 0:724d74f5a99a | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
itead | 0:724d74f5a99a | 19 | * THE SOFTWARE. |
itead | 0:724d74f5a99a | 20 | */ |
itead | 0:724d74f5a99a | 21 | #include "mbed.h" |
itead | 0:724d74f5a99a | 22 | #include "ArduinoAPI.h" |
itead | 0:724d74f5a99a | 23 | #include "ESP8266.h" |
itead | 0:724d74f5a99a | 24 | |
itead | 0:724d74f5a99a | 25 | extern void setup(void); |
itead | 0:724d74f5a99a | 26 | extern void loop(void); |
itead | 0:724d74f5a99a | 27 | |
itead | 0:724d74f5a99a | 28 | ArduinoSerial esp_uart(p28, p27); |
itead | 0:724d74f5a99a | 29 | ESP8266 wifi(esp_uart); |
itead | 0:724d74f5a99a | 30 | Serial pc(USBTX, USBRX); |
itead | 0:724d74f5a99a | 31 | |
itead | 0:724d74f5a99a | 32 | int main () { |
itead | 0:724d74f5a99a | 33 | setup(); |
itead | 0:724d74f5a99a | 34 | while(1) { |
itead | 0:724d74f5a99a | 35 | loop(); |
itead | 0:724d74f5a99a | 36 | } |
itead | 0:724d74f5a99a | 37 | } |
itead | 0:724d74f5a99a | 38 | |
itead | 0:724d74f5a99a | 39 | |
itead | 0:724d74f5a99a | 40 | void setup(void) |
itead | 0:724d74f5a99a | 41 | { |
itead | 0:724d74f5a99a | 42 | printf("setup begin\r\n"); |
itead | 0:724d74f5a99a | 43 | |
itead | 0:724d74f5a99a | 44 | printf("FW Version: %s\r\n", wifi.getVersion().c_str()); |
itead | 0:724d74f5a99a | 45 | |
itead | 0:724d74f5a99a | 46 | if (wifi.setOprToStationSoftAP()) { |
itead | 0:724d74f5a99a | 47 | printf("to station + softap ok\r\n"); |
itead | 0:724d74f5a99a | 48 | } else { |
itead | 0:724d74f5a99a | 49 | printf("to station + softap err\r\n"); |
itead | 0:724d74f5a99a | 50 | } |
itead | 0:724d74f5a99a | 51 | |
itead | 0:724d74f5a99a | 52 | if (wifi.joinAP("ITEAD", "12345678")) { |
itead | 0:724d74f5a99a | 53 | printf("Join AP success\r\n"); |
itead | 0:724d74f5a99a | 54 | printf("IP: [%s]\r\n", wifi.getLocalIP().c_str()); |
itead | 0:724d74f5a99a | 55 | } else { |
itead | 0:724d74f5a99a | 56 | printf("Join AP failure\r\n"); |
itead | 0:724d74f5a99a | 57 | } |
itead | 0:724d74f5a99a | 58 | |
itead | 0:724d74f5a99a | 59 | if (wifi.enableMUX()) { |
itead | 0:724d74f5a99a | 60 | printf("multiple ok\r\n"); |
itead | 0:724d74f5a99a | 61 | } else { |
itead | 0:724d74f5a99a | 62 | printf("multiple err\r\n"); |
itead | 0:724d74f5a99a | 63 | } |
itead | 0:724d74f5a99a | 64 | |
itead | 0:724d74f5a99a | 65 | if (wifi.startTCPServer(8090)) { |
itead | 0:724d74f5a99a | 66 | printf("start tcp server ok\r\n"); |
itead | 0:724d74f5a99a | 67 | } else { |
itead | 0:724d74f5a99a | 68 | printf("start tcp server err\r\n"); |
itead | 0:724d74f5a99a | 69 | } |
itead | 0:724d74f5a99a | 70 | |
itead | 0:724d74f5a99a | 71 | if (wifi.setTCPServerTimeout(10)) { |
itead | 0:724d74f5a99a | 72 | printf("set tcp server timout 10 seconds\r\n"); |
itead | 0:724d74f5a99a | 73 | } else { |
itead | 0:724d74f5a99a | 74 | printf("set tcp server timout err\r\n"); |
itead | 0:724d74f5a99a | 75 | } |
itead | 0:724d74f5a99a | 76 | |
itead | 0:724d74f5a99a | 77 | printf("setup end\r\n"); |
itead | 0:724d74f5a99a | 78 | } |
itead | 0:724d74f5a99a | 79 | |
itead | 0:724d74f5a99a | 80 | void loop(void) |
itead | 0:724d74f5a99a | 81 | { |
itead | 0:724d74f5a99a | 82 | uint8_t buffer[1024] = {0}; |
itead | 0:724d74f5a99a | 83 | uint8_t mux_id; |
itead | 0:724d74f5a99a | 84 | |
itead | 0:724d74f5a99a | 85 | uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100); |
itead | 0:724d74f5a99a | 86 | if (len > 0) { |
itead | 0:724d74f5a99a | 87 | printf("Status:[%s]\r\n", wifi.getIPStatus().c_str()); |
itead | 0:724d74f5a99a | 88 | |
itead | 0:724d74f5a99a | 89 | printf("Received from %u:[", mux_id); |
itead | 0:724d74f5a99a | 90 | for(uint32_t i = 0; i < len; i++) { |
itead | 0:724d74f5a99a | 91 | printf("%c", buffer[i]); |
itead | 0:724d74f5a99a | 92 | } |
itead | 0:724d74f5a99a | 93 | printf("]\r\n"); |
itead | 0:724d74f5a99a | 94 | |
itead | 0:724d74f5a99a | 95 | if(wifi.send(mux_id, buffer, len)) { |
itead | 0:724d74f5a99a | 96 | printf("send back ok\r\n"); |
itead | 0:724d74f5a99a | 97 | } else { |
itead | 0:724d74f5a99a | 98 | printf("send back err\r\n"); |
itead | 0:724d74f5a99a | 99 | } |
itead | 0:724d74f5a99a | 100 | |
itead | 0:724d74f5a99a | 101 | if (wifi.releaseTCP(mux_id)) { |
itead | 0:724d74f5a99a | 102 | printf("release tcp %u ok\r\n", mux_id); |
itead | 0:724d74f5a99a | 103 | } else { |
itead | 0:724d74f5a99a | 104 | printf("release tcp %u err\r\n", mux_id); |
itead | 0:724d74f5a99a | 105 | } |
itead | 0:724d74f5a99a | 106 | |
itead | 0:724d74f5a99a | 107 | printf("Status:[%s]\r\n", wifi.getIPStatus().c_str()); |
itead | 0:724d74f5a99a | 108 | } |
itead | 0:724d74f5a99a | 109 | } |