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:
geky
Date:
Tue Feb 02 22:22:15 2016 +0000
Revision:
26:4242277cf9c7
Parent:
25:eef7d93d9f5b
Child:
27:ccedb4ad0033
Reverted "Updated AT firmware version"

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 24:471a07e886ae 28 t.attach(flash, 0.4f);
bridadan 14:c47437f5dae8 29 printf("NetworkSocketAPI Example\r\n");
sam_grove 4:cb8a17dd6746 30
sam_grove 2:7283ce112304 31 wifi.init();
geky 26:4242277cf9c7 32 wifi.connect("Lakehouse", "Sandcastle");
sarahmarshy 16:bb0a5b830dc5 33
sam_grove 24:471a07e886ae 34 char* ip = wifi.getIPAddress();
sam_grove 20:4cb9ef3b0cc9 35 printf("IP Address is: %s\n", (ip) ? ip : "No IP");
sarahmarshy 22:1d355289fc18 36 char host_ip[50];
sam_grove 24:471a07e886ae 37 wifi.getHostByName("time-a.nist.gov", host_ip);
sam_grove 24:471a07e886ae 38 printf("time-a.nist.gov resolved to: %s\n", host_ip);
sarahmarshy 22:1d355289fc18 39
sarahmarshy 22:1d355289fc18 40 SocketInterface* mySocket = wifi.allocateSocket(SOCK_TCP);
sam_grove 24:471a07e886ae 41 char recieved[100] = {0};
sam_grove 20:4cb9ef3b0cc9 42 int recv_amnt = 0;
bridadan 14:c47437f5dae8 43
sarahmarshy 22:1d355289fc18 44 //Sending and receiving from echo server
geky 26:4242277cf9c7 45 //mySocket->setAddressPort("129.6.15.28", 37);
sam_grove 24:471a07e886ae 46 mySocket->setAddressPort(host_ip, 37);
sarahmarshy 17:72de842c27b4 47 mySocket->open();
sam_grove 24:471a07e886ae 48 //mySocket->send("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx", 50, 10000);
sam_grove 24:471a07e886ae 49 recv_amnt = mySocket->recv(recieved, sizeof(recieved), 10000);
sarahmarshy 17:72de842c27b4 50 mySocket->close();
sam_grove 2:7283ce112304 51 wifi.disconnect();
sam_grove 24:471a07e886ae 52
sam_grove 24:471a07e886ae 53 printf("Recieved: %d bytes, %02x%02x%02x%02x\n", recv_amnt, recieved[0], recieved[1], recieved[2], recieved[3]);
sam_grove 24:471a07e886ae 54 printf("NetworkSocketAPI Example Finished\r\n");
sam_grove 24:471a07e886ae 55 while(1);
sam_grove 0:1984a177ff56 56 }