ESP8266 & Ubidots MQTT Connection Tutorial

by Rustie0125 in Circuits > Arduino

334 Views, 1 Favorites, 0 Comments

ESP8266 & Ubidots MQTT Connection Tutorial

How to connect ESP8266 to Ubidots

I made a short but presise video about how to setup the ES8266 with Ubidots

Ubidots

Low Code IoT Plateform that allows for the connection of IoT devices via alot of different technologies. This totorial is to help anyone get the connection between the two systems going via MQTT so that you can build out your project from there.


Supplies

All you need is a micro USB cable and ESP8266 12F model, link below is what i used

https://amzn.to/3kHFfML ESP 12F

But it should work with any ESP, just might have different settings

Open Ubidots Acount

Screenshot 2023-01-26 052835.png

Create a ubidots account using this link https://stem.ubidots.com/


Install Ubidots Libraries

Download the Ubidots MQTT Arduino Library from this address

https://github.com/ubidots/ubidots-mqtt-esp

and install it via the library manager in arduino IDE

Also from the Library manager tab in Arduino, search for "PubSubClient" and install that aswell


Arduino should be setup and ready to go

Copy and Past the Below Example

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


Then look for this section of the code and update it with your wifi detials aswell as the Ubidots account Token( see video for screenshots if you can't find it.


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


Try and compile the skecth

Select the Correct Board and Comport

Next you want to make sure you have the correct settings for your board type and comport and upload

after the upload was succesful if you open the comport in 115200

you should see something like this


That means it was succesful and we can try and see if data is on ubidots

UBidots Device View

If you look at your ubidots account you should see a new device was automatically created under the device tab. it will looks something like this

If you click on the device, you will see your new data that was sent from the device.


and thats it, expand, play, build from here!

Let me know if you have any questions