A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

Committer:
itead
Date:
Wed Feb 11 06:13:37 2015 +0000
Revision:
1:3cd29bea2447
Parent:
0:ada82e95e5e4
Change buffer size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
itead 0:ada82e95e5e4 1 /**
itead 0:ada82e95e5e4 2 * @file TCPClientSingle.cpp
itead 0:ada82e95e5e4 3 * @brief The TCPClientSingle demo of library WeeESP8266.
itead 0:ada82e95e5e4 4 * @author Wu Pengfei<pengfei.wu@itead.cc>
itead 0:ada82e95e5e4 5 * @date 2015.02
itead 0:ada82e95e5e4 6 *
itead 0:ada82e95e5e4 7 * @par Copyright:
itead 0:ada82e95e5e4 8 * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
itead 0:ada82e95e5e4 9 * This program is free software; you can redistribute it and/or
itead 0:ada82e95e5e4 10 * modify it under the terms of the GNU General Public License as
itead 0:ada82e95e5e4 11 * published by the Free Software Foundation; either version 2 of
itead 0:ada82e95e5e4 12 * the License, or (at your option) any later version. \n\n
itead 0:ada82e95e5e4 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
itead 0:ada82e95e5e4 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
itead 0:ada82e95e5e4 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
itead 0:ada82e95e5e4 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
itead 0:ada82e95e5e4 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
itead 0:ada82e95e5e4 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
itead 0:ada82e95e5e4 19 * THE SOFTWARE.
itead 0:ada82e95e5e4 20 */
itead 0:ada82e95e5e4 21 #include "mbed.h"
itead 0:ada82e95e5e4 22 #include "ArduinoAPI.h"
itead 0:ada82e95e5e4 23 #include "ESP8266.h"
itead 0:ada82e95e5e4 24
itead 0:ada82e95e5e4 25 extern void setup(void);
itead 0:ada82e95e5e4 26 extern void loop(void);
itead 0:ada82e95e5e4 27
itead 0:ada82e95e5e4 28 ArduinoSerial esp_uart(p28, p27);
itead 0:ada82e95e5e4 29 ESP8266 wifi(esp_uart);
itead 0:ada82e95e5e4 30 Serial pc(USBTX, USBRX);
itead 0:ada82e95e5e4 31
itead 0:ada82e95e5e4 32 int main () {
itead 0:ada82e95e5e4 33 setup();
itead 0:ada82e95e5e4 34 while(1) {
itead 0:ada82e95e5e4 35 loop();
itead 0:ada82e95e5e4 36 }
itead 0:ada82e95e5e4 37 }
itead 0:ada82e95e5e4 38
itead 0:ada82e95e5e4 39 void setup(void)
itead 0:ada82e95e5e4 40 {
itead 0:ada82e95e5e4 41 printf("setup begin\r\n");
itead 0:ada82e95e5e4 42
itead 0:ada82e95e5e4 43 printf("FW Version: %s\r\n", wifi.getVersion().c_str());
itead 0:ada82e95e5e4 44
itead 0:ada82e95e5e4 45 if (wifi.setOprToStationSoftAP()) {
itead 0:ada82e95e5e4 46 printf("to station + softap ok\r\n");
itead 0:ada82e95e5e4 47 } else {
itead 0:ada82e95e5e4 48 printf("to station + softap err\r\n");
itead 0:ada82e95e5e4 49 }
itead 0:ada82e95e5e4 50
itead 0:ada82e95e5e4 51 if (wifi.joinAP("ITEAD", "12345678")) {
itead 0:ada82e95e5e4 52 printf("Join AP success\r\n");
itead 0:ada82e95e5e4 53 printf("IP: [%s]\r\n", wifi.getLocalIP().c_str());
itead 0:ada82e95e5e4 54 } else {
itead 0:ada82e95e5e4 55 printf("Join AP failure\r\n");
itead 0:ada82e95e5e4 56 }
itead 0:ada82e95e5e4 57
itead 0:ada82e95e5e4 58 if (wifi.disableMUX()) {
itead 0:ada82e95e5e4 59 printf("single ok\r\n");
itead 0:ada82e95e5e4 60 } else {
itead 0:ada82e95e5e4 61 printf("single err\r\n");
itead 0:ada82e95e5e4 62 }
itead 0:ada82e95e5e4 63
itead 0:ada82e95e5e4 64 printf("setup end\r\n");
itead 0:ada82e95e5e4 65 }
itead 0:ada82e95e5e4 66
itead 0:ada82e95e5e4 67 void loop(void)
itead 0:ada82e95e5e4 68 {
itead 0:ada82e95e5e4 69 uint8_t buffer[1024] = {0};
itead 0:ada82e95e5e4 70
itead 0:ada82e95e5e4 71 if (wifi.createTCP("172.16.5.12", 8090)) {
itead 0:ada82e95e5e4 72 printf("create tcp ok\r\n");
itead 0:ada82e95e5e4 73 } else {
itead 0:ada82e95e5e4 74 printf("create tcp err\r\n");
itead 0:ada82e95e5e4 75 }
itead 0:ada82e95e5e4 76
itead 0:ada82e95e5e4 77 char *hello = "Hello, this is client!";
itead 0:ada82e95e5e4 78 wifi.send((const uint8_t*)hello, strlen(hello));
itead 0:ada82e95e5e4 79
itead 0:ada82e95e5e4 80 uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
itead 0:ada82e95e5e4 81 if (len > 0) {
itead 0:ada82e95e5e4 82 printf("Received:[");
itead 0:ada82e95e5e4 83 for(uint32_t i = 0; i < len; i++) {
itead 0:ada82e95e5e4 84 printf("%c", buffer[i]);
itead 0:ada82e95e5e4 85 }
itead 0:ada82e95e5e4 86 printf("]\r\n");
itead 0:ada82e95e5e4 87 }
itead 0:ada82e95e5e4 88
itead 0:ada82e95e5e4 89 if (wifi.releaseTCP()) {
itead 0:ada82e95e5e4 90 printf("release tcp ok\r\n");
itead 0:ada82e95e5e4 91 } else {
itead 0:ada82e95e5e4 92 printf("release tcp err\r\n");
itead 0:ada82e95e5e4 93 }
itead 0:ada82e95e5e4 94 delay(5000);
itead 0:ada82e95e5e4 95 }