尝试使用Arduino Adafruit CC3000网络客户端循环http请求
我试图以5秒的间隔从网页检索数据到我的Arduino。我正在构建Adafruit CC3000“Webclient”示例 完整的代码可以是 一切正常,Arduino连接到网络,然后向指定网站发出一个请求 现在我尝试添加循环,以便Arduino获得刷新的数据。我不想断开连接,然后必须重新连接到wifi网络,因此我尝试循环以下代码尝试使用Arduino Adafruit CC3000网络客户端循环http请求,arduino,arduino-ide,Arduino,Arduino Ide,我试图以5秒的间隔从网页检索数据到我的Arduino。我正在构建Adafruit CC3000“Webclient”示例 完整的代码可以是 一切正常,Arduino连接到网络,然后向指定网站发出一个请求 现在我尝试添加循环,以便Arduino获得刷新的数据。我不想断开连接,然后必须重新连接到wifi网络,因此我尝试循环以下代码 void myLoop(uint32_t ip) { // START LOOPING int count = 0; while(count == 0) {
void myLoop(uint32_t ip)
{
// START LOOPING
int count = 0;
while(count == 0) {
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
if (www.connected()) {
www.fastrprint(F("GET "));
www.fastrprint(WEBPAGE);
www.fastrprint(F(" HTTP/1.1\r\n"));
www.fastrprint(F("Host: ")); www.fastrprint(WEBSITE); www.fastrprint(F("\r\n"));
www.fastrprint(F("\r\n"));
www.println();
} else {
Serial.println(F("Connection failed"));
return;
}
/* Read data until either the connection is closed, or the idle timeout is reached. */
//unsigned long lastRead = millis();
while (www.connected()) {
while (www.available()) {
char c = www.read();
content = c;
Serial.print(c);
//lastRead = millis();
}
}
www.close();
Serial.println(F("\n\nDisconnecting"));
cc3000.disconnect();
delay(5000);
}
}
循环完美地运行了一次,但在第一次迭代之后,不断地输出“连接失败”
看来我不能多次运行connectTCP函数,但我不明白为什么
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
我还试着从循环中删除它,并删除www.close()代码>和cc3000.disconnect()代码>但仍无法保持连接打开
任何帮助都将不胜感激。我也是新使用CC3000的人,以前在代码方面也遇到过同样的问题
无法再次连接,因为您放置了cc3000.disconnect()代码>循环内()
所以只需删除它,但保留www.close()代码>在循环中()。我也有同样的问题,cc3000在崩溃之前只连接一次或3-4个http请求(Get)
现在我让它像这样工作:
在循环()上:
希望这对某人有帮助。。我花了这么长时间才让这件简单的事情奏效 该公司明确建议不要这样做:“您需要确保在自己之后进行清理,否则下次尝试连接时CC3000可能会崩溃。”
Adafruit_CC3000_Client www = cc3000.connectTCP(ip, WEBPORT);
delay(300);
if (www.connected()) {
//your http request get
//with a connection close:
www.fastrprint(F("GET "));
//inser your stuff
www.fastrprint(F("Connection: close\r\n"));
www.fastrprint(F("\r\n"));
www.println();
} else {
Serial.println(F("Connection failed"));
return;
}
//reading response
while(www.connected()){
while (www.available()) {
char c = www.read();
Serial.print(c);
}
}
www.stop();
delay(100);
www.close();
delay(200);
and stop&close the client at the end.