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

Z Elproma Wiki Knowledge Base
Skocz do: nawigacja, szukaj
 
Linia 38: Linia 38:
/usr/bin/python /root/gpio_sms
/usr/bin/python /root/gpio_sms
Use ''Save'' button and reboot the router for the changes to take effect.
Use ''Save'' button and reboot the router for the changes to take effect.
[[test_translate]]

Aktualna wersja na dzień 15:02, 19 kwi 2024

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: gpio_sms

Script uploading

You can upload the program to /root/ directory over scp protocol. Simple use scp command in Linux 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). Below is the pinout of GPIO connector in RBMTX-Lite-IO variant.

Gpio pinout.png

* - can be extended to Digital Input, range from 0 to max. 30V (option)

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. Then, add below command before exit 0 line: /usr/bin/python /root/gpio_sms Use Save button and reboot the router for the changes to take effect.