本站改版新增arduino频道

Micropython
Arduino

arduino 获取黄金现货价格


arduino 获取黄金现货价格

#include <WiFi.h>
#include <HTTPClient.h>

#define LED12  12
#define LED13  13
#define BEEP  4

// 定义 Wi-Fi 名与密码
const char * ssid = "NBWIFI2";
const char * password = "z7758521";

void setup() {
  Serial.begin(115200);
  Serial.print("start...");

  // 断开之前的连接
  WiFi.disconnect(true);
  // 连接 Wi-Fi
  WiFi.begin(ssid, password);

  Serial.print("正在连接 Wi-Fi");

  // 检测是否链接成功
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print(".");
  digitalWrite(LED12, HIGH);
  delay(300);
  digitalWrite(LED12, LOW);
  delay(300);

    }
  // WiFi连接成功
  Serial.println("");
  Serial.println("WiFi 已连接");
  Serial.print("IP 地址: ");
  Serial.println(WiFi.localIP());

  // 使用板载 LED 反馈连接成功
  pinMode(LED12, OUTPUT);
  pinMode(LED13, OUTPUT);
  pinMode(BEEP, OUTPUT);
  digitalWrite(BEEP, HIGH);

  digitalWrite(BEEP, LOW);
  delay(500);
  digitalWrite(BEEP, HIGH);
  delay(500);

}

void loop() {

    // 检查WiFi连接状态
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("WiFi 连接正常");
  } else {
    Serial.println("WiFi 断开连接");
    // 重启设备
    ESP.restart();
  }


  // 发送HTTP GET请求
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    
    // 指定请求的URL
    http.begin("https://api.jijinhao.com/sQuoteCenter/realTime.htm?code=JO_92233&isCalc=true&_=1739595929101");
    
    // 设置请求头中的Referrer
    http.addHeader("Referer", "https://www.cngold.org/");
    
    // 发送请求
    int httpResponseCode = http.GET();
    
    if (httpResponseCode > 0) {
      // 请求成功,获取响应内容
      String response = http.getString();
      Serial.println(httpResponseCode);
      Serial.println(response);


  //String hq_str = "现货黄金,0,686.27905,685.65173,686.73505,685.5377,0,0,706.2024,0.0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2025-02-19,08:41:19,00,2,-0.6273743,-0.0914,685.65173,685.8673,686.73505,100,2025-02-19,08:41:15,";
  String hq_str =response;
  // 查找第一个逗号的位置
  int firstCommaIndex = hq_str.indexOf(',');
  // 查找第二个逗号的位置
  int secondCommaIndex = hq_str.indexOf(',', firstCommaIndex + 1);
  // 查找第三个逗号的位置
  int thirdCommaIndex = hq_str.indexOf(',', secondCommaIndex + 1);
  // 查找第四个逗号的位置
  int fourthCommaIndex = hq_str.indexOf(',', thirdCommaIndex + 1);
  
  // 提取第三个和第四个逗号之间的子字符串
  String value = hq_str.substring(thirdCommaIndex + 1, fourthCommaIndex);
  
  Serial.println(value); // 输出 685.65173



    } else {
      // 请求失败
      Serial.print("Error on sending GET request: ");
      Serial.println(httpResponseCode);
    }
    
    // 结束HTTP连接
    http.end();
  }

  /*
  digitalWrite(LED12, HIGH);
  delay(500);
  digitalWrite(LED12, LOW);
  delay(500);

  digitalWrite(LED13, HIGH);
  delay(500);
  digitalWrite(LED13, LOW);
  delay(500);
*/

delay(60000);
}



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号

执行时间: 0.0096158981323242 seconds