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:
sam_grove
Date:
Thu Jul 16 05:20:12 2015 +0000
Revision:
15:e48b3c6558b1
Parent:
14:c47437f5dae8
Child:
16:bb0a5b830dc5
small formatting updates

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"
sam_grove 0:1984a177ff56 19
sam_grove 0:1984a177ff56 20 DigitalOut myled(LED1);
sam_grove 0:1984a177ff56 21
bridadan 14:c47437f5dae8 22 WiFiRadioInterface wifi(D1, D0);
sam_grove 2:7283ce112304 23
sam_grove 0:1984a177ff56 24 int main()
bridadan 14:c47437f5dae8 25 {
bridadan 14:c47437f5dae8 26 printf("NetworkSocketAPI Example\r\n");
sam_grove 4:cb8a17dd6746 27
sam_grove 2:7283ce112304 28 wifi.init();
sam_grove 6:751245e265f6 29 wifi.connect("apname", "phrase", WI_WPA2);
sam_grove 6:751245e265f6 30
bridadan 14:c47437f5dae8 31 printf("Wifi is %s\r\n", (wifi.isConnected() > 0) ? "Connected" : "Disconnected");
sam_grove 4:cb8a17dd6746 32
bridadan 14:c47437f5dae8 33 printf("IP: %s\r\n", wifi.getIPAddress());
bridadan 14:c47437f5dae8 34 printf("Gateway: %s\r\n", wifi.getGateway());
bridadan 14:c47437f5dae8 35 printf("Mask: %s\r\n", wifi.getNetworkMask());
bridadan 14:c47437f5dae8 36 printf("MAC: %s\r\n", wifi.getMACAddress());
bridadan 14:c47437f5dae8 37
bridadan 14:c47437f5dae8 38 SocketInterface* myTCPSocket = wifi.allocateSocket(SOCK_TCP);
sam_grove 15:e48b3c6558b1 39 SocketInterface* myUDPSocket = wifi.allocateSocket(SOCK_UDP);
sam_grove 9:da5a230fa4f4 40
bridadan 14:c47437f5dae8 41 myTCPSocket->setAddress(myTCPSocket->getHostByName("www.google.com"));
bridadan 14:c47437f5dae8 42 myTCPSocket->setAddress("216.58.192.46");
bridadan 14:c47437f5dae8 43 myTCPSocket->setPort(80);
bridadan 14:c47437f5dae8 44
bridadan 14:c47437f5dae8 45 myTCPSocket->close();
sam_grove 9:da5a230fa4f4 46
bridadan 14:c47437f5dae8 47 myUDPSocket->setAddress(myUDPSocket->getHostByName("www.google.com"));
bridadan 14:c47437f5dae8 48 myUDPSocket->setAddress("216.58.192.46");
bridadan 14:c47437f5dae8 49 myUDPSocket->setPort(80);
bridadan 14:c47437f5dae8 50
bridadan 14:c47437f5dae8 51 myUDPSocket->close();
bridadan 14:c47437f5dae8 52
sam_grove 2:7283ce112304 53 wifi.disconnect();
bridadan 14:c47437f5dae8 54
sam_grove 0:1984a177ff56 55 while(1) {
sam_grove 2:7283ce112304 56 myled = !myled;
sam_grove 0:1984a177ff56 57 }
sam_grove 0:1984a177ff56 58 }