APC Australia

Control the GPIO

-

In the tutorial we created a function that would react to a command from the user and post data to Telegram. We can also use Telegram to control the GPIO of a Raspberry Pi.

The code remains very similar to the tutorial code, but we use the GPIO Zero library and import the LED class, then we set GPIO 17 as the pin for our LED. from gpiozero import LED led = LED(17)

We then change the message handler to look for /led-on . If that command is issued it runs the GPIO Zero code to turn on the LED.

@tb.message_handler(func=lambda msg: msg.text is not None and ‘/led-on’ in msg.text) def send_welcome(message): tb.reply_to(message, ‘LED On’) led.on()

The code to turn off the LED is very similar to turning it on. @tb.message_handler(func=lambda msg: msg.text is not None and ‘/led-off’ in msg.text) def send_welcome(message): tb.reply_to(message, ‘LED Off’) led.off()

Run the code and you can control an LED over Telegram!

We’ve made a video demonstrat­ing for more go see this: http://bit.ly/lxf256tele­gram2

 ??  ?? Setting up an LED with the Pi is very simple, as you can see here.
Setting up an LED with the Pi is very simple, as you can see here.

Newspapers in English

Newspapers from Australia