article

vidhan avatar image
vidhan posted

Control IoT Device With Telnyx Messaging

IoTRpiPython.png

Introduction

An IoT use case where developer can control an IoT device(controlling LED) with a message using Telnyx messaging API.


Prerequisite


Steps

Step 1: Telnyx Setup

Sign up for Telnyx account, obtain a number with SMS capabilities and configure the number for messaging.

If you already have a Telnyx number with messaging enabled, move to Step 2

  • Sign up for Telnyx account - Set up a developer account with Telnyx.

  • Obtain a number with SMS capabilities for auto-responder app - After creating an account and signing in, you need to acquire a number for the application. Search for a number by selecting your preferred 'Region' or 'Area Code'. - Make sure that the number supports SMS feature(Very Important!) as it will be used by our application.

  • Create a messaging profile

    • Next create a messaging profile by clicking on "Add new profile" and provide a suitable profile name to it(you do not need to provide any other detail for now).

  • Configure the number for messaging

    • Go to the numbers page, look for the number you created and set the number's Messaging Profile to the profile you created in the previous step.

      What if the Telnyx number is an international number for a User

      • If you want to send the message to a Telnyx number which is not in the country where you are, then you need to click on the 'Routing' option.

      • After clicking on 'Routing', a dialog box will open. In there, select the traffic type as "P2P" to allow International Inbound and Outbound SMS deliverability. And do not forget to save the changes!

  • Keep the Telnyx number with you because you are going to send SMS to it!


Step 2: Install and configure ngrok

A tunneling tool is required to provide internet access to Python Flask application. You need to sign-up for ngrok account, download the setup file and edit Telnyx messaging profile to add a webhook.

  • Sign up for ngrok account and download the setup file - Go to https://dashboard.ngrok.com/signup and create an account.

  • Obtain the ngrok setup file and follow the steps mentioned - Download the ngrok setup file as per your OS from https://dashboard.ngrok.com/get-started/setup and follow the steps mentioned on the page.

    • You need to run the setup file (It has zero run-time dependencies!)

      • In the Step 3, you need to change the command to

        ngrok http 5000
      • After running the above command, you would see something similar to following:

      • Copy the highlighted 'Forwarding' address. we will need it in next step.

      http://0ab4-2405-201-300a-ecf1-201a-6ad8-c0d4-eddd.ngrok.io
  • Edit Telnyx messaging profile to add webhook

    • Go to messaging profile and click on the message profile you created earlier.

      • It will open "Edit Messaging Profile" page, here under "Inbound Settings" you need to provide value to 'Send a webhook to this URL'

      • The value is Forwarding address we copied in the previous step. Append it with '/webhooks'. It will look like this -

      http://0ab4-2405-201-300a-ecf1-201a-6ad8-c0d4-eddd.ngrok.io/webhooks

      • Always keep the ngrok process running, do not stop it or restart it! Because it will lead to a changed URL, which then will require you to repeat the above steps each time.

Step 3: Raspberry Pi connection and Python code

  • Connect LED and resistor to Raspberry Pi with the help of jumper wires and breadboard

    • Before making connection, test if your LED is working fine or not. Follow these steps here to test LED and resistor connection

    • For the purpose of controlling LED, connect the jumper wire from LED to GP17 of Raspberry Pi (check the pin diagram for connection)

  • Python code to run on Raspberry Pi (IoT device)

    • Once you are done with the connection. Create a Python file and paste the code into your newly created python file

from flask import Flask, request
import RPi.GPIO as GPIO

LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)

app = Flask(__name__)

@app.route('/webhooks', methods=['POST'])

def webhooks():    
    payload = request.json['data']['payload']
    if payload['direction'] == 'inbound':
        takeAction(payload)
    return 'success', 200

def takeAction(payload):
    incomingText    = payload['text']
    if incomingText == "ON":
        GPIO.output(LED_PIN, GPIO.HIGH)
        print(incomingText)
    elif incomingText == "OFF":
        GPIO.output(LED_PIN, GPIO.LOW)
        print(incomingText)
    else:
        print("You have entered "+incomingText+".")
if __name__ =="__main__":
    app.run(port=5000)
  • Run the above Python code, it will start a flask server. Keep ngrok terminal open all the time


Send ON or OFF text to the Telnyx number to control the LED connected to Raspberry Pi!

messagingpythoniotraspberrypi
10 |600

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Manage Your Content

Article

Contributors

vidhan contributed to this article michaelp contributed to this article