A program of WeeESP8266 library.

Dependencies:   WeeESP8266 mbed

Committer:
itead
Date:
Sat Feb 07 10:03:21 2015 +0000
Revision:
0:eddc0f190142
First commit

Who changed what in which revision?

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