# Control your nightlight over the Internet - Wio Terminal
The IoT device needs to be coded to communicate with *test.mosquitto.org* using MQTT to send telemetry values with the light sensor reading, and receive commands to control the LED.
In this part of the lesson, you will connect your Wio Terminal to an MQTT broker.
To communicate with the MQTT broker, you need to install some Arduino libraries to use the WiFi chip in the Wio Terminal, and communicate with MQTT. When developing for Arduino devices, you can use a wide range of libraries that contain open-source code and implement a huge range of capabilities. Seeed publishes libraries for the Wio Terminal that allows it to communicate over WiFi. Other developers have published libraries to communicate with MQTT brokers, and you will be using these with your device.
These libraries are provided as source code that can be imported automatically into PlatformIO and compiled for your device. This way Arduino libraries will work on any device that supports the Arduino framework, assuming that the device has any specific hardware needed by that library. Some libraries, such as the Seeed WiFi libraries, are specific to certain hardware.
Libraries can be installed globally and compiled if needed, or into a specific project. For this assignment, the libraries will be installed into the project.
✅ You can learn more about library management and how to find and install libraries in the [PlatformIO library documentation](https://docs.platformio.org/en/latest/librarymanager/index.html).
> 💁 You can remove the `@ <number>` to always use the latest version of the libraries, but there are no guarantees the later versions will work with the code below. The code here has been tested with this version of the libraries.
This is all you need to do to add the libraries. Next time PlatformIO builds the project it will download the source code for these libraries and compile it into your project.
1. Add the following to the `lib_deps`:
```ini
knolleary/PubSubClient @ 2.8
```
This imports [PubSubClient](https://github.com/knolleary/pubsubclient), an Arduino MQTT client
1. Create a new file in the `src` folder called `config.h`. You can do this by selecting the `src` folder, or the `main.cpp` file inside, and selecting the **New file** button from the explorer. This button only appears when your cursor is over the explorer.
![The new file button](../../../images/vscode-new-file-button.png)
1. Add the following code to this file to define constants for your WiFi credentials:
```cpp
#pragma once
#include<string>
using namespace std;
// WiFi credentials
const char *SSID = "<SSID>";
const char *PASSWORD = "<PASSWORD>";
```
Replace `<SSID>` with the SSID of your WiFi. Replace `<PASSWORD>` with your WiFi password.
1. Open the `main.cpp` file
1. Add the following `#include` directives to the top of the file:
This includes header files for the libraries you added earlier, as well as the config header file. These header files are needed to tell PlatformIO to bring in the code from the libraries. Without explicitly including these header files, some code won't be compiled in and you will get compiler errors.
Replace `<ID>` with a unique ID that will be used the name of this device client, and later for the topics that this device publishes and subscribes to. The *test.mosquitto.org* broker is public and used by many people, including other students working through this assignment. Having a unique MQTT client name and topic names ensures your code won't clash with anyone else's. You will also need this ID when you are creating the server code later in this assignment.
This function tests the connection to the MQTT broker and reconnects if it is not connected. It loops all the time it is not connected and attempts to connect using the unique client name defined in the config header file.
If the connection fails, it retries after 5 seconds.
1. Add the following code below the `reconnectMQTTClient` function:
```cpp
void createMQTTClient()
{
client.setServer(BROKER.c_str(), 1883);
reconnectMQTTClient();
}
```
This code sets the MQTT broker for the client, as well as setting up the callback when a message is received. It then attempts to connect to the broker.
1. Call the `createMQTTClient` function in the `setup` function after the WiFi is connected.
1. Replace the entire `loop` function with the following:
```cpp
void loop()
{
reconnectMQTTClient();
client.loop();
delay(2000);
}
```
This code starts by reconnecting to the MQTT broker. These connections can be broken easily, so it's worth regularly checking and reconnecting if necessary. It then calls the `loop` method on the MQTT client to process any messages that are coming in on the topic subscribed to. This app is single-threaded, so messages cannot be received on a background thread, therefore time on the main thread needs to be allocated to processing any messages that are waiting on the network connection.