Skip to content

Commit 768d81e

Browse files
committed
initial commit
0 parents  commit 768d81e

28 files changed

+2396
-0
lines changed

examples/blink/Blink.ino

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <Parse.h>
2+
#include <Bridge.h>
3+
4+
/***** A simple demo of Parse for IoT SDK *****/
5+
/*
6+
* Press button(A button shield needed) - the device will cycle to next LED states(off->on->blink->off...)
7+
* and sends the latest LED state to Parse
8+
* Push message received - {"alert":"blink/on/off"} -> the led will behave correspondingly and sends
9+
* back an ack to Parse
10+
*/
11+
12+
ParseClient parse;
13+
String ledState;
14+
const int BUFSIZE = 200;
15+
16+
// set LED state and send state to Parse
17+
// This method must be called after parse.initialize()
18+
void setLedState(char state[]) {
19+
ledState = state;
20+
21+
// send current led state to parse
22+
ParseObjectCreate create;
23+
create.setClassName("Event");
24+
create.add("installationId", parse.getInstallationId());
25+
create.add("alarm", true);
26+
String value = "{\"state\":\"";
27+
value += state;
28+
value += "\"}";
29+
create.addJSONValue("value", value);
30+
ParseResponse response = create.send();
31+
32+
String eventId;
33+
Serial.println("\nSetting LED state...");
34+
Serial.print(response.getJSONBody());
35+
if (!response.getErrorCode()) {
36+
eventId = response.getString("objectId");
37+
Serial.print("Event id:");
38+
Serial.println(eventId);
39+
} else {
40+
Serial.println("Failed to notify Parse the LED state.");
41+
}
42+
response.close();
43+
}
44+
45+
void setup() {
46+
// Initialize digital pin 13 as an output.
47+
pinMode(13, OUTPUT);
48+
49+
// Initialize Bridge (with linux)
50+
Bridge.begin();
51+
52+
// Initialize Serial
53+
Serial.begin(9600);
54+
55+
while (!Serial); // wait for a console connection
56+
57+
// appId and clientKey will be provided in provisioning
58+
parse.begin(NULL, NULL);
59+
60+
// do provisioning now
61+
Serial.println("Please go to arduino.local/parse_config.html to complete device provisioning. Press y when you are done.");
62+
while (1) {
63+
if(Serial.available()) {
64+
char c = (char)Serial.read();
65+
if (c == 'y') {
66+
break; // start blink example
67+
}
68+
}
69+
}
70+
71+
Serial.println("Parse blinky example started");
72+
Serial.println(parse.getInstallationId());
73+
Serial.println(parse.getSessionToken());
74+
75+
// Turn off LED
76+
setLedState("off");
77+
78+
/* start push service */
79+
if(parse.startPushService()) {
80+
Serial.println("\nParse push started\n");
81+
}
82+
}
83+
84+
void loop() {
85+
// set LED light in board based on state
86+
if (ledState == "off") {
87+
digitalWrite(13, LOW); // turn the LED off (LOW is the voltage level)
88+
} else if (ledState == "on") {
89+
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
90+
} else if (ledState == "blink") {
91+
digitalWrite(13, HIGH);
92+
delay(500); // wait half a second
93+
digitalWrite(13, LOW);
94+
delay(500);
95+
}
96+
97+
// check if any new push message
98+
if (parse.pushAvailable()) {
99+
ParsePush push = parse.nextPush();
100+
101+
// print whole JSON body
102+
Serial.print("New push message content: ");
103+
Serial.println(push.getJSONBody());
104+
105+
// set LED state correspondingly
106+
String command = push.getString("alert");
107+
if(command == "on" || command == "off" || command == "blink") {
108+
Serial.print("LED state set to: ");
109+
Serial.println(command);
110+
ledState = command;
111+
} else {
112+
Serial.println("Unknown message");
113+
}
114+
push.close();
115+
}
116+
}

examples/quickstart/QuickStart.ino

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <Parse.h>
2+
#include <Bridge.h>
3+
4+
/***** Quickstart of Parse Arduino Yún SDK *****/
5+
// https://www.parse.com/apps/quickstart#embedded/arduinoyun
6+
7+
ParseClient client;
8+
9+
void setup() {
10+
11+
}
12+
13+
void loop() {
14+
15+
}

keywords.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map For Parse
3+
#######################################
4+
5+
#######################################
6+
# Library (KEYWORD3)
7+
#######################################
8+
9+
Parse KEYWORD3
10+
11+
#######################################
12+
# Datatypes (KEYWORD1)
13+
#######################################
14+
15+
ParseClient KEYWORD1
16+
ParseResponse KEYWORD1
17+
ParsePush KEYWORD1
18+
19+
#######################################
20+
# Methods and Functions (KEYWORD2)
21+
#######################################
22+
23+
begin KEYWORD2

library.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=Parse Arduino Yún SDK
2+
version=1.0.0
3+
author=Parse, LLC.
4+
maintainer=Parse, LLC.
5+
sentence=A library that provides access to Parse
6+
paragraph=Provides convenience methods to access the REST API on Parse.com from Arduino
7+
url=https://github.com/ParsePlatform/parse-embedded-sdks
8+
architectures=avr

src/Parse.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2015, Parse, LLC. All rights reserved.
3+
*
4+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
5+
* copy, modify, and distribute this software in source code or binary form for use
6+
* in connection with the web services and APIs provided by Parse.
7+
*
8+
* As with any software that integrates with the Parse platform, your use of
9+
* this software is subject to the Parse Terms of Service
10+
* [https://www.parse.com/about/terms]. This copyright notice shall be
11+
* included in all copies or substantial portions of the software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
*
20+
*/
21+
22+
#ifndef PARSE_H_
23+
#define PARSE_H_
24+
25+
/*! \mainpage Parse Arduino Yun SDK
26+
*
27+
* The Parse Arduino Yun SDK offers set of classes that enable the use of the
28+
* <a href="https://parse.com">Parse platform</a> Wire-API applications running on
29+
* Arduino Yun platform.
30+
*/
31+
32+
/*! \file Parse.h
33+
* \brief Main include file for object in Yun SDK
34+
* include this file, not individual object files.
35+
*/
36+
37+
#include <internal/ParseClient.h>
38+
#include <internal/ParseResponse.h>
39+
#include <internal/ParsePush.h>
40+
#include <internal/ParseQuery.h>
41+
#include <internal/ParseUtils.h>
42+
#include <internal/ParseObjectCreate.h>
43+
#include <internal/ParseObjectDelete.h>
44+
#include <internal/ParseObjectGet.h>
45+
#include <internal/ParseObjectUpdate.h>
46+
#include <internal/ParseCloudFunction.h>
47+
#include <internal/ParseTrackEvent.h>
48+
49+
#endif

src/internal/ParseClient.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2015, Parse, LLC. All rights reserved.
3+
*
4+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
5+
* copy, modify, and distribute this software in source code or binary form for use
6+
* in connection with the web services and APIs provided by Parse.
7+
*
8+
* As with any software that integrates with the Parse platform, your use of
9+
* this software is subject to the Parse Terms of Service
10+
* [https://www.parse.com/about/terms]. This copyright notice shall be
11+
* included in all copies or substantial portions of the software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
*
20+
*/
21+
22+
#include "ParseClient.h"
23+
24+
ParseClient::ParseClient() {
25+
}
26+
27+
void ParseClient::begin(const char *applicationId, const char *clientKey) {
28+
Console.println("begin");
29+
30+
// send the info to linux through Bridge
31+
if(applicationId) {
32+
Bridge.put("appId", applicationId);
33+
}
34+
if (clientKey) {
35+
Bridge.put("clientKey", clientKey);
36+
}
37+
}
38+
39+
void ParseClient::setInstallationId(const char *installationId) {
40+
this->installationId = installationId;
41+
if (installationId) {
42+
Bridge.put("installationId", installationId);
43+
} else {
44+
Bridge.put("installationId", "");
45+
}
46+
}
47+
48+
const char* ParseClient::getInstallationId() {
49+
if (!this->installationId) {
50+
char *buf = new char[37];
51+
52+
requestClient.begin("parse_request");
53+
requestClient.addParameter("-i");
54+
requestClient.run();
55+
read(&requestClient, buf, 37);
56+
this->installationId = buf;
57+
}
58+
return this->installationId;
59+
}
60+
61+
void ParseClient::setSessionToken(const char *sessionToken) {
62+
if ((sessionToken != NULL) && (strlen(sessionToken) > 0 )){
63+
this->sessionToken = sessionToken;
64+
Bridge.put("sessionToken", sessionToken);
65+
} else {
66+
Bridge.put("sessionToken", "");
67+
}
68+
}
69+
70+
void ParseClient::clearSessionToken() {
71+
setSessionToken(NULL);
72+
}
73+
74+
const char* ParseClient::getSessionToken() {
75+
if (!this->sessionToken) {
76+
char *buf = new char[33];
77+
78+
requestClient.begin("parse_request");
79+
requestClient.addParameter("-s");
80+
requestClient.run();
81+
read(&requestClient, buf, 33);
82+
this->sessionToken = buf;
83+
}
84+
return this->sessionToken;
85+
}
86+
87+
ParseResponse ParseClient::sendRequest(const char* httpVerb, const char* httpPath, const char* requestBody, const char* urlParams) {
88+
return sendRequest(String(httpVerb), String(httpPath), String(requestBody), String(urlParams));
89+
}
90+
91+
ParseResponse ParseClient::sendRequest(const String& httpVerb, const String& httpPath, const String& requestBody, const String& urlParams) {
92+
requestClient.begin("parse_request"); // start a process that launch the "parse_request" command
93+
94+
requestClient.addParameter("-v");
95+
requestClient.addParameter(httpVerb);
96+
requestClient.addParameter("-e");
97+
requestClient.addParameter(httpPath);
98+
if (requestBody != "") {
99+
requestClient.addParameter("-d");
100+
requestClient.addParameter(requestBody);
101+
}
102+
if (urlParams != "") {
103+
requestClient.addParameter("-p");
104+
requestClient.addParameter(urlParams);
105+
requestClient.runAsynchronously();
106+
} else {
107+
requestClient.run(); // Run the process and wait for its termination
108+
}
109+
110+
ParseResponse response(&requestClient);
111+
return response;
112+
}
113+
114+
bool ParseClient::startPushService() {
115+
pushClient.begin("parse_push"); // start a process that launch the "parse_request" command
116+
pushClient.runAsynchronously();
117+
118+
while(1) {
119+
if(pushClient.available()) {
120+
// read the push starting result
121+
char c = pushClient.read();
122+
while(pushClient.available()) {
123+
pushClient.read();
124+
}
125+
if (c == 's') {
126+
pushClient.write('n'); // send a signal that ready to consume next available push
127+
return true;
128+
} else {
129+
return false;
130+
}
131+
}
132+
}
133+
}
134+
135+
ParsePush ParseClient::nextPush() {
136+
ParsePush push(&pushClient);
137+
return push;
138+
}
139+
140+
bool ParseClient::pushAvailable() {
141+
pushClient.write('n'); // send a signal that ready to consume next available push
142+
if(pushClient.available()) {
143+
return 1;
144+
} else {
145+
return 0;
146+
}
147+
}
148+
149+
void ParseClient::stopPushService() {
150+
pushClient.close();
151+
}
152+
153+
void ParseClient::end() {
154+
stopPushService();
155+
}
156+
157+
void ParseClient::read(Process* client, char* buf, int len) {
158+
memset(buf, 0, len);
159+
int p = 0;
160+
while(1) {
161+
if(client->available()) {
162+
while (p < (len-1) && client->available()) {
163+
buf[p++] = client->read();
164+
}
165+
break;
166+
}
167+
}
168+
}
169+
170+
ParseClient Parse;

0 commit comments

Comments
 (0)