Micro:bit - Getting On WiFi
Written by Harry Fairhead   
Monday, 11 July 2022
Article Index
Micro:bit - Getting On WiFi
AT Commands
Utilities
Configuring WiFi
A Web Server
Full Listing
Summary

Program Listing

Putting all this together with a main program:

#include "MicroBit.h"
MicroBit uBit;
void initWiFi();
int ATWiFi();
int find(ManagedString c, ManagedString s);
void debug(ManagedString s);
//Comment out for V2
#define Tx MICROBIT_PIN_P0
#define Rx MICROBIT_PIN_P1
//
/* Uncomment For V2
#define baud setBaudrate
#define USBTX uBit.io.usbTx
#define USBRX uBit.io.usbRx
#define Tx  uBit.io.P0
#define Rx  uBit.io.P1
*/
#define DEBUG 1
int main() {
    initWiFi();
    ATWiFi();
    while (1)uBit.sleep(1000);
    release_fiber();
}
void initWiFi() {
    uBit.serial.redirect(Tx, Rx);
    uBit.serial.baud(115200);
    uBit.serial.setRxBufferSize((uint8_t)+500);
}
int ATWiFi() {
    uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
    uBit.sleep(150);
    ManagedString s = uBit.serial.read(500, ASYNC);
    if (DEBUG)debug("\n\rAT \n\r" + s + "\n\r");
    return find("OK", s);
}
void debug(ManagedString s) {
    uBit.serial.redirect(USBTX, USBRX);
    uBit.serial.send(s, SYNC_SPINWAIT);
    uBit.serial.redirect(Tx, Rx);
}
int find(ManagedString c, ManagedString s) {
    int i;
    for (i = 0; i < (s.length() - c.length()); i++) {
        if (c == s.substring(i, c.length())) break;
    }
    if (i == (s.length() - c.length())) return 0;
    return 1;
}

When you run this program then you should see:

AT
OK

printed on the serial console.

If you don't there are five possible reasons:

  1. You have connected the wrong pins - check

  2. The power supply you are using is inadequate - check/replace

  3. The serial console isn't working - check you can see any message

  4. The baud rate is wrong - try 9600

  5. The ESP8266 is broken - try another

Some Utility Functions

Before we get into the details of connecting to a network and transferring data, it is worth constructing some useful utility functions.

The need to wait for an OK or similar response is so common that it is worth having a function to do the job:

int  waitForWiFi(ManagedString target,
int retry,int pause){ ManagedString s; do { uBit.sleep(pause); if(s.length()>500)s=""; s = s + uBit.serial.read(500, ASYNC); retry--; } while (find(target, s) == 0 && retry != 0); if (DEBUG)debug("\n\r" + s + "\n\r"); return retry; }

Notice that now we can specify the target, a pause in milliseconds between polling attempts, and a timeout in terms of how many retries before the function gives up. Notice that the function returns 0 if there was a failure and the retry loop timed out and the number of retries remaining otherwise.

Using this utility function the ATWiFi function can be written:

int ATWiFi() {
   uBit.serial.send("AT\r\n", SYNC_SPINWAIT);
   return waitForWiFi("OK",150,10);
}

This is a lot shorter and easier to understand.

Assuming that you have managed to make the AT command work, it is time to move on to other AT commands. The first useful command is to find the version number of the firmware. This is more or less the same as the AT function, but the command is AT+GMR and we get back more data - hence the longer wait time:

int getVersionWiFi() {
   uBit.serial.send("AT+GMR\r\n", SYNC_SPINWAIT);
   return waitForWiFi("OK",200,10);
}

The device in use is returned:

AT+GMR
AT version:0.60.0.0(Jan 29 2016 15:10:17)
SDK version:1.5.2(7eee54f4)
Ai-Thinker Technology Co. Ltd.
May  5 2016 17:30:30
OK

The manual says that it should return AT, SDK and the time it was compiled, so we get a little extra with this third-party device.

Another useful function initiates a reset. The device often gets stuck and then a reset is all that you can try. This is a software reset; another option is to use a GPIO line to connect to reset pin 6. By controlling this line you can force the device to hard reset. The soft reset command is AT+RST and all you get back in theory is OK, but in practice the device sends a few hundred bytes of configuration data:

int resetWiFi() {
    uBit.serial.send("AT+RST\r\n", SYNC_SPINWAIT);
    return waitForWiFi("OK", 1000, 10);
}

 

The final utility function is to set the serial connection parameters to 115200 baud, 8 bits, 1 stop bit, no parity, no flow control:

int setUARTWiFi() {
 uBit.serial.send("AT+UART_CUR=115200,8,1,0,0\r\n",
SYNC_SPINWAIT); return waitForWiFi("OK", 200, 10); }

If you change the baud rate to something other than what is in use you will, of course, lose communication with the device until you reconfigure the micro:bit's serial connection.



Last Updated ( Tuesday, 12 July 2022 )