This is a demo for the WiflyInterface Library.

Dependencies:   WiflyInterface mbed

Wifly Firmware Update

Part of the demo will use the software update API to install 1 of 2 versions of the Wifly firmware, from the Microchip web site. You can see in the demo below the commands to install each of the 2 versions.

WiflyInterface - Broadcast packet

Part of the demo will send a broadcast packet to the network.

// TeraTerm monitoring mbed                         |  Windows network monitor 
// Initializing network interface...                |  > ReceiveUDP 40000 
// Connecting to network...                         |    Receiving datagrams... 
// Ethernet connected as 192.168.1.188              |
// Ethernet MAC is 00:06:66:71:5d:ef                |
// Connected at 24 Mb/s                             |
// Wifly info: wifly-EZX Ver: 4.75 Build: r1777,    |
//      Oct  2 2015 10:18:50 on RN-171              |
//                                                  |
//  ctrl-a    to download Wifly firmward v4.41      |
//  ctrl-b    to download Wifly firmward v4.75      |
//  ctrl-c    to send a broadcast message           |
//                                                  |
// User types ctrl-c in TeraTerm                    |
// Broadcast sending 'Hello World' on port 40000    |  Rx[10.789]: Hello World
// done.                                            |
// User types ctrl-c in TeraTerm                    |
// Broadcast sending 'Hello World' on port 40000    |  Rx[13.673]: Hello World
// done.                                            |
Committer:
WiredHome
Date:
Mon Dec 21 00:03:34 2015 +0000
Revision:
0:3878b62facf1
Small demonstration for the WiflyInterface library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:3878b62facf1 1 #include "mbed.h"
WiredHome 0:3878b62facf1 2 #include "RawSerial.h"
WiredHome 0:3878b62facf1 3
WiredHome 0:3878b62facf1 4 #include "WiflyInterface.h"
WiredHome 0:3878b62facf1 5
WiredHome 0:3878b62facf1 6 // Used later in hw adaptation
WiredHome 0:3878b62facf1 7 #define MBED_APP_BOARD 1 /* http://mbed.org/components/mbed-Application-Board/ */
WiredHome 0:3878b62facf1 8 #define SMART_BOARD 2 /* http://mbed.org/users/WiredHome/notebook/SmartBoard-baseboard/ */
WiredHome 0:3878b62facf1 9
WiredHome 0:3878b62facf1 10 // ########################################################################
WiredHome 0:3878b62facf1 11 #define HW_ADAPTER SMART_BOARD /* Which board are we compiling against? */
WiredHome 0:3878b62facf1 12 // ########################################################################
WiredHome 0:3878b62facf1 13
WiredHome 0:3878b62facf1 14
WiredHome 0:3878b62facf1 15 #define SSID "ssid"
WiredHome 0:3878b62facf1 16 #define PASS "pass"
WiredHome 0:3878b62facf1 17
WiredHome 0:3878b62facf1 18 const char PROGINFO[] = "\r\n\r\nWifly Demo - built " __DATE__ " " __TIME__ "\r\n\r\n";
WiredHome 0:3878b62facf1 19
WiredHome 0:3878b62facf1 20 char Message[] = "Hello World";
WiredHome 0:3878b62facf1 21 char BCASTADDR[] = "255.255.255.255";
WiredHome 0:3878b62facf1 22 int Port = 40000;
WiredHome 0:3878b62facf1 23
WiredHome 0:3878b62facf1 24 #if HW_ADAPTER == MBED_APP_BOARD
WiredHome 0:3878b62facf1 25 WiflyInterface eth( p9, p10, p30, p29);
WiredHome 0:3878b62facf1 26 #elif HW_ADAPTER == SMART_BOARD
WiredHome 0:3878b62facf1 27 WiflyInterface eth(p28, p27, p23, p24);
WiredHome 0:3878b62facf1 28 #endif
WiredHome 0:3878b62facf1 29
WiredHome 0:3878b62facf1 30 RawSerial pc(USBTX, USBRX);
WiredHome 0:3878b62facf1 31 DigitalOut myled(LED1);
WiredHome 0:3878b62facf1 32
WiredHome 0:3878b62facf1 33 extern "C" void mbed_reset();
WiredHome 0:3878b62facf1 34
WiredHome 0:3878b62facf1 35 void ShowIPAddress(bool show)
WiredHome 0:3878b62facf1 36 {
WiredHome 0:3878b62facf1 37 pc.printf("Ethernet connected as %s\r\n", show ? eth.getIPAddress() : "---.---.---.---");
WiredHome 0:3878b62facf1 38 }
WiredHome 0:3878b62facf1 39
WiredHome 0:3878b62facf1 40 void ShowMACAddress(bool show)
WiredHome 0:3878b62facf1 41 {
WiredHome 0:3878b62facf1 42 pc.printf("Ethernet MAC is %s\r\n", show ? eth.getMACAddress() : "--.--.--.--.--.--");
WiredHome 0:3878b62facf1 43 }
WiredHome 0:3878b62facf1 44
WiredHome 0:3878b62facf1 45
WiredHome 0:3878b62facf1 46 int main() {
WiredHome 0:3878b62facf1 47 pc.baud(460800);
WiredHome 0:3878b62facf1 48 pc.printf(PROGINFO);
WiredHome 0:3878b62facf1 49 pc.printf("Configuration for %s\r\n\r\n", (HW_ADAPTER == SMART_BOARD) ? "SmartBoard" : "mbed Application Board");
WiredHome 0:3878b62facf1 50
WiredHome 0:3878b62facf1 51 eth.SetSecurity(SSID, PASS, WPA);
WiredHome 0:3878b62facf1 52 pc.printf("***\r\n");
WiredHome 0:3878b62facf1 53 pc.printf("Initializing network interface...\r\n");
WiredHome 0:3878b62facf1 54 if (0 == eth.init()) { //Use DHCP
WiredHome 0:3878b62facf1 55 eth.setName("TestWiFi_01");
WiredHome 0:3878b62facf1 56 do {
WiredHome 0:3878b62facf1 57 pc.printf("Connecting to network...\r\n");
WiredHome 0:3878b62facf1 58 if (0 == eth.connect()) {
WiredHome 0:3878b62facf1 59 ShowIPAddress(true);
WiredHome 0:3878b62facf1 60 ShowMACAddress(true);
WiredHome 0:3878b62facf1 61 int speed = eth.get_connection_speed();
WiredHome 0:3878b62facf1 62 pc.printf("Connected at %d Mb/s\r\n", speed);
WiredHome 0:3878b62facf1 63 pc.printf("Wifly info: %s\r\n", eth.getWiflyVersionString());
WiredHome 0:3878b62facf1 64
WiredHome 0:3878b62facf1 65 pc.printf("\r\n");
WiredHome 0:3878b62facf1 66 pc.printf(" ctrl-a to download Wifly firmward v4.41\r\n");
WiredHome 0:3878b62facf1 67 pc.printf(" ctrl-b to download Wifly firmward v4.75\r\n");
WiredHome 0:3878b62facf1 68 pc.printf(" ctrl-c to send a broadcast message\r\n");
WiredHome 0:3878b62facf1 69 while (eth.is_connected()) {
WiredHome 0:3878b62facf1 70 if (eth.readable())
WiredHome 0:3878b62facf1 71 pc.putc(eth.getc());
WiredHome 0:3878b62facf1 72 else if (pc.readable()) {
WiredHome 0:3878b62facf1 73 int c = pc.getc();
WiredHome 0:3878b62facf1 74 if (c == 0x01) { // ctrl-a // Install Software v4.41
WiredHome 0:3878b62facf1 75 pc.printf("Trying to download wifly7-441.mif from microchip...\r\n");
WiredHome 0:3878b62facf1 76 if (eth.SWUpdateWifly("wifly7-441.mif")) {
WiredHome 0:3878b62facf1 77 pc.printf(" Success - now rebooting to this Wifly image.\r\n");
WiredHome 0:3878b62facf1 78 wait_ms(100);
WiredHome 0:3878b62facf1 79 mbed_reset();
WiredHome 0:3878b62facf1 80 }
WiredHome 0:3878b62facf1 81 } else if (c == 0x02) { // ctrl-b // Install Software v4.75
WiredHome 0:3878b62facf1 82 pc.printf("Trying to download wifly7-475.mif from microchip...\r\n");
WiredHome 0:3878b62facf1 83 if (eth.SWUpdateWifly("wifly7-475.mif")){
WiredHome 0:3878b62facf1 84 pc.printf(" Success - now rebooting to this Wifly image.\r\n");
WiredHome 0:3878b62facf1 85 wait_ms(100);
WiredHome 0:3878b62facf1 86 mbed_reset();
WiredHome 0:3878b62facf1 87 }
WiredHome 0:3878b62facf1 88 } else if (c == 0x03) { // ctrl-c
WiredHome 0:3878b62facf1 89 UDPSocket bcast;
WiredHome 0:3878b62facf1 90 Endpoint ep;
WiredHome 0:3878b62facf1 91 pc.printf("Broadcast sending '%s' on port %d\r\n", Message, Port);
WiredHome 0:3878b62facf1 92 int h = ep.set_address(BCASTADDR, Port);
WiredHome 0:3878b62facf1 93 int i = bcast.bind(40000);
WiredHome 0:3878b62facf1 94 int j = bcast.set_broadcasting(true);
WiredHome 0:3878b62facf1 95 int k = bcast.sendTo(ep, Message, strlen(Message));
WiredHome 0:3878b62facf1 96 bcast.close();
WiredHome 0:3878b62facf1 97 pc.printf("done.\r\n");
WiredHome 0:3878b62facf1 98 } else {
WiredHome 0:3878b62facf1 99 eth.putc(c);
WiredHome 0:3878b62facf1 100 }
WiredHome 0:3878b62facf1 101 }
WiredHome 0:3878b62facf1 102 }
WiredHome 0:3878b62facf1 103
WiredHome 0:3878b62facf1 104 pc.printf("lost connection.\r\n");
WiredHome 0:3878b62facf1 105 ShowIPAddress(false);
WiredHome 0:3878b62facf1 106 eth.disconnect();
WiredHome 0:3878b62facf1 107 }
WiredHome 0:3878b62facf1 108 else {
WiredHome 0:3878b62facf1 109 pc.printf(" ... failed to connect.\r\n");
WiredHome 0:3878b62facf1 110 }
WiredHome 0:3878b62facf1 111 }
WiredHome 0:3878b62facf1 112 while (1);
WiredHome 0:3878b62facf1 113 }
WiredHome 0:3878b62facf1 114 else {
WiredHome 0:3878b62facf1 115 pc.printf(" ... failed to initialize, rebooting...\r\n");
WiredHome 0:3878b62facf1 116 mbed_reset();
WiredHome 0:3878b62facf1 117 }
WiredHome 0:3878b62facf1 118 }