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.

Branch:
api-changes
Revision:
30:f80540b6e2db
Parent:
27:ccedb4ad0033
Child:
32:28a909a3748e
--- a/main.cpp	Tue Feb 23 18:21:23 2016 +0000
+++ b/main.cpp	Mon Feb 22 23:58:09 2016 -0600
@@ -16,11 +16,13 @@
 
 #include "mbed.h"
 #include "ESP8266Interface.h"
+#include "TCPSocket.h"
 
 DigitalOut myled(LED1);
-void flash(){myled = !myled;}
+void flash(){ myled = !myled; }
 
 ESP8266Interface wifi(D1, D0);
+TCPSocket socket(&wifi);
 
 int main()
 {
@@ -28,28 +30,24 @@
     t.attach(flash, 0.4f);
     printf("NetworkSocketAPI Example\r\n");
 
-    wifi.init();
     wifi.connect("WifiDemo", "");
     
-    char* ip = wifi.getIPAddress();
-    printf("IP Address is: %s\n", (ip) ? ip : "No IP");
-    char host_ip[50];
-    wifi.getHostByName("time-a.nist.gov", host_ip);
-    printf("time-a.nist.gov resolved to: %s\n", host_ip);
+    const char *ip = wifi.getIPAddress();
+    const char *mac = wifi.getMACAddress();
+    printf("IP Address is: %s\r\n", (ip) ? ip : "No IP");
+    printf("MAC Address is: %s\r\n", (mac) ? mac : "No MAC");
 
-    SocketInterface* mySocket = wifi.allocateSocket(SOCK_TCP);
+    socket.open("time-a.nist.gov", 37);
+    printf("time-a.nist.gov resolved to: %s\r\n", socket.getIPAddress());
+
     char recieved[100] = {0};
-    int recv_amnt = 0;
-    
-    //Sending and receiving from echo server
-    mySocket->setAddressPort(host_ip, 37);
-    mySocket->open();
-    //mySocket->send("xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx", 50, 10000);
-    recv_amnt = mySocket->recv(recieved, sizeof(recieved), 10000);
-    mySocket->close();
+    int32_t size = 0;
+    size = socket.recv(recieved, sizeof(recieved));
+
+    socket.close();
     wifi.disconnect();
-    
-    printf("Recieved: %d bytes, %02x%02x%02x%02x\n", recv_amnt, recieved[0], recieved[1], recieved[2], recieved[3]);
+
+    printf("Recieved: %ld bytes, %02x%02x%02x%02x\r\n", size,
+            recieved[0], recieved[1], recieved[2], recieved[3]);
     printf("NetworkSocketAPI Example Finished\r\n");
-    while(1);
 }