This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
allthingstalk [2018/11/13 17:32] dan.tudose [Trimiterea de date] |
allthingstalk [2018/11/16 10:07] (current) dan.tudose [Trimiterea de date] |
||
|---|---|---|---|
| Line 58: | Line 58: | ||
| </ | </ | ||
| - | În Python, folosiți următorul exemplu pentru a deschide o conexiune serială cu placa Sparrow și a citi datele: | + | În Python, folosiți următorul exemplu pentru a deschide o conexiune serială cu placa Sparrow și a citi datele. Va trebui să instalați în prealabil biblioteca // |
| + | |||
| + | <code shell> > sudo pip3 install pyserial</ | ||
| <code python> | <code python> | ||
| Line 70: | Line 72: | ||
| while True: | while True: | ||
| line = ser.readline() | line = ser.readline() | ||
| - | print line | + | print(line) |
| sleep(1.0) | sleep(1.0) | ||
| </ | </ | ||
| Line 117: | Line 119: | ||
| < | < | ||
| - | În mod similar, puteți să adăugați un actuator cu input analogic | + | În mod similar, puteți să adăugați un actuator cu input analogic. |
| Folosiți următorul exemplu de cod pentru a-l testa: | Folosiți următorul exemplu de cod pentru a-l testa: | ||
| <code python> | <code python> | ||
| - | from devicehub | + | import time |
| - | from time import sleep | + | from allthingstalk |
| - | PROJECT_ID | + | # Parameters used to authorize and identify |
| - | DEVICE_UUID | + | # Get them on maker.allthingstalk.com |
| - | API_KEY | + | DEVICE_TOKEN |
| - | AN_SENSOR_NAME | + | DEVICE_ID |
| - | | ||
| - | def act1_callback(payload): | ||
| - | """ | ||
| - | :param payload: mqtt payload message | ||
| - | """ | ||
| - | print ACT1.state | + | class LedActuator(Device): |
| + | picker = IntegerAsset(kind=Asset.ACTUATOR) | ||
| - | project = Project(PROJECT_ID) | ||
| - | device = Device(project, | ||
| - | ACT1 = Actuator(Actuator.ANALOG, ACTUATOR_NAME1) | + | # Authorize and connect your device with the Cloud |
| + | client | ||
| + | device = LedActuator(client=client, id=DEVICE_ID) | ||
| - | device.addActuator(ACT1, | ||
| - | try: | + | @LedActuator.command.picker |
| - | while True: | + | def on_picker(device, |
| - | pass | + | print('Led brightness updated to %s.' |
| - | except KeyboardInterrupt: | + | |
| - | print 'Goodbye!' | + | |
| + | |||
| + | while True: | ||
| + | print(' | ||
| + | time.sleep(5) | ||
| </ | </ | ||
| - | < | + | < |