Skip to content

Commit eb8d383

Browse files
committed
Added examples
1 parent e361d4c commit eb8d383

File tree

3 files changed

+472
-0
lines changed

3 files changed

+472
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
**************************************************
3+
*
4+
* @file Inkplate_6_Motion_HTTP_POST.ino
5+
* @brief Send some sensor data via HTTP POST request to webhook.site
6+
*
7+
* For info on how to quickly get started with Inkplate 6MOTION visit docs.inkplate.com
8+
*
9+
* @authors Borna Biro and Robert Soric for soldered.com
10+
* @date January 2025
11+
***************************************************/
12+
13+
// Add an Inkplate Motion Libray to the Sketch
14+
#include <InkplateMotion.h>
15+
16+
// Include ArduinoJSON library. Get it from here: https://github.com/bblanchon/ArduinoJson
17+
#include <ArduinoJson.h>
18+
19+
// Change WiFi SSID and password here
20+
#define WIFI_SSID ""
21+
#define WIFI_PASS ""
22+
23+
// Go to webhook.site and get a unique URL, something like:
24+
// https://webhook.site/0e92fcdc-test-1234-test-44e5ca8a0b47
25+
// Copy and paste it here, this is the URL where the POST request will be made:
26+
char httpUrl[] = {""};
27+
// On webhook.site, the post requests will be in visible in the sidebar to the left
28+
29+
// Create an Inkplate Motion Object
30+
Inkplate inkplate;
31+
32+
void setup()
33+
{
34+
// Initialize the Inkplate Motion Library
35+
inkplate.begin(INKPLATE_1BW);
36+
37+
// Shuffle the seed to make random numbers
38+
randomSeed(analogRead(PA2) ^ analogRead(PA2));
39+
40+
// Clear the screen
41+
inkplate.display();
42+
43+
// Set the text
44+
inkplate.setCursor(0, 0);
45+
inkplate.setTextSize(2);
46+
inkplate.setTextColor(BLACK, WHITE);
47+
inkplate.setTextWrap(true);
48+
49+
// Let's initialize the Wi-Fi library:
50+
WiFi.init();
51+
52+
// Set mode to Station
53+
WiFi.setMode(INKPLATE_WIFI_MODE_STA);
54+
55+
// Connect to WiFi:
56+
WiFi.begin(WIFI_SSID, WIFI_PASS);
57+
// Wait until we're connected, this is strongly reccomended to do!
58+
// At least do delay(3000) to give the modem some time to connect
59+
inkplate.print("Connecting to Wi-Fi...");
60+
while (!WiFi.connected())
61+
{
62+
inkplate.print('.');
63+
inkplate.partialUpdate(true);
64+
delay(1000);
65+
}
66+
// Show connection successful screen and keep it for a second
67+
inkplate.println("\nSuccessfully connected to Wi-Fi!");
68+
inkplate.display();
69+
delay(1000);
70+
71+
// Clear the screen and print info message
72+
inkplate.clearDisplay();
73+
inkplate.setCursor(0, 0);
74+
inkplate.println("Sending POST request each 30 seconds...\n\n");
75+
inkplate.partialUpdate(true);
76+
}
77+
78+
void loop()
79+
{
80+
// Let's periodically send the POST request with a random number:
81+
inkplate.println("-> Data sent, response:");
82+
inkplate.partialUpdate(true);
83+
sendPost();
84+
delay(30000); // Wait 30 seconds
85+
86+
// Now let's do the same but with JSON
87+
inkplate.println("-> Data sent with JSON, response:");
88+
inkplate.partialUpdate(true);
89+
sendPostWithJson();
90+
delay(30000); // Wait 30 seconds
91+
}
92+
93+
// This function sends a POST request to the set URL
94+
void sendPost()
95+
{
96+
// Create the WiFi client object for HTTP request
97+
WiFiClient client;
98+
99+
// Since the file will be received in chunks, keeps track of the byte received
100+
uint32_t totalSize = 0;
101+
102+
// Begin connection to URL
103+
if (client.begin(httpUrl))
104+
{
105+
// Create request body, a simple request with one data field
106+
char bodyStr[256];
107+
sprintf(bodyStr, "data=%d", random(-127, 128)); // Add a random number from -126 to 128
108+
// Do POST
109+
if (client.POST(bodyStr, strlen(bodyStr)))
110+
{
111+
while (client.available())
112+
{
113+
// Use blocking method to get all chunks of the HTTP reply
114+
{
115+
if (client.available() > 0)
116+
{
117+
char myBuffer[2000];
118+
int n = client.read(myBuffer, sizeof(myBuffer));
119+
totalSize += n;
120+
121+
// Print out the chunk
122+
for (int i = 0; i < n; i++)
123+
{
124+
inkplate.print(myBuffer[i]);
125+
}
126+
}
127+
}
128+
}
129+
}
130+
// Add new line at the end
131+
inkplate.println();
132+
133+
// Refresh the screen
134+
inkplate.partialUpdate(true);
135+
}
136+
else
137+
{
138+
// Error? inform the user
139+
inkplate.println("Failed to POST");
140+
inkplate.partialUpdate(true);
141+
}
142+
143+
// End client HTTP request (MUST HAVE otherwise any other AT command to the modem will fail)
144+
if (!client.end())
145+
{
146+
inkplate.println("Client end problem");
147+
}
148+
}
149+
150+
// This function sends the POST request but with a JSON body
151+
void sendPostWithJson()
152+
{
153+
// Create the WiFi client object for HTTP request
154+
WiFiClient client;
155+
156+
// Since the file will be received in chunks, keeps track of the byte received
157+
uint32_t totalSize = 0;
158+
159+
// Connect to the URL
160+
if (client.begin(httpUrl))
161+
{
162+
// Add a HTTP header (must be added for JSON in this case)
163+
client.addHeader("Content-Type: application/json");
164+
165+
// Create body with JSON
166+
char jsonData[256];
167+
StaticJsonDocument<200> webhookSiteJSON;
168+
// Add number data to field1
169+
webhookSiteJSON["dataField"] = random(-127, 128);
170+
// Serialize it so that it can be added to the POST request
171+
serializeJson(webhookSiteJSON, jsonData);
172+
173+
// Do POST
174+
if (client.POST(jsonData, strlen(jsonData)))
175+
{
176+
while (client.available())
177+
{
178+
// Use blocking method to get all chunks of the HTTP reply
179+
{
180+
if (client.available() > 0)
181+
{
182+
char myBuffer[2000];
183+
int n = client.read(myBuffer, sizeof(myBuffer));
184+
totalSize += n;
185+
186+
// Print out the chunk
187+
for (int i = 0; i < n; i++)
188+
{
189+
inkplate.print(myBuffer[i]);
190+
}
191+
}
192+
}
193+
}
194+
}
195+
// Update the display
196+
inkplate.partialUpdate(true);
197+
}
198+
else
199+
{
200+
// Error? Inform the user
201+
inkplate.println("Failed to POST");
202+
inkplate.partialUpdate(true);
203+
}
204+
205+
// End client HTTP request (MUST HAVE otherwise any other AT command to the modem will fail)
206+
if (!client.end())
207+
{
208+
inkplate.println("Client end problem");
209+
}
210+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
**************************************************
3+
*
4+
* @file Inkplate_6_Motion_Image_From_Web.ino
5+
* @brief Connect to Wi-Fi and download and show an image from the web
6+
*
7+
*
8+
* For info on how to quickly get started with Inkplate 6MOTION visit docs.inkplate.com
9+
*
10+
* @authors Borna Biro and Robert Soric for soldered.com
11+
* @date January 2025
12+
***************************************************/
13+
14+
// Include Inkplate Motion library
15+
#include <InkplateMotion.h>
16+
17+
Inkplate inkplate; // Create Inkplate object
18+
19+
// Change WiFi SSID and password here
20+
#define WIFI_SSID "Soldered"
21+
#define WIFI_PASS "dasduino"
22+
23+
// Link to download the .jpg image from
24+
char imageUrl[] = {"https://gssc.esa.int/navipedia/images/a/a9/Example.jpg"};
25+
26+
void setup()
27+
{
28+
inkplate.begin(INKPLATE_GRAYSCALE); // Initialize Inkplate in grayscale
29+
30+
// Set text options
31+
inkplate.setCursor(0, 0);
32+
inkplate.setTextSize(2);
33+
inkplate.setTextColor(0);
34+
inkplate.setTextWrap(true);
35+
36+
// Let's initialize the Wi-Fi library:
37+
WiFi.init();
38+
39+
// Set mode to Station
40+
WiFi.setMode(INKPLATE_WIFI_MODE_STA);
41+
42+
// Connect to WiFi:
43+
WiFi.begin(WIFI_SSID, WIFI_PASS);
44+
// Wait until we're connected, this is strongly reccomended to do!
45+
// At least do delay(3000) to give the modem some time to connect
46+
inkplate.print("Connecting to Wi-Fi...");
47+
while (!WiFi.connected())
48+
{
49+
inkplate.print('.');
50+
inkplate.partialUpdate(true);
51+
delay(1000);
52+
}
53+
inkplate.println("\nSuccessfully connected to Wi-Fi!");
54+
inkplate.display();
55+
56+
/*
57+
Let's draw the image from the URL
58+
First parameters is the URL
59+
Second and third parameters are x, y coordinates where to print the image (upper left corner)
60+
Fourth parameter is color invert (true, false)
61+
Fifth is dither (1 - enabled, 0 - disabled)
62+
Fourth and fifth are kernel selection, you can use:
63+
FS_KERNEL: Floyd-Steinberg
64+
STUCKI_KERNEL: Stucki
65+
SIERRA_KERNEL: Sierra
66+
SIERRA_LITE_KERNEL: Sierra Lite
67+
ATKINSON_KERNEL: Atkinson
68+
BURKES_KERNEL: Burke
69+
The fifth parameter, depending on the Kernel just has the suffix '_SIZE', eg. FS_KERNEL_SIZE
70+
*/
71+
72+
// Note that progressive-encoded jpg's aren't supported
73+
if (!inkplate.image.draw(imageUrl, 0, 0, false, 1, FS_KERNEL, FS_KERNEL_SIZE))
74+
{
75+
// Show error on the screen if decode failed
76+
inkplate.println("Image download or decode failed!");
77+
inkplate.printf("Decode Err: %d\r\n", inkplate.image.getError());
78+
inkplate.partialUpdate();
79+
}
80+
else
81+
{
82+
// Otherwise, refresh the screen with a partial update
83+
inkplate.partialUpdate();
84+
}
85+
86+
// Show the image on the display
87+
inkplate.display();
88+
}
89+
90+
void loop()
91+
{
92+
}

0 commit comments

Comments
 (0)