There are several scenarios where using SMS to send data from cellular devices makes more sense than directly reporting data from the IoT device to the internet. Some examples include:
Remote Locations with Limited or Unstable Internet Connectivity: In remote or rural areas where cellular internet connectivity is unreliable or non-existent.
Cost-Sensitive IoT Applications: When the cost of cellular data plans can be prohibitive, SMS can be a cost-effective alternative.
Low Data Volume Applications: When the amount of data is minimal, such as simple status updates from a smart meter, or alarm notifications, SMS can be more efficient than purchasing a cellular data plan.
Backup Communication Channel: For critical systems that require high reliability, SMS can serve as a backup communication channel. If the primary internet connection fails, devices can fall back to SMS to ensure important messages still get through.
Low-Power Devices: If managing a 3G/4G, or even an LTE-M/NB-IoT connection, drains your device's battery too quickly, then using SMS can help optimize battery life.
Legacy Systems Integration: In scenarios where legacy systems can't be modified, sending SMS may be simpler than trying to set up an HTTP or MQTT connection.
With this guide, you'll be able to couple the power of Teltonika's SMS-to-HTTP feature, with the flexibility of UbiFunctions, in order to create your own private inbound SMS gateway.
Requirements
A Teltonika gateway with the SMS-to-HTTP feature. In this guide, we'll be using a TRB140.
An active Ubidots account in the Industrial plan.
1. Set Up Your Gateway and Ensure It's Connected to the Internet
Follow Teltonika's guide to set up your device, including a SIM card with a valid phone number. Once configured, make sure you can access the device web GUI, which looks like this:
2. Create an UbiFunction
In your Ubidots account, go to the "Devices" tab, then click on "Functions", then create a new function. In the runtime option, select "Python 3.11 Lite":
Insert the following code in the function:
Note that the following code assumes a known SMS structure, containing sample variables. It's provided as an example, but you're encouraged to modify it to decode the format used by your devices.
import requests
def main(args):
# Extract the SMS content
sms_content = args.get('sms', '')
# Split the SMS content to get device label and sensor values
try:
device_label, temperature, humidity, battery = sms_content.split(',')
except ValueError:
return {"status": "error", "message": "Invalid SMS format"}
# Prepare data for Ubidots
ubidots_token = args.get('_auth_token', '')
ubidots_url = f"https://industrial.api.ubidots.com/api/v1.6/devices/{device_label}"
headers = {"X-Auth-Token": ubidots_token, "Content-Type": "application/json"}
payload = {
"temperature": float(temperature),
"humidity": float(humidity),
"battery": float(battery)
}
# Send data to Ubidots
response = requests.post(ubidots_url, headers=headers, data=json.dumps(payload))
if response.status_code == 200:
return {"status": "success", "message": "Data sent to Ubidots"}
else:
return {"status": "error", "message": f"Failed to send data to Ubidots: {response.text}"}
Lastly, copy the URL of the function, which will be used in the next step:
3. Configure SMS Forwarding to HTTP in Teltonika's Gateway
In Teltonika's web GUI, go to "Services" → "Mobile Utilities" → "SMS Gateway", then click on the "SMS Forwarding" tab. Fill in these values:
Enable: Make sure the enabling toggle is on.
Method: POST.
URL: Paste your function's URL.
Message value name: We'll simply name it "sms". This is the JSON key that will be sent to the function.
Finally, click on "SAVE & APPLY".
4. Send a Test SMS Message
To test the SMS gateway, send an SMS with the following structure to the phone number of your SIM card installed in the Teltonika device:
device-abc,22,53,89
Once the Teltonika Gateway forwards the SMS body to the UbiFunction, a new device will appear in your Ubidots account:
5. Wrap Up
In this guide we leveraged Teltonika's SMS to HTTP feature to create our own SMS gateway, enabling cellular IoT applications without the need of data plans. The use of UbiFunctions provides all the flexibility you need to adapt the decoding to your desired data format.