如何在 Particle 上读取网站数据
How to read data from websites on Particle
我正在使用 Particle 设备进行一个项目(如 particle.io - 很难搜索这些东西,因为它的名称如此模棱两可)。我一直在做教程,但我没能找到任何关于通过蜂窝网络访问 HTTPS 网站的内容。我找到了这个 https://build.particle.io/libs/HttpClient/0.0.5/tab/HttpClient.cpp 但它不适用于 https。
我需要为一个项目(不是家庭作业)阅读一个可公开访问的 HTTPS 网站,并基于此设置变量。我该怎么做,是否有任何我找不到的关于这个确切问题的资源?非常感谢。
要进行 HTTPS 调用,您需要一个支持 SSL 的客户端。一个例子是 Glowfish 制作的 httpsclient-particle
,它使用 matrixSSL。
您可以这样使用:
// 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;
}
我正在使用 Particle 设备进行一个项目(如 particle.io - 很难搜索这些东西,因为它的名称如此模棱两可)。我一直在做教程,但我没能找到任何关于通过蜂窝网络访问 HTTPS 网站的内容。我找到了这个 https://build.particle.io/libs/HttpClient/0.0.5/tab/HttpClient.cpp 但它不适用于 https。
我需要为一个项目(不是家庭作业)阅读一个可公开访问的 HTTPS 网站,并基于此设置变量。我该怎么做,是否有任何我找不到的关于这个确切问题的资源?非常感谢。
要进行 HTTPS 调用,您需要一个支持 SSL 的客户端。一个例子是 Glowfish 制作的 httpsclient-particle
,它使用 matrixSSL。
您可以这样使用:
// 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;
}