A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPClientSingle.cpp Source File

UDPClientSingle.cpp

Go to the documentation of this file.
00001 /**
00002  * @file UDPClientSingle.cpp
00003  * @brief The UDPClientSingle 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 void setup(void)
00040 {
00041     printf("setup begin\r\n");
00042     
00043     printf("FW Version: %s\r\n", wifi.getVersion().c_str());
00044       
00045     if (wifi.setOprToStationSoftAP()) {
00046         printf("to station + softap ok\r\n");
00047     } else {
00048         printf("to station + softap err\r\n");
00049     }
00050 
00051     if (wifi.joinAP("ITEAD", "12345678")) {
00052         printf("Join AP success\r\n");
00053         printf("IP: [%s]\r\n", wifi.getLocalIP().c_str());       
00054     } else {
00055         printf("Join AP failure\r\n");
00056     }
00057     
00058     if (wifi.disableMUX()) {
00059         printf("single ok\r\n");
00060     } else {
00061         printf("single err\r\n");
00062     }
00063     
00064     printf("setup end\r\n");
00065 }
00066 
00067 void loop(void)
00068 {
00069     uint8_t buffer[1024] = {0};
00070     
00071     if (wifi.registerUDP("172.16.5.12", 5416)) {
00072         printf("register udp ok\r\n");
00073     } else {
00074         printf("register udp err\r\n");
00075     }
00076     
00077     char *hello = "Hello, this is client!";
00078     wifi.send((const uint8_t*)hello, strlen(hello));
00079     
00080     uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
00081     if (len > 0) {
00082         printf("Received:[");
00083         for(uint32_t i = 0; i < len; i++) {
00084             printf("%c", buffer[i]);
00085         }
00086         printf("]\r\n");
00087     }
00088     
00089     if (wifi.unregisterUDP()) {
00090         printf("unregister udp ok\r\n");
00091     } else {
00092         printf("unregister udp err\r\n");
00093     }
00094     delay(5000);
00095 }