|
| 1 | +/* |
| 2 | + Get twitter status |
| 3 | +
|
| 4 | + This example shows a REST API GET using OAuth |
| 5 | + authentication. It then parses the JSON response. |
| 6 | +
|
| 7 | + OAuth credentials can be retrieved from your Twitter |
| 8 | + developer account after creating a new app: |
| 9 | +
|
| 10 | + https://developer.twitter.com/en/apps |
| 11 | +
|
| 12 | + Circuit: |
| 13 | + |
| 14 | + - Arduino 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 <WiFiNINA.h> |
| 24 | +#include <JSON.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 | + Serial.print("Waiting for the network time to sync "); |
| 71 | + while (getTime() == 0) { |
| 72 | + Serial.print("."); |
| 73 | + delay(1000); |
| 74 | + } |
| 75 | + Serial.println(); |
| 76 | + Serial.println(); |
| 77 | + |
| 78 | + // assign the OAuth credentials |
| 79 | + oauthClient.setCredentials(consumerKey, consumerKeySecret, accessToken, accessTokenSecret); |
| 80 | + |
| 81 | + // assign the callback to get the current epoch time, the epoch time is |
| 82 | + // needed for every OAuth request, as it's used in the HTTP "Authorization" |
| 83 | + // request header value and to calculate the request's signature |
| 84 | + oauthClient.onGetTime(getTime); |
| 85 | +} |
| 86 | + |
| 87 | +unsigned long getTime() { |
| 88 | + // get the current time from the WiFi module |
| 89 | + return WiFi.getTime(); |
| 90 | +} |
| 91 | + |
| 92 | +void loop() { |
| 93 | + // Twitter API requests latest Arduino status |
| 94 | + oauthClient.get("/1.1/statuses/user_timeline.json?screen_name=arduino&count=1"); |
| 95 | + |
| 96 | + int statusCode = oauthClient.responseStatusCode(); |
| 97 | + String response = oauthClient.responseBody(); |
| 98 | + |
| 99 | + if (statusCode != 200) { |
| 100 | + // An error occurred |
| 101 | + Serial.println(statusCode); |
| 102 | + Serial.println(response); |
| 103 | + } |
| 104 | + |
| 105 | + else |
| 106 | + |
| 107 | + { |
| 108 | + // Parse JSON response |
| 109 | + JSONVar statusesObject = JSON.parse(response); |
| 110 | + Serial.println(""); |
| 111 | + Serial.println("@arduino twitter status: "); |
| 112 | + Serial.println(statusesObject[0]["text"]); |
| 113 | + Serial.print("Retweeted: "); |
| 114 | + Serial.println(statusesObject[0]["retweet_count"]); |
| 115 | + Serial.print("Favorited: "); |
| 116 | + Serial.println(statusesObject[0]["favorite_count"]); |
| 117 | + } |
| 118 | + |
| 119 | + // Wait one minute (see Twitter API rate limits before changing) |
| 120 | + delay(60 * 1000L); |
| 121 | +} |
0 commit comments