【问题标题】:Trying to loop a http request using the Arduino Adafruit CC3000 Webclient尝试使用 Arduino Adafruit CC3000 Webclient 循环 http 请求
【发布时间】:2015-04-21 12:05:13
【问题描述】:

我正在尝试以 5 秒的循环间隔将数据从网页检索到我的 Arduino。我正在构建 Adafruit CC3000 'Webclient' 示例。

完整代码可以seen here

一切正常,Arduino 连接到网络,然后向指定网站发出单个请求。

现在我正在尝试添加循环,以便 Arduino 获得刷新的数据。我不想断开连接然后必须重新连接到 wifi 网络,所以我尝试循环以下代码。

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();,但仍然无法保持连接打开。

任何帮助将不胜感激。

【问题讨论】:

    标签: arduino arduino-ide


    【解决方案1】:

    我也是使用 CC3000 的新手,之前的代码也遇到过同样的问题。

    您无法再次连接,因为您将 cc3000.disconnect(); 放入了 loop()

    所以只需删除它,但将 www.close(); 保留在循环()中。

    【讨论】:

    • examples 明确建议不要这样做:“您需要确保自己清理干净,否则下次尝试连接时 CC3000 可能会崩溃
    【解决方案2】:

    我遇到了同样的问题,cc3000 在崩溃之前只连接了一次或 3-4 个 http 请求 (Get)。

    现在我让它像这样工作: 在循环()上:

    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. 
    

    希望这对某人有所帮助.. 花了很长时间才让这些简单的东西正常工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-22
      • 2020-05-09
      • 2011-04-10
      • 1970-01-01
      • 2016-01-16
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多