Skip to content

Commit 168f395

Browse files
committed
Added Quotables examples
1 parent 4ae1af7 commit 168f395

File tree

19 files changed

+13778
-0
lines changed

19 files changed

+13778
-0
lines changed

examples/Inkplate10/Projects/Quotables_Example/Fonts/exmouth_32pt7b.h

Lines changed: 1427 additions & 0 deletions
Large diffs are not rendered by default.

examples/Inkplate10/Projects/Quotables_Example/Fonts/exmouth_40pt7b.h

Lines changed: 2165 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
Network.cpp
3+
Inkplate 6 Arduino library
4+
David Zovko, Borna Biro, Denis Vajak, Zvonimir Haramustek @ e-radionica.com
5+
September 24, 2020
6+
https://github.com/e-radionicacom/Inkplate-6-Arduino-library
7+
8+
For support, please reach over forums: forum.e-radionica.com/en
9+
For more info about the product, please check: www.inkplate.io
10+
11+
This code is released under the GNU Lesser General Public License v3.0: https://www.gnu.org/licenses/lgpl-3.0.en.html
12+
Please review the LICENSE file included with this example.
13+
If you have any questions about licensing, please contact [email protected]
14+
Distributed as-is; no warranty is given.
15+
*/
16+
17+
#include "Network.h"
18+
19+
#include <HTTPClient.h>
20+
#include <WiFi.h>
21+
#include <WiFiClientSecure.h>
22+
23+
#include "Inkplate.h"
24+
25+
// Must be installed for this example to work
26+
#include <ArduinoJson.h>
27+
28+
// external parameters from our main file
29+
extern char ssid[];
30+
extern char pass[];
31+
32+
// Get our Inkplate object from main file to draw debug info on
33+
extern Inkplate display;
34+
35+
// Static Json from ArduinoJson library
36+
StaticJsonDocument<30000> doc;
37+
38+
void Network::begin()
39+
{
40+
// Initiating wifi, like in BasicHttpClient example
41+
WiFi.mode(WIFI_STA);
42+
WiFi.begin(ssid, pass);
43+
44+
int cnt = 0;
45+
Serial.print(F("Waiting for WiFi to connect..."));
46+
while ((WiFi.status() != WL_CONNECTED))
47+
{
48+
Serial.print(F("."));
49+
delay(1000);
50+
++cnt;
51+
52+
if (cnt == 20)
53+
{
54+
Serial.println("Can't connect to WIFI, restarting");
55+
delay(100);
56+
ESP.restart();
57+
}
58+
}
59+
Serial.println(F(" connected"));
60+
61+
// Find internet time
62+
setTime();
63+
}
64+
65+
// Gets time from ntp server
66+
void Network::getTime(char *timeStr)
67+
{
68+
// Get seconds since 1.1.1970.
69+
time_t nowSecs = time(nullptr);
70+
71+
// Used to store time
72+
struct tm timeinfo;
73+
gmtime_r(&nowSecs, &timeinfo);
74+
75+
// Copies time string into timeStr
76+
strncpy(timeStr, asctime(&timeinfo) + 4, 12);
77+
78+
// Setting time string timezone
79+
int hr = 10 * timeStr[7] + timeStr[8] + timeZone;
80+
81+
// Better defined modulo, in case timezone makes hours to go below 0
82+
hr = (hr % 24 + 24) % 24;
83+
84+
// Adding time to '0' char makes it into whatever time char, for both digits
85+
timeStr[7] = hr / 10 + '0';
86+
timeStr[8] = hr % 10 + '0';
87+
}
88+
89+
bool Network::getData(char* text, char* auth)
90+
{
91+
bool f = 0;
92+
93+
// If not connected to wifi reconnect wifi
94+
if (WiFi.status() != WL_CONNECTED)
95+
{
96+
WiFi.reconnect();
97+
98+
delay(5000);
99+
100+
int cnt = 0;
101+
Serial.println(F("Waiting for WiFi to reconnect..."));
102+
while ((WiFi.status() != WL_CONNECTED))
103+
{
104+
// Prints a dot every second that wifi isn't connected
105+
Serial.print(F("."));
106+
delay(1000);
107+
++cnt;
108+
109+
if (cnt == 7)
110+
{
111+
Serial.println("Can't connect to WIFI, restart initiated.");
112+
delay(100);
113+
ESP.restart();
114+
}
115+
}
116+
}
117+
118+
// Wake up if sleeping and save inital state
119+
bool sleep = WiFi.getSleep();
120+
WiFi.setSleep(false);
121+
122+
// Http object used to make get request
123+
HTTPClient http;
124+
125+
http.getStream().setTimeout(10);
126+
http.getStream().flush();
127+
128+
// Initiate http
129+
char link[] = "https://api.quotable.io/random";
130+
http.begin(link);
131+
132+
// Actually do request
133+
int httpCode = http.GET();
134+
if (httpCode == 200)
135+
{
136+
while (http.getStream().available() && http.getStream().peek() != '{')
137+
(void)http.getStream().read();
138+
139+
// Try parsing JSON object
140+
DeserializationError error = deserializeJson(doc, http.getStream());
141+
142+
if (error)
143+
{
144+
Serial.print(F("deserializeJson() failed: "));
145+
Serial.println(error.c_str());
146+
f = 1;
147+
}
148+
else
149+
{
150+
// Set all data got from internet using formatTemp and formatWind defined above
151+
// This part relies heavily on ArduinoJson library
152+
153+
Serial.println("Success");
154+
155+
const char *buff1 = doc["content"];
156+
157+
strcpy(text, buff1);
158+
159+
const char *buff2 = doc["author"];
160+
161+
strcpy(auth, buff2);
162+
163+
// Save our data to data pointer from main file
164+
f = 0;
165+
}
166+
}
167+
else if (httpCode == 404)
168+
{
169+
// Coin id not found
170+
display.clearDisplay();
171+
display.setCursor(50, 230);
172+
display.setTextSize(2);
173+
display.println(F("Info has not been found!"));
174+
display.display();
175+
while (1)
176+
;
177+
}
178+
else
179+
{
180+
f = 1;
181+
}
182+
183+
// Clear document and end http
184+
doc.clear();
185+
http.end();
186+
187+
// Return to initial state
188+
WiFi.setSleep(sleep);
189+
190+
return !f;
191+
}
192+
193+
// Function for initial time setting ovet the ntp server
194+
void Network::setTime()
195+
{
196+
// Used for setting correct time
197+
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
198+
199+
Serial.print(F("Waiting for NTP time sync: "));
200+
time_t nowSecs = time(nullptr);
201+
while (nowSecs < 8 * 3600 * 2)
202+
{
203+
delay(500);
204+
Serial.print(F("."));
205+
yield();
206+
nowSecs = time(nullptr);
207+
}
208+
209+
Serial.println();
210+
211+
// Used to store time info
212+
struct tm timeinfo;
213+
gmtime_r(&nowSecs, &timeinfo);
214+
215+
Serial.print(F("Current time: "));
216+
Serial.print(asctime(&timeinfo));
217+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Network.h
3+
Inkplate 6 Arduino library
4+
David Zovko, Borna Biro, Denis Vajak, Zvonimir Haramustek @ e-radionica.com
5+
September 24, 2020
6+
https://github.com/e-radionicacom/Inkplate-6-Arduino-library
7+
8+
For support, please reach over forums: forum.e-radionica.com/en
9+
For more info about the product, please check: www.inkplate.io
10+
11+
This code is released under the GNU Lesser General Public License v3.0: https://www.gnu.org/licenses/lgpl-3.0.en.html
12+
Please review the LICENSE file included with this example.
13+
If you have any questions about licensing, please contact [email protected]
14+
Distributed as-is; no warranty is given.
15+
*/
16+
17+
#include "Arduino.h"
18+
19+
#include <HTTPClient.h>
20+
#include <WiFi.h>
21+
#include <WiFiClientSecure.h>
22+
23+
// To get timeZone from main file
24+
extern int timeZone;
25+
26+
// Wifi ssid and password
27+
extern char ssid[];
28+
extern char pass[];
29+
30+
struct channelInfo
31+
{
32+
char name[64];
33+
uint32_t subscribers;
34+
uint32_t total_views;
35+
uint16_t video_count;
36+
};
37+
38+
#ifndef NETWORK_H
39+
#define NETWORK_H
40+
41+
// All functions defined in Network.cpp
42+
43+
class Network
44+
{
45+
public:
46+
// Functions we can access in main file
47+
void begin();
48+
void getTime(char *timeStr);
49+
bool getData(char* text, char* auth);
50+
51+
private:
52+
// Functions called from within our class
53+
void setTime();
54+
};
55+
56+
#endif
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Quotables example for Soldered Inkplate 10
3+
For this example you will need only USB cable and Inkplate 10.
4+
Select "Inkplate 2(ESP32)" from Tools -> Board menu.
5+
Don't have "Inkplate 2(ESP32)" option? Follow our tutorial and add it:
6+
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
7+
8+
This example shows you how to use simple API call without API key. Response
9+
from server is in JSON format, so that will be shown too how it is used. What happens
10+
here is basically ESP32 connects to WiFi and sends API call and server returns HTML
11+
document containing one quote and some information about it, then using library ArduinoJSON
12+
we extract only quote from JSON data and show it on Inkplate 10. After displaying quote
13+
ESP32 goes to sleep and wakes up every 300 seconds to show new quote(you can change time interval).
14+
15+
IMPORTANT:
16+
Make sure to change wifi credentials below
17+
Also have ArduinoJSON installed in your Arduino libraries, download here: https://arduinojson.org/
18+
You can deserialize JSON data easily using JSON assistant https://arduinojson.org/v6/assistant/
19+
20+
Want to learn more about Inkplate? Visit www.inkplate.io
21+
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
22+
7 April 2022 by Soldered
23+
*/
24+
25+
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
26+
#ifndef ARDUINO_INKPLATE10
27+
#error "Wrong board selection for this example, please select Inkplate 10 in the boards menu."
28+
#endif
29+
30+
//---------- CHANGE HERE -------------:
31+
32+
// Put in your ssid and password
33+
char ssid[] = "";
34+
char pass[] = "";
35+
36+
//----------------------------------
37+
38+
// Include Inkplate library to the sketch
39+
#include "Inkplate.h"
40+
41+
// Include fonts used
42+
#include "Fonts/exmouth_40pt7b.h"
43+
44+
// Our networking functions, declared in Network.cpp
45+
#include "Network.h"
46+
47+
// create object with all networking functions
48+
Network network;
49+
50+
// create display object
51+
Inkplate display(INKPLATE_1BIT);
52+
53+
// Delay between API calls in seconds, 300 seconds is 5 minutes
54+
#define DELAY_S 300
55+
56+
// Our functions declared below setup and loop
57+
void drawAll();
58+
59+
char quote[128]; // Buffer to store quote
60+
char author[64];
61+
62+
void setup()
63+
{
64+
// Begin serial communitcation, sed for debugging
65+
Serial.begin(115200);
66+
67+
// Initial display settings
68+
display.begin();
69+
display.setTextWrap(false); // Set text wrapping to true
70+
display.setTextColor(BLACK);
71+
72+
// Our begin function
73+
network.begin();
74+
75+
while (!network.getData(quote, author))
76+
{
77+
Serial.println("Retrying retriving data!");
78+
delay(1000);
79+
}
80+
81+
// Our main drawing function
82+
drawAll();
83+
// Full refresh
84+
display.display();
85+
86+
// Go to sleep before checking again
87+
// This is set in microseconds, so it needs to be
88+
// multiplied by million to get seconds
89+
esp_sleep_enable_timer_wakeup(1000000 * DELAY_S);
90+
(void)esp_deep_sleep_start();
91+
}
92+
93+
void loop()
94+
{
95+
// Never here
96+
}
97+
98+
// Our main drawing function
99+
void drawAll()
100+
{
101+
uint8_t rows = strlen(quote) / 60, row = 0;
102+
display.setFont(&exmouth_40pt7b); // Set custom font
103+
display.setTextSize(1);
104+
display.setTextColor(BLACK); //Set text color to black
105+
display.setCursor(60, display.height() / 2 - 30 * rows); //Place text in the middle
106+
uint16_t cnt = 0;
107+
while (quote[cnt] != '\0')
108+
{
109+
if (display.getCursorX() > display.width() - 80)
110+
{
111+
row++;
112+
display.setCursor(60, display.height() / 2 - 30 * rows + row * 60);
113+
}
114+
display.print(quote[cnt]);
115+
cnt++;
116+
}
117+
display.setCursor(display.width() - 32 * strlen(author), display.height() - 30); // Set cursor to fit author name in lower right corner
118+
display.print("-");
119+
display.println(author); // Print author
120+
}

0 commit comments

Comments
 (0)