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:
sarahmarshy
Date:
Wed Aug 05 21:59:08 2015 +0000
Revision:
22:1d355289fc18
Parent:
20:4cb9ef3b0cc9
Child:
24:471a07e886ae
DNS example in main

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"
sarahmarshy 16:bb0a5b830dc5 18 #include "ESP8266Interface.h"
sam_grove 0:1984a177ff56 19
sam_grove 0:1984a177ff56 20 DigitalOut myled(LED1);
sam_grove 20:4cb9ef3b0cc9 21 void flash(){myled = !myled;}
sam_grove 0:1984a177ff56 22
sarahmarshy 16:bb0a5b830dc5 23 ESP8266Interface wifi(D1, D0);
sam_grove 2:7283ce112304 24
sam_grove 0:1984a177ff56 25 int main()
sam_grove 20:4cb9ef3b0cc9 26 {
sam_grove 20:4cb9ef3b0cc9 27 Ticker t;
sam_grove 20:4cb9ef3b0cc9 28 t.attach(flash, 0.2f);
bridadan 14:c47437f5dae8 29 printf("NetworkSocketAPI Example\r\n");
sam_grove 4:cb8a17dd6746 30
sam_grove 2:7283ce112304 31 wifi.init();
sarahmarshy 22:1d355289fc18 32 wifi.connect("iotlab", "42F67YxLX4AawRdcj");
sarahmarshy 16:bb0a5b830dc5 33
sarahmarshy 18:a5830a6d3e11 34 char* ip;
sarahmarshy 18:a5830a6d3e11 35 ip = wifi.getIPAddress();
sam_grove 20:4cb9ef3b0cc9 36 printf("IP Address is: %s\n", (ip) ? ip : "No IP");
sarahmarshy 22:1d355289fc18 37 char host_ip[50];
sarahmarshy 22:1d355289fc18 38 wifi.getHostByName("google.com",host_ip);
sarahmarshy 22:1d355289fc18 39 printf("Google.com resolved to: %s\n",host_ip);
sam_grove 9:da5a230fa4f4 40
sarahmarshy 22:1d355289fc18 41
sarahmarshy 22:1d355289fc18 42 SocketInterface* mySocket = wifi.allocateSocket(SOCK_TCP);
sam_grove 20:4cb9ef3b0cc9 43 char recieved[100];
sam_grove 20:4cb9ef3b0cc9 44 int recv_amnt = 0;
bridadan 14:c47437f5dae8 45
sarahmarshy 22:1d355289fc18 46 //Sending and receiving from echo server
sarahmarshy 22:1d355289fc18 47 mySocket->setAddressPort("192.168.13.8", 7);
sarahmarshy 17:72de842c27b4 48 mySocket->open();
sarahmarshy 17:72de842c27b4 49 mySocket->send("Hello",5, 10000);
sarahmarshy 17:72de842c27b4 50 recieved[5] = 0;
sarahmarshy 17:72de842c27b4 51 recv_amnt = mySocket->recv(recieved, 5, 10000);
sam_grove 20:4cb9ef3b0cc9 52 printf("Recieved: %d, %s\n",recv_amnt, recieved);
sarahmarshy 17:72de842c27b4 53 mySocket->close();
sam_grove 2:7283ce112304 54 wifi.disconnect();
sam_grove 0:1984a177ff56 55 }