A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCPServer.cpp Source File

TCPServer.cpp

Go to the documentation of this file.
00001 /**
00002  * @file TCPServer.cpp
00003  * @brief The TCPServer demo of library WeeESP8266. 
00004  * @author Wu Pengfei<pengfei.wu@itead.cc> 
00005  * @date 2015.02
00006  * 
00007  * @par Copyright:
00008  * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License as
00011  * published by the Free Software Foundation; either version 2 of
00012  * the License, or (at your option) any later version. \n\n
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00019  * THE SOFTWARE.
00020  */
00021 #include "mbed.h"
00022 #include "ArduinoAPI.h"
00023 #include "ESP8266.h"
00024 
00025 extern void setup(void);
00026 extern void loop(void);
00027 
00028 ArduinoSerial esp_uart(p28, p27);
00029 ESP8266 wifi(esp_uart);
00030 Serial pc(USBTX, USBRX);
00031 
00032 int main () {
00033     setup();
00034     while(1) {
00035         loop();
00036     }
00037 }
00038 
00039 
00040 void setup(void)
00041 {
00042     printf("setup begin\r\n");
00043     
00044     printf("FW Version: %s\r\n", wifi.getVersion().c_str());
00045       
00046     if (wifi.setOprToStationSoftAP()) {
00047         printf("to station + softap ok\r\n");
00048     } else {
00049         printf("to station + softap err\r\n");
00050     }
00051 
00052     if (wifi.joinAP("ITEAD", "12345678")) {
00053         printf("Join AP success\r\n");
00054         printf("IP: [%s]\r\n", wifi.getLocalIP().c_str());       
00055     } else {
00056         printf("Join AP failure\r\n");
00057     }
00058     
00059     if (wifi.enableMUX()) {
00060         printf("multiple ok\r\n");
00061     } else {
00062         printf("multiple err\r\n");
00063     }
00064     
00065     if (wifi.startTCPServer(8090)) {
00066         printf("start tcp server ok\r\n");
00067     } else {
00068         printf("start tcp server err\r\n");
00069     }
00070     
00071     if (wifi.setTCPServerTimeout(10)) {
00072         printf("set tcp server timout 10 seconds\r\n");
00073     } else {
00074         printf("set tcp server timout err\r\n");
00075     }
00076     
00077     printf("setup end\r\n");
00078 }
00079 
00080 void loop(void)
00081 {
00082     uint8_t buffer[1024] = {0};
00083     uint8_t mux_id;
00084     
00085     uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
00086     if (len > 0) {
00087         printf("Status:[%s]\r\n", wifi.getIPStatus().c_str());
00088         
00089         printf("Received from %u:[", mux_id);
00090         for(uint32_t i = 0; i < len; i++) {
00091             printf("%c", buffer[i]);
00092         }
00093         printf("]\r\n");
00094         
00095         if(wifi.send(mux_id, buffer, len)) {
00096             printf("send back ok\r\n");
00097         } else {
00098             printf("send back err\r\n");
00099         }
00100         
00101         if (wifi.releaseTCP(mux_id)) {
00102             printf("release tcp %u ok\r\n", mux_id);
00103         } else {
00104             printf("release tcp %u err\r\n", mux_id);
00105         }
00106         
00107         printf("Status:[%s]\r\n", wifi.getIPStatus().c_str());
00108     }
00109 }