inkplate-6-arduino-library/examples/3. Projects/2-Crypto_tracker_example/Network.cpp

174 lines
4.0 KiB
C++

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