Advice and answers from the Ubidots Team


The Development Kit based on ESP8266, integates GPIO, PWM, IIC, 1-Wire and ADC all in one board. Power your developement in the fastest way combinating with NodeMCU Firmware!

Requirements

Step 1. Setup the Arduino IDE with your device 

To start this tutorial, we will begin by connecting your NodeMCU to your computers port to configure the device.

1.- Download the Arduino IDE if you not already  have it. Open the Arduino IDE, select Files -> Preferences and enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas

NOTE: If you're a Mac user, please note that Arduino and File contain different drop down functions compaired to Windows users. Also, you have to install the following driver to be able to upload your NodeMCU.

2.- Open Boards Manager from Tools -> Board -> Boards Manager and install esp8266 platform. To simply find the correct device, search ESP8266 within the search bar.

3.- Select your NodeMCU 1.0 (ESP-12 Module) from Tools > Board menu.

Additionally, we need to be able to communicate with the NodeMCU, we also need to select the port com. Go to Tools > Port >  Select the appropriate PORT for your device.


Also, to keep everything running fast and smooth - let's make sure the upload speed is optimized to 115200. Go to Tools > Upload Speed > 115200:

4.- Go to the library repository to download the Ubidots MQTT ESP library. To download the library clicking the green button called "Clone or download" and select "Download ZIP".

5.- Now back in the Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library

6.- Select the .ZIP file of ubidotsMQTTESP and then “Accept” or “Choose

If successful you will receive the message below in the Arduino IDE:

7.- Go to Sketch/Program -> Include Library -> Library Manager and install the PubSubClient library. To simply find the correct library, search PubSubClient within the search bar.  

8.- Close the Arduino IDE and open it again.

Step 2. Set up your Ubidots account with your device

a) Subscribe to a variable

Sign up into your Ubidots account, if you don't have one click here

Go to the device tab and create a new Device called “ESP8266”, also add a variable called “relay”. Verify that their labels are the same of their names.

Go to the dashboard tab to create a control widget to be able to control your NodeMCU. To add the a Widget, click on the "+" icon in the top right of the dashboard. Reference below:

To create the widget click Control > Switch > ESP8266 > relay > finish.


Once your Widget is created you should see somenthing like this appear on your Dashboard:  

With your active device, go back to the Arduino IDE and paste the code below. Once you have copied the code, you will need to assign the parameters: Wi-Fi name and password, plus your individual, unique Ubidots TOKEN. If you don't how get your Ubidots TOKEN, please reference this article below. 

NOTE: You will need to make some adjustments to the first part of the code after you have pasted into Arduino IDE. This is to ensure your NodeMCU is connecting to the correct server.

FOR EXAMPLE: Ubidots Code:

COMPARED TO SAMPLE USER´S PARAMETERS

Please copy and paste the below code into the Arduino IDE including your specific user parameters. 

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "Put_your_Ubidots_Token_here" // Your Ubidots TOKEN
#define WIFINAME "Put_your_WiFi_name_here" //Your SSID
#define WIFIPASS "Put_your_WiFi_password_here" // Your Wifi Pass
#define MQTTCLIENTNAME "Put_a_random_ASCII_name_here" //Your MQTT Client Name. Please refer to README at github.com/ubidots-mqtt-esp to know how to build this name


#define pin D1

Ubidots client(TOKEN, MQTTCLIENTNAME);

/****************************************
 * Auxiliar Functions
 ****************************************/

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]);
  }
  if ((char)payload[0]=='1'){
    digitalWrite(pin, HIGH);
  }
  else{
    digitalWrite(pin, LOW);
  }
  Serial.println();
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  pinMode(pin, OUTPUT);
  client.ubidotsSubscribe("esp8266","relay"); //Insert the dataSource and Variable's Labels
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe("esp8266","relay"); //Insert the dataSource and Variable's Labels
      }
  client.loop();
  }

Once you pasted the code and updated the WiFi parameters, you must Verify this within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the below icons. Choose the Check Mark icon to verify any code.

Once the code is verified, you will receive the message bellow in the Arduino IDE:

Next, your have to upload the code into your NodeMCU. To do this, choose the right-arrow icon beside the check mark icon.

Once the code is uploaded, you will receive the message bellow in the Arduino IDE:

Now you're able to control a variable! Go back to your Ubidots dashboard and change the status of the switch.
If your device sensors are not connected, you can still visualize if your device is responing through the Serial Monitor. To visualize the response of the server and the connectivity of the device open the Serial Monitor of the Arduino IDE. To do this, in the top right corner of our Arduino IDE select the mangnifying glass icon to display a serial monitor.

b) Publish values 

With the following example you will be able to publish ANALOG reading taken from the A0 pin of the NodeMCU.

To publish the value to Ubidots open a new window in the Arduino IDE and paste the code below. To open a new window select the sheet icon: 

Remember change the parameters needed (TOKEN,WIFINAME, and WIFIPASS) as we made on the step before.

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "Put_your_Ubidots_Token_here" // Your Ubidots TOKEN
#define WIFINAME "Put_your_WiFi_name_here" // Your SSID
#define WIFIPASS "Put_your_WiFi_password_here" // Your Wifi Pass
#define MQTTCLIENTNAME "Put_a_random_ASCII_name_here" // Your MQTT Client Name. Please refer to README at github.com/ubidots-mqtt-esp to know how to build this name
#define sensor A0

Ubidots client(TOKEN, MQTTCLIENTNAME);

/****************************************
 * Auxiliar Functions
 ****************************************/
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();
}

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(sensor,INPUT);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  }
void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
 
  float sensorValue = analogRead(sensor);
  client.add("temperature", sensorValue); //Insert your variable Labels and the value to be sent
  client.ubidotsPublish("esp8266");
  client.loop();
  }

To verify the connectivity of the device open the serial monitor. Now you should see the published data in your Ubidots account, inside the device called "esp8266".

c) Subscribes and Publish 

Following the steps before, you're now able to subscribes and publish data to Ubidots. If you wish to compile both subscribe and publish into a single code, please see the code below for complete sensor functionality with Ubidots.

Once you have pasted the code into the Arduino IDE don't forget change the parameters needed (TOKEN,WIFINAME, and WIFIPASS) as we made before.

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "....." //Your SSID
#define WIFIPASS "....." // Your Wifi Pass
#define MQTTCLIENTNAME "....." //Your MQTT Client Name. Please refer to README at github.com/ubidots-mqtt-esp to know how to build this name
#define pin D1
#define sensor A0

Ubidots client(TOKEN, MQTTCLIENTNAME);

/****************************************
 * Auxiliar Functions
 ****************************************/

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]);
  }
  if ((char)payload[0]=='1'){
    digitalWrite(16, HIGH);
  }
  else{
    digitalWrite(16, LOW);
  }
  Serial.println();
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  pinMode(sensor, INPUT);
  pinMode(pin, OUTPUT);
  client.ubidotsSubscribe("esp8266","relay"); //Insert the dataSource and Variable's Labels
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      client.ubidotsSubscribe("esp8266","relay"); //Insert the dataSource and Variable's Labels
      }
  float sensorValue = analogRead(sensor);
  client.add("temperature", sensorValue);
  client.ubidotsPublish("esp8266");
  client.loop();
  }

To verify the connectivity of the device open the serial monitor. Now you should see the published data in your Ubidots account, inside the device called "esp8266", also you're able to control a variable.

Did this answer your question?