
Connect Robustel R1520 Industrial Global 4G Router to Ubidots IoT Platform through MQTT
|
|
Time to read 7 min
|
|
Time to read 7 min
Written by Sergio M
Robustel's R1520 is a high-performance, industrial-grade gateway provisioned with dual-SIM 4G LTE network connectivity, thus, allowing fast and reliable internet access in remote areas. It has the required interfaces to connect to sensors/devices featuring several different connection protocols such as Modbus Serial RS-485, RS-232, Modbus TCP/IP, Wi-Fi AP Mode, digital Input/Output (DI/DO), analogue input (AI).
Aside its broad hardware features, the software of the R1520 also stands out: You can manage and configure the R1520 by accessing RobustOS, Robustel's proprietary router operating system, from any web browser .RobustOS makes it straightforward for the user to configure and manage the services on the R1520 such as MQTT, Modbus, etc., thanks to its no-code approach.
Requirements
- An active Ubidots account
- Robustel R1520 Industrial Global 4G Router
- A Robustel RCMS Account
- Any sensor with Modbus RS485 interface. This guide will use the S6000U Smart IoT Sensor
1. Configure the R1520 to connect to the internet
- Provide power to the R1520 by connecting it using the included power connector as shown below:
- When prompted for credentials, use admin in both fields and tap the LOGIN button.
Note : The following steps are meant to configure the R1520 to connect to the internet using Wi-Fi Client mode. If you require to connect it to the internet using Ethernet or cellular, please refer to the official documentation .
- Once the web interface loads, click Interface → WiFi and set the following parameters:
Mode : Client.
Region : set your country's code according to ISO 3166-1 alpha-2.
- Click Interface → Link Manager . Click on the Primary Link drop-down menu and select WLAN . Once you have done this, the WLAN entry will be displayed under the Link Settings section.
- Click the edit button corresponding to the WLAN entry.
- Once you click the "+" icon, the following visualization will be displayed. There, edit the settings as follows:
Tag : This is the label associated to this variable. I.e, in the output JSON file, this variable will appear as
{"xy-md02-temperature" : }
Type : This is the interface through which the Modbus protocol will run. In this guide, RTU is used since the sensor is connected through the serial port to the R1520. Nonetheless, you must select this according to your particular implementation.
Slave ID : This is the address of the slave device that you want to read from.
Function Code : You should select this according to what you wish to read from the sensor. In this case, both the temperature and humidity are input register , however, select this according to your requirements.
Address : This is the address of the input register required to be read. Match this value according to your requirements.
Click the Submit button to save the changes.
5. Configure the E2C-MQTT
- Go to your Ubidots account Devices → Functions .
- Create a new Function and edit its code. Paste the following one, but first, make the following changes:
Set the Ubidots token that you wish to use for this application on the variable token at line 7.
Edit the operations_on_data dictionary so its keys match the Tags that you are assigning to each Modbus register on the R1520 E2C Modbus App .
Edit the operations_on_data dictionary so its values match the required operations on each variable.
- For instance, if you used "xyz-sensor-reading-1" and "xyz-sensor-reading-2" as the Tags names on the E2C Modbus App , then the operations_on_data dictionary keys should be "xyz-sensor-reading-1" and "xyz-sensor-reading-2".
- Also, you should edit the lambda function according to the required operations on the data according to your sensor's datasheet. For instance, the sensor used on this guide reports the temperature and humidity values, both multiplied by a factor of 10, so in order to get the actual measurement, it is needed to divide the obtained value by 10.
import requests
import time
REQUESTS_FUNCTIONS = {"get": requests.get, "post": requests.post}
BASE_URL = "https://industrial.api.ubidots.com"
excluded_keys = ["deviceType", "subDeviceId", "timestamp"]
token = ''
operations_on_data = {
"xy-md02-temperature": lambda x: x / 10,
"xy-md02-humidity" : lambda x: x / 10,
}
def main(args):
#Print arguments for debugging purpose
print(args)
incoming_payload_list = args['payload']['payload']
device_label = args['topic'].rsplit('/')[-1]
payload = {}
for each_device_json in incoming_payload_list:
for each_device_key, each_device_value in each_device_json.items():
if each_device_key in excluded_keys:
continue
if isinstance(each_device_json[each_device_key], dict):
if each_device_key in excluded_keys:
continue
inner_json = each_device_json[each_device_key]
for inner_key,inner_value in inner_json.items():
if not inner_key in excluded_keys:
payload[inner_key] = {"value" : operations_on_data[inner_key](inner_value), "timestamp" :inner_json['timestamp'] }
else:
payload[each_device_key] = each_device_value
# Use the remaining parameters as payload
req = update_device(device_label, payload, token)
# Prints the request result
print("[INFO] Request result:")
print(req.text)
return {"status": "Ok", "result": req.json()}
def update_device(device, payload, token):
"""
updates a variable with a single dot
"""
url = "{}/api/v1.6/devices/{}".format(BASE_URL, device)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
req = create_request(url, headers, payload, attempts=5, request_type="post")
return req
def create_request(url, headers, data, attempts, request_type):
"""
Function to create a request to the server
"""
request_func = REQUESTS_FUNCTIONS.get(request_type)
kwargs = {"url": url, "headers": headers}
if request_type == "post":
kwargs["json"] = data
try:
req = request_func(**kwargs)
print("[INFO] Request result: {}".format(req.text))
status_code = req.status_code
time.sleep(1)
while status_code >= 400 and attempts < 5:
req = request_func(**kwargs)
print("[INFO] Request result: {}".format(req.text))
status_code = req.status_code
attempts += 1
time.sleep(1)
return req
except Exception as e:
print("[ERROR] There was an error with the request, details:")
print(e)
return None
- After editing the code, click the Make it live button.
- Copy the HTTPs Endpoint URL corresponding to this function. It will be something like:
https://parse.ubidots.com/prv//
- Go to Edge2Cloud → E2C MQTT . There, select the MQTT tab.
- In the General Settings tab edit the following settings:
Enable : Set the slider to the ON position in order to enable the MQTT service.
MQTT TLS Enable : Disable this option. This tutorial will not use encryption. If you require TLS, please refer to the documentation to configure it.
Remote Server : functions.ubidots.com. This is Ubidots dedicated endpoint for UbiFunctions .
Remote Port : 1883. This is the port for no-secured connection.
Username : Your Ubidots username.
Password : Your Ubidots Token .
Client ID : Any random and unique 15 length character string.
- All other unmentioned settings are up for the user to be configured.
- In the Topic Settings tab, set the Publish Topic , Response Topic and Subscribe Topic to:
/prv///
- For instance, if your Ubidots username is JohnDoe, the function's name is Robustel App, and your device's MAC address is F1:AB:7D:C1:56:AF, then the topic is:
/prv/johndoe/robustel-app/f1ab7dc156af
Original Link: https://help.ubidots.com/en/articles/7203647-connect-robustel-r1520-dual-sim-cellular-vpn-router-to-ubidots-through-mqtt