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 Jun 18 15:25:36 2015 +0000
Revision:
11:b7e531a4d661
Parent:
9:da5a230fa4f4
Child:
14:c47437f5dae8
change wifi->connect to wifi->open for sockets

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
sam_grove 7:6787b920b496 22 WiFiRadioInterface wifi;
sam_grove 2:7283ce112304 23 // or
sam_grove 2:7283ce112304 24 //NetworkInterface *wifi = new WiFiInterface;
sam_grove 2:7283ce112304 25
sam_grove 4:cb8a17dd6746 26 //TCPSocket tcp(wifi.getInstance());
sam_grove 2:7283ce112304 27 // or
sam_grove 2:7283ce112304 28 //SocketInterface tcp = new TCPSocket(wifi->getInstance();
sam_grove 2:7283ce112304 29
sam_grove 4:cb8a17dd6746 30 //UDPSocket udp(wifi.getInstance());
sam_grove 2:7283ce112304 31 // or
sam_grove 2:7283ce112304 32 //SocketInterface udp = new UDPSocket(wifi->getInstance();
sam_grove 2:7283ce112304 33
sam_grove 0:1984a177ff56 34 int main()
sam_grove 0:1984a177ff56 35 {
sam_grove 2:7283ce112304 36 puts("NetworkSocketAPI Example");
sam_grove 4:cb8a17dd6746 37
sam_grove 2:7283ce112304 38 wifi.init();
sam_grove 6:751245e265f6 39 wifi.connect("apname", "phrase", WI_WPA2);
sam_grove 6:751245e265f6 40
sam_grove 4:cb8a17dd6746 41 printf("Wifi is %s\n", (wifi.isConnected() > 0) ? "Connected" : "Disconnected");
sam_grove 4:cb8a17dd6746 42
sam_grove 2:7283ce112304 43 printf("IP: %s\n", wifi.getIPAddress());
sam_grove 2:7283ce112304 44 printf("Gateway: %s\n", wifi.getGateway());
sam_grove 2:7283ce112304 45 printf("Mask: %s\n", wifi.getNetworkMask());
sam_grove 2:7283ce112304 46 printf("MAC: %s\n", wifi.getMACAddress());
sam_grove 9:da5a230fa4f4 47
sam_grove 9:da5a230fa4f4 48 //Endpoint google;
sam_grove 9:da5a230fa4f4 49 wifi.set_address(wifi.get_host_by_name("www.google.com"));
sam_grove 9:da5a230fa4f4 50 wifi.set_address("216.58.192.46");
sam_grove 9:da5a230fa4f4 51 wifi.set_port(80);
sam_grove 9:da5a230fa4f4 52
sam_grove 9:da5a230fa4f4 53 //Socket socket;
sam_grove 9:da5a230fa4f4 54 char buf[1024];
sam_grove 11:b7e531a4d661 55 wifi.open(*wifi.endpoint);
sam_grove 9:da5a230fa4f4 56 wifi.recv(buf, 1024);
sam_grove 9:da5a230fa4f4 57 wifi.send(buf, sizeof(buf));
sam_grove 9:da5a230fa4f4 58 wifi.close(*wifi.endpoint);
sam_grove 2:7283ce112304 59
sam_grove 2:7283ce112304 60 wifi.disconnect();
sam_grove 4:cb8a17dd6746 61
sam_grove 0:1984a177ff56 62 while(1) {
sam_grove 2:7283ce112304 63 myled = !myled;
sam_grove 0:1984a177ff56 64 }
sam_grove 0:1984a177ff56 65 }