GPIO over SMS example (Python): Różnice pomiędzy wersjami

Z Elproma Wiki Knowledge Base
Skocz do: nawigacja, szukaj
Linia 1: Linia 1:
In this page we are describing the example of ''GPIO over SMS'' function in RBMTX-Lite-IO router variant. The purpose of this example is to add extra logic to the device to control inputs and outputs remotely.
In this page we are describing the example of ''GPIO over SMS'' function in RBMTX-Lite-IO router variant. The purpose of this example is to add extra logic to the device to control inputs and outputs remotely.


==Description==
==Python script==


Router is based on OpenWrt system so you can add scripts or programs to device. One of the simplest, powerful and most popular programming language is Python. RBMTX-Lite supports Python version 3.9.12 so you can easily write, upload and run your scripts on the device. Here is the description of Python script whose main task is to receive/send SMS messages and control GPIO lines of the router.
Router is based on OpenWrt system so you can add scripts or programs to device. One of the simplest, powerful and most popular programming language is Python. RBMTX-Lite supports Python version 3.9.12 so you can easily write, upload and run your scripts on the device. Here is the description of Python script whose main task is to receive/send SMS messages and control GPIO lines of the router.
==Python script==


Script can be downloaded here:  
Script can be downloaded here:  
Linia 11: Linia 9:
===Script uploading===
===Script uploading===


You can upload the program to /root/ over ssh interface.
You can upload the program to /root/ directory over scp protocol. Simple use scp command in Linus system or use WinSCP software in Windows.


===Script behavior===
===Description===


smbus module is imported in the script. Thanks to it you can configure and communicate with GPIO over i2c interface.
smbus module is imported in the script. Thanks to it you can configure and communicate with GPIO over i2c interface.
Linia 22: Linia 20:
you will set OUT3 to high.
you will set OUT3 to high.


Scripts periodically checking if SMS in format "SET OUTx y" has arrived (where x is the number of output and y is state: H for high and L for low). If yes, it will change the state of a given output and sends SMS confirmation to the SMS sender.
Scripts periodically checking if SMS in format "SET OUTx y" has arrived (where x is the number of output and y is state: H for high and L for low). If yes, it will change the state of a given output, sends SMS confirmation to the SMS sender and delete all messages from memory (AT+CMGD command).


It is also checking if the state of input changed and if yes, it is sends SMS confirmation to the number from phone file (you need to send this file to the router first).
It is also checking if the state of input changed and if yes, it is sends SMS confirmation to the number from phone file (you need to send this file to the router first).
Linia 28: Linia 26:
'''NOTE:''' For security reasons it is recomended to add numbner filtering (allowlist) and thoroughly check and test the operation of the script.
'''NOTE:''' For security reasons it is recomended to add numbner filtering (allowlist) and thoroughly check and test the operation of the script.


===Script executing===
To execute uploaded script you can send below command over ssh interface:
python gpio_sms


If you want to run the script automatically every time you turn on the router go to ''System'' -> ''Startup'' -> ''Local Startup''


[[File:example_gpio_sms_py.png|400px]]
[[File:example_gpio_sms_py.png|400px]]

Wersja z 14:33, 8 gru 2022

In this page we are describing the example of GPIO over SMS function in RBMTX-Lite-IO router variant. The purpose of this example is to add extra logic to the device to control inputs and outputs remotely.

Python script

Router is based on OpenWrt system so you can add scripts or programs to device. One of the simplest, powerful and most popular programming language is Python. RBMTX-Lite supports Python version 3.9.12 so you can easily write, upload and run your scripts on the device. Here is the description of Python script whose main task is to receive/send SMS messages and control GPIO lines of the router.

Script can be downloaded here:

Script uploading

You can upload the program to /root/ directory over scp protocol. Simple use scp command in Linus system or use WinSCP software in Windows.

Description

smbus module is imported in the script. Thanks to it you can configure and communicate with GPIO over i2c interface. To read the GPIO state use read_byte_data() function. Router will response with six bits corresponding to the actual GPIO states (OUT3, OUT2, OUT1, IN3, IN2 and IN1).

To change the state of a given output use write_byte() function. For example, if all outputs and inputs are in a low state, if you send: bus.write_byte(i2c_chip_address, int('100000',2)) you will set OUT3 to high.

Scripts periodically checking if SMS in format "SET OUTx y" has arrived (where x is the number of output and y is state: H for high and L for low). If yes, it will change the state of a given output, sends SMS confirmation to the SMS sender and delete all messages from memory (AT+CMGD command).

It is also checking if the state of input changed and if yes, it is sends SMS confirmation to the number from phone file (you need to send this file to the router first).

NOTE: For security reasons it is recomended to add numbner filtering (allowlist) and thoroughly check and test the operation of the script.

Script executing

To execute uploaded script you can send below command over ssh interface: python gpio_sms

If you want to run the script automatically every time you turn on the router go to System -> Startup -> Local Startup

Plik:Example gpio sms py.png