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 (" \n Successfully 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+ }
0 commit comments