1
1
/*
2
2
Get twitter status
3
3
4
- This example shows a REST API GET using OAuth
4
+ This example shows a REST API GET using OAuth
5
5
authentication. It then parses the JSON response.
6
6
7
- OAuth credentials can be retrieved from your Twitter
7
+ OAuth credentials can be retrieved from your Twitter
8
8
developer account after creating a new app:
9
9
10
10
https://developer.twitter.com/en/apps
11
11
12
12
Circuit:
13
-
13
+
14
14
- Arduino MKR WiFi 1010 board
15
15
16
16
This example code is in the public domain.
20
20
#include < ArduinoBearSSL.h> // Arduino_OAuth depends on ArduinoBearSSL
21
21
#include < ArduinoHttpClient.h> // Arduino_OAuth depends on ArduinoHttpClient
22
22
#include < Arduino_OAuth.h>
23
+ #include < Arduino_JSON.h>
23
24
#include < WiFiNINA.h>
24
- #include < JSON.h>
25
25
26
26
#include " arduino_secrets.h"
27
27
// /////please enter your sensitive data in the Secret tab/arduino_secrets.h
@@ -38,6 +38,8 @@ int status = WL_IDLE_STATUS; // the Wifi radio's status
38
38
WiFiSSLClient wifiSSLClient;
39
39
OAuthClient oauthClient (wifiSSLClient, " api.twitter.com" , 443 );
40
40
41
+ String twitterHandle = " arduino" ; // Twitter handle to retrieve Tweets from
42
+
41
43
void setup () {
42
44
// Initialize serial and wait for port to open:
43
45
Serial.begin (9600 );
@@ -91,7 +93,7 @@ unsigned long getTime() {
91
93
92
94
void loop () {
93
95
// Twitter API requests latest Arduino status
94
- oauthClient.get (" /1.1/statuses/user_timeline.json?screen_name=arduino &count=1" );
96
+ oauthClient.get (" /1.1/statuses/user_timeline.json?screen_name=" + twitterHandle + " &count=1" );
95
97
96
98
int statusCode = oauthClient.responseStatusCode ();
97
99
String response = oauthClient.responseBody ();
@@ -100,21 +102,24 @@ void loop() {
100
102
// An error occurred
101
103
Serial.println (statusCode);
102
104
Serial.println (response);
103
- }
104
-
105
- else
106
-
107
- {
105
+ } else {
108
106
// Parse JSON response
109
107
JSONVar statusesObject = JSON.parse (response);
110
- Serial.println (" " );
111
- Serial.println (" @arduino twitter status: " );
108
+
109
+ // print the handle
110
+ Serial.print (" @" );
111
+ Serial.print (twitterHandle);
112
+ Serial.println (" 's twitter status: " );
113
+
114
+ // print the tweet text, retweet + favorite counts
115
+ // we only care about the first item
112
116
Serial.println (statusesObject[0 ][" text" ]);
113
- Serial.print (" Retweeted : " );
117
+ Serial.print (" Retweets : " );
114
118
Serial.println (statusesObject[0 ][" retweet_count" ]);
115
- Serial.print (" Favorited : " );
119
+ Serial.print (" Likes : " );
116
120
Serial.println (statusesObject[0 ][" favorite_count" ]);
117
121
}
122
+ Serial.println ();
118
123
119
124
// Wait one minute (see Twitter API rate limits before changing)
120
125
delay (60 * 1000L );
0 commit comments