首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从粒子上的网站读取数据

如何从粒子上的网站读取数据
EN

Stack Overflow用户
提问于 2019-03-25 00:04:23
回答 1查看 381关注 0票数 0

我正在做一个带有粒子设备的项目(就像在particle.io中一样--考虑到它有一个模棱两可的名字,很难找到这个东西)。我一直在做教程,但我还没能找到任何关于通过蜂窝网络访问HTTPS网站的东西。我已经找到了这个https://build.particle.io/libs/HttpClient/0.0.5/tab/HttpClient.cpp,但它不能与https一起工作。

我需要阅读一个项目(而不是家庭作业)的可公开访问的HTTPS网站,并在此基础上设置变量。我如何做到这一点,是否有任何关于这个问题的资源我找不到?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-26 00:35:41

要进行HTTPS调用,您需要一个支持SSL的客户端。一个例子是由Glowfish开发的使用matrixSSL的httpsclient-particle

您可以这样使用它:

代码语言:javascript
复制
// This #include statement was automatically added by the Particle IDE.
#include <httpsclient-particle.h>

static int anomalyLed = D7;
static int heartbeatLed = D7;

const bool g_https_trace = true;  // This controls debug info print to Serial
const char host [] = "www.timeapi.org";
const char endpoint [] = "/utc/now/";
const int g_port = 443; // DoNOT change this unless you know what's up
static unsigned int freemem;
bool g_https_complete;
uint32 g_bytes_received;

TCPClient client;

unsigned char httpRequestContent[] = "GET %s HTTP/1.0\r\n"
  "User-Agent: MatrixSSL/" MATRIXSSL_VERSION "\r\n"
  "Host: www.timeapi.org\r\n"
  "Accept: */*\r\n"
  "Content-Type: application/json\r\n"
  "Content-Length: %d\r\n\r\n%s";

void setup() {
  if (g_https_trace) {
    Serial.begin(9600);
  }
  pinMode(anomalyLed, OUTPUT);
  httpsclientSetup(host, endpoint);
}

unsigned int nextTime = 0;    // Next time to contact the server
int g_connected;
void loop() {
  if (nextTime > millis()) return;
  g_connected = client.connect(host, g_port);
  if (!g_connected) {
    client.stop();
    // If TCP Client can't connect to host, exit here.
    return;
  }
  g_https_complete = false;
  g_bytes_received = 0;
  if (g_https_trace) {
    freemem = System.freeMemory();
    Serial.print("free memory: ");
    Serial.println(freemem);
  }
  int32 rc;
  if ((rc = httpsClientConnection(httpRequestContent, 0, NULL) < 0)) {
    // TODO: When massive FAIL
    httpsclientCleanUp();
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
    delay(500);
    digitalWrite(anomalyLed, HIGH);
    delay(500);
    digitalWrite(anomalyLed, LOW);
  } else {
    digitalWrite(heartbeatLed, HIGH);
    delay(250);
    digitalWrite(heartbeatLed, LOW);
  }
  client.stop();
  nextTime = millis() + 5000;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55325731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档