2020-09-11 12:28:42 +02:00
|
|
|
// Network.cpp contains various functions and classes that enable Weather station
|
|
|
|
// They have been declared in seperate file to increase readability
|
2020-09-07 11:40:01 +02:00
|
|
|
#include "Network.h"
|
|
|
|
|
|
|
|
#include <HTTPClient.h>
|
2020-09-11 12:28:42 +02:00
|
|
|
#include <WiFi.h>
|
2020-09-07 11:40:01 +02:00
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Static Json from ArduinoJson library
|
2020-09-07 11:40:01 +02:00
|
|
|
StaticJsonDocument<32000> doc;
|
|
|
|
|
|
|
|
void Network::begin(char *city)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Initiating wifi, like in BasicHttpClient example
|
2020-09-07 11:40:01 +02:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.begin(ssid, pass);
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
Serial.print(F("Waiting for WiFi to connect..."));
|
|
|
|
while ((WiFi.status() != WL_CONNECTED))
|
|
|
|
{
|
|
|
|
Serial.print(F("."));
|
|
|
|
delay(1000);
|
|
|
|
++cnt;
|
|
|
|
|
|
|
|
if (cnt == 20)
|
|
|
|
{
|
|
|
|
Serial.println("Can't connect to WIFI, restarting");
|
|
|
|
delay(100);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println(F(" connected"));
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Find internet time
|
2020-09-07 11:40:01 +02:00
|
|
|
setTime();
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// reduce power by making WiFi module sleep
|
2020-09-07 11:40:01 +02:00
|
|
|
WiFi.setSleep(1);
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Gets time from ntp server
|
2020-09-07 11:40:01 +02:00
|
|
|
void Network::getTime(char *timeStr)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Get seconds since 1.1.1970.
|
2020-09-07 11:40:01 +02:00
|
|
|
time_t nowSecs = time(nullptr);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Used to store time
|
2020-09-07 11:40:01 +02:00
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Copies time string into timeStr
|
2020-09-07 11:40:01 +02:00
|
|
|
strncpy(timeStr, asctime(&timeinfo) + 11, 5);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Setting time string timezone
|
|
|
|
int hr = 10 * (timeStr[0] - '0') + (timeStr[1] - '0') + timeZone;
|
2020-09-07 11:40:01 +02:00
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Better defined modulo, in case timezone makes hours to go below 0
|
2020-09-07 11:40:01 +02:00
|
|
|
hr = (hr % 24 + 24) % 24;
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Adding time to '0' char makes it into whatever time char, for both digits
|
2020-09-07 11:40:01 +02:00
|
|
|
timeStr[0] = hr / 10 + '0';
|
|
|
|
timeStr[1] = hr % 10 + '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatTemp(char *str, float temp)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Built in function for float to char* conversion
|
2020-09-07 11:40:01 +02:00
|
|
|
dtostrf(temp, 2, 0, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatWind(char *str, float wind)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Built in function for float to char* conversion
|
2020-09-07 11:40:01 +02:00
|
|
|
dtostrf(wind, 2, 0, str);
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *temp4, char *currentTemp,
|
|
|
|
char *currentWind, char *currentTime, char *currentWeather, char *currentWeatherAbbr, char *abbr1,
|
|
|
|
char *abbr2, char *abbr3, char *abbr4)
|
2020-09-07 11:40:01 +02:00
|
|
|
{
|
|
|
|
bool f = 0;
|
|
|
|
// If not connected to wifi reconnect wifi
|
|
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
|
|
|
delay(5000);
|
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
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);
|
|
|
|
++cnt;
|
|
|
|
|
|
|
|
if (cnt == 7)
|
|
|
|
{
|
|
|
|
Serial.println("Can't connect to WIFI, restart initiated.");
|
|
|
|
delay(100);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Wake up if sleeping and save inital state
|
2020-09-07 11:40:01 +02:00
|
|
|
bool sleep = WiFi.getSleep();
|
|
|
|
WiFi.setSleep(false);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Http object used to make get request
|
2020-09-07 11:40:01 +02:00
|
|
|
HTTPClient http;
|
|
|
|
|
|
|
|
http.getStream().setNoDelay(true);
|
|
|
|
http.getStream().setTimeout(1);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Add woeid to api call
|
2020-09-07 11:40:01 +02:00
|
|
|
char url[256];
|
|
|
|
sprintf(url, "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s", lon, lat, apiKey);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Initiate http
|
2020-09-07 11:40:01 +02:00
|
|
|
http.begin(url);
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Actually do request
|
2020-09-07 11:40:01 +02:00
|
|
|
int httpCode = http.GET();
|
|
|
|
if (httpCode == 200)
|
|
|
|
{
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Try parsing JSON object
|
2020-09-07 11:40:01 +02:00
|
|
|
DeserializationError error = deserializeJson(doc, http.getStream());
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// If an error happens print it to Serial monitor
|
2020-09-07 11:40:01 +02:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
|
|
|
|
f = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Set all data got from internet using formatTemp and formatWind defined above
|
|
|
|
// This part relies heavily on ArduinoJson library
|
2020-09-07 11:40:01 +02:00
|
|
|
|
|
|
|
dataEpoch = doc["current"]["dt"].as<time_t>();
|
|
|
|
|
|
|
|
formatTemp(currentTemp, doc["current"]["temp"].as<float>() - 273.15);
|
|
|
|
formatWind(currentWind, doc["current"][F("wind_speed")].as<float>());
|
|
|
|
|
|
|
|
strcpy(currentWeather, doc["current"]["weather"][0]["main"].as<char *>());
|
|
|
|
strcpy(currentWeatherAbbr, doc["current"]["weather"][0]["icon"].as<char *>());
|
|
|
|
|
|
|
|
formatTemp(temp1, doc["hourly"][0]["temp"].as<float>() - 273.15);
|
|
|
|
formatTemp(temp2, doc["hourly"][1]["temp"].as<float>() - 273.15);
|
|
|
|
formatTemp(temp3, doc["hourly"][2]["temp"].as<float>() - 273.15);
|
|
|
|
formatTemp(temp4, doc["hourly"][3]["temp"].as<float>() - 273.15);
|
|
|
|
|
|
|
|
strcpy(abbr1, doc["hourly"][0]["weather"][0]["icon"].as<char *>());
|
|
|
|
strcpy(abbr2, doc["hourly"][1]["weather"][0]["icon"].as<char *>());
|
|
|
|
strcpy(abbr3, doc["hourly"][2]["weather"][0]["icon"].as<char *>());
|
|
|
|
strcpy(abbr4, doc["hourly"][3]["weather"][0]["icon"].as<char *>());
|
|
|
|
|
|
|
|
Serial.println(abbr1);
|
|
|
|
Serial.println(abbr2);
|
|
|
|
Serial.println(abbr3);
|
|
|
|
Serial.println(abbr4);
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
f = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (httpCode == 401)
|
|
|
|
{
|
|
|
|
display.setCursor(50, 290);
|
|
|
|
display.setTextSize(3);
|
|
|
|
display.print(F("Network error, probably wrong api key"));
|
|
|
|
display.display();
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Stop http and clear document
|
2020-09-07 11:40:01 +02:00
|
|
|
doc.clear();
|
|
|
|
http.end();
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Return to initial state
|
2020-09-07 11:40:01 +02:00
|
|
|
WiFi.setSleep(sleep);
|
|
|
|
|
|
|
|
return !f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::setTime()
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Used for setting correct time
|
2020-09-07 11:40:01 +02:00
|
|
|
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)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Print a dot every half a second while time is not set
|
2020-09-07 11:40:01 +02:00
|
|
|
delay(500);
|
|
|
|
Serial.print(F("."));
|
|
|
|
yield();
|
|
|
|
nowSecs = time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println();
|
|
|
|
|
2020-09-11 12:28:42 +02:00
|
|
|
// Used to store time info
|
2020-09-07 11:40:01 +02:00
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
|
|
|
Serial.print(F("Current time: "));
|
|
|
|
Serial.print(asctime(&timeinfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4)
|
|
|
|
{
|
2020-09-11 12:28:42 +02:00
|
|
|
// Format hours info
|
2020-09-07 11:40:01 +02:00
|
|
|
sprintf(hour1, "%2dh", (dataEpoch / 3600L + timeZone + 24) % 24);
|
|
|
|
sprintf(hour2, "%2dh", (dataEpoch / 3600L + 1 + timeZone + 24) % 24);
|
|
|
|
sprintf(hour3, "%2dh", (dataEpoch / 3600L + 2 + timeZone + 24) % 24);
|
|
|
|
sprintf(hour4, "%2dh", (dataEpoch / 3600L + 3 + timeZone + 24) % 24);
|
|
|
|
}
|