4. First step: Connect the micro:bit to the Wi-Fi

Connecting Wi-Fi is the very first step to access internet services, including ThingSpeak, IFTTT, email, etc. Before you start, make sure your micro:bit device is connected to the Internet.

auto_fit

4.1. Open the Home Router or Mobile Phone Hotspot


Please sure there is a Wi-Fi signal (with 2.4GHz) available in your area.
*5GHz Wi-Fi signal is not supported

You can either setup a home router or a Wi-Fi hotspot from your mobile phone.

a) Setup your home/office Wi-Fi

auto_fit

b) Setup your mobile phone Hotspot

* make sure your phone has internet service.

auto_fit

For android:

auto_fit

For iPhone:

auto_fit

4.2. Programming (Makecode)


Step 1: Initialize the IoT:bit

Before you start, you need to initialize the IoT:bit.

  • Go to IoT:bit tab.

  • Snap Initialize IoT:bit TX P16 RX P8 to on start auto_fit

Step 2: Connect to the Wi-Fi

After initializing, you can connect to the Wi-Fi

  • Go to IoT:bit tab

  • Snap Set Wi-Fi to ssid pwd to on start following initialize

  • Enter your Wi-Fi name and password. Here we set smarthon as SSID and “12345678” as password. auto_fit

Step 3: Show the Tick if it is connected

On WiFi connected is an event handler. It will be triggered once after connected with WiFi. After the WiFi is connected, it provide two variable IP_Address and Device_ID for you to control the IoT:bit though network.

  • Snap On WiFi connected to the stage

  • On Basic tab, snap show icon heart inside On WiFi connected

  • Swtich the heart to tick

auto_fit

Step 4: Show you IP

If you want to see the IP address, you may show it on the OLED display.

  • Go to OLED tab

  • Snap “initialize OLED with width…height..” to on start

  • Snap the “show string“ inside the On WiFi connected

  • Draw the variable from On WiFi connected to the “show string” block placeholder

  • You may control the text format by the function in Text tab

auto_fit

Optional:
On WiFi disconnected handler will be triggered when the WiFi connection is loss, it may useful when you get disconnection to the WiFi Station during the application running.

auto_fit

Full Solution

MakeCode: https://makecode.microbit.org/_53Yf8qcx521m

You could also download the program from the following website:

4.3. Result


Step 1

Load the program to the micro:bit and turn on the IoT:bit by using USB

. auto_fit

Step 2

Wi-Fi IoT:bit will start to connect to internet, when the connection is successful, the IP Address would be shown. AND the tick icon will be shown.

auto_fit

After the IoT:bit is connected to the wifi, when disconnect to the router. It will show “cross” symbol and the error code will be shown. Normally, the code 201 means “No AP Found”.

4.4. Reasons might cause unable to connect or disconnect:

  1. Router is too far away

  2. No that AP actually

  3. The typo on SSID/PWD

  4. The battery have run out, please change to new battery

  5. Not using 2.4Ghz frequency Wi-Fi channel

  6. Follow the best practice on using the iot:bit

You may get more detailed information on error codes from NodeMCU Documentation auto_fit