2020-07-31 15:31:48 +02:00
|
|
|
#include "Network.h"
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <HTTPClient.h>
|
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
|
|
|
|
|
|
|
|
void Network::begin()
|
|
|
|
{
|
|
|
|
// Initiating wifi, like in BasicHttpClient example
|
|
|
|
WiFi.mode(WIFI_STA);
|
2020-08-06 14:43:47 +02:00
|
|
|
WiFi.begin(ssid, pass);
|
2020-07-31 15:31:48 +02:00
|
|
|
|
|
|
|
Serial.print(F("Waiting for WiFi to connect..."));
|
2020-08-06 14:43:47 +02:00
|
|
|
while ((WiFi.status() != WL_CONNECTED))
|
2020-07-31 15:31:48 +02:00
|
|
|
{
|
2020-08-03 14:28:10 +02:00
|
|
|
// Prints a dot every second that wifi isn't connected
|
2020-07-31 15:31:48 +02:00
|
|
|
Serial.print(F("."));
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
Serial.println(F(" connected"));
|
|
|
|
|
|
|
|
// Find internet time
|
|
|
|
setTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets time from ntp server
|
|
|
|
void Network::getTime(char *timeStr, long offSet)
|
|
|
|
{
|
|
|
|
// Get seconds since 1.1.1970.
|
|
|
|
time_t nowSecs = time(nullptr) + (long)timeZone * 3600L + offSet;
|
|
|
|
|
|
|
|
// Used to store time
|
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
|
|
|
// Copies time string into timeStr
|
|
|
|
strcpy(timeStr, asctime(&timeinfo));
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:28:10 +02:00
|
|
|
// Function to get all war data from web
|
2020-07-31 15:31:48 +02:00
|
|
|
bool Network::getData(char *data)
|
|
|
|
{
|
2020-08-03 14:28:10 +02:00
|
|
|
// Variable to store fail
|
2020-07-31 15:31:48 +02:00
|
|
|
bool f = 0;
|
2020-08-06 14:43:47 +02:00
|
|
|
// If not connected to wifi reconnect wifi
|
|
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
|
|
|
delay(5000);
|
|
|
|
|
|
|
|
Serial.println(F("Waiting for WiFi to reconnect..."));
|
|
|
|
while ((WiFi.status() != WL_CONNECTED))
|
|
|
|
{
|
|
|
|
// Prints a dot every second that wifi isn't connected
|
|
|
|
Serial.print(F("."));
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 15:31:48 +02:00
|
|
|
|
|
|
|
// Http object used to make get request
|
|
|
|
HTTPClient http;
|
|
|
|
|
|
|
|
http.getStream().setTimeout(10);
|
|
|
|
http.getStream().flush();
|
|
|
|
|
2020-08-03 14:28:10 +02:00
|
|
|
// Begin http by passing url to it
|
2020-07-31 15:31:48 +02:00
|
|
|
http.begin(calendarURL);
|
|
|
|
|
|
|
|
delay(300);
|
|
|
|
|
|
|
|
// Actually do request
|
|
|
|
int httpCode = http.GET();
|
2020-08-06 14:43:47 +02:00
|
|
|
|
2020-07-31 15:31:48 +02:00
|
|
|
if (httpCode == 200)
|
|
|
|
{
|
|
|
|
long n = 0;
|
|
|
|
while (http.getStream().available())
|
|
|
|
data[n++] = http.getStream().read();
|
2020-08-06 14:43:47 +02:00
|
|
|
data[n++]= 0;
|
2020-07-31 15:31:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-06 14:43:47 +02:00
|
|
|
Serial.println(httpCode);
|
2020-07-31 15:31:48 +02:00
|
|
|
f = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// end http
|
|
|
|
http.end();
|
|
|
|
|
|
|
|
return !f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::setTime()
|
|
|
|
{
|
|
|
|
// Used for setting correct time
|
|
|
|
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
|
|
|
|
|
|
|
|
Serial.print(F("Waiting for NTP time sync: "));
|
|
|
|
time_t nowSecs = time(nullptr);
|
|
|
|
while (nowSecs < 8 * 3600 * 2)
|
|
|
|
{
|
|
|
|
delay(500);
|
|
|
|
Serial.print(F("."));
|
|
|
|
yield();
|
|
|
|
nowSecs = time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
// Used to store time info
|
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
|
|
|
Serial.print(F("Current time: "));
|
|
|
|
Serial.print(asctime(&timeinfo));
|
|
|
|
}
|