PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwitterClient.ino 4.3KB

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Twitter Client with Strings
  3. This sketch connects to Twitter using an Ethernet shield. It parses the XML
  4. returned, and looks for <text>this is a tweet</text>
  5. You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
  6. either one will work, as long as it's got a Wiznet Ethernet module on board.
  7. This example uses the DHCP routines in the Ethernet library which is part of the
  8. Arduino core from version 1.0 beta 1
  9. This example uses the String library, which is part of the Arduino core from
  10. version 0019.
  11. Circuit:
  12. * Ethernet shield attached to pins 10, 11, 12, 13
  13. created 21 May 2011
  14. modified 9 Apr 2012
  15. by Tom Igoe
  16. This code is in the public domain.
  17. */
  18. #include <SPI.h>
  19. #include <NativeEthernet.h>
  20. // Enter a MAC address and IP address for your controller below.
  21. // The IP address will be dependent on your local network:
  22. byte mac[] = {
  23. 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01
  24. };
  25. IPAddress ip(192, 168, 1, 20);
  26. // initialize the library instance:
  27. EthernetClient client;
  28. const unsigned long requestInterval = 60000; // delay between requests
  29. char serverName[] = "api.twitter.com"; // twitter URL
  30. boolean requested; // whether you've made a request since connecting
  31. unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
  32. String currentLine = ""; // string to hold the text from server
  33. String tweet = ""; // string to hold the tweet
  34. boolean readingTweet = false; // if you're currently reading the tweet
  35. void setup() {
  36. // You can use Ethernet.init(pin) to configure the CS pin
  37. //Ethernet.init(10); // Most Arduino shields
  38. //Ethernet.init(5); // MKR ETH shield
  39. //Ethernet.init(0); // Teensy 2.0
  40. //Ethernet.init(20); // Teensy++ 2.0
  41. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  42. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  43. // reserve space for the strings:
  44. currentLine.reserve(256);
  45. tweet.reserve(150);
  46. // Open serial communications and wait for port to open:
  47. Serial.begin(9600);
  48. while (!Serial) {
  49. ; // wait for serial port to connect. Needed for Leonardo only
  50. }
  51. // attempt a DHCP connection:
  52. Serial.println("Attempting to get an IP address using DHCP:");
  53. if (!Ethernet.begin(mac)) {
  54. // if DHCP fails, start with a hard-coded address:
  55. Serial.println("failed to get an IP address using DHCP, trying manually");
  56. Ethernet.begin(mac, ip);
  57. }
  58. Serial.print("My address:");
  59. Serial.println(Ethernet.localIP());
  60. // connect to Twitter:
  61. connectToServer();
  62. }
  63. void loop()
  64. {
  65. if (client.connected()) {
  66. if (client.available()) {
  67. // read incoming bytes:
  68. char inChar = client.read();
  69. // add incoming byte to end of line:
  70. currentLine += inChar;
  71. // if you get a newline, clear the line:
  72. if (inChar == '\n') {
  73. currentLine = "";
  74. }
  75. // if the current line ends with <text>, it will
  76. // be followed by the tweet:
  77. if ( currentLine.endsWith("<text>")) {
  78. // tweet is beginning. Clear the tweet string:
  79. readingTweet = true;
  80. tweet = "";
  81. }
  82. // if you're currently reading the bytes of a tweet,
  83. // add them to the tweet String:
  84. if (readingTweet) {
  85. if (inChar != '<') {
  86. tweet += inChar;
  87. } else {
  88. // if you got a "<" character,
  89. // you've reached the end of the tweet:
  90. readingTweet = false;
  91. Serial.println(tweet);
  92. // close the connection to the server:
  93. client.stop();
  94. }
  95. }
  96. }
  97. }
  98. else if (millis() - lastAttemptTime > requestInterval) {
  99. // if you're not connected, and two minutes have passed since
  100. // your last connection, then attempt to connect again:
  101. connectToServer();
  102. }
  103. }
  104. void connectToServer() {
  105. // attempt to connect, and wait a millisecond:
  106. Serial.println("connecting to server...");
  107. if (client.connect(serverName, 80)) {
  108. Serial.println("making HTTP request...");
  109. // make HTTP GET request to twitter:
  110. client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
  111. client.println("HOST: api.twitter.com");
  112. client.println("Connection: close");
  113. client.println();
  114. }
  115. // note the time of this connect attempt:
  116. lastAttemptTime = millis();
  117. }