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

294 lines
8.4 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,
2020-07-31 10:33:14 +02:00
in this example Metaweather public weather API which provide weather info.
As a result, you get a functional weather station which shows today's
forecast and 3 days forecast on your Inkplate display.
2020-07-29 08:51:24 +02:00
IMPORTANT:
2020-07-31 10:33:14 +02:00
Make sure you have changed your desired city, timezone and WiFi credentials below.
You will also need to install ArduinoJSON library.
Download from here: https://github.com/bblanchon/ArduinoJson
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
*/
2020-07-31 10:33:14 +02:00
// ---------------- CHANGE HERE ---------------:
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
int timeZone = 2; //Update your timezone here. 2 means UTC+2
2020-07-29 08:51:24 +02:00
2020-07-31 10:35:39 +02:00
char city[128] = "ZAGREB"; //Enter city name you wish to get forecast for
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
// Change to your WiFi SSID and password
2020-07-31 10:35:39 +02:00
char *ssid = "";
char *pass = "";
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
#define DELAY_MS 15000 //Delay between screen refreshes goes here
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
// ----------------------------------------------
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
#include "Inkplate.h"
#include "Network.h" //Header file for easier code readability
//Include custom fonts & icons 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-31 10:33:14 +02:00
#include "icons.h" //Generated using embedded iconConvert.py script
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
Inkplate display(INKPLATE_1BIT); //Inkplate object
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
Network network; // All our network functions are in this object, see Network.h
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +02:00
//Constants 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-31 10:33:14 +02:00
//Variables for storing temperature
2020-07-29 08:51:24 +02:00
char temps[8][4] = {
"0F",
"0F",
"0F",
"0F",
};
2020-07-31 10:33:14 +02:00
//Variables for storing days of the week
2020-07-29 08:51:24 +02:00
char days[8][4] = {
"",
"",
"",
"",
};
2020-07-31 10:33:14 +02:00
long refreshes = 0; //Variable for counting partial refreshes
const int fullRefresh = 10; //Constant to determine when to full update
2020-07-29 09:37:28 +02:00
2020-07-31 10:33:14 +02:00
//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()
{
2020-07-31 10:33:14 +02:00
//Begin serial and and begin display
2020-07-29 09:37:28 +02:00
Serial.begin(115200);
2020-07-31 10:33:14 +02:00
display.begin(); //Call this function only once!
2020-07-29 09:37:28 +02:00
2020-07-31 10:33:14 +02:00
//Initial cleaning of buffer and physical screen
2020-07-29 09:37:28 +02:00
display.clearDisplay();
display.clean();
2020-07-31 10:33:14 +02:00
//Calling our begin from network.h file
2020-07-29 09:37:28 +02:00
network.begin(city);
2020-07-31 10:33:14 +02:00
//If city not found, write error message and stop
2020-07-29 09:37:28 +02:00
if (network.location == -1)
{
display.setCursor(50, 290);
display.setTextSize(3);
display.print(F("City not in Metaweather Database"));
display.display();
while (1)
;
}
2020-07-31 10:33:14 +02:00
//Welcome screen
2020-07-29 09:37:28 +02:00
display.setCursor(50, 290);
display.setTextSize(3);
display.print(F("Welcome to Inkplate 6 weather example!"));
display.display();
2020-07-31 10:33:14 +02:00
//Wait a bit before proceeding
delay(3000);
2020-07-29 09:37:28 +02:00
}
void loop()
{
2020-07-31 10:33:14 +02:00
//Clear display
2020-07-29 09:37:28 +02:00
display.clearDisplay();
2020-07-31 10:33:14 +02:00
//Get all relevant data, see Network.cpp for info
2020-07-29 09:37:28 +02:00
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);
2020-07-31 10:33:14 +02:00
//Draw data, see functions below in Network.cpp for info
2020-07-29 09:37:28 +02:00
drawWeather();
drawCurrent();
drawTemps();
drawCity();
drawTime();
2020-07-31 10:33:14 +02:00
//Refresh full screen every fullRefresh times
2020-07-29 09:37:28 +02:00
if (refreshes % fullRefresh == 0)
display.display();
else
display.partialUpdate();
2020-07-31 10:33:14 +02:00
//Go to sleep before checking again
2020-07-29 09:37:28 +02:00
esp_sleep_enable_timer_wakeup(1000L * DELAY_MS);
(void)esp_light_sleep_start();
++refreshes;
}
2020-07-31 10:33:14 +02:00
//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-31 10:33:14 +02:00
//If found draw specified icon, draw it
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-31 10:33:14 +02:00
//Draw current 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-31 10:33:14 +02:00
//Function for drawing current time
2020-07-29 08:51:24 +02:00
void drawTime()
{
2020-07-31 10:33:14 +02:00
//Drawing current time stored in currentTime variable
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-31 10:33:14 +02:00
//Function for drawing city name
2020-07-29 08:51:24 +02:00
void drawCity()
{
2020-07-31 10:33:14 +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-31 10:33:14 +02:00
//Function for drawing temperatures
2020-07-29 08:51:24 +02:00
void drawTemps()
{
2020-07-31 10:33:14 +02:00
//Drawing 4 black rectangles into 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;
2020-07-31 10:33:14 +02:00
//Setting font specifics, writing the actual weather info
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 + 70);
2020-07-29 09:44:00 +02:00
display.println(days[0]);
2020-07-29 08:51:24 +02:00
display.setCursor(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:44:00 +02:00
display.println(days[1]);
2020-07-29 08:51:24 +02:00
display.setCursor(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:44:00 +02:00
display.println(days[2]);
2020-07-29 08:51:24 +02:00
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 70);
2020-07-29 09:44:00 +02:00
display.println(days[3]);
2020-07-29 08:51:24 +02:00
2020-07-31 10:33:14 +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);
2020-07-29 09:44:00 +02:00
display.print(temps[0]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(2 * rectSpacing + 1 * rectWidth + textMargin, 300 + textMargin + 160);
2020-07-29 09:44:00 +02:00
display.print(temps[1]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(3 * rectSpacing + 2 * rectWidth + textMargin, 300 + textMargin + 160);
2020-07-29 09:44:00 +02:00
display.print(temps[2]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
display.setCursor(4 * rectSpacing + 3 * rectWidth + textMargin, 300 + textMargin + 160);
2020-07-29 09:44:00 +02:00
display.print(temps[3]);
display.println(F("C"));
2020-07-29 08:51:24 +02:00
}
2020-07-31 10:33:14 +02:00
//Current weather drawing function
2020-07-29 08:51:24 +02:00
void drawCurrent()
{
2020-07-31 10:33:14 +02:00
//Drawing current information
2020-07-29 09:37:28 +02:00
// 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-31 10:33:14 +02:00
//Wind:
2020-07-29 09:37:28 +02:00
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-31 10:33:14 +02:00
//Labels underneath
2020-07-29 09:37:28 +02:00
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
}