2020-07-30 09:22:33 +02:00
|
|
|
#include "Network.h"
|
|
|
|
|
|
|
|
#include <WiFi.h>
|
|
|
|
#include <WiFiMulti.h>
|
|
|
|
#include <HTTPClient.h>
|
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
|
2020-07-30 13:52:43 +02:00
|
|
|
#include "Inkplate.h"
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Must be installed for this example to work
|
2020-07-30 09:22:33 +02:00
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//external parameters from our main file
|
2020-07-30 09:22:33 +02:00
|
|
|
extern char *ssid;
|
|
|
|
extern char *pass;
|
|
|
|
extern char *currency;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Get our Inkplate object from main file to draw debug info on
|
2020-07-30 13:52:43 +02:00
|
|
|
extern Inkplate display;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//WiFiMulti object declaration
|
2020-07-30 09:22:33 +02:00
|
|
|
WiFiMulti WiFiMulti;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Static Json from ArduinoJson library
|
2020-07-30 09:22:33 +02:00
|
|
|
StaticJsonDocument<30000> doc;
|
|
|
|
|
|
|
|
void Network::begin()
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Initiating wifi, like in BasicHttpClient example
|
2020-07-30 09:22:33 +02:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFiMulti.addAP(ssid, pass);
|
|
|
|
|
|
|
|
Serial.print(F("Waiting for WiFi to connect..."));
|
|
|
|
while ((WiFiMulti.run() != WL_CONNECTED))
|
|
|
|
{
|
|
|
|
Serial.print(F("."));
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
Serial.println(F(" connected"));
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find internet time
|
2020-07-30 09:22:33 +02:00
|
|
|
setTime();
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Gets time from ntp server
|
2020-07-30 09:22:33 +02:00
|
|
|
void Network::getTime(char *timeStr)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Get seconds since 1.1.1970.
|
2020-07-30 09:22:33 +02:00
|
|
|
time_t nowSecs = time(nullptr);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used to store time
|
2020-07-30 09:22:33 +02:00
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Copies time string into timeStr
|
2020-07-30 09:22:33 +02:00
|
|
|
strncpy(timeStr, asctime(&timeinfo) + 4, 12);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Setting time string timezone
|
2020-07-30 09:22:33 +02:00
|
|
|
int hr = 10 * timeStr[7] + timeStr[8] + timeZone;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Better defined modulo, in case timezone makes hours to go below 0
|
2020-07-30 09:22:33 +02:00
|
|
|
hr = (hr % 24 + 24) % 24;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Adding time to '0' char makes it into whatever time char, for both digits
|
2020-07-30 09:22:33 +02:00
|
|
|
timeStr[7] = hr / 10 + '0';
|
|
|
|
timeStr[8] = hr % 10 + '0';
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:52:43 +02:00
|
|
|
bool Network::getData(double *data)
|
2020-07-30 09:22:33 +02:00
|
|
|
{
|
|
|
|
bool f = 0;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Return if wifi isn't connected
|
2020-07-30 09:22:33 +02:00
|
|
|
if (WiFi.status() != WL_CONNECTED)
|
|
|
|
return 0;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Wake up if sleeping and save inital state
|
2020-07-30 09:22:33 +02:00
|
|
|
bool sleep = WiFi.getSleep();
|
|
|
|
WiFi.setSleep(false);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Http object used to make get request
|
2020-07-30 09:22:33 +02:00
|
|
|
HTTPClient http;
|
|
|
|
|
|
|
|
http.getStream().setTimeout(10);
|
|
|
|
http.getStream().flush();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Initiate http
|
2020-07-30 09:22:33 +02:00
|
|
|
char temp[128];
|
|
|
|
sprintf(temp, "https://api.coingecko.com/api/v3/coins/%s/market_chart?vs_currency=usd&days=92", currency);
|
|
|
|
|
|
|
|
http.begin(temp);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Actually do request
|
2020-07-30 09:22:33 +02:00
|
|
|
int httpCode = http.GET();
|
|
|
|
if (httpCode == 200)
|
|
|
|
{
|
|
|
|
while (http.getStream().available() && http.getStream().peek() != '{')
|
|
|
|
(void)http.getStream().read();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Try parsing JSON object
|
2020-07-30 09:22:33 +02:00
|
|
|
DeserializationError error = deserializeJson(doc, http.getStream());
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
f = 1;
|
|
|
|
}
|
|
|
|
else if (doc["prices"].size() > 31)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Set all data got from internet using formatTemp and formatWind defined above
|
|
|
|
//This part relies heavily on ArduinoJson library
|
2020-07-30 09:22:33 +02:00
|
|
|
|
|
|
|
Serial.println("Success");
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Save our data to data pointer from main file
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 31; ++i)
|
|
|
|
{
|
2020-07-30 13:52:43 +02:00
|
|
|
data[i] = doc["prices"][92 - 31 + i][1].as<double>();
|
2020-07-30 09:22:33 +02:00
|
|
|
//Serial.println(data[i]);
|
|
|
|
}
|
2020-07-30 13:52:43 +02:00
|
|
|
f = 0;
|
2020-07-30 09:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 13:52:43 +02:00
|
|
|
else if (httpCode == 404)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Coin id not found
|
2020-07-30 13:52:43 +02:00
|
|
|
display.clearDisplay();
|
|
|
|
display.setCursor(50, 230);
|
|
|
|
display.setTextSize(2);
|
|
|
|
display.println(F("Your entered coin does not exsist!"));
|
|
|
|
display.display();
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
2020-07-30 09:22:33 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
f = 1;
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Clear document and end http
|
2020-07-30 09:22:33 +02:00
|
|
|
doc.clear();
|
|
|
|
http.end();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Return to initial state
|
2020-07-30 09:22:33 +02:00
|
|
|
WiFi.setSleep(sleep);
|
|
|
|
|
|
|
|
return !f;
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Function for initial time setting ovet the ntp server
|
2020-07-30 09:22:33 +02:00
|
|
|
void Network::setTime()
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used for setting correct time
|
2020-07-30 09:22:33 +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)
|
|
|
|
{
|
|
|
|
delay(500);
|
|
|
|
Serial.print(F("."));
|
|
|
|
yield();
|
|
|
|
nowSecs = time(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used to store time info
|
2020-07-30 09:22:33 +02:00
|
|
|
struct tm timeinfo;
|
|
|
|
gmtime_r(&nowSecs, &timeinfo);
|
|
|
|
|
|
|
|
Serial.print(F("Current time: "));
|
|
|
|
Serial.print(asctime(&timeinfo));
|
|
|
|
}
|