|
| 1 | +/* |
| 2 | + Inkplate2_Image_Frame_From_Web example for Soldered Inkplate 2 |
| 3 | + For this example you will need only a USB-C cable and Inkplate 2. |
| 4 | + Select "Soldered Inkplate2" from Tools -> Board menu. |
| 5 | + Don't have "Soldered Inkplate2" option? Follow our tutorial and add it: |
| 6 | + https://soldered.com/learn/add-inkplate-6-board-definition-to-arduino-ide/ |
| 7 | +
|
| 8 | + This example shows how you can set Inkplate to show random pictures from web. |
| 9 | + What happens here is basically ESP32 connects to the WiFi and sends GET request |
| 10 | + to the random image on the server and the server returns a link to the image. |
| 11 | + Then Inkplate draws that image to the screen. |
| 12 | +
|
| 13 | + Want to learn more about Inkplate? Visit www.inkplate.io |
| 14 | + Looking to get support? Write on our forums: https://forum.soldered.com/ |
| 15 | + 23 May 2023 by Soldered |
| 16 | +*/ |
| 17 | + |
| 18 | +// Next 3 lines are a precaution, you can ignore those, and the example would also work without them |
| 19 | +#ifndef ARDUINO_INKPLATE2 |
| 20 | +#error "Wrong board selection for this example, please select Soldered Inkplate2 in the boards menu." |
| 21 | +#endif |
| 22 | + |
| 23 | +// Include Inkplate library in the sketch |
| 24 | +#include "Inkplate.h" |
| 25 | + |
| 26 | +// Create an object on Inkplate library |
| 27 | +Inkplate display; |
| 28 | + |
| 29 | +// ---------------- CHANGE HERE ---------------------: |
| 30 | + |
| 31 | +// WiFi credentials |
| 32 | +const char *ssid = ""; // Your WiFi SSID |
| 33 | +const char *pass = ""; // Your WiFi password |
| 34 | + |
| 35 | +// Define delay between 2 images in seconds |
| 36 | +#define SECS_BETWEEN_IMAGES 30 |
| 37 | + |
| 38 | +// --------------------------------------------------- |
| 39 | + |
| 40 | +void setup() |
| 41 | +{ |
| 42 | + // Init serial communication |
| 43 | + Serial.begin(115200); |
| 44 | + |
| 45 | + // Init Inkplate library (you should call this function ONLY ONCE) |
| 46 | + display.begin(); |
| 47 | + |
| 48 | + // Join wifi |
| 49 | + display.connectWiFi(ssid, pass); |
| 50 | + Serial.println("Connected"); |
| 51 | + |
| 52 | + // Get the URL of the next image |
| 53 | + char url[256]; |
| 54 | + imageUrl(url); |
| 55 | + |
| 56 | + // Draw an image on the screen |
| 57 | + Serial.println(display.drawImage(url, display.PNG, 0, 0)); |
| 58 | + display.display(); |
| 59 | + |
| 60 | + // Go to deep sleep |
| 61 | + Serial.println("Going to sleep, bye"); |
| 62 | + esp_sleep_enable_timer_wakeup(SECS_BETWEEN_IMAGES * 1000 * 1000LL); // Activate wakeup timer |
| 63 | + esp_deep_sleep_start(); // Start deep sleep (this function does not return). Program stops here. |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + // Never here! If you are using deep sleep, the whole program should be in setup() because the board restarts each |
| 69 | + // time. loop() must be empty! |
| 70 | +} |
| 71 | + |
| 72 | +// Get random image from web |
| 73 | +void imageUrl(char *a) |
| 74 | +{ |
| 75 | + String url; |
| 76 | + HTTPClient http; |
| 77 | + |
| 78 | + // Do GET request |
| 79 | + if (http.begin("https://source.unsplash.com/random/212x104?crop=entropy&fit=crop&fm=png&h=104&w=212") && http.GET() > 0) |
| 80 | + { |
| 81 | + url = http.getString(); |
| 82 | + |
| 83 | + int urlStart = url.indexOf("href=\"") + 6; |
| 84 | + int urlEnd = url.indexOf("\">", urlStart); |
| 85 | + |
| 86 | + url = url.substring(urlStart, urlEnd); |
| 87 | + url = url.substring(0, url.indexOf("?")) + "?crop=entropy&fit=crop&fm=png&h=104&w=212"; |
| 88 | + |
| 89 | + // Print url to the Serial Monitor and copy to the buffer |
| 90 | + Serial.println(url); |
| 91 | + strcpy(a, url.c_str()); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + // Something went wrong, print an error message |
| 96 | + display.println("HTTP error"); |
| 97 | + display.display(); |
| 98 | + } |
| 99 | + http.end(); |
| 100 | +} |
0 commit comments