__________________________________________________________________________________
#include "C:\Users\xx\Documents\Arduino\src\gtfs-realtime.pb.h"
#include "C:\Users\xx\Documents\Arduino\src\gtfs-realtime.pb.c"
#include "pb_common.h"
#include "pb_decode.h"
#include "pb.h"
#include <Arduino.h>
#include <Arduino_JSON.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("xxxxx", "xxxx");
}
void loop() {
size_t message_length;
bool status;
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
// bool mfln = client->probeMaxFragmentLength("
tls.mbed.org", 443, 1024);
// Serial.printf("\nConnecting to
https://tls.mbed.org\n");
// Serial.printf("Maximum fragment Length negotiation supported: %s\n", mfln ? "yes" : "no");
//if (mfln) { client->setBufferSizes(1024, 1024); }
Serial.print("[HTTPS] begin...\n");
// configure server and url
const uint8_t fingerprint[20] = { 0x15, 0x77, 0xdc, 0x04, 0x7c, 0x00, 0xf8, 0x70, 0x09, 0x34, 0x24, 0xf4, 0xd3, 0xa1, 0x7a, 0x6c, 0x1e, 0xa3, 0xe0, 0x2a };
// client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient https;
if (https.begin(*client, "
https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw")) { // HTTPS
https.addHeader("x-api-key", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
https.addHeader("Content-Type", "application/json\r\n");
Serial.print("[HTTPS] GET...\n");
// start connection and send HTTP header
// start connection and send HTTP header
int httpCode = https.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println("Response body:");
int len = https.getSize();
Serial.print("Content length: ");
Serial.println(len);
char buffer[128];
String gtfs_buffer = "";
while (https.connected() && (len > 0 || len == -1)) {
// Read data in chunks
int size = std::min((int)sizeof(buffer) - 1, len);
int bytesRead = client->readBytes(buffer, size);
if (bytesRead == 0) {
break;
}
buffer[bytesRead] = '\0';
gtfs_buffer += buffer;
if (len > 0) {
len -= bytesRead;
}
}
Serial.println(gtfs_buffer);
}
}
}
}
}
The serial output looks like this
__________________________________________________________________________________