这可能是一个新手的错误,但我完全不知道该怎么做。
我试图根据我在互联网上找到的代码制作一个代码来阅读推文,但发现Twitter改变了他们的API并且不能再这样做了。
我想要做的就是阅读一条消息并从我的Arduino激活一个引脚来打开模型车库。
到目前为止,我在wix上创建了一个网站,我的arduino能够阅读它。
但是,看它是否适合第一种情况的比较根本不起作用,即使它应该与之前的印刷品相同。
我使用了tweet.length(),它告诉我我的字符串长31个字符,这意味着没有任何隐藏的字符,对吧?
非常感谢任何帮助!!
#include <SPI.h>
#include <Ethernet.h>
boolean requested; // whether you've made a request since connecting
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
boolean garage_status = false;
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false;
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "gusat92.wix.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10,32,153,88);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
pinMode(2, OUTPUT);
Serial.begin(9600);
currentLine.reserve(256);
tweet.reserve(150);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /garage?_escaped_fragment_ HTTP/1.1");
client.println("Host: gusat92.wix.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("\"Text\">")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '&') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
Serial.println(tweet.length());
if(tweet == " <h2 class=\"font_2\">Opener"){//this part is never true
digitalWrite(2, HIGH);
delay(5000);
Serial.println("LED ON!");
garage_status=true;
digitalWrite(2, LOW);
}
else {Serial.println("no out");}
if(tweet != " <h2 class=\"font_2\">Closer" && garage_status==true){
digitalWrite(3, HIGH);
delay(5000);
Serial.println("LED OFF!");
garage_status=false;
digitalWrite(3, LOW);
}
// close the connection to the server:
client.stop();
}
}
}
}
}
这是打印的&gt;
connecting...
connected
>
<h2 class="font_2">Opener
31
no out
推文部分与比较完全相同,对吗? :S