element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs Send a Command to the MKR WiFi 1010 using Wia and MQTT
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: anpivey
  • Date Created: 15 Aug 2018 4:00 PM Date Created
  • Views 1243 views
  • Likes 5 likes
  • Comments 1 comment
  • mqtt
  • internet of things
  • mkr
  • arduino ide
  • arduinos
  • mkr1010
  • iot
  • wia
Related
Recommended

Send a Command to the MKR WiFi 1010 using Wia and MQTT

anpivey
anpivey
15 Aug 2018

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

image

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.

 

image

 

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.

image

 

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.

image

 

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.

image

  • Sign in to reply
  • jomoenginer
    jomoenginer over 6 years ago

    Austin,

     

    Nice post.  I had seen this on the wia site but am looking to use a local MQTT broker.

     

    One thing, I hope the SSID and password you have in your pic is not your actual credentials.

     

    I'm curious, did you use the default Firmware on the MKR WiFi 1010 or did you update it?  Also, have you run into an issue with the 1010 connecting to your WiFi network? I seem to be having an issue with the board connect to my WiFi.

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube