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

306 lines
8.0 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 12:09:01 +02:00
e.g. Metaweather public weather API
2020-07-29 08:51:24 +02:00
IMPORTANT:
2020-07-31 12:09:01 +02:00
Make sure to change your desired city, timezone and wifi credentials below
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
*/
2020-07-31 12:09:01 +02:00
// ---------- CHANGE HERE -------------:
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// Time zone for adding hours
int timeZone = 2;
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// City search query
char city[128] = "ZAGREB";
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// Change to your wifi ssid and password
char *ssid = "e-radionica.com";
char *pass = "croduino";
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// ----------------------------------
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// Include Inkplate library to the sketch
2020-07-31 10:33:14 +02:00
#include "Inkplate.h"
2020-07-31 12:09:01 +02:00
// Header file for easier code readability
#include "Network.h"
// 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-31 12:09:01 +02:00
// Including icons generated by the py file
#include "icons.h"
// Delay between API calls
#define DELAY_MS 15000
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// Inkplate object
Inkplate display(INKPLATE_1BIT);
2020-07-29 08:51:24 +02:00
2020-07-31 12:09:01 +02:00
// All our network functions are in this object, see Network.h
Network network;
// 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-31 12:09:01 +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 12:09:01 +02:00
// Variables for storing days of the week
2020-07-29 08:51:24 +02:00
char days[8][4] = {
"",
"",
"",
"",
};
2020-07-31 12:09:01 +02:00
// Variable for counting partial refreshes
long refreshes = 0;
// Constant to determine when to full update
const int fullRefresh = 10;
2020-07-29 09:37:28 +02:00
2020-07-31 12:09:01 +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";
2020-07-31 12:09:01 +02:00
2020-07-29 08:51:24 +02:00
char currentTime[16] = "9:41";
2020-07-31 12:09:01 +02:00
2020-07-29 08:51:24 +02:00
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 12:09:01 +02:00
// Begin serial and display
2020-07-29 09:37:28 +02:00
Serial.begin(115200);
2020-07-31 12:09:01 +02:00
display.begin();
2020-07-29 09:37:28 +02:00
2020-07-31 12:09:01 +02:00
// Initial cleaning of buffer and physical screen
2020-07-29 09:37:28 +02:00
display.clearDisplay();
display.clean();
2020-07-31 12:09:01 +02:00
// Calling our begin from network.h file
2020-07-29 09:37:28 +02:00
network.begin(city);
2020-07-31 12:09:01 +02:00
// If city not found, do nothing
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 12:09:01 +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 12:09:01 +02:00
// Wait a bit before proceeding
delay(5000);
2020-07-29 09:37:28 +02:00
}
void loop()
{
2020-07-31 12:09:01 +02:00
// Clear display
2020-07-29 09:37:28 +02:00
display.clearDisplay();
2020-07-31 12:09:01 +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 12:09:01 +02:00
// Draw data, see functions below for info
2020-07-29 09:37:28 +02:00
drawWeather();
drawCurrent();
drawTemps();
drawCity();
drawTime();
2020-07-31 12:09:01 +02:00
// Refresh full screen every fullRefresh times, defined above
2020-07-29 09:37:28 +02:00
if (refreshes % fullRefresh == 0)
display.display();
else
display.partialUpdate();
2020-07-31 12:09:01 +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 12:09:01 +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 12:09:01 +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-31 12:09:01 +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-31 12:09:01 +02:00
// Function for drawing current time
2020-07-29 08:51:24 +02:00
void drawTime()
{
2020-07-31 12:09:01 +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-31 12:09:01 +02:00
// Function for drawing city name
2020-07-29 08:51:24 +02:00
void drawCity()
{
2020-07-31 12:09:01 +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 12:09:01 +02:00
// Function for drawing temperatures
2020-07-29 08:51:24 +02:00
void drawTemps()
{
2020-07-31 12:09:01 +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: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 12:09:01 +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 12:09:01 +02:00
// Current weather drawing function
2020-07-29 08:51:24 +02:00
void drawCurrent()
{
2020-07-31 12:09:01 +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 12:09:01 +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 12:09:01 +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
}