Skip to content

Commit 37de0f1

Browse files
committed
Initial import
0 parents  commit 37de0f1

File tree

10 files changed

+703
-0
lines changed

10 files changed

+703
-0
lines changed

README.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
= Arduino_OAuth =
2+
3+
OAuth client library for Arduino.
4+
5+
== License ==
6+
7+
Copyright (c) 2019 Arduino SA. All rights reserved.
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

examples/Tweeter/Tweeter.ino

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Tweeter
3+
4+
This sketch demonstrates how to post a Tweet directly to
5+
Twitter via the Twitter's HTTP API using OAuth for authenticaion.
6+
7+
OAuth credentials can be retrieved from the following
8+
website, using your Twitter account and creating a new
9+
app:
10+
11+
https://developer.twitter.com/en/apps
12+
13+
Circuit:
14+
- MKR WiFi 1010 board
15+
16+
This example code is in the public domain.
17+
*/
18+
19+
#include <ArduinoECCX08.h> // ArduinoBearSSL depends on ArduinoECCX08
20+
#include <ArduinoBearSSL.h> // Arduino_OAuth depends on ArduinoBearSSL
21+
#include <ArduinoHttpClient.h> // Arduino_OAuth depends on ArduinoHttpClient
22+
#include <Arduino_OAuth.h>
23+
#include <utility/PercentEncoder.h> // from Arduino_OAuth
24+
#include <WiFiNINA.h>
25+
26+
#include "arduino_secrets.h"
27+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28+
const char ssid[] = SECRET_SSID; // your network SSID (name)
29+
const char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30+
31+
const char consumerKey[] = SECRET_CONSUMER_KEY;
32+
const char consumerKeySecret[] = SECRET_CONSUMER_KEY_SECRET;
33+
const char accessToken[] = SECRET_ACCESS_TOKEN;
34+
const char accessTokenSecret[] = SECRET_ACCESS_TOKEN_SECRET;
35+
36+
int status = WL_IDLE_STATUS; // the Wifi radio's status
37+
38+
WiFiSSLClient wifiSSLClient;
39+
OAuthClient oauthClient(wifiSSLClient, "api.twitter.com", 443);
40+
41+
void setup() {
42+
//Initialize serial and wait for port to open:
43+
Serial.begin(9600);
44+
while (!Serial) {
45+
; // wait for serial port to connect. Needed for native USB port only
46+
}
47+
48+
// check for the WiFi module:
49+
if (WiFi.status() == WL_NO_MODULE) {
50+
Serial.println("Communication with WiFi module failed!");
51+
// don't continue
52+
while (true);
53+
}
54+
55+
// attempt to connect to Wifi network:
56+
while (status != WL_CONNECTED) {
57+
Serial.print("Attempting to connect to WPA SSID: ");
58+
Serial.println(ssid);
59+
// Connect to WPA/WPA2 network:
60+
status = WiFi.begin(ssid, pass);
61+
62+
// wait 10 seconds for connection:
63+
delay(10000);
64+
}
65+
66+
// you're connected now
67+
Serial.println("You're connected to the network");
68+
Serial.println();
69+
70+
// assign the OAuth credentials
71+
oauthClient.setCredentials(consumerKey, consumerKeySecret, accessToken, accessTokenSecret);
72+
73+
// assign the callback to get the current epoch time from the WiFi module
74+
oauthClient.onGetTime(getTime);
75+
}
76+
77+
void loop() {
78+
String status;
79+
80+
// create the status text
81+
status += "millis() is now: ";
82+
status += millis();
83+
84+
tweet(status);
85+
86+
// wait one minute before Tweeting again
87+
delay(60 * 1000L);
88+
}
89+
90+
unsigned long getTime() {
91+
// get the current time from the WiFi module
92+
return WiFi.getTime();
93+
}
94+
95+
void tweet(String text) {
96+
Serial.println("Sending tweet: ");
97+
Serial.println(text);
98+
99+
String requestBody;
100+
101+
// build the URL encoded request body, the text must be percent encoded
102+
requestBody += "status=";
103+
requestBody += PercentEncoder.encode(text);
104+
105+
// HTTP POST it via the OAuth client, which sets the Authorization header for us
106+
oauthClient.post("/1.1/statuses/update.json", "application/x-www-form-urlencoded", requestBody);
107+
108+
// read the HTTP status code and body
109+
int statusCode = oauthClient.responseStatusCode();
110+
String responseBody = oauthClient.responseBody();
111+
112+
Serial.print("statusCode = ");
113+
Serial.println(statusCode);
114+
115+
Serial.print("responseBody = ");
116+
Serial.println(responseBody);
117+
118+
Serial.println();
119+
}

examples/Tweeter/arduino_secrets.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
3+
4+
// from https://developer.twitter.com/en/apps
5+
#define SECRET_CONSUMER_KEY ""
6+
#define SECRET_CONSUMER_KEY_SECRET ""
7+
#define SECRET_ACCESS_TOKEN ""
8+
#define SECRET_ACCESS_TOKEN_SECRET ""

keywords.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#######################################
2+
# Syntax Coloring Map For Arduino_OAuth
3+
#######################################
4+
# Class
5+
#######################################
6+
7+
Arduino_OAuth KEYWORD1
8+
OAuthClient KEYWORD1
9+
10+
#######################################
11+
# Methods and Functions
12+
#######################################
13+
14+
setCredentials KEYWORD2
15+
onGetTime KEYWORD2
16+
17+
get KEYWORD2
18+
post KEYWORD2
19+
put KEYWORD2
20+
patch KEYWORD2
21+
del KEYWORD2
22+
23+
responseStatusCode KEYWORD2
24+
responseBody KEYWORD2
25+
26+
#######################################
27+
# Constants
28+
#######################################

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Arduino_OAuth
2+
version=0.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=[BETA] OAuth client library for Arduino.
6+
paragraph=
7+
category=Communication
8+
url=http://github.com/arduino-libraries/Arduino_OAuth
9+
architectures=*
10+
includes=Arduino_OAuth.h

src/Arduino_OAuth.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
This file is part of the Arduino OAuth library.
3+
Copyright (c) 2019 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef ARDUINO_OAUTH_H_
21+
#define ARDUINO_OAUTH_H_
22+
23+
#include "OAuthClient.h"
24+
25+
#endif

0 commit comments

Comments
 (0)