All Collections
Connect your Devices
Connect an ESP8266 stand-alone to Ubidots over MQTT
Connect an ESP8266 stand-alone to Ubidots over MQTT

Learn how to connect the ESP8266 chipset to Ubidots Application Development Platform over HTTP.

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

The ESP8266 is a low-cost WiFi module built by Espressif Systems. Its popularity has been growing among the hardware community thanks to it’s nice features and stability, to the point that it can be easily programmed using your Arduino IDE.

By following this guide you will be able to PUBLISH and SUBSCRIBE data to/from Ubidots using the ESP8266 as stand alone module in just a couple of minutes!

Requirements

Step-by-Step

1. Hardware Setup
2. Setting up the Arduino IDE
3. Sending (PUBLISH) Data to Ubidots
4. Receiving (SUBSCRIBE) Data from Ubidots
5. Summary  

1. Hardware Setup

To be able to program the ESP8266 as stand alone module you will need any UART to USB device such as the UARTbee, or you can program the module through any Arduino board by setting the Arduino RST to GND

1. To begin, establish the connection between the device used to program the ESP8266 by following the table below. First, let's identify the pin out of the ESP8266 to avoid any future hardware issue.

IMPORTANT NOTE: If you are using an Arduino UNO to access the ESP8266 you will need to set Arduino RST to GND. Please be careful with the VCC of the ESP8266, it works only with a 3.3V supply.

  • FAQs and Troubleshooting

One of the most common errors with the ESP8266 is the espcom_sync_failed:

If you receive this error when the RST is not set to GND. To correct this error, please verify:

  1. if the jumper wire correctly connected and use the above Pinout table;

  2. confirm that the Arduino RST is set to GND;

  3. confirm that the board: Generic ESP8266 Module is selected in the Arduino IDE. For additional assistance selecting the board, see step 2 in the Setting up the Arduino IDE below.

2. Setting up the Arduino IDE

1. To be able to work with the ESP8266 platform in the Arduino IDE, you will need to install the ESP8266 board using the preconfigured Arduino Board Manager. If you are not familiar with adding a board with the Arduino IDE, refer to this article for additional guidance.

2. With the ESP8266 platform installed, select the ESP8266 device you are working with. In the case, we are working with a “Generic ESP8266 Module”. To select your board from the Arduino IDE, select Tools > Board Generic ESP8266 Module”.

3. Download and install the UbidotsMQTTESP8266. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.  

3. Sending (PUBLISH) Data to Ubidots

With the following sample code you will be able to post the readings taken from GPIO pins of the ESP8266.

1. To publish your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN, SSID (WiFi Name) and Password of the available network. 

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

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "...." //Your SSID
#define WIFIPASS "....." // Your Wifi Pass

Ubidots client(TOKEN);

/****************************************
 * 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:
  //client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
 
  float value1 = analogRead(0);
  //float value2 = analogRead(2)
  client.add("temperature", value1);
  //client.add("status", value2);
  client.ubidotsPublish("my-new-device");
  client.loop();
}

2. Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code. 

3. Upload the code into your “Generic ESP8266 Module”. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen, try unplugging the UART to USB device and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, located the device called "my-new-device". 

NOTE: To change the name ID of the device being updated in Ubidots, replace the string assigned in the following line of your code: 

client.ubidotsPublish("my-new-device");

4. Receiving (SUBSCRIBE) Data from Ubidots

With the following sample code you will be able to subscribe a value from Ubidots to start controlling any asset you desire. 

1. To begin receiving values from Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, be sure to assign the following parameters:

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

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "....." // Your Ubidots TOKEN
#define WIFINAME "....." //Your SSID
#define WIFIPASS "....." // Your Wifi Pass
#define DEVICE_LABEL  "..."  // Put here your Ubidots device label
#define VARIABLE_LABEL  "..."  // Put here your Ubidots variable label

Ubidots client(TOKEN);

/****************************************
 * 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:
  client.ubidotsSetBroker("industrial.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(false); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
  }

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

2. Verify & Upload the code into the board following the same steps provided in the PUBLISH step above.

3. To verify the connectivity of the device and the data which is being received, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen, try unplugging the UART to USB device and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

4. In the serial monitor, you will be able to see the last value received in Ubidots of the variable specified in the firmware.

5. Summary  

With this simple tutorial you are able to PUBLISH & SUBSCRIBE data to/from Ubidots with the ease of the Arduino IDE and an ESP8266 as stand alone module. If your wish to find more examples to handle context or timestamp values in your request checkout Ubidots documentation with the ESP8266 by clicking here

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Hacking! :) 

Other readers have also found useful...

Did this answer your question?