|
| 1 | +/* |
| 2 | + Inkplate5_News_API example for Soldered Inkplate 5 |
| 3 | + For this example you will need only a USB-C cable and Inkplate 5. |
| 4 | + Select "Soldered Inkplate5" from Tools -> Board menu. |
| 5 | + Don't have "Soldered Inkplate5" option? Follow our tutorial and add it: |
| 6 | + https://soldered.com/learn/add-inkplate-6-board-definition-to-arduino-ide/ |
| 7 | + |
| 8 | + This example will show you how you can use Inkplate 5 to display API data. |
| 9 | + Here we use News API to get headline news and short description and display |
| 10 | + them on the Inkplate screen. For this you will need an API key which you can obtain |
| 11 | + here: https://newsapi.org/ |
| 12 | + On the Serial Monitor at 115200 baud rate, you can see what's happening. |
| 13 | +
|
| 14 | + IMPORTANT: |
| 15 | + Make sure to change your timezone and wifi credentials below. |
| 16 | + Also have ArduinoJson installed in your Arduino libraries, download here: https://arduinojson.org/ |
| 17 | +
|
| 18 | + Want to learn more about Inkplate? Visit www.inkplate.io |
| 19 | + Looking to get support? Write on our forums: https://forum.soldered.com/ |
| 20 | + 29 March 2023 by Soldered |
| 21 | +*/ |
| 22 | + |
| 23 | +// Next 3 lines are a precaution, you can ignore those, and the example would also work without them |
| 24 | +#ifndef ARDUINO_INKPLATE5 |
| 25 | +#error "Wrong board selection for this example, please select Soldered Inkplate5 in the boards menu." |
| 26 | +#endif |
| 27 | + |
| 28 | +//---------- CHANGE HERE -------------: |
| 29 | + |
| 30 | +// Put in your WiFi name (ssid) and password |
| 31 | +char ssid[] = ""; |
| 32 | +char pass[] = ""; |
| 33 | +char apiKey[] = ""; //You can obtain one here: https://newsapi.org/ |
| 34 | + |
| 35 | +// Delay between API calls in miliseconds (first 60 represents minutes so you can change to your need) |
| 36 | +// Here is set to 1 call per hour, but if you want to change it, have in mind that in the free plan there are only 100 free API calls |
| 37 | +#define DELAY_MS 60 * 60 * 1000 |
| 38 | + |
| 39 | +//------------------------------------- |
| 40 | + |
| 41 | +// Include Inkplate library to the sketch |
| 42 | +#include "Inkplate.h" |
| 43 | + |
| 44 | +// Our networking functions, declared in Network.cpp |
| 45 | +#include "Network.h" |
| 46 | + |
| 47 | +// Include used fonts |
| 48 | +#include "Fonts/Inter12pt7b.h" |
| 49 | +#include "Fonts/GT_Pressura16pt7b.h" |
| 50 | + |
| 51 | +// Create object with all networking functions |
| 52 | +Network network; |
| 53 | + |
| 54 | +// Create display object |
| 55 | +Inkplate display(INKPLATE_1BIT); |
| 56 | + |
| 57 | +void setup() |
| 58 | +{ |
| 59 | + // Begin serial communitcation, sed for debugging |
| 60 | + Serial.begin(115200); |
| 61 | + |
| 62 | + // Initial display settings |
| 63 | + display.begin(); // Init Inkplate library (you should call this function ONLY ONCE) |
| 64 | + display.setTextWrap(false); |
| 65 | + |
| 66 | + // Our begin function |
| 67 | + network.begin(ssid, pass); |
| 68 | + |
| 69 | + // Pointer to the struct that will hold all news data |
| 70 | + struct news *entities = NULL; |
| 71 | + |
| 72 | + // Fetch news. If something went wrong the function returns NULL |
| 73 | + entities = network.getData(apiKey); |
| 74 | + if(entities == NULL) |
| 75 | + { |
| 76 | + Serial.println(); |
| 77 | + Serial.println("Error fetching news"); |
| 78 | + ESP.restart(); |
| 79 | + } |
| 80 | + |
| 81 | + // Draw the news and display it on the screen |
| 82 | + drawNews(entities); |
| 83 | + display.display(); |
| 84 | + |
| 85 | + // Go to sleep before checking again |
| 86 | + esp_sleep_enable_timer_wakeup(1000LL * DELAY_MS); // Set wakeup timer |
| 87 | + (void)esp_deep_sleep_start(); // Put ESP32 into deep sleep (this function returns nothing). Program stops here! |
| 88 | +} |
| 89 | + |
| 90 | +void loop() |
| 91 | +{ |
| 92 | + // Nothing! If you use deep sleep, whole program should be in setup() because each time the board restarts, not in a |
| 93 | + // loop()! loop() must be empty! |
| 94 | +} |
| 95 | + |
| 96 | +// Function that draw the news |
| 97 | +void drawNews(struct news *entities) |
| 98 | +{ |
| 99 | + uint8_t coll = 0; // For keeping track of columns |
| 100 | + uint16_t y = 32; // Y coordinate for drawing |
| 101 | + uint8_t rows = 0; // For keeping track of rows |
| 102 | + int i = 0; // For each piece of news |
| 103 | + |
| 104 | + // Printing the news until we fill 3 columns |
| 105 | + // If an entire piece of news doesn't fit on the screen, don't print it |
| 106 | + while (coll < 2) |
| 107 | + { |
| 108 | + display.setCursor(10 + 320 * coll, y); // Set the cursor to the beginning of the current column |
| 109 | + display.setFont(>_Pressura16pt7b); // Set the font for the title |
| 110 | + uint16_t cnt = 0; // Index of each character in the title or description that is printing |
| 111 | + |
| 112 | + // Let's print the title |
| 113 | + while (*(entities[i].title + cnt) != '\0') |
| 114 | + { |
| 115 | + // Go to the new line if needed |
| 116 | + if (display.getCursorX() > 320 * coll + 280 || (*(entities[i].title + cnt) == ' ' && display.getCursorX() > 320 * coll + 245)) |
| 117 | + { |
| 118 | + *(entities[i].title + cnt) == ' ' ? cnt++ : 0; |
| 119 | + rows++; |
| 120 | + y += 32; |
| 121 | + display.setCursor(10 + 320 * coll, y); |
| 122 | + } |
| 123 | + |
| 124 | + // Go to the next column if there is the end of the current one |
| 125 | + if (display.getCursorY() > E_INK_HEIGHT - 20) |
| 126 | + { |
| 127 | + coll++; |
| 128 | + y = 32; |
| 129 | + display.setCursor(10 + 320 * coll, y); |
| 130 | + } |
| 131 | + |
| 132 | + // Print the text in the frame buffer in before calculated positions |
| 133 | + display.print(*(entities[i].title + cnt)); |
| 134 | + cnt++; |
| 135 | + } |
| 136 | + |
| 137 | + // Move the cursor a bit down and add indentation for the beginning of the sentence |
| 138 | + y = y + 40; |
| 139 | + display.setCursor(10 + 320 * coll, y); |
| 140 | + display.print(" "); |
| 141 | + |
| 142 | + // Reset the counter |
| 143 | + cnt = 0; |
| 144 | + |
| 145 | + // Set font for description and print description |
| 146 | + display.setFont(&Inter12pt7b); |
| 147 | + while (*(entities[i].description + cnt) != '\0') |
| 148 | + { |
| 149 | + // Go to the new line (row) if needed |
| 150 | + if (display.getCursorX() > 320 * coll + 280 || (*(entities[i].description + cnt) == ' ' && display.getCursorX() > 320 * coll + 255)) |
| 151 | + { |
| 152 | + *(entities[i].description + cnt) == ' ' ? cnt++ : 0; |
| 153 | + rows++; |
| 154 | + y += 26; |
| 155 | + display.setCursor(10 + 320 * coll, y); |
| 156 | + } |
| 157 | + |
| 158 | + // Go to the next column if there is the end of the current one |
| 159 | + if (display.getCursorY() > E_INK_HEIGHT - 20) |
| 160 | + { |
| 161 | + coll++; |
| 162 | + y = 32; |
| 163 | + display.setCursor(10 + 320 * coll, y); |
| 164 | + } |
| 165 | + |
| 166 | + // Print the text in the frame buffer in before calculated positions |
| 167 | + display.print(*(entities[i].description + cnt)); |
| 168 | + cnt++; |
| 169 | + } |
| 170 | + |
| 171 | + // Add a bit of spacing between 2 news and go to the other piece of news |
| 172 | + y += 48; |
| 173 | + i++; |
| 174 | + } |
| 175 | +} |
0 commit comments