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 16:35] dan.tudose [AllThingsTalk] |
allthingstalk [2018/11/16 10:07] (current) dan.tudose [Trimiterea de date] |
||
---|---|---|---|
Line 28: | Line 28: | ||
import time | import time | ||
- | from allthingstalk import Client, Device, | + | from allthingstalk import Client, Device, |
+ | |||
+ | # Parameters used to authorize and identify your device | ||
+ | # Get them on maker.allthingstalk.com | ||
+ | DEVICE_TOKEN = '< | ||
+ | DEVICE_ID = '< | ||
class RandomDevice(Device): | class RandomDevice(Device): | ||
- | temperature = IntegerAsset() | + | temperature = NumberAsset(unit=' |
- | client = Client('your Device token') | + | client = Client(DEVICE_TOKEN) |
- | device = RandomDevice(client=client, | + | device = RandomDevice(client=client, |
while True: | while True: | ||
Line 53: | 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 65: | Line 72: | ||
while True: | while True: | ||
line = ser.readline() | line = ser.readline() | ||
- | print line | + | print(line) |
sleep(1.0) | sleep(1.0) | ||
</ | </ | ||
Line 76: | Line 83: | ||
==== Trimiterea de comenzi ==== | ==== Trimiterea de comenzi ==== | ||
- | Putem să trimitem și comenzi din interfața | + | Putem să trimitem și comenzi din interfața |
- | Rulați următorul exemplu Python pentru a citi starea actuatorului vostru din DeviceHub. | + | Rulați următorul exemplu Python pentru a citi starea actuatorului vostru din AllThingsTalk. Nu vă faceți griji în legătură cu actuatorul, el va fi creat automat de către platformă la rularea codului Python. |
<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 | ||
- | project = Project(PROJECT_ID) | + | class LedActuator(Device): |
- | device | + | |
- | ACT1 = Actuator(Actuator.DIGITAL, | ||
- | device.addActuator(ACT1, act1_callback) | + | # Authorize and connect your device |
+ | client = Client(DEVICE_TOKEN) | ||
+ | device = LedActuator(client=client, id=DEVICE_ID) | ||
- | try: | + | |
- | while True: | + | @LedActuator.command.led |
- | pass | + | def on_led(device, |
- | except KeyboardInterrupt: | + | |
- | print 'Goodbye!' | + | |
+ | |||
+ | while True: | ||
+ | print('Waiting for actuation...') | ||
+ | time.sleep(5) | ||
</ | </ | ||
Line 113: | 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) | ||
</ | </ | ||
- | < | + | < |