A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPClientMultiple.cpp Source File

UDPClientMultiple.cpp

Go to the documentation of this file.
00001 /**
00002  * @file UDPClientMultiple.cpp
00003  * @brief The UDPClientMultiple 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     printf("setup end\r\n");
00066 }
00067 
00068 void loop(void)
00069 {
00070     uint8_t buffer[1024] = {0};
00071     static uint8_t mux_id = 0;
00072     
00073     if (wifi.registerUDP(mux_id, "172.16.5.12", 5416)) {
00074         printf("register udp %u ok\r\n", mux_id);
00075     } else {
00076         printf("register udp %u err\r\n", mux_id);
00077     }
00078     
00079     char *hello = "Hello, this is client!";
00080     wifi.send(mux_id, (const uint8_t*)hello, strlen(hello));
00081     
00082     uint32_t len = wifi.recv(mux_id, buffer, sizeof(buffer), 10000);
00083     if (len > 0) {
00084         printf("Received:[");
00085         for(uint32_t i = 0; i < len; i++) {
00086             printf("%c", buffer[i]);
00087         }
00088         printf("]\r\n");
00089     }
00090     
00091     if (wifi.unregisterUDP(mux_id)) {
00092         printf("unregister udp %u ok\r\n", mux_id);
00093     } else {
00094         printf("unregister udp %u err\r\n", mux_id);
00095     }
00096     delay(5000);
00097     mux_id++;
00098     if (mux_id >= 5) {
00099         mux_id = 0;
00100     }
00101 }