In this tutorial, we are going to show you how to control your Arduino MKR1010 with MQTT and Wia.
Components
- MKR1010
- USB to Micro USB Cable
- A Wia Account. You can create one here
- Arduino IDE. You can install the latest version here
Setup your environment
Connect the MKR1010 to your computer using the USB. In Arduino, create a new sketch and remove the code that is automatically generated.
If this is your first time using the MKR1010, we recommend you follow this tutorial first to set up your environment. If you do not follow this tutorial first, you are likely to find trouble when selecting the correct board and port in the Arduino IDE.
In the Arduino IDE
- Select Tools > Board > MKR WiFi 1010
- Select Tools > Port > Arduino WiFi MKR 1010
Next, install the required libraries. In the Ardui IDE
- Select Sketch > Include Libraries > Manage Libraries
In the search bar, search for the below libraries. When found, click on the library and a button will appear in the bottom right of the box that will allow you to install the library.
- WiFiNINA
- MQTT
Setup your Wia space
Go to the Wia Dashboard and Create a Space
. Name it whatever you like.
Open your space. In the left side menu, select Devices
and Add a Device
.
Give your device a name and finish.
Now, in the Devices
tab, you can see your device. Select your device. Navigate to the Commands
tab in the top bar menu.
Click Add Command
and name the command Ping
.
In the top menu, navigate to the Configuration
tab. Here, you can see your Device ID
as well as your Device-Secret-Key
. We will need this information for the next step.
Program the device
Go to the Arduino IDE. In the sketch, copy and paste the following code:
#include <WiFiNINA.h>
#include <MQTT.h>
const char WIFI_SSID[] = "your-wifi-network"; // WiFI ssid
const char WIFI_PASS[] = "your-wifi-password"; //WiFI password
//WiFiSSLClient ipCloudStack;
WiFiClient wifiClient;
MQTTClient mqttClient;
int status = WL_IDLE_STATUS;
// Wia Cloud MQTT params
char mqttCloudServer[] = "api.wia.io";
int mqttCloudPort = 1883;
// get this from the wia dashboard. it should start with `d_sk`
char mqttCloudUsername[] = "your-device-secret-key";
char mqttCloudPassword[] = " ";
// Wia API parameters
char server[] = "api.wia.io";
const int mqttPort = 1883; // Default MQTT port
const String deviceId = "your-device-id"; // starts with dev_, found in Wia Dashboard
// Topics
String pingCommandTopic = "devices/" + deviceId + "/commands/ping/run";
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
if (topic.equals(pingCommandTopic)) {
Serial.println("Sending ping...");
Serial.println("Pinged!");
// Start rotating
// wait 5 seconds for connection:
delay(5000);
}
}
void connect() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
// wait 5 seconds for connection:
delay(5000);
}
Serial.print("\nconnecting...");
Serial.println("\nconnected!\n");
Serial.print("\nIP address: ");
Serial.println(WiFi.localIP());
// You need to set the IP address directly.
mqttClient.begin(mqttCloudServer, mqttCloudPort, wifiClient);
Serial.println("start wia connect"); Serial.println();
while (!mqttClient.connect("wiatest", mqttCloudUsername, mqttCloudPassword)) {
Serial.print("*");
delay(500);
}
Serial.println("Connected to MQTT");
mqttClient.onMessage(messageReceived);
mqttClient.subscribe(pingCommandTopic);
}
void setup() {
Serial.begin(115200);
}
void loop() {
mqttClient.loop();
delay(1000);
if (!wifiClient.connected()) {
connect();
}
}
This code must be edited.
- Replace the value of
WIFI_SSID
with the name of your WiFi netowrk - Raplce the value of
WIFI_PASS
with the password of your WiFi network - Replace
mqttCloudUsername[]
with your-device-secret-key from the Wia Dashboard - Replace
deviceID
with your device ID from the Wia Dashboard
Once those changes are made, upload the program to your device by clicking the Upload
icon in the upper right corner.
In the Wia Dashboard, navigate back to Devices > Commands
and run the Ping
command.
In the Arduino IDE, open the Serial Monitor by clicking the icon in the upper right corner of the sketch. Here, you can see that your Ping
message from Wia was received.