A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

UDPClientSingle.cpp

Committer:
itead
Date:
2015-02-07
Revision:
0:719de8678b39

File content as of revision 0:719de8678b39:

/**
 * @file UDPClientSingle.cpp
 * @brief The UDPClientSingle demo of library WeeESP8266. 
 * @author Wu Pengfei<pengfei.wu@itead.cc> 
 * @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 "mbed.h"
#include "ArduinoAPI.h"
#include "ESP8266.h"

extern void setup(void);
extern void loop(void);

ArduinoSerial esp_uart(p28, p27);
ESP8266 wifi(esp_uart);
Serial pc(USBTX, USBRX);

int main () {
    setup();
    while(1) {
        loop();
    }
}

void setup(void)
{
    printf("setup begin\r\n");
    
    printf("FW Version: %s\r\n", wifi.getVersion().c_str());
      
    if (wifi.setOprToStationSoftAP()) {
        printf("to station + softap ok\r\n");
    } else {
        printf("to station + softap err\r\n");
    }

    if (wifi.joinAP("ITEAD", "12345678")) {
        printf("Join AP success\r\n");
        printf("IP: [%s]\r\n", wifi.getLocalIP().c_str());       
    } else {
        printf("Join AP failure\r\n");
    }
    
    if (wifi.disableMUX()) {
        printf("single ok\r\n");
    } else {
        printf("single err\r\n");
    }
    
    printf("setup end\r\n");
}

void loop(void)
{
    uint8_t buffer[1024] = {0};
    
    if (wifi.registerUDP("172.16.5.12", 5416)) {
        printf("register udp ok\r\n");
    } else {
        printf("register udp err\r\n");
    }
    
    char *hello = "Hello, this is client!";
    wifi.send((const uint8_t*)hello, strlen(hello));
    
    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
        printf("Received:[");
        for(uint32_t i = 0; i < len; i++) {
            printf("%c", buffer[i]);
        }
        printf("]\r\n");
    }
    
    if (wifi.unregisterUDP()) {
        printf("unregister udp ok\r\n");
    } else {
        printf("unregister udp err\r\n");
    }
    delay(5000);
}