#include <WiFi.h>
#include <HTTPClient.h>
String payload;
int JSONLoop;
const int jsonPayloadSize = 50;
const int tmpdocSize = 8000 ; // 200 x jsonPayloadSize
const int MaxPIDs = 231; //Master
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.begin(9600);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
httpPost();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
/* ======================================================================
Function: httpPost
Purpose : Do a http post
Input : hostname
port
url
Output : true if received 200 OK
Comments: -
====================================================================== */
//boolean httpPost(char * host, uint16_t port, char * url)
boolean httpPost()
{
HTTPClient http;
bool ret = false;
if ((JSONLoop + jsonPayloadSize) > MaxPIDs) {
JSONLoop=0;
}
else
{
JSONLoop = JSONLoop + jsonPayloadSize;
}
unsigned long start = millis();
// configure target server and url
//http.begin(host, port, url, port==443 ? true : false);
//http.begin("http://emoncms.org/input/post.json?node=20&apikey=apikey&json={PAPP:100}");
String out = "https://api.tfgm.com/odata/Metrolinks?$top=" ;
//Serial.print("JSONLoop:");
//Serial.println(JSONLoop);
//Serial.print("jsonPayloadSize:");
//Serial.println(jsonPayloadSize);
//Serial.print("MaxPIDs:");
//Serial.println(MaxPIDs);
if((JSONLoop + jsonPayloadSize) > MaxPIDs)
{
out += (MaxPIDs - JSONLoop);
}
else
{
out += jsonPayloadSize;
}
out += "&$skip=";
out += JSONLoop;
out += "&$select=Wait0,PIDREF,Status0,Id,dest0";
//Serial.println(out);
http.begin(out);
//http.begin("https://api.tfgm.com/odata/Metrolinks?$filter=Line eq 'Trafford Park'&$top=10 HTTP/1.1"); doesn't work
http.addHeader("Ocp-Apim-Subscription-Key", "012ac3b8b4f34600b75e90d9ed155872");
http.addHeader("Host", "api.tfgm.com");
//Debugf("http%s://%s:%d%s => ", port==443?"s":"", host, port, url);
// start connection and send HTTP header
//Serial.println("start GET");
int httpCode = http.GET();
//Serial.println("End GET");
if(httpCode) {
// HTTP header has been send and Server response header has been handled
//Serial.println(httpCode);
//Serial.println(" ");
// file found at server
if( httpCode > 199 && httpCode < 300 ) {
//String payload = http.getString();
payload = http.getString();
Serial.println(payload);
// Parse();
ret = true;
}
} else {
Serial.println("failed!");
}
//Serial.println(" in %d ms\r\n",millis()-start);
return ret;
}