26. Arduino and Python
>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
>>> while True:
... print ser.readline()
'1 Hello world!rn'
'2 Hello world!rn'
'3 Hello world!rn'
>>> import serial # if you have not already done so
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
>>> ser.write('5')
http://playground.arduino.cc/interfacing/python 26
62. /*
IMPORTANT NOTE about TembooAccount.h
TembooAccount.h contains your Temboo account information and must be included
alongside your sketch. To do so, make a new tab in Arduino, call it TembooAccount.h,
and copy this content into it.
*/
#define TEMBOO_ACCOUNT "caterpillar" // Your Temboo account name
#define TEMBOO_APP_KEY_NAME "myFirstApp" // Your Temboo app key name
#define TEMBOO_APP_KEY "e2f55cf1c2524ae59e70a89d3f96f831" // Your Temboo app key
/*
The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
Keeping your account information in a separate file means you can share the
main .ino file without worrying that you forgot to delete your credentials.
*/
TembooAccount.h
62
63. #include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below
int numRuns = 1; // Execution count, so this doesn't run forever
int maxRuns = 10; // Maximum number of times the Choreo should be executed
void setup() {
Serial.begin(9600);
// For debugging, wait until the serial console is connected
delay(4000);
while(!Serial);
Bridge.begin();
}
void loop() {
if (numRuns <= maxRuns) {
Serial.println("Running GetWeatherByAddress - Run #" + String(numRuns++));
TembooChoreo GetWeatherByAddressChoreo;
// Invoke the Temboo client
GetWeatherByAddressChoreo.begin();
// Set Temboo account credentials
GetWeatherByAddressChoreo.setAccountName(TEMBOO_ACCOUNT);
GetWeatherByAddressChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
GetWeatherByAddressChoreo.setAppKey(TEMBOO_APP_KEY);
// Set Choreo inputs
GetWeatherByAddressChoreo.addInput("Address", "Taipei");
// Identify the Choreo to run
GetWeatherByAddressChoreo.setChoreo("/Library/Yahoo/Weather/GetWeatherByAddress");
// Run the Choreo; when results are available, print them to serial
GetWeatherByAddressChoreo.run();
while(GetWeatherByAddressChoreo.available()) {
char c = GetWeatherByAddressChoreo.read();
Serial.print(c);
}
GetWeatherByAddressChoreo.close();
}
Serial.println("Waiting...");
delay(30000); // wait 30 seconds between GetWeatherByAddress calls
}
透過 USB 連接
TembooAccount.ino
63
74. 儲存物件
#include <Bridge.h>
#include <Parse.h>
void setup() {
// Initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
// Initialize Bridge
Bridge.begin();
// Initialize Serial
Serial.begin(9600);
while (!Serial); // wait for a serial connection
Serial.println("Parse Starter Project");
// Initialize Parse
Parse.begin("dOpHkBs6A2XOToydYC7r7r1BmFxCgd6nU7JQ85Fw",
"BGyFLJuHJvje0LB6Ms9ZN30Mh1DfoKE43V3bq3FN");
74
75. ParseObjectCreate create;
create.setClassName("TestObject");
create.add("foo", "bar");
ParseResponse response = create.send();
Serial.println("nResponse for saving a TestObject:");
Serial.print(response.getJSONBody());
if (!response.getErrorCode()) {
String objectId = response.getString("objectId");
Serial.print("Test object id:");
Serial.println(objectId);
} else {
Serial.println("Failed to save the object");
}
response.close(); // Do not forget to free the resource
}
75
78. void loop() {
// Check if there is a new push
// A push with message {"alert":"A test push from Parse!"}
// will turn on LED for 3 seconds
if (Parse.pushAvailable()) {
ParsePush push = Parse.nextPush();
String message = push.getJSONBody();
Serial.print("New push message size: ");
Serial.println(message.length());
Serial.print("New push message content: ");
Serial.println(message);
String command = push.getString("alert");
if (command == "A test push from Parse!") {
digitalWrite(13, HIGH); // turn on LED
delay(3000); // wait 3 seconds
digitalWrite(13, LOW); // turn off LED
}
// NOTE: ensure to close current push message
// otherwise next push won't be available
push.close();
}
}
78