# 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](images/ConnectWiFi/Wifi_01.png)

## 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](images/ConnectWiFi/Wifi_02.png)

b) Setup your mobile phone Hotspot

* make sure your phone has internet service.

![auto_fit](images/ConnectWiFi/Wifi_03.png)

For android:

![auto_fit](images/ConnectWiFi/Wifi_03a.png)

For iPhone:

![auto_fit](images/ConnectWiFi/Wifi_03b.png)

## 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](images/ConnectWiFi/Wifi_p1.png)

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](images/ConnectWiFi/Wifi_p2.png)

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](images/ConnectWiFi/Wifi_p3.png)

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](images/ConnectWiFi/Wifi_p3_5.png)

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](images/ConnectWiFi/Wifi_p5.png)

Full Solution

MakeCode: [https://makecode.microbit.org/S14324-29481-76234-16745](https://makecode.microbit.org/S14324-29481-76234-16745)

You could also download the program from the following website:
## Result


Step 1

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

. ![auto_fit](images/ConnectWiFi/Wifi_result1.png)

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](images/ConnectWiFi/Wifi_result2.png)

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”.
## 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](https://smarthon-docs-en.readthedocs.io/en/latest/smartcity/2_IOTbit.html#best-practice) on using the iot:bit You may get more detailed information on error codes from [NodeMCU Documentation](https://nodemcu.readthedocs.io/en/dev-esp32/modules/wifi/) ![auto_fit](images/ConnectWiFi/Wifi_result3.png)