Requirements

  1. An ESP32, ESP8266 or any other device based on those two chips with an up-to-date Tasmota firmware flashed version

  2. A wireless internet connection

  3. A DHT-11 or DHT-22 sensor

  4. A DS18B20 sensor

  5. Jumper wires

Table of Contents

  1. Configuring the GPIOs for the sensors

  2. Configuring the MQTT parameters to establish a connection with Ubidots

  3. Creating the UbiFunction that will handle the data going to Ubidots

  4. Visualizing the data on Ubidots dashboard

1. Configuring the GPIOs for the sensors

This article assumes that you already have an ESP device running Tasmota firmware. You can learn how to flash Tasmota to your device in a couple of minutes by following this guide.

First of all, we’ll need to go into our device’s dashboard and, in order to do so, we need to know its IP address on the local Wi-Fi network. If you don’t remember its IP address, you can get to know it by doing the following:

If you are running any Linux distribution, you can open your command console and type the command “ip addr”. This command will let you know your IP address, with which you can inspect the devices connected to your local network. After running the command, the following data should be displayed:

Component 5.png

Your PC's wireless interface information is under the section labeled "wlp3s0". The number enclosed in the red box is your IP address and your subnet mask. We’ll use that number to scan the devices in our local network, so copy it.

Now, in the same command console let’s run the following command replacing <your-ip-address> by the number that you copied in the previous step.

sudo nmap -sn <your-ip-address>

After running the previous command, a list of the devices connected to your local network will be displayed, there you’ll have to look for a device named something like “Espressif”

image823.png

Pro tip: If you do not have "nmap" utility installed, you can do so by running “sudo apt install nmap”. It takes only a couple of seconds!


Now that you know your device’s IP address, you can head to its Tasmota dashboard by using your web browser to go to the address you just got. The IP address assigned to the device used for this guide is “192.168.1.35”, so by typing this into the browser’s search bar, the following screen will be displayed:

rect843-3-6.png

Now, here's where the actual GPIO configuration takes place. Please click on the “Configuration” option and the following page will load in your web browser:

image895.png

Click on “Configure Module” to proceed to the next configuration step. A new screen will be displayed with all the options of configuration for the device type, and the various GPIO functions and sensors supported by Tasmota. Today we will be working with two different sensors to stream data to Ubidots. The following GIF shows you how to configure the GPIOs for those sensors:

Notice that after configuring the GPIOs, certain data is displayed on the dashboard’s main page as follows:

image921.png

That is precisely the data that we have to stream into Ubidots!

2. Configure the MQTT Parameters to Establish Connection with Ubidots

Now that we have the data to ingest into Ubidots, all that’s left is to configure the MQTT connection in order to stream the data to Ubidots. To do so, head over to the “Configuration” option in the dashboard’s main page, and then, on the newly loaded screen, go to “Configure MQTT”. The following screen will be displayed:

image1911.png

We only need to configure the parameters enclosed in the red boxes. Be careful doing this: typing mistakes here will invalidate any further step and will only be noticeable at the end, when no data reaches Ubidots. The parameters from the picture above must be configured as follows:

  • Host: Type “functions.ubidots.com”. This is the endpoint that the MQTT protocol will publish into when streaming data.

  • Port: Leave it on its default value.

  • Client: Leave it on its default value.

  • User: Here goes your Ubidots account username.

  • Password: Here goes your Ubidots personal Token. Learn how to find your Token here.

  • Topic: This is up to you and depends on what suits your implementation. However, in this article we’ll use an identifier for the device type as topic, “esp8266_office”. The reason will be clear soon.

  • Full Topic: Delete all the content of this field, and then type the following: /prv/your-username/tasmota/%topic%/. For example, if your Ubidots username is “johnDoe” then your Full Topic should be /prv/johndoe/tasmota/%topic%/

Now you can hit the “Save” button to continue on to the next step.

3. Creating the UbiFunction that Will Handle the Data Going to Ubidots

Up to this point, data is being ingested into the Ubidots platform through the endpoint configured in the previous step each time Tasmota publishes. However, due to the format of Tasmota's payload, we need to filter out the data of our interest. In order to achieve this, head to your Ubidots account and create a UbiFunction from the “Devices" section by hitting the "+" button at the upper right side of the screen.

path1839.png

After clicking on “Create Function”, the next page will be displayed on your browser.

rect1877.png

Configure the above settings like this:

  • Name: type the name of the UbiFunction. This is directly related to the MQTT parameters configured in the earlier step, so type “Tasmota” in the function’s name field.

  • Method: select the “POST” option from the drop down menu.

  • Runtime: select “Python 3.7”.

  • Token: select the “Default Token” option from the drop down menu, this will use your Ubidots personal Token by default.

  • In the code section, erase all the default content and paste the following code there.

import requests

def main(args: dict = {}):

#gets Tasmota subtopic. IE: SENSOR, STATE, SWITCH, etc.
fullTopic = args['topic']
fullTopic = fullTopic.rsplit("/", 2)
#this is actually the Tasmota subtopic
topic = fullTopic[-1]
#this is the device's name
device = fullTopic[-2]
#this is your Ubidots token
token = args.get("_auth_token")

#this will be the payload to send
payload = {}

publish = False

#This is a publish related to tasmota's status, not actually
#sensor data, so we will send an "empty" payload.
if(topic == 'STATE'):

publish = True
payload = {
"trigger":{
"value":0,
"context":{
"source":"tasmota-status-update"
}
}
};

#this is actually sensor data stream
#this happens every telePeriod
elif(topic == 'SENSOR'):

publish = True
payload = {
"trigger":{
"value":1,
"context":{
"source":"tasmota-telemetry-period-update"
}
},
"DS18B20-temperature":{
"value":args["payload"]["DS18B20"]["Temperature"],
"context":{
"source":"tasmota-telemetry-period"
}
},
"DHT-temperature":{
"value":args["payload"]["AM2301"]["Temperature"],
"context":{
"source":"tasmota-telemetry-period"
}
},
"DHT-humidity":{
"value":args["payload"]["AM2301"]["Humidity"],
"context":{
"source":"tasmota-telemetry-period"
}
},
"DHT-dew-point":{
"value":args["payload"]["AM2301"]["DewPoint"],
"context":{
"source":"tasmota-telemetry-period"
}
},
};

if(publish):
res = make_request(
"POST",
f"https://industrial.api.ubidots.com/api/v1.6/devices/{device}",
headers={"X-Auth-Token": token},
body_json = payload,
)


#This topic is sent every telePeriod and reports tasmota status

print(f"args: {args}")
print(f"topic: {topic}")
print(f"device: {device}")

return {"published":publish}


def make_request(
method,
url,
params=None,
headers=None,
body=None,
body_json=None,
attempts=1,
timeout=10,
):
"""
Function to make a request with timeout and retries
"""

req_session = requests.Session()
req_adapter = requests.adapters.HTTPAdapter(max_retries=attempts)
req_session.mount("https://", req_adapter)

response = req_session.request(
method=method.upper(),
url=url,
params=params,
headers=headers,
data=body,
json=body_json,
timeout=timeout,
)

req_session.close()
response.raise_for_status()

return response

After pasting the code in the corresponding text field, you can hit the “Make it live” button.

4. Visualizing the Data on Ubidots

Head over to the “Devices” section in your Ubidots account and there you’ll see a newly created device labeled “esp8266_office”. Click on it and you’ll see the data.

image.png
Did this answer your question?