Add comments

This commit is contained in:
David Zovko 2020-07-31 10:43:38 +02:00 committed by GitHub
parent 2b90157efe
commit 5e961c5d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 49 deletions

View File

@ -57,7 +57,7 @@ void Network::getTime(char *timeStr)
//Get seconds since 1.1.1970. //Get seconds since 1.1.1970.
time_t nowSecs = time(nullptr); time_t nowSecs = time(nullptr);
// Used to store time //Struct used to store time
struct tm timeinfo; struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo); gmtime_r(&nowSecs, &timeinfo);
@ -75,42 +75,45 @@ void Network::getTime(char *timeStr)
timeStr[1] = hr % 10 + '0'; timeStr[1] = hr % 10 + '0';
} }
//Helper function to convert float to char*
void formatTemp(char *str, float temp) void formatTemp(char *str, float temp)
{ {
//Built in function for float to char* conversion //Built in function for float to char* conversion
dtostrf(temp, 2, 0, str); dtostrf(temp, 2, 0, str);
} }
//Helper function to convert float to char*
void formatWind(char *str, float wind) void formatWind(char *str, float wind)
{ {
//Built in function for float to char* conversion //Built in function for float to char* conversion
dtostrf(wind, 2, 0, str); dtostrf(wind, 2, 0, str);
} }
//Function that connects to API and gets the weather data
void Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *temp4, char *currentTemp, char *currentWind, char *currentTime, char *currentWeather, char *currentWeatherAbbr) void Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *temp4, char *currentTemp, char *currentWind, char *currentTime, char *currentWeather, char *currentWeatherAbbr)
{ {
//Return if wifi isn't connected //Return if wifi isn't connected
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
return; return;
// Wake up if sleeping and save inital state //Wake up WiFi if sleeping and save inital state
bool sleep = WiFi.getSleep(); bool sleep = WiFi.getSleep();
WiFi.setSleep(false); WiFi.setSleep(false);
// Http object used to make get request //HTTP object used to make get request
HTTPClient http; HTTPClient http;
http.getStream().setNoDelay(true); http.getStream().setNoDelay(true);
http.getStream().setTimeout(1); http.getStream().setTimeout(1);
// Add woeid to api call //Add woeid to api call. woeid is API specific variable name used to set location
char url[256]; char url[256];
sprintf(url, "https://www.metaweather.com/api/location/%d/", location); sprintf(url, "https://www.metaweather.com/api/location/%d/", location);
//Initiate http //Initiate http
http.begin(url); http.begin(url);
// Actually do request //Actually make HTTPS GET request
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode == 200) if (httpCode == 200)
{ {
@ -154,6 +157,7 @@ void Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t
WiFi.setSleep(sleep); WiFi.setSleep(sleep);
} }
//Used to set correct time received from NTP server
void Network::setTime() void Network::setTime()
{ {
//Used for setting correct time //Used for setting correct time
@ -180,6 +184,7 @@ void Network::setTime()
Serial.print(asctime(&timeinfo)); Serial.print(asctime(&timeinfo));
} }
//From epoch received from NTP server, get day of the week
void Network::getDays(char *day, char *day1, char *day2, char *day3) void Network::getDays(char *day, char *day1, char *day2, char *day3)
{ {
// Seconds since 1.1.1970. // Seconds since 1.1.1970.
@ -199,31 +204,32 @@ void Network::getDays(char *day, char *day1, char *day2, char *day3)
strncpy(day3, weekDays[(dayWeek + 3) % 7], 3); strncpy(day3, weekDays[(dayWeek + 3) % 7], 3);
} }
//Make API query to receive city woeid - city identification specific to API
void Network::findCity(char *city) void Network::findCity(char *city)
{ {
// If not connected to wifi, return //If not connected to WiFi, return
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
return; return;
// Wake wifi module and save initial state //Wake WiFi module and save initial state
bool sleep = WiFi.getSleep(); bool sleep = WiFi.getSleep();
WiFi.setSleep(false); WiFi.setSleep(false);
// Http object //HTTP object
HTTPClient http; HTTPClient http;
http.getStream().setNoDelay(true); http.getStream().setNoDelay(true);
http.getStream().setTimeout(1); http.getStream().setTimeout(1);
// Add query param to url //Add query parameter to URL
char url[256]; char url[256];
strcpy(url, "https://www.metaweather.com/api/location/search/?query="); strcpy(url, "https://www.metaweather.com/api/location/search/?query=");
strcat(url, city); strcat(url, city);
// Initiate http //Initiate HTTP
http.begin(url); http.begin(url);
// Do get request //Make GET HTTP request
int httpCode = http.GET(); int httpCode = http.GET();
if (httpCode == 200) // 200: http success if (httpCode == 200) // 200: http success
{ {
@ -234,7 +240,7 @@ void Network::findCity(char *city)
//Try to parse JSON object //Try to parse JSON object
DeserializationError error = deserializeJson(doc, http.getStream()); DeserializationError error = deserializeJson(doc, http.getStream());
// Print error to Serial monitor if one exsists //Print error to Serial monitor if one exists
if (error) if (error)
{ {
Serial.print(F("deserializeJson() failed: ")); Serial.print(F("deserializeJson() failed: "));
@ -242,14 +248,14 @@ void Network::findCity(char *city)
} }
else else
{ {
// Empty list means no matches //Empty list means no matches for the city
if (doc.size() == 0) if (doc.size() == 0)
{ {
Serial.println(F("City not found")); Serial.println(F("City not found"));
} }
else else
{ {
// Woeid id used for fetching data later on //woeid id used for fetching data later on
location = doc[0]["woeid"].as<int>(); location = doc[0]["woeid"].as<int>();
Serial.println(F("Found city, woied:")); Serial.println(F("Found city, woied:"));