Thanks, Theron WIerenga
Code: Untitled.cpp Select all
// Retrieve news headlines from newsapi.org
// *****************************************************************************************************************
void getNewsFromInternet()
{
const char* server = "newsapi.org";
WiFiClientSecure client;
client.setInsecure();
const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
int httpCode, count = 0;
news_payload = " ";
Serial.println("\nStarting connection to server...");
httpCode = client.connect(server, 443);
Serial.println(httpCode);
if (httpCode == 0)
Serial.println("Connection failed!");
else {
Serial.println("Connected to server!");
// Make a HTTP request:
client.println("GET /v2/top-headlines?country=us&apiKey=myKey HTTP/1.1");
client.println("Host: newsapi.org");
client.println("Connection: close");
client.println("User-Agent: ArduinoWiFi/1.0"); // This is an example User-Agent; you can set it to anything relevant
client.println();
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
count = 0;
while (client.available()) {
n_payload[count++] = client.read();
// news_payload += c;
}
n_payload[count] = 0x00;
// Convert to String
news_payload = String(n_payload);
Serial.print("news_payload = ");
Serial.println(news_payload);
Serial.println(news_payload.length());
Serial.println(count);
client.stop();
}
}