|
| 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 | +} |
0 commit comments