Ubidots – Arduino Communication using MQTT

In this post, I will show you how you can use MQTT Protocol to fetch or push data, to and from the Ubidots Cloud and Arduino Nano 33 IOT.

IOT and MQTT

The so called Internet of Things is creating a massive machine-to-machine network so that all of the devices, sensors, systems and actuators can connect to and communicate on the Internet.

With that they need a communication protocol so that they can understand each other. One of those protocols is MQTT or Message Queue Telemetry Transport.



What is MQTT

MQTT is a TCP based Subscribe and Publish messaging protocol designed for lightweight machine-to-machine communications. Using this protocol, we are not only  able to interact with these devices, these IOT devices and sensors out there themselves are communicating with each other using MQTT on the back end without our knowledge.

In the previous article, I explained everything you need to know about MQTT – Application, Advantages and Working. If you wanna check it out, click on the below link.

What is Ubidots?

Ubidots provides a simple and secure method for sending and receiving data to and from IoT devices using the global cloud network in real time. Ubidots provides a firm platform for hobbyists, enthusiasts as well as professionals, enabling them to easily retrieve and use the sensor data around the world and turn it into something useful.

We can use the Ubidots platform to send various sensor values or other data to the cloud, store it there safely and retrieve them anytime we want using simple API calls.

In the previous tutorials, I showed you how to make an Arduino Home Automation System and Home Safety Monitoring System using Ubidots.

Objective

In this project, we will be making a Button Widget and a Metric Widget in the Ubidots Dashboard and assign two variables to it.

When you push the button widget, it will send a command to the Arduino Nano 33 IOT using the MQTT protocol.

When the data reaches the Arduino, Arduino sends back an acknowledgement to the Ubidots and it will be displayed on the text widget.

By the end of this tutorial, you should be able to make use of MQTT protocol to send and receive data from Ubidots Cloud to the Arduino and incorporate it in your own projects.

Steps – Arduino – Ubidots MQTT Communication

Step 1 – Setting Up an Account in Ubidots STEM

Next, Go to this link and create a free account in Ubidots STEM.

If you already have an account simply sign in with your credentials.

Step 2 – Create a Device

Next we have to create a device. Click on “Add New Device” and select “Blank Device”.

Since this is a tutorial to show you how to use MQTT with Arduino and Ubidots, let us name the device “MQTT Demo.

Step 3 – Setup the Variables

Now we can create two variables – One to Assign the value of Button (1 and 0) and the other to store the value of the Text where we display the acknowledgment from the arduino.

Take a note of the Device Label and Variable Labels of all variables as we will be using it in the code.

Step 4 – Create Widgets

Now create a dashboard by clicking “Add New Dashboard” From the dashboard, click on add new widget.

For this tutorial, let us create a Switch Widget and a Metric Widget.

Step 5 – Connect Widget to Variables

Now connect the Switch Widget to the Button variable and Metric Widget to the Received text variable.

The finished dashboard should look somewhat like this.

Step 6 – Authentication

Every packets requires a TOKEN. The easiest way to get yours is clicking on “API Credentials” under your profile tab:

You’ll notice there are two types of keys in your Ubidots account:

  • Tokens: Temporary and revocable keys that is to be embedded in all your API requests.
  • API Key: This is your “Master Key”; a unique and immutable key that is used only to generate your account’s tokens.

Take note of all the parameters – >Device Label,  Device ID, Variable ID, and Token. We will be using it in the code.

Step 6 – Set Up Arduino

Now its time to set up our Arduino. You can use Arduino IDE Desktop version as well as online editor to upload the code to you Arduino Board.

First you must install Arduino Nano 33 IOT from the boards manager. Then install 2 libraries from library manager – WiFiNina and PubSubClient.

Step 7 – Upload the Code

First, let us take a look at the code. The code is pretty simple and I will explain the main parts. Let me know in the comments, if you need more clarification.

First enter the credentials and credentials.h tab.

char networkSSID[] = "WiFi Name";
char networkPASSWORD[] = "WiFi Pass";

char mqttSERVER[] = "169.55.61.243";
char mqttUSERNAME[] = "Ubidots Token";
char mqttPASSWORD[] = "";

Here you have to enter your WiFi name, password, MQTT Server IP, username and password. The IP Address is 169.55.61.243 and the Username is the Token you get from the ubidots account.

Then set up the variables and the header files.

char subTopic[] = "/v1.6/devices/mqtt-demo/button/lv";  
char pubTopic[] = "/v1.6/devices/mqtt-demo/received-text"; 

We will be subscribing to ‘subTopic’, which will be used to read the last value which is stored in Ubidots cloud and publishing ‘pubTopic’ which contains the acknowledgement which will be read by Ubidots.

Please note that, here ‘mqtt-demo’ is the device name and ‘button’ and ‘received-text’ are the variables we created in the Ubidots cloud.

void setup_wifi() 
{
  delay(10);
  
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

This will initiate the WiFi Connection and print the status in Serial Monitor.

void callback(char* topic, byte* payload, unsigned int length) 
{
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) 
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if 1 was received as first character
  if ((char)payload[0] == '1') 
  {
    digitalWrite(LED_PIN, HIGH);   
    ledState = 1;
  client.publish(pubTopic, "1");
  } 
  else 
  {
    digitalWrite(LED_PIN, LOW); 
    ledState = 0;
    client.publish(pubTopic, "0");
  }

}

The callback function will be called when a new message is received in the subscribed topic. It will first display the received payload in the serial terminal. It then switches On or Off the built-in LED of the Arduino Nano 33 IOT and then publishes the state of LED through the pubTopic.

Now you can connect your board and upload the code to your Arduino Nano 33 IOT.

Step 8 – Testing

Now start the serial monitor. Now open up the browser and click on the button we created in the Ubidots dashboard. You will see that the Arduino receives the changed code in no time. Once the command is received and the LED is truned ON, it will send an acknowledgement back to the Ubidots Server which will be displayed in the Dashboard.

Ubidots Dashboard and Arduino Serial Monitor

Thats it guys. In the next video, I will show you how you can create a Home Automation system using Ubidots and Arduino Nano 33 IOT.

New to Robotics?

We have a beginners guide on “Getting Started with Robotics” which will give you a kick start in this field. Check out our free video tutorial below for a brief introduction.

Top Arduino Projects You can Try this Summer Vacation


Top Robotics Projects You can Try this Summer Vacation

Did you find this page useful? Help us to improve by rating this page.

[RICH_REVIEWS_FORM]

[RICH_REVIEWS_SNIPPET stars_only=”true”]

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *