简体   繁体   中英

How to read data from websites on Particle

I am working on a project with a Particle device (as in particle.io - its tough searching for this stuff given that it has such an ambiguous name). I've been doing the tutorials, but I haven't been able to find anything on accessing an HTTPS website over the cellular network. I have found this https://build.particle.io/libs/HttpClient/0.0.5/tab/HttpClient.cpp but it does not work with https.

I need to read a publicly accessible HTTPS website for a project (not homework) and set variables based on this. How do I do this, and are there any resources on this exact question that I failed to find? Thanks a lot.

To do HTTPS calls you need a client that supports SSL. One example is httpsclient-particle made by Glowfish that uses matrixSSL.

You can use it as so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM