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

306 lines
7.9 KiB
Arduino
Raw Normal View History

2020-07-29 08:51:24 +02:00
/*
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
2020-07-29 09:37:28 +02:00
Also have ArduinoJSON installed in your Arduino libraries
2020-07-29 08:51:24 +02:00
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 -------------:
2020-07-29 09:37:28 +02:00
// Time zone for adding hours
2020-07-29 08:51:24 +02:00
int timeZone = 2;
2020-07-29 09:37:28 +02:00
// City search query
2020-07-29 08:51:24 +02:00
char city[128] = "ZAGREB";
2020-07-29 09:37:28 +02:00
// Change to your wifi ssid and password
2020-07-29 09:38:34 +02:00
char *ssid = "";
char *pass = "";
2020-07-29 08:51:24 +02:00
// ----------------------------------
// Include Inkplate library to the sketch
#include "Inkplate.h"
2020-07-29 09:37:28 +02:00
// Header file for easier code readability
2020-07-29 08:51:24 +02:00
#include "Network.h"
2020-07-29 09:37:28 +02:00
// Including fonts used
2020-07-29 08:51:24 +02:00
#include "Fonts/Roboto_Light_48.h"
#include "Fonts/Roboto_Light_36.h"
#include "Fonts/Roboto_Light_120.h"
2020-07-29 09:37:28 +02:00
// Including icons generated by the py file
2020-07-29 08:51:24 +02:00
#include "icons.h"
2020-07-29 09:37:28 +02:00
// Delay between API calls
2020-07-29 08:51:24 +02:00
#define DELAY_MS 15000
// Inkplate object
Inkplate display(INKPLATE_1BIT);
2020-07-29 09:37:28 +02:00
// All our network functions are in this object, see Network.h
2020-07-29 08:51:24 +02:00
Network network;
2020-07-29 09:37:28 +02:00
// Contants used for drawing icons
2020-07-29 08:51:24 +02:00
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};
2020-07-29 09:37:28 +02:00
// Variables for storing temperature
2020-07-29 08:51:24 +02:00
char temps[8][4] = {
"0F",
"0F",
"0F",
"0F",
};
2020-07-29 09:37:28 +02:00
// Variables for storing days of the week
2020-07-29 08:51:24 +02:00
char days[8][4] = {
"",
"",
"",
"",
};
2020-07-29 09:37:28 +02:00
// Variable for counting partial refreshes
long refreshes = 0;
// Constant to determine when to full update
const int fullRefresh = 10;
// Variables for storing current time and weather info
2020-07-29 08:51:24 +02:00
char currentTemp[16] = "0F";
char currentWind[16] = "0m/s";
char currentTime[16] = "9:41";
char currentWeather[32] = "-";
char currentWeatherAbbr[8] = "th";
2020-07-29 09:37:28 +02:00
// functions defined below
void drawWeather();
void drawCurrent();
void drawTemps();
void drawCity();
void drawTime();
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, see functions below for info
drawWeather();
drawCurrent();
drawTemps();
drawCity();
drawTime();
// Refresh full screen every fullRefresh times, defined above
if (refreshes % fullRefresh == 0)
display.display();
else
display.partialUpdate();
// 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
2020-07-29 08:51:24 +02:00
void drawWeather()
{
2020-07-29 09:37:28 +02:00
// Searching for weather state abbreviation
2020-07-29 08:51:24 +02:00
for (int i = 0; i < 10; ++i)
{
2020-07-29 09:37:28 +02:00
// If found draw specified icon
2020-07-29 08:51:24 +02:00
if (strcmp(abbrs[i], currentWeatherAbbr) == 0)
display.drawBitmap(50, 50, logos[i], 152, 152, BLACK);
}
2020-07-29 09:37:28 +02:00
// Draw weather state
2020-07-29 08:51:24 +02:00
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(40, 270);
display.println(currentWeather);
}
2020-07-29 09:37:28 +02:00
// Function for drawing current time
2020-07-29 08:51:24 +02:00
void drawTime()
{
2020-07-29 09:37:28 +02:00
// Drawing current time
2020-07-29 08:51:24 +02:00
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(800 - 20 * strlen(currentTime), 35);
display.println(currentTime);
}
2020-07-29 09:37:28 +02:00
// Function for drawing city name
2020-07-29 08:51:24 +02:00
void drawCity()
{
2020-07-29 09:37:28 +02:00
// Drawing city name
2020-07-29 08:51:24 +02:00
display.setTextColor(BLACK, WHITE);
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
display.setCursor(400 - 9 * strlen(city), 570);
display.println(city);
}
2020-07-29 09:37:28 +02:00
// Function for drawing temperatures
2020-07-29 08:51:24 +02:00
void drawTemps()
{
2020-07-29 09:37:28 +02:00
// Drawing 4 black rectangles in which temperatures will be written
2020-07-29 08:51:24 +02:00
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);
2020-07-29 09:43:08 +02:00
display.print(days[0]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:43:08 +02:00
display.print(days[1]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:43:08 +02:00
display.print(days[2]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:43:08 +02:00
display.print(days[3]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
// Drawing temperature values into black rectangles
2020-07-29 08:51:24 +02:00
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]);
}
2020-07-29 09:37:28 +02:00
// Current weather drawing function
2020-07-29 08:51:24 +02:00
void drawCurrent()
{
2020-07-29 09:37:28 +02:00
// Drawing current information
// Temperature:
2020-07-29 08:51:24 +02:00
display.setFont(&Roboto_Light_120);
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
display.setCursor(245, 150);
2020-07-29 09:37:28 +02:00
display.print(currentTemp);
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
int x = display.getCursorX();
int y = display.getCursorY();
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setFont(&Roboto_Light_48);
2020-07-29 08:51:24 +02:00
display.setTextSize(1);
2020-07-29 09:37:28 +02:00
display.setCursor(x, y);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
// Wind:
display.setFont(&Roboto_Light_120);
display.setTextSize(1);
display.setTextColor(BLACK, WHITE);
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setCursor(480, 150);
display.print(currentWind);
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
x = display.getCursorX();
y = display.getCursorY();
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setFont(&Roboto_Light_48);
display.setTextSize(1);
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setCursor(x, y);
display.println(F("m/s"));
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
// Labels underneath
display.setFont(&Roboto_Light_36);
display.setTextSize(1);
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setCursor(215, 210);
display.println(F("TEMPERATURE"));
2020-07-29 08:51:24 +02:00
2020-07-29 09:37:28 +02:00
display.setCursor(500, 210);
display.println(F("WIND SPEED"));
2020-07-29 08:51:24 +02:00
}