Example program for the ESP8266Interface running Espressif Firmware

Dependencies:   ESP8266Interface NetworkSocketAPI mbed

Note

This example code assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.

This program demonstrates how to open, close, send and recv from the ESP8266 using the Network Socket API.

Committer:
bridadan
Date:
Wed Jul 15 23:23:14 2015 +0000
Revision:
14:c47437f5dae8
Parent:
11:b7e531a4d661
Child:
15:e48b3c6558b1
Updating libraries and example program to match new APIs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:1984a177ff56 1 /* NetworkSocketAPI Example Program
sam_grove 0:1984a177ff56 2 * Copyright (c) 2015 ARM Limited
sam_grove 0:1984a177ff56 3 *
sam_grove 0:1984a177ff56 4 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:1984a177ff56 5 * you may not use this file except in compliance with the License.
sam_grove 0:1984a177ff56 6 * You may obtain a copy of the License at
sam_grove 0:1984a177ff56 7 *
sam_grove 0:1984a177ff56 8 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:1984a177ff56 9 *
sam_grove 0:1984a177ff56 10 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:1984a177ff56 11 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:1984a177ff56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:1984a177ff56 13 * See the License for the specific language governing permissions and
sam_grove 0:1984a177ff56 14 * limitations under the License.
sam_grove 0:1984a177ff56 15 */
sam_grove 0:1984a177ff56 16
sam_grove 0:1984a177ff56 17 #include "mbed.h"
sam_grove 7:6787b920b496 18 #include "WiFiRadioInterface.h"
bridadan 14:c47437f5dae8 19 #include <map>
sam_grove 0:1984a177ff56 20
sam_grove 0:1984a177ff56 21 DigitalOut myled(LED1);
sam_grove 0:1984a177ff56 22
bridadan 14:c47437f5dae8 23 WiFiRadioInterface wifi(D1, D0);
sam_grove 2:7283ce112304 24
sam_grove 0:1984a177ff56 25 int main()
bridadan 14:c47437f5dae8 26 {
bridadan 14:c47437f5dae8 27 printf("NetworkSocketAPI Example\r\n");
sam_grove 4:cb8a17dd6746 28
sam_grove 2:7283ce112304 29 wifi.init();
sam_grove 6:751245e265f6 30 wifi.connect("apname", "phrase", WI_WPA2);
sam_grove 6:751245e265f6 31
bridadan 14:c47437f5dae8 32 printf("Wifi is %s\r\n", (wifi.isConnected() > 0) ? "Connected" : "Disconnected");
sam_grove 4:cb8a17dd6746 33
bridadan 14:c47437f5dae8 34 printf("IP: %s\r\n", wifi.getIPAddress());
bridadan 14:c47437f5dae8 35 printf("Gateway: %s\r\n", wifi.getGateway());
bridadan 14:c47437f5dae8 36 printf("Mask: %s\r\n", wifi.getNetworkMask());
bridadan 14:c47437f5dae8 37 printf("MAC: %s\r\n", wifi.getMACAddress());
bridadan 14:c47437f5dae8 38
bridadan 14:c47437f5dae8 39 SocketInterface* myTCPSocket = wifi.allocateSocket(SOCK_TCP);
bridadan 14:c47437f5dae8 40 SocketInterface* myUDPSocket = wifi.allocateSocket(SOCK_TCP);
sam_grove 9:da5a230fa4f4 41
bridadan 14:c47437f5dae8 42 myTCPSocket->setAddress(myTCPSocket->getHostByName("www.google.com"));
bridadan 14:c47437f5dae8 43 myTCPSocket->setAddress("216.58.192.46");
bridadan 14:c47437f5dae8 44 myTCPSocket->setPort(80);
bridadan 14:c47437f5dae8 45
bridadan 14:c47437f5dae8 46 myTCPSocket->close();
sam_grove 9:da5a230fa4f4 47
bridadan 14:c47437f5dae8 48 myUDPSocket->setAddress(myUDPSocket->getHostByName("www.google.com"));
bridadan 14:c47437f5dae8 49 myUDPSocket->setAddress("216.58.192.46");
bridadan 14:c47437f5dae8 50 myUDPSocket->setPort(80);
bridadan 14:c47437f5dae8 51
bridadan 14:c47437f5dae8 52 myUDPSocket->close();
bridadan 14:c47437f5dae8 53
sam_grove 2:7283ce112304 54 wifi.disconnect();
bridadan 14:c47437f5dae8 55
sam_grove 0:1984a177ff56 56 while(1) {
sam_grove 2:7283ce112304 57 myled = !myled;
sam_grove 0:1984a177ff56 58 }
sam_grove 0:1984a177ff56 59 }