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.

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Thu Apr 21 06:14:45 2016 -0500
Parent:
53:99fe1d0e81fa
Commit message:
Updated to match changes to the NSAPI

Changed in this revision

ESP8266Interface.lib Show annotated file Show diff for this revision Revisions of this file
NetworkSocketAPI.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 99fe1d0e81fa -r 4b14327faaaf ESP8266Interface.lib
--- a/ESP8266Interface.lib	Fri Apr 15 23:59:04 2016 +0000
+++ b/ESP8266Interface.lib	Thu Apr 21 06:14:45 2016 -0500
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/components/code/ESP8266Interface/#d6244089a657
+https://developer.mbed.org/teams/components/code/ESP8266Interface/#34829ec3a3da
diff -r 99fe1d0e81fa -r 4b14327faaaf NetworkSocketAPI.lib
--- a/NetworkSocketAPI.lib	Fri Apr 15 23:59:04 2016 +0000
+++ b/NetworkSocketAPI.lib	Thu Apr 21 06:14:45 2016 -0500
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#aa343098aa61
+http://developer.mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#96627c4b83d5
diff -r 99fe1d0e81fa -r 4b14327faaaf main.cpp
--- a/main.cpp	Fri Apr 15 23:59:04 2016 +0000
+++ b/main.cpp	Thu Apr 21 06:14:45 2016 -0500
@@ -18,36 +18,39 @@
 #include "ESP8266Interface.h"
 #include "TCPSocket.h"
 
-DigitalOut myled(LED_GREEN);
-void flash(){ myled = !myled; }
+ESP8266Interface wifi(D1, D0);
 
-ESP8266Interface wifi(D1, D0);
-TCPSocket socket(&wifi);
+DigitalOut led(LED_GREEN);
+void blink()
+{
+    led = !led;
+}
 
 int main()
 {
-    Ticker t;
-    t.attach(flash, 0.4f);
+    Ticker blinky;
+    blinky.attach(blink, 0.4f);
+
     printf("NetworkSocketAPI Example\r\n");
 
-    wifi.connect("ssid", "password");
+    wifi.connect("Sniffer", "Sandcastle");
+    const char *ip = wifi.get_ip_address();
+    const char *mac = wifi.get_mac_address();
+    printf("IP address is: %s\r\n", ip ? ip : "No IP");
+    printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
     
-    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");
+    SocketAddress addr(&wifi, "mbed.org");
+    printf("mbed.org resolved to: %s\r\n", addr.get_ip_address());
 
-    socket.open("time-a.nist.gov", 37);
-    printf("time-a.nist.gov resolved to: %s\r\n", socket.getIPAddress());
+    TCPSocket socket(&wifi);
+    socket.connect("4.ifcfg.me", 23);
 
-    char recieved[100] = {0};
-    int32_t size = 0;
-    size = socket.recv(recieved, sizeof(recieved));
-
+    char buffer[64];
+    int count = socket.recv(buffer, sizeof buffer);
+    printf("public IP address is: %.15s\r\n", &buffer[15]);
+    
     socket.close();
     wifi.disconnect();
 
-    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");
+    printf("Done\r\n");
 }