【问题标题】:can arduino send repeated http requests and get and parse the the data from responsearduino 可以发送重复的 http 请求并从响应中获取和解析数据吗
【发布时间】:2019-05-29 07:25:39
【问题描述】:

我想在一个项目中使用 Arduino。我想要的是arduino应该向服务器发送带有一些数据(很可能是IP地址)的重复http请求(比如每分钟)。服务器将返回带有一些 JSON 格式数据的响应,arduino 应该解析数据并将其写入文本文件。数据是来自数据库的一些配置参数。我可以用 Arduino 做吗?看到有帖子说不能重复http请求?有什么帮助吗?示例代码将非常有帮助。我正在使用带有以太网屏蔽的 Arduino mega。

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>

// 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 };

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// 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:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
// try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }

// give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting... ");
}


void loop(){

  if (Ethernet.begin(mac) !=0){
    HttpClient http; 
    http.begin("http://jsonplaceholder.typicode.com/comments?id=10"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
    if (httpCode > 0) { //Check for the returning code
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      } 
    else {
      Serial.println("Error on HTTP request");
    }
    http.end(); //Free the resources
  }
  delay(10000);  

    }

我尝试了上面的代码来发送一个 http 请求。但是出现错误 调用 'HttpClient::HttpClient()' 没有匹配的函数

任何建议都会很有帮助。

【问题讨论】:

    标签: php http arduino


    【解决方案1】:

    除了每个请求的时间之外,您对发送重复请求没有任何限制。您的代码很好,除了发出 HTTP 请求的部分。默认的 Arduino 库处理 HTTP 请求有点复杂(例如没有HttpClient.GET)。

    有许多高级 API 可以为您处理请求,还可以设置各种标头类型。

    例如ArduinoHTTPClient 是一个不错的选择。只需抓住图书馆并查看示例。我基于此重写您的代码,您将获得 JSON 响应正文。然后,您可以简单地使用其中一种 JSON 解析器,例如 ArduinoJSON 来解析结果。

    #include <ArduinoHttpClient.h>
    #include <SPI.h>
    #include <Ethernet.h>
    
    // 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};
    
    // Set the static IP address to use if the DHCP fails to assign
    IPAddress ip(192, 168, 0, 177);
    IPAddress myDns(192, 168, 0, 1);
    
    char serverAddress[] = "http://jsonplaceholder.typicode.com"; // server address
    int port = 80;
    
    EthernetClient EthClient;
    HttpClient client = HttpClient(EthClient, serverAddress, port);
    
    void setup()
    {
        Serial.begin(9600);
    
        // start the Ethernet connection:
        Serial.println("Initialize Ethernet with DHCP:");
        if (Ethernet.begin(mac) == 0)
        {
            Serial.println("Failed to configure Ethernet using DHCP");
            // Check for Ethernet hardware present
            if (Ethernet.hardwareStatus() == EthernetNoHardware)
            {
                Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
                while (true)
                {
                    delay(1); // do nothing, no point running without Ethernet hardware
                }
            }
            if (Ethernet.linkStatus() == LinkOFF)
            {
                Serial.println("Ethernet cable is not connected.");
            }
            // try to congifure using IP address instead of DHCP:
            Ethernet.begin(mac, ip, myDns);
        }
        else
        {
            Serial.print("  DHCP assigned IP ");
            Serial.println(Ethernet.localIP());
        }
    }
    void loop()
    {
        Serial.println("making GET request");
        client.get("/comments?id=10");
    
        // read the status code and body of the response
        int statusCode = client.responseStatusCode();
        String response = client.responseBody();
    
        Serial.print("Status code: ");
        Serial.println(statusCode);
        Serial.print("Response: ");
        Serial.println(response);
        Serial.println("Wait five seconds");
        delay(5000);
    }
    

    【讨论】:

    • 非常感谢。我会检查一下,然后再找你。我真的很感谢你的努力...... :)
    • 我总是得到一个状态码 -2。没有任何其他输出!
    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2013-09-29
    相关资源
    最近更新 更多