Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
tutorials_devicehub [2016/12/16 05:00] admin [Data Transmission] |
tutorials_devicehub [2017/04/01 06:50] (current) admin [Sending Commands to the Nodes] |
||
---|---|---|---|
Line 36: | Line 36: | ||
def analog_input(dev, sensor): | def analog_input(dev, sensor): | ||
value = randint(0, 1023) | value = randint(0, 1023) | ||
- | sensor.addValue(value) | + | sensor.addValue(float(value.strip())) |
dev.send() | dev.send() | ||
print value | print value | ||
Line 178: | Line 178: | ||
DEVICE_UUID = 'your Device UUID' | DEVICE_UUID = 'your Device UUID' | ||
API_KEY = 'your API KEY' | API_KEY = 'your API KEY' | ||
- | AN_SENSOR_NAME = 'LED' #make sure your actuator has the same name on DeviceHub! | + | ACTUATOR_NAME1 = 'LED' #make sure your actuator has the same name on DeviceHub! |
Line 201: | Line 201: | ||
</code> | </code> | ||
- | Similarly, you can add an actuator with an Analog input to the DeviceHub interface. Name it RED, and use the following code for testing: | + | {{ ::actuators.png?600 |}} |
+ | |||
+ | Similarly, you can add an actuator with an Analog input to the DeviceHub interface. Let's add three of them and control the RGB LED on the Sparrow Node. Name them Red, Green and Blue and use the following code for testing: | ||
<code python> | <code python> | ||
from devicehub import Sensor, Actuator, Device, Project | from devicehub import Sensor, Actuator, Device, Project | ||
from time import sleep | from time import sleep | ||
+ | import serial | ||
PROJECT_ID = 'your project ID' | PROJECT_ID = 'your project ID' | ||
DEVICE_UUID = 'your Device UUID' | DEVICE_UUID = 'your Device UUID' | ||
API_KEY = 'your API KEY' | API_KEY = 'your API KEY' | ||
- | AN_SENSOR_NAME = 'RED' #make sure your actuator has the same name on DeviceHub! | + | AN1_SENSOR_NAME = 'Red' #make sure your actuator has the same name on DeviceHub! |
- | + | AN2_SENSOR_NAME = 'Green' #make sure your actuator has the same name on DeviceHub! | |
- | + | AN3_SENSOR_NAME = 'Blue' #make sure your actuator has the same name on DeviceHub! | |
+ | |||
+ | red = "0" | ||
+ | green = "0" | ||
+ | blue = "0" | ||
def act1_callback(payload): | def act1_callback(payload): | ||
- | """ | + | global red |
- | :param payload: mqtt payload message | + | |
- | """ | + | |
- | + | ||
print ACT1.state | print ACT1.state | ||
- | + | red = str(ACT1.state) | |
+ | |||
+ | def act2_callback(payload): | ||
+ | global green | ||
+ | print ACT2.state | ||
+ | green = str(ACT2.state) | ||
+ | |||
+ | def act3_callback(payload): | ||
+ | global blue | ||
+ | print ACT3.state | ||
+ | blue = str(ACT3.state) | ||
+ | |||
project = Project(PROJECT_ID) | project = Project(PROJECT_ID) | ||
device = Device(project, DEVICE_UUID, API_KEY) | device = Device(project, DEVICE_UUID, API_KEY) | ||
- | ACT1 = Actuator(Actuator.ANALOG, ACTUATOR_NAME1) | + | ACT1 = Actuator(Actuator.ANALOG, AN1_SENSOR_NAME) |
- | + | ACT2 = Actuator(Actuator.ANALOG, AN2_SENSOR_NAME) | |
+ | ACT3 = Actuator(Actuator.ANALOG, AN3_SENSOR_NAME) | ||
device.addActuator(ACT1, act1_callback) | device.addActuator(ACT1, act1_callback) | ||
- | + | device.addActuator(ACT2, act2_callback) | |
+ | device.addActuator(ACT3, act3_callback) | ||
+ | |||
+ | # Make sure you have the correct port and baud rate selected | ||
+ | # For Windows, use COMx instead of ttyUSB | ||
+ | ser=serial.Serial('/dev/ttyUSB0', 9600) | ||
try: | try: | ||
while True: | while True: | ||
- | pass | + | ser.write(red + ',' + green + ',' + blue + '\n') #send RGB values as CSV |
+ | sleep(1.0) | ||
except KeyboardInterrupt: | except KeyboardInterrupt: | ||
print 'Goodbye!' | print 'Goodbye!' | ||
| | ||
+ | </code> | ||
+ | |||
+ | Now we need to write an Arduino sketch for the Sparrow node to read the CSV data from the serial port and refresh the color on the LED: | ||
+ | |||
+ | <code C> | ||
+ | |||
+ | // pins for the LEDs: | ||
+ | const int redPin = 8; | ||
+ | const int greenPin = 11; | ||
+ | const int bluePin = 10; | ||
+ | |||
+ | void setup() { | ||
+ | // initialize serial: | ||
+ | Serial.begin(9600); | ||
+ | // make the pins outputs: | ||
+ | pinMode(redPin, OUTPUT); | ||
+ | pinMode(greenPin, OUTPUT); | ||
+ | pinMode(bluePin, OUTPUT); | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // if there's any serial available, read it: | ||
+ | while (Serial.available() > 0) { | ||
+ | |||
+ | // look for the next valid integer in the incoming serial stream: | ||
+ | int red = Serial.parseInt(); | ||
+ | // do it again: | ||
+ | int green = Serial.parseInt(); | ||
+ | // do it again: | ||
+ | int blue = Serial.parseInt(); | ||
+ | |||
+ | // look for the newline. That's the end of your | ||
+ | // sentence: | ||
+ | if (Serial.read() == '\n') { | ||
+ | // constrain the values to 0 - 255 and invert | ||
+ | // if you're using a common-cathode LED, just use "constrain(color, 0, 255);" | ||
+ | red = 255 - constrain(red, 0, 255); | ||
+ | green = 255 - constrain(green, 0, 255); | ||
+ | blue = 255 - constrain(blue, 0, 255); | ||
+ | |||
+ | // fade the red, green, and blue legs of the LED: | ||
+ | analogWrite(redPin, red); | ||
+ | analogWrite(greenPin, green); | ||
+ | analogWrite(bluePin, blue); | ||
+ | |||
+ | // print the three numbers in one string as hexadecimal: | ||
+ | Serial.print(red, HEX); | ||
+ | Serial.print(green, HEX); | ||
+ | Serial.println(blue, HEX); | ||
+ | } | ||
+ | } | ||
+ | } | ||
</code> | </code> |