7 years, 1 month ago.

Why do TCPSocketWiFi and HelloESP8266Interface share D1/D0 to send both debug and AT commands ?

Usually we will use one UART for debug, another UART to interface with MODEM (Wi-Fi or Cellular). In some situations, we will use semihost to print out message, for debug purpose.

However, when I try to debug code with two ESP8266 related projects, I found both debug and Wi-Fi connections are all through D1/D0 ?

  1. https://os.mbed.com/teams/components/code/HelloESP8266Interface/
  2. https://docs.mbed.com/docs/mbed-os-api-reference/en/latest/APIs/communication/wifi/

I pasted two main.cpp here, and you will find:

ESP8266Interface wifi(D1, D0);

are used in both files. And most of the debug information are via printf().

And I running these projects on NUCLEO board, connect to TeraTerm in my PC, and I will find both debug printf() and AT commands are showed on terminal.

TeraTerm Session Snapshot

WiFi example

Scan:
AT+CWLAP
AT+CWLAP
0 networks available.

Connecting...
AT+RST
AT+RST

Connection error

"WiFi example/Scan/0 networks avaiable" messages are send from D1 via printf(), and I double checked with source, "AT+CWLAP/AT+RST“ are only send from D1 via _parser.send("AT+CWLAP=") and other _parse.send() in ESP8266.cpp.

So, Why do these projects share same UART (D1/D0) for both debug and ESP8266?

In my understanding, sending both debug and AT commands to UART should get a lot of errors from ESP8266 ?

main.cpp from HelloESP8266Interface

#include "mbed.h"
#include "ESP8266Interface.h"
#include "TCPSocket.h"
 
ESP8266Interface wifi(D1, D0);
 
DigitalOut led(LED_GREEN);
void blink()
{
    led = !led;
}
 
int main()
{
    Ticker blinky;
    blinky.attach(blink, 0.4f);
 
    printf("NetworkSocketAPI Example\r\n");
 
    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");
    
    SocketAddress addr(&wifi, "mbed.org");
    printf("mbed.org resolved to: %s\r\n", addr.get_ip_address());
 
    TCPSocket socket(&wifi);
    socket.connect("4.ifcfg.me", 23);
 
    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("Done\r\n");
}
 

“main.cpp from TCPSocketWiFi_example”

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

#if TARGET_UBLOX_EVK_ODIN_W2
#include "OdinWiFiInterface.h"
OdinWiFiInterface wifi;
#else
#if !TARGET_FF_ARDUINO
#error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time
#endif
#include "ESP8266Interface.h"
ESP8266Interface wifi(D1, D0);
#endif

const char *sec2str(nsapi_security_t sec)
{
    switch (sec) {
        case NSAPI_SECURITY_NONE:
            return "None";
        case NSAPI_SECURITY_WEP:
            return "WEP";
        case NSAPI_SECURITY_WPA:
            return "WPA";
        case NSAPI_SECURITY_WPA2:
            return "WPA2";
        case NSAPI_SECURITY_WPA_WPA2:
            return "WPA/WPA2";
        case NSAPI_SECURITY_UNKNOWN:
        default:
            return "Unknown";
    }
}

void scan_demo(WiFiInterface *wifi)
{
    WiFiAccessPoint *ap;

    printf("Scan:\r\n");

    int count = wifi->scan(NULL,0);

    /* Limit number of network arbitrary to 15 */
    count = count < 15 ? count : 15;

    ap = new WiFiAccessPoint[count];
    count = wifi->scan(ap, count);
    for (int i = 0; i < count; i++)
    {
        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
    }
    printf("%d networks available.\r\n", count);

    delete[] ap;
}

void http_demo(NetworkInterface *net)
{
    TCPSocket socket;

    printf("Sending HTTP request to www.arm.com...\r\n");

    // Open a socket on the network interface, and create a TCP connection to www.arm.com
    socket.open(net);
    socket.connect("www.arm.com", 80);

    // Send a simple http request
    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
    int scount = socket.send(sbuffer, sizeof sbuffer);
    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);

    // Recieve a simple http response and print out the response line
    char rbuffer[64];
    int rcount = socket.recv(rbuffer, sizeof rbuffer);
    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);

    // Close the socket to return its memory and bring down the network interface
    socket.close();
}

int main()
{
    printf("WiFi example\r\n\r\n");

    scan_demo(&wifi);

    printf("\r\nConnecting...\r\n");
    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
    if (ret != 0) {
        printf("\r\nConnection error\r\n");
        return -1;
    }

    printf("Success\r\n\r\n");
    printf("MAC: %s\r\n", wifi.get_mac_address());
    printf("IP: %s\r\n", wifi.get_ip_address());
    printf("Netmask: %s\r\n", wifi.get_netmask());
    printf("Gateway: %s\r\n", wifi.get_gateway());
    printf("RSSI: %d\r\n\r\n", wifi.get_rssi());

    http_demo(&wifi);

    wifi.disconnect();

    printf("\r\nDone\r\n");
}

1 Answer

7 years, 1 month ago.

The standard Arduino headers have UART on D0/D1, that's why many example programs use these pins. However, on NUCLEO boards (as you noted) D0/D1 is used for USB communications with the host. Change the code to use D2/D8 instead (another UART bus on NUCLEO boards) to mitigate the issue.