Example fix.

This commit is contained in:
nitko12 2020-09-11 12:28:42 +02:00
parent 35a4ea4ef0
commit 8c4303180b
7 changed files with 123 additions and 147 deletions

View File

@ -19,116 +19,85 @@
//---------- CHANGE HERE -------------:
//Time zone for adding hours
// Time zone for adding hours
int timeZone = 2;
//City name to de displayed on the bottom
// City name to de displayed on the bottom
char city[128] = "OSIJEK";
//Coordinates sent to the api
// Coordinates sent to the api
char *lon = "45.5510548";
char *lat = "18.5947808";
//Change to your wifi ssid and password
// Change to your wifi ssid and password
char *ssid = "";
char *pass = "";
//Change to your api key, if you don't have one, head over to:
//https://openweathermap.org/guide , register and copy the key provided
// Change to your api key, if you don't have one, head over to:
// https://openweathermap.org/guide , register and copy the key provided
char *apiKey = "";
//----------------------------------
//Include Inkplate library to the sketch
// Include Inkplate library to the sketch
#include "Inkplate.h"
//Header file for easier code readability
// Header file for easier code readability
#include "Network.h"
//Including fonts used
#include "Fonts/Roboto_Light_48.h"
#include "Fonts/Roboto_Light_36.h"
// Including fonts used
#include "Fonts/Roboto_Light_120.h"
#include "Fonts/Roboto_Light_36.h"
#include "Fonts/Roboto_Light_48.h"
//Including icons generated by the py file
// Including icons generated by the py file
#include "icons.h"
//Delay between API calls, about 1000 per month, which is the free tier limit
// Delay between API calls, about 1000 per month, which is the free tier limit
#define DELAY_MS 267800L
//Inkplate object
// Inkplate object
Inkplate display(INKPLATE_1BIT);
//All our network functions are in this object, see Network.h
// All our network functions are in this object, see Network.h
Network network;
//Contants used for drawing icons
char abbrs[32][32] ={ "01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d", "01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n" };
const uint8_t *logos[18] ={
icon_01d,
icon_02d,
icon_03d,
icon_04d,
icon_09d,
icon_10d,
icon_11d,
icon_13d,
icon_50d,
icon_01n,
icon_02n,
icon_03n,
icon_04n,
icon_09n,
icon_10n,
icon_11n,
icon_13n,
icon_50n,
// Contants used for drawing icons
char abbrs[32][32] = {"01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d",
"01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"};
const uint8_t *logos[18] = {
icon_01d, icon_02d, icon_03d, icon_04d, icon_09d, icon_10d, icon_11d, icon_13d, icon_50d,
icon_01n, icon_02n, icon_03n, icon_04n, icon_09n, icon_10n, icon_11n, icon_13n, icon_50n,
};
const uint8_t *s_logos[18] ={
icon_s_01d,
icon_s_02d,
icon_s_03d,
icon_s_04d,
icon_s_09d,
icon_s_10d,
icon_s_11d,
icon_s_13d,
icon_s_50d,
icon_s_01n,
icon_s_02n,
icon_s_03n,
icon_s_04n,
icon_s_09n,
icon_s_10n,
icon_s_11n,
icon_s_13n,
icon_s_50n,
const uint8_t *s_logos[18] = {
icon_s_01d, icon_s_02d, icon_s_03d, icon_s_04d, icon_s_09d, icon_s_10d, icon_s_11d, icon_s_13d, icon_s_50d,
icon_s_01n, icon_s_02n, icon_s_03n, icon_s_04n, icon_s_09n, icon_s_10n, icon_s_11n, icon_s_13n, icon_s_50n,
};
//Variables for storing temperature
char temps[8][4] ={
// Variables for storing temperature
char temps[4][8] = {
"0F",
"0F",
"0F",
"0F",
};
//Variables for storing hour strings
char hours[8][4] ={
// Variables for storing hour strings
char hours[4][8] = {
"",
"",
"",
"",
};
//Variable for counting partial refreshes
// Variable for counting partial refreshes
long refreshes = 0;
//Constant to determine when to full update
// Constant to determine when to full update
const int fullRefresh = 10;
//Variables for storing current time and weather info
// Variables for storing current time and weather info
char currentTemp[16] = "0F";
char currentWind[16] = "0m/s";
@ -142,7 +111,7 @@ char abbr2[16];
char abbr3[16];
char abbr4[16];
//functions defined below
// functions defined below
void drawWeather();
void drawCurrent();
void drawTemps();
@ -151,37 +120,38 @@ void drawTime();
void setup()
{
//Begin serial and display
// Begin serial and display
Serial.begin(115200);
display.begin();
//Initial cleaning of buffer and physical screen
// Initial cleaning of buffer and physical screen
display.clearDisplay();
display.clean();
//Calling our begin from network.h file
// Calling our begin from network.h file
network.begin(city);
//Welcome screen
// Welcome screen
display.setCursor(50, 290);
display.setTextSize(3);
display.print(F("Welcome to Inkplate 6 weather example!"));
display.display();
//Wait a bit before proceeding
// Wait a bit before proceeding
delay(5000);
}
void loop()
{
//Clear display
// Clear display
display.clearDisplay();
//Get all relevant data, see Network.cpp for info
// Get all relevant data, see Network.cpp for info
network.getTime(currentTime);
if (refreshes % fullRefresh == 0)
{
while (!network.getData(city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime, currentWeather, currentWeatherAbbr, abbr1, abbr2, abbr3, abbr4))
while (!network.getData(city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime,
currentWeather, currentWeatherAbbr, abbr1, abbr2, abbr3, abbr4))
{
Serial.println("Retrying fetching data!");
delay(5000);
@ -189,37 +159,37 @@ void loop()
network.getHours(hours[0], hours[1], hours[2], hours[3]);
}
//Draw data, see functions below for info
// Draw data, see functions below for info
drawWeather();
drawCurrent();
drawTemps();
drawCity();
drawTime();
//Refresh full screen every fullRefresh times, defined above
// Refresh full screen every fullRefresh times, defined above
if (refreshes % fullRefresh == 0)
display.display();
else
display.partialUpdate();
//Go to sleep before checking again
// Go to sleep before checking again
esp_sleep_enable_timer_wakeup(1000L * DELAY_MS);
(void)esp_light_sleep_start();
++refreshes;
}
//Function for drawing weather info
// Function for drawing weather info
void drawWeather()
{
//Searching for weather state abbreviation
// Searching for weather state abbreviation
for (int i = 0; i < 18; ++i)
{
//If found draw specified icon
// If found draw specified icon
if (strcmp(abbrs[i], currentWeatherAbbr) == 0)
display.drawBitmap(50, 50, logos[i], 152, 152, BLACK);
}
//Draw weather state
// Draw weather state
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
@ -227,10 +197,10 @@ void drawWeather()
display.println(currentWeather);
}
//Function for drawing current time
// Function for drawing current time
void drawTime()
{
//Drawing current time
// Drawing current time
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
@ -239,10 +209,10 @@ void drawTime()
display.println(currentTime);
}
//Function for drawing city name
// Function for drawing city name
void drawCity()
{
//Drawing city name
// Drawing city name
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
@ -251,10 +221,10 @@ void drawCity()
display.println(city);
}
//Function for drawing temperatures
// Function for drawing temperatures
void drawTemps()
{
//Drawing 4 black rectangles in which temperatures will be written
// Drawing 4 black rectangles in which temperatures will be written
int rectWidth = 150;
int rectSpacing = (800 - rectWidth * 4) / 5;
@ -281,7 +251,7 @@ void drawTemps()
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 40);
display.println(hours[3]);
//Drawing temperature values into black rectangles
// Drawing temperature values into black rectangles
display.setFont(&Roboto_Light_48);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
@ -304,39 +274,43 @@ void drawTemps()
for (int i = 0; i < 18; ++i)
{
//If found draw specified icon
// If found draw specified icon
if (strcmp(abbr1, abbrs[i]) == 0)
display.drawBitmap(1 * rectSpacing + 0 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48, WHITE, BLACK);
display.drawBitmap(1 * rectSpacing + 0 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48,
WHITE, BLACK);
}
for (int i = 0; i < 18; ++i)
{
//If found draw specified icon
// If found draw specified icon
if (strcmp(abbr2, abbrs[i]) == 0)
display.drawBitmap(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48, WHITE, BLACK);
display.drawBitmap(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48,
WHITE, BLACK);
}
for (int i = 0; i < 18; ++i)
{
//If found draw specified icon
// If found draw specified icon
if (strcmp(abbr3, abbrs[i]) == 0)
display.drawBitmap(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48, WHITE, BLACK);
display.drawBitmap(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48,
WHITE, BLACK);
}
for (int i = 0; i < 18; ++i)
{
//If found draw specified icon
// If found draw specified icon
if (strcmp(abbr4, abbrs[i]) == 0)
display.drawBitmap(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48, WHITE, BLACK);
display.drawBitmap(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 150, s_logos[i], 48, 48,
WHITE, BLACK);
}
}
//Current weather drawing function
// Current weather drawing function
void drawCurrent()
{
//Drawing current information
// Drawing current information
//Temperature:
// Temperature:
display.setFont(&Roboto_Light_120);
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
@ -353,7 +327,7 @@ void drawCurrent()
display.setCursor(x, y);
display.println(F("C"));
//Wind:
// Wind:
display.setFont(&Roboto_Light_120);
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
@ -370,7 +344,7 @@ void drawCurrent()
display.setCursor(x, y);
display.println(F("m/s"));
//Labels underneath
// Labels underneath
display.setFont(&Roboto_Light_36);
display.setTextSize(1);

View File

@ -1,19 +1,19 @@
//Network.cpp contains various functions and classes that enable Weather station
//They have been declared in seperate file to increase readability
// Network.cpp contains various functions and classes that enable Weather station
// They have been declared in seperate file to increase readability
#include "Network.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
//Static Json from ArduinoJson library
// Static Json from ArduinoJson library
StaticJsonDocument<32000> doc;
void Network::begin(char *city)
{
//Initiating wifi, like in BasicHttpClient example
// Initiating wifi, like in BasicHttpClient example
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
@ -34,50 +34,52 @@ void Network::begin(char *city)
}
Serial.println(F(" connected"));
//Find internet time
// Find internet time
setTime();
//reduce power by making WiFi module sleep
// reduce power by making WiFi module sleep
WiFi.setSleep(1);
}
//Gets time from ntp server
// Gets time from ntp server
void Network::getTime(char *timeStr)
{
//Get seconds since 1.1.1970.
// Get seconds since 1.1.1970.
time_t nowSecs = time(nullptr);
//Used to store time
// Used to store time
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
//Copies time string into timeStr
// Copies time string into timeStr
strncpy(timeStr, asctime(&timeinfo) + 11, 5);
//Setting time string timezone
int hr = 10 * timeStr[0] + timeStr[1] + timeZone;
// Setting time string timezone
int hr = 10 * (timeStr[0] - '0') + (timeStr[1] - '0') + timeZone;
//Better defined modulo, in case timezone makes hours to go below 0
// 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
// Adding time to '0' char makes it into whatever time char, for both digits
timeStr[0] = hr / 10 + '0';
timeStr[1] = hr % 10 + '0';
}
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);
}
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);
}
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)
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)
{
bool f = 0;
// If not connected to wifi reconnect wifi
@ -105,32 +107,32 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t
}
}
//Wake up if sleeping and save inital state
// Wake up if sleeping and save inital state
bool sleep = WiFi.getSleep();
WiFi.setSleep(false);
//Http object used to make get request
// Http object used to make get request
HTTPClient http;
http.getStream().setNoDelay(true);
http.getStream().setTimeout(1);
//Add woeid to api call
// Add woeid to api call
char url[256];
sprintf(url, "https://api.openweathermap.org/data/2.5/onecall?lat=%s&lon=%s&appid=%s", lon, lat, apiKey);
//Initiate http
// Initiate http
http.begin(url);
//Actually do request
// Actually do request
int httpCode = http.GET();
if (httpCode == 200)
{
//Try parsing JSON object
// Try parsing JSON object
DeserializationError error = deserializeJson(doc, http.getStream());
//If an error happens print it to Serial monitor
// If an error happens print it to Serial monitor
if (error)
{
Serial.print(F("deserializeJson() failed: "));
@ -140,8 +142,8 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t
}
else
{
//Set all data got from internet using formatTemp and formatWind defined above
//This part relies heavily on ArduinoJson library
// Set all data got from internet using formatTemp and formatWind defined above
// This part relies heavily on ArduinoJson library
dataEpoch = doc["current"]["dt"].as<time_t>();
@ -180,11 +182,11 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t
;
}
//Stop http and clear document
// Stop http and clear document
doc.clear();
http.end();
//Return to initial state
// Return to initial state
WiFi.setSleep(sleep);
return !f;
@ -192,14 +194,14 @@ bool Network::getData(char *city, char *temp1, char *temp2, char *temp3, char *t
void Network::setTime()
{
//Used for setting correct time
// 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)
{
//Print a dot every half a second while time is not set
// Print a dot every half a second while time is not set
delay(500);
Serial.print(F("."));
yield();
@ -208,7 +210,7 @@ void Network::setTime()
Serial.println();
//Used to store time info
// Used to store time info
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
@ -218,7 +220,7 @@ void Network::setTime()
void Network::getHours(char *hour1, char *hour2, char *hour3, char *hour4)
{
//Format hours info
// Format hours info
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);

View File

@ -28,15 +28,15 @@ class Inkplate : public System, public Graphics
bool joinAP(const char *ssid, const char *pass)
{
return Network::joinAP(ssid, pass);
return NetworkClient::joinAP(ssid, pass);
}
void disconnect()
{
Network::disconnect();
NetworkClient::disconnect();
};
bool isConnected()
{
return Network::isConnected();
return NetworkClient::isConnected();
};
private:

View File

@ -4,11 +4,11 @@
#include "../libs/Adafruit-GFX-Library/Adafruit_GFX.h"
#include "../libs/SdFat/SdFat.h"
#include "Arduino.h"
#include "Network.h"
#include "NetworkClient.h"
#include "WiFiClient.h"
#include "defines.h"
class Image : virtual public Network, virtual public Adafruit_GFX
class Image : virtual public NetworkClient, virtual public Adafruit_GFX
{
public:
Image(int16_t w, int16_t h);

View File

@ -1,6 +1,6 @@
#include "Network.h"
#include "NetworkClient.h"
bool Network::joinAP(const char *ssid, const char *pass)
bool NetworkClient::joinAP(const char *ssid, const char *pass)
{
WiFi.mode(WIFI_MODE_STA);
WiFi.begin(ssid, pass);
@ -17,18 +17,18 @@ bool Network::joinAP(const char *ssid, const char *pass)
return 1;
}
void Network::disconnect()
void NetworkClient::disconnect()
{
WiFi.mode(WIFI_OFF);
}
bool Network::isConnected()
bool NetworkClient::isConnected()
{
return WiFi.status() == WL_CONNECTED;
}
uint8_t *Network::downloadFile(const char *url, int32_t *defaultLen)
uint8_t *NetworkClient::downloadFile(const char *url, int32_t *defaultLen)
{
if (!isConnected())
return NULL;
@ -82,7 +82,7 @@ uint8_t *Network::downloadFile(const char *url, int32_t *defaultLen)
return buffer;
}
uint8_t *Network::downloadFile(WiFiClient *s, int32_t len)
uint8_t *NetworkClient::downloadFile(WiFiClient *s, int32_t len)
{
if (!isConnected())
return NULL;

View File

@ -1,5 +1,5 @@
#ifndef __NETWORK_H__
#define __NETWORK_H__
#ifndef __NETWORKCLIENT_H__
#define __NETWORKCLIENT_H__
#include "Arduino.h"
#include "HTTPClient.h"
@ -18,7 +18,7 @@ struct bitmapHeader
uint32_t compression;
};
class Network
class NetworkClient
{
public:
bool joinAP(const char *ssid, const char *pass);

View File

@ -7,9 +7,9 @@
#include "Esp.h"
#include "Mcp.h"
#include "Network.h"
#include "NetworkClient.h"
class System : public Esp, public Mcp, virtual public Network
class System : public Esp, public Mcp, virtual public NetworkClient
{
public:
void setPanelState(uint8_t s);