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.

main.cpp

Committer:
Christopher Haster
Date:
2016-02-25
Revision:
41:83cf71e499ee
Parent:
40:6a90de1da5e2
Parent:
37:4d946ea769fd
Child:
42:d8ea089e5606

File content as of revision 41:83cf71e499ee:

/* NetworkSocketAPI Example Program
 * Copyright (c) 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "ESP8266Interface.h"
#include "TCPSocket.h"

DigitalOut myled(LED_GREEN);
void flash(){ myled = !myled; }

ESP8266Interface wifi(D1, D0);
TCPSocket socket(&wifi);

int main()
{
    Ticker t;
    t.attach(flash, 0.4f);
    printf("NetworkSocketAPI Example\r\n");

    wifi.connect("ssid", "password");
    
    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");

    socket.open("time-a.nist.gov", 37);
    printf("time-a.nist.gov resolved to: %s\r\n", socket.getIPAddress());

    char recieved[100] = {0};
    int32_t size = 0;
    size = socket.recv(recieved, sizeof(recieved));

    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");
}