inkplate-6-arduino-library/examples/3. Projects/1.Weather_station_example/1.Weather_station_example.ino

258 lines
6.8 KiB
C++

/*
Weather station example for e-radionica.com Inkplate 6
For this example you will need only USB cable and Inkplate 6.
Select "Inkplate 6(ESP32)" from Tools -> Board menu.
Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it:
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
This example will show you how you can use Inkplate 6 to display API data,
e.g. Metaweather public weather API
IMPORTANT:
Make sure to change your desired city, timezone and wifi credentials below
Want to learn more about Inkplate? Visit www.inkplate.io
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
28 July 2020 by e-radionica.com
*/
// ---------- CHANGE HERE -------------:
// time zone for adding hours
int timeZone = 2;
// city search query
char city[128] = "ZAGREB";
// change to your wifi ssid and password
char *ssid = "e-radionica.com";
char *pass = "croduino";
// ----------------------------------
// Include Inkplate library to the sketch
#include "Inkplate.h"
// header file for easier code readability
#include "Network.h"
// including fonts used
#include "Fonts/Roboto_Light_48.h"
#include "Fonts/Roboto_Light_36.h"
#include "Fonts/Roboto_Light_120.h"
// including icons generated by the py file
#include "icons.h"
// delay between API calls
#define DELAY_MS 15000
// Inkplate object
Inkplate display(INKPLATE_1BIT);
// all our network functions are in this object, see Network.h
Network network;
// contants used for drawing icons
char abbrs[32][16] = {"sn", "sl", "h", "t", "hr", "lr", "s", "hc", "lc", "c"};
const uint8_t *logos[16] = {icon_sn, icon_sl, icon_h, icon_t, icon_hr, icon_lr, icon_s, icon_hc, icon_lc, icon_c};
// variables for storing temperature
char temps[8][4] = {
"0F",
"0F",
"0F",
"0F",
};
// variables for storing days of the week
char days[8][4] = {
"",
"",
"",
"",
};
// variables for storing current time and weather info
char currentTemp[16] = "0F";
char currentWind[16] = "0m/s";
char currentTime[16] = "9:41";
char currentWeather[32] = "-";
char currentWeatherAbbr[8] = "th";
// function for drawing weather info
void drawWeather()
{
// searching for weather state abbreviation
for (int i = 0; i < 10; ++i)
{
// if found draw specified icon
if (strcmp(abbrs[i], currentWeatherAbbr) == 0)
display.drawBitmap(50, 50, logos[i], 152, 152, BLACK);
}
// draw weather state
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(40, 270);
display.println(currentWeather);
}
// function for drawing current time
void drawTime()
{
// drawing current time
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(800 - 20 * strlen(currentTime), 35);
display.println(currentTime);
}
// function for drawing city name
void drawCity()
{
// drawing city name
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(400 - 9 * strlen(city), 570);
display.println(city);
}
// function for drawing temperatures
void drawTemps()
{
// drawing 4 black rectangles in which temperatures will be written
int rectWidth = 150;
int rectSpacing = (800 - rectWidth * 4) / 5;
display.fillRect(1 * rectSpacing + 0 * rectWidth, 300, rectWidth, 220, BLACK);
display.fillRect(2 * rectSpacing + 1 * rectWidth, 300, rectWidth, 220, BLACK);
display.fillRect(3 * rectSpacing + 2 * rectWidth, 300, rectWidth, 220, BLACK);
display.fillRect(4 * rectSpacing + 3 * rectWidth, 300, rectWidth, 220, BLACK);
int textMargin = 6;
display.setFont(&Roboto_Light_48);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(1 * rectSpacing + 0 * rectWidth + textMargin, 300 + textMargin + 70);
display.println(days[0]);
display.setCursor(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 70);
display.println(days[1]);
display.setCursor(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 70);
display.println(days[2]);
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 70);
display.println(days[3]);
// drawing temperature values into black rectangles
display.setFont(&Roboto_Light_48);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.setCursor(1 * rectSpacing + 0 * rectWidth + textMargin, 300 + textMargin + 160);
display.println(temps[0]);
display.setCursor(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 160);
display.println(temps[1]);
display.setCursor(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 160);
display.println(temps[2]);
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 160);
display.println(temps[3]);
}
// current weather drawing function
void drawCurrent()
{
// drawing current information
display.setFont(&Roboto_Light_120);
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
// temperature:
display.setCursor(245, 150);
display.println(currentTemp);
// wind
display.setCursor(480, 150);
display.println(currentWind);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
// labels underneath
display.setCursor(215, 210);
display.println(F("TEMPERATURE"));
display.setCursor(500, 210);
display.println(F("WIND SPEED"));
}
void setup()
{
// begin serial and display
Serial.begin(115200);
display.begin();
// initial cleaning of buffer and physical screen
display.clearDisplay();
display.clean();
// calling our begin from network.h file
network.begin(city);
// if city not found, do nothing
if (network.location == -1)
{
display.setCursor(50, 290);
display.setTextSize(3);
display.print(F("City not in Metaweather Database"));
display.display();
while (1)
;
}
// 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
delay(5000);
}
void loop()
{
// clear display
display.clearDisplay();
// get all relevant data, see Network.cpp for info
network.getTime(currentTime);
network.getDays(days[0], days[1], days[2], days[3]);
network.getData(city, temps[0], temps[1], temps[2], temps[3], currentTemp, currentWind, currentTime, currentWeather, currentWeatherAbbr);
// draw data
drawWeather();
drawCurrent();
drawTemps();
drawCity();
drawTime();
// display and wait before doing it again
display.display();
delay(DELAY_MS);
}