-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio-basic.py
37 lines (26 loc) · 1.04 KB
/
gpio-basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/python3
import time
import random
import gpiod
from gpiod.line import Direction, Value
from arduino_iot_cloud import ArduinoCloudClient
DEVICE_ID = b"YOUR_DEVICE_ID"
SECRET_KEY = b"YOUR_SECRET_KEY"
LED=14 # GPIO14, Pin 6
chip = gpiod.Chip('/dev/gpiochip4')
req=chip.request_lines(consumer="rpi-acloud-gpio-basic",config={LED : gpiod.LineSettings(direction=Direction.OUTPUT)})
# This function is executed every 10.0 seconds (as defined in the registration)
def read_value(client):
return random.randint(0, 100)
# This function is executed each time the "led" variable changes
def on_led_changed(client, value):
if value:
req.set_value(LED, Value.ACTIVE)
else:
req.set_value(LED, Value.INACTIVE)
print("LED change! Status is: ", value)
if __name__ == "__main__":
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
client.register("test_value", on_read=read_value, interval=10.0)
client.register("led", value=None, on_write=on_led_changed)
client.start()