|
| 1 | +/* |
| 2 | + Inkplate10_News_API Example for Soldered Inkplate 10 |
| 3 | + This example demonstrates how to use the Inkplate 10 to display news headlines and descriptions |
| 4 | + fetched from the News API. You will need an API key from https://newsapi.org/ to use this example. |
| 5 | +
|
| 6 | + IMPORTANT: |
| 7 | + - Update your WiFi credentials and API key in the "CHANGE HERE" section below. |
| 8 | + - Ensure you have the ArduinoJSON library installed: https://arduinojson.org/ |
| 9 | + - Adjust the timezone as needed. |
| 10 | +
|
| 11 | + For more information, visit: |
| 12 | + - Inkplate documentation: https://www.inkplate.io |
| 13 | + - Support forums: https://forum.soldered.com/ |
| 14 | +
|
| 15 | + Created by Soldered, 14.5.2025 |
| 16 | +*/ |
| 17 | + |
| 18 | +// Ensure the correct board is selected in the Arduino IDE |
| 19 | +#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2) |
| 20 | +#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu." |
| 21 | +#endif |
| 22 | + |
| 23 | +// ---------- CHANGE HERE ------------- |
| 24 | +// Adjust your timezone (e.g., 2 means UTC+2) |
| 25 | +int timeZone = 2; |
| 26 | + |
| 27 | +// WiFi credentials (replace with your WiFi network details) |
| 28 | +char ssid[] = "YourWiFiSSID"; // Replace with your WiFi SSID |
| 29 | +char pass[] = "YourWiFiPassword"; // Replace with your WiFi password |
| 30 | + |
| 31 | +// News API key (get one from https://newsapi.org/) |
| 32 | +char api_key_news[] = "YourNewsAPIKey"; // Replace with your News API key |
| 33 | +// ------------------------------------ |
| 34 | + |
| 35 | +// Include necessary libraries |
| 36 | +#include "Inkplate.h" |
| 37 | +#include "src/Network.h" |
| 38 | + |
| 39 | +// Include necessary libraries for fonts |
| 40 | +#include "Fonts/Inter12pt7b.h" |
| 41 | +#include "Fonts/GT_Pressura16pt7b.h" |
| 42 | +#include "Fonts/FreeSerifItalic24pt7b.h" |
| 43 | + |
| 44 | +// Create network and display objects |
| 45 | +Network network; |
| 46 | +Inkplate inkplate(INKPLATE_1BIT); |
| 47 | + |
| 48 | +// Constants for delays and refreshes |
| 49 | +#define DELAY_MS (uint32_t)60 * 60 * 1000 // Delay between API calls (1 hour) |
| 50 | +#define DELAY_WIFI_RETRY_SECONDS 10 // Delay for WiFi retry |
| 51 | +#define DELAY_API_RETRY_SECONDS 10 // Delay for API retry |
| 52 | + |
| 53 | +// Function declarations |
| 54 | +void setTime(); |
| 55 | +void drawNews(struct news *entities); |
| 56 | + |
| 57 | +void setup() |
| 58 | +{ |
| 59 | + // Initialize serial communication for debugging |
| 60 | + Serial.begin(115200); |
| 61 | + Serial.println(F("Starting Inkplate10_News example...")); |
| 62 | + |
| 63 | + // Initialize the display |
| 64 | + inkplate.begin(); |
| 65 | + inkplate.setTextWrap(false); |
| 66 | + Serial.println(F("Display initialized.")); |
| 67 | + |
| 68 | + // Connect to WiFi |
| 69 | + Serial.println(F("Setting WiFi credentials...")); |
| 70 | + network.setCredentials(ssid, pass, api_key_news); |
| 71 | + network.setTimeZone(timeZone); |
| 72 | + Serial.println(F("Connecting to WiFi...")); |
| 73 | + network.begin(); |
| 74 | + |
| 75 | + // Set the current time |
| 76 | + Serial.println(F("Setting time...")); |
| 77 | + setTime(); |
| 78 | + |
| 79 | + // Fetch news data and display it |
| 80 | + Serial.println(F("Fetching news data...")); |
| 81 | + struct news *entities = network.getData(inkplate); |
| 82 | + if (entities != nullptr) |
| 83 | + { |
| 84 | + Serial.println(F("News data fetched successfully. Drawing news...")); |
| 85 | + drawNews(entities); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + Serial.println(F("Failed to fetch news data.")); |
| 90 | + // Display an error message if fetching news fails |
| 91 | + inkplate.clearDisplay(); |
| 92 | + inkplate.setCursor(50, 230); |
| 93 | + inkplate.setTextSize(2); |
| 94 | + inkplate.println(F("Failed to fetch news")); |
| 95 | + inkplate.display(); |
| 96 | + } |
| 97 | + |
| 98 | + // Update the display |
| 99 | + Serial.println(F("Updating display...")); |
| 100 | + inkplate.display(); |
| 101 | + |
| 102 | + // Enter deep sleep until the next update |
| 103 | + Serial.println(F("Entering deep sleep...")); |
| 104 | + esp_sleep_enable_timer_wakeup(1000 * DELAY_MS); |
| 105 | + esp_deep_sleep_start(); |
| 106 | +} |
| 107 | + |
| 108 | +// Function to draw news items on the display |
| 109 | +void drawNews(struct news *entities) |
| 110 | +{ |
| 111 | + inkplate.setRotation(3); |
| 112 | + |
| 113 | + // Display the title "World News" |
| 114 | + inkplate.setFont(&FreeSerifItalic24pt7b); |
| 115 | + int textWidth = strlen("World News") * 12; // Approximate width (12 pixels per character) |
| 116 | + int centerX = (inkplate.width() - textWidth) / 2; |
| 117 | + inkplate.setCursor(centerX - 70, 60); // Adjust text position to center |
| 118 | + inkplate.print("World News"); |
| 119 | + |
| 120 | + // Draw a dividing line below the title |
| 121 | + int xStart = inkplate.width() * 0.03; |
| 122 | + int xEnd = inkplate.width() * 0.97; |
| 123 | + for (int lineY = 80; lineY < 83; lineY++) |
| 124 | + { |
| 125 | + inkplate.drawLine(xStart, lineY, xEnd, lineY, BLACK); |
| 126 | + } |
| 127 | + |
| 128 | + // Display the current date and time |
| 129 | + struct tm timeInfo; |
| 130 | + time_t nowSec; |
| 131 | + inkplate.getNTPEpoch(&nowSec); |
| 132 | + while (nowSec < 8 * 3600 * 2) |
| 133 | + { |
| 134 | + delay(500); |
| 135 | + yield(); |
| 136 | + nowSec = time(nullptr); |
| 137 | + } |
| 138 | + gmtime_r(&nowSec, &timeInfo); |
| 139 | + |
| 140 | + // Prepare date and time strings |
| 141 | + char dateStr[20]; |
| 142 | + char updateStr[20]; |
| 143 | + sprintf(dateStr, "Date : %02d.%02d.%04d", |
| 144 | + timeInfo.tm_mday, |
| 145 | + timeInfo.tm_mon + 1, |
| 146 | + timeInfo.tm_year + 1900); |
| 147 | + |
| 148 | + sprintf(updateStr, "Last update : %02d:%02d", |
| 149 | + timeInfo.tm_hour, |
| 150 | + timeInfo.tm_min); |
| 151 | + |
| 152 | + // Choose font |
| 153 | + inkplate.setFont(&Inter12pt7b); |
| 154 | + |
| 155 | + // Y position for the row |
| 156 | + int yPos = 105; |
| 157 | + |
| 158 | + // Print date left-aligned |
| 159 | + inkplate.setCursor(23, yPos); // 23 px from left |
| 160 | + inkplate.print(dateStr); |
| 161 | + |
| 162 | + // Calculate width of 'Last update' string for right alignment |
| 163 | + int updateStrWidth = strlen(updateStr) * 12; // Adjust 12 for your font's avg char width |
| 164 | + int xRight = inkplate.width() - updateStrWidth - 19; // 19 px margin from right |
| 165 | + |
| 166 | + // Print 'Last update' right-aligned |
| 167 | + inkplate.setCursor(xRight, yPos); |
| 168 | + inkplate.print(updateStr); |
| 169 | + |
| 170 | + // Draw a line below the date |
| 171 | + for (int lineY = 113; lineY < 116; lineY++) |
| 172 | + { |
| 173 | + inkplate.drawLine(xStart, lineY, xEnd, lineY, BLACK); |
| 174 | + } |
| 175 | + |
| 176 | + // Render news items |
| 177 | + int startY = 160; |
| 178 | + int boxHeight = 170; |
| 179 | + int boxSpacing = 35; |
| 180 | + int leftMargin = 20; |
| 181 | + int rightMargin = 20; |
| 182 | + int maxBoxes = (inkplate.height() - startY) / (boxHeight + boxSpacing); |
| 183 | + |
| 184 | + for (int i = 0; i < maxBoxes && entities[i].title[0] != '\0' && entities[i].description[0] != '\0'; i++) |
| 185 | + { |
| 186 | + Serial.printf("Drawing news item %d...\n", i + 1); |
| 187 | + int y0 = startY + i * (boxHeight + boxSpacing); |
| 188 | + int y1 = y0 + boxHeight; |
| 189 | + |
| 190 | + // Draw the title |
| 191 | + inkplate.drawTextBox(leftMargin, y0 + 5, inkplate.width() - rightMargin, y0 + 80, entities[i].title, 1, >_Pressura16pt7b, 30, false, 18); |
| 192 | + |
| 193 | + // Draw the description |
| 194 | + inkplate.drawTextBox(leftMargin, y0 + 80, inkplate.width() - rightMargin, y1 + 20, entities[i].description, 1, &Inter12pt7b, 30, false, 16); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +void setTime() |
| 199 | +{ |
| 200 | + struct tm timeInfo; |
| 201 | + time_t nowSec; |
| 202 | + inkplate.getNTPEpoch(&nowSec); |
| 203 | + while (nowSec < 8 * 3600 * 2) |
| 204 | + { |
| 205 | + delay(500); |
| 206 | + yield(); |
| 207 | + nowSec = time(nullptr); |
| 208 | + } |
| 209 | + gmtime_r(&nowSec, &timeInfo); |
| 210 | + Serial.print(F("Current time: ")); |
| 211 | + Serial.print(asctime(&timeInfo)); |
| 212 | +} |
| 213 | + |
| 214 | +void loop() |
| 215 | +{ |
| 216 | + // Not used |
| 217 | +} |
0 commit comments