You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino中如何显示JSON子数组的指定风速值

问题

调用Meteomatics API获取3米和500米高度的风速数据,当前脚本能返回两个高度的数值,但API返回的JSON包含两个parameter条目,无法区分,不知道如何在串口显示指定高度的风速值。原代码及修改后的解决方案如下:

修改后的代码
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>

const char* ssid = "**";
const char* password = "**";

unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
String jsonBuffer;

// 补充定义经纬度变量,替换为你的实际坐标
double lat = 26.4307;
double longs = -81.4078;

void setup() {
  Serial.begin(9600);
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Timer set to 10 seconds, first reading will be published after 10 seconds.");
}

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    if(WiFi.status()== WL_CONNECTED){
      String serverPath = "http://personal_presbrey:vfDmDSb956@api.meteomatics.com/2022-11-01T00:00:00Z/wind_speed_3m:mph,wind_speed_500m:mph/" + String(lat) + "," + String(longs) + "/json";
      
      jsonBuffer = httpGETRequest(serverPath.c_str());
      Serial.println(jsonBuffer);
      
      StaticJsonDocument<768> doc;
      DeserializationError error = deserializeJson(doc, jsonBuffer);
      
      if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.f_str());
        lastTime = millis();
        return;
      }

      // 遍历data数组,根据parameter字段区分高度
      for (JsonObject data_item : doc["data"].as<JsonArray>()) {
        const char* param = data_item["parameter"];
        float windValue = data_item["coordinates"][0]["dates"][0]["value"];
        
        // 根据参数名匹配对应高度,输出指定信息
        if (strcmp(param, "wind_speed_3m:mph") == 0) {
          Serial.println("Wind at 3m (mph):");
          Serial.println(windValue, 6);
        } else if (strcmp(param, "wind_speed_500m:mph") == 0) {
          Serial.println("Wind at 500m (mph):");
          Serial.println(windValue, 6);
        }
      }
    } else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, serverName);
  
  int httpResponseCode = http.GET();
  String payload = "{}"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  } else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  http.end();
  return payload;
}
解决要点
  1. 移除冗余库:删除了功能重叠的Arduino_JSON库,仅保留更适合嵌入式设备的ArduinoJson,减少内存占用。
  2. 通过parameter字段区分数据:API返回的data数组中,每个元素的parameter字段包含明确的高度标识(如wind_speed_3m:mph),用strcmp函数对比该字段值,即可判断当前条目对应的风速高度,实现定向输出。
  3. 修复未定义变量:补充了原代码中缺失的经纬度变量定义,替换为实际坐标即可正常请求数据。

内容的提问来源于stack exchange,提问作者user2400470

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:25:30