Skip to content

Commit 62d9ae1

Browse files
committed
Support HTTP GET INTERRUPT COUNT with optional parameters
1 parent 6f3bd3c commit 62d9ae1

File tree

3 files changed

+101
-34
lines changed

3 files changed

+101
-34
lines changed

ESP8266-MyWidget-Demo.ino

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2323
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
SOFTWARE.
2525
26-
Version: 1.1.0
26+
Version: 1.1.1
2727
*/
2828
#include <ESP8266WiFi.h>
2929
#include <WiFiClient.h>
@@ -61,7 +61,6 @@ Version: 1.1.0
6161
const char* ssid = "YOUR-SSID";
6262
const char* password = "YOUR-SSID-PASSWORD";
6363

64-
6564
// Your MQTT broker address here
6665
#define MQTT_HOST IPAddress(192, 168, 0, 100)
6766

@@ -70,14 +69,15 @@ const char* host = "mywidget";
7069

7170
// global LED state
7271
bool ledState1 = 0;
73-
// global counter
72+
// global MQTT publish counter
7473
int counter = 1;
7574

7675
// interrupt variables
7776
volatile unsigned long interruptCounter1 = 0;
7877
volatile unsigned long interruptCounter2 = 0;
7978
volatile unsigned long interruptCounter3 = 0;
8079

80+
// timer intervals
8181
unsigned long interruptInterval1 = INT_SLOW;
8282
unsigned long interruptInterval2 = INT_FAST;
8383
unsigned long interruptInterval3 = INT_FASTEST;
@@ -87,7 +87,6 @@ int timer1_idx=0; // ledTimerISR()
8787
int timer2_idx=0; // counterTimer1ISR()
8888
int timer3_idx=0; // counterTimer2ISR()
8989

90-
9190
// init ESP8266 timer
9291
ESP8266Timer ITimer;
9392
ESP8266_ISR_Timer ISR_Timer;
@@ -417,6 +416,33 @@ void handleStatusJson(AsyncWebServerRequest* request)
417416
json += "}";
418417
request->send(200, "application/json", json);
419418
} // handleStatusJson()
419+
//
420+
// Based on args, return interrupt counters
421+
//
422+
void handleIntCounter(AsyncWebServerRequest* request)
423+
{
424+
String buf;
425+
Serial.println("GET intcount");
426+
427+
if(request->params() != 0) {
428+
if (request->arg("1") == "true") {
429+
Serial.println("intcount1");
430+
buf += ":intcount1=" + String(interruptCounter1);
431+
}
432+
if (request->arg("2") == "true") {
433+
Serial.println("intcount2");
434+
buf += ":intcount2=" + String(interruptCounter2);
435+
}
436+
if (request->arg("3") == "true") {
437+
Serial.println("intcount3");
438+
buf += ":intcount3=" + String(interruptCounter3);
439+
}
440+
441+
} else {
442+
buf += ":intcount1=" + String(interruptCounter1);
443+
}
444+
request->send(200, "text/plain", buf);
445+
} // handleIntCounter()
420446

421447
void handleStatus(AsyncWebServerRequest* request)
422448
{
@@ -480,21 +506,19 @@ void initWeb() {
480506
Serial.println("GET heap");
481507
request->send(200, "text/plain", "freeHeap=" + String(ESP.getFreeHeap()));
482508
});
483-
484-
// get intcount (TEXT response)
509+
// get intcount with optional parameters (TEXT response)
485510
webServer.on("/intcount", HTTP_GET, [](AsyncWebServerRequest *request) {
486-
Serial.println("GET intcount");
487-
request->send(200, "text/plain", "Interrupt Counter=" + String(interruptCounter1));
511+
handleIntCounter(request);
488512
});
489513
// get intcount2 (TEXT response)
490514
webServer.on("/intcount2", HTTP_GET, [](AsyncWebServerRequest *request) {
491515
Serial.println("GET intcount2");
492-
request->send(200, "text/plain", "Interrupt Counter2=" + String(interruptCounter2));
516+
request->send(200, "text/plain", ":intcount2=" + String(interruptCounter2));
493517
});
494518
// get intcount3 (TEXT response)
495519
webServer.on("/intcount3", HTTP_GET, [](AsyncWebServerRequest *request) {
496520
Serial.println("GET intcount3");
497-
request->send(200, "text/plain", "Interrupt Counter3=" + String(interruptCounter3));
521+
request->send(200, "text/plain", ":intcount3=" + String(interruptCounter3));
498522
});
499523
// get status (TEXT response)
500524
webServer.on("/status", HTTP_GET, [](AsyncWebServerRequest* request) {
@@ -504,7 +528,6 @@ void initWeb() {
504528
webServer.on("/status-json", HTTP_GET, [](AsyncWebServerRequest* request) {
505529
handleStatusJson(request);
506530
});
507-
508531
// post counter (TEXT response)
509532
webServer.on("/counter", HTTP_POST, [](AsyncWebServerRequest *request){
510533
String message;

README.html

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,53 @@ <h2>HTTP API - GET and POST</h2>
207207
<br>
208208
<li>HTTP GET INTERRUPT COUNT (TEXT)</li>
209209
<ul>
210-
<li>URL: http://mywidget.local/intcount</li>
211-
<li>Returns interrupt counter - interruptCounter1 variable</li>
210+
<li>URL: http://mywidget.local/intcount + optional query string parameters</li>
211+
<li>Returns interrupt counter(s) based on query string parameters, if no query string parameters, then intcount1</li>
212212
<li>Request:</li>
213213
<ul>
214-
<li>Name/Value Pair: N/A</li>
214+
<li>Query String Parameters (optional): N/A</li>
215215
</ul>
216216
<li>Response:</li>
217217
<ul>
218-
<li>Interrupt Counter=[Interrupt Counter]</li>
218+
<li>:intcount1=[Interrupt Counter1]</li>
219219
</ul>
220-
<br>
221220
<li>Example Request:</li>
222221
<ul>
223222
<li>http://mywidget.local/intcount</li>
224223
</ul>
225224
<li>Example Response:</li>
226225
<ul>
227-
<li>Interrupt Counter=4533456</li>
226+
<li>:intcount=4533456</li>
227+
</ul>
228+
<li>Query string parameters (optional):</li>
229+
<ul>
230+
<li>Request:</li>
231+
<ul>
232+
<li>1=true, 2=true, 3=true</li>
233+
</ul>
234+
<li>Response:</li>
235+
<ul>
236+
<li>Interrupt Counter(s) based on query string</li>
237+
<li>Name value pairs are delimited using colon ":".</li>
238+
<li>Name and value fields are delimited using a equal "=" sign.</li>
239+
</ul>
240+
<li>Example Request:</li>
241+
<ul>
242+
<li>http://mywidget.local/intcount?1=true&2=true&3=true</li>
228243
</ul>
244+
<li>Example Response:</li>
245+
<ul>
246+
<li>:intcount1=4533456:intcount2=23236:intcount3=98434445</li>
247+
</ul>
248+
<li>Example Request:</li>
249+
<ul>
250+
<li>http://mywidget.local/intcount?3=true</li>
251+
</ul>
252+
<li>Example Response:</li>
253+
<ul>
254+
<li>:intcount3=98434445</li>
255+
</ul>
256+
</ul>
229257
</ul>
230258
<br>
231259
<li>HTTP GET INTERRUPT COUNT2 (TEXT)</li>
@@ -234,11 +262,11 @@ <h2>HTTP API - GET and POST</h2>
234262
<li>Returns interrupt counter - interruptCounter2 variable</li>
235263
<li>Request:</li>
236264
<ul>
237-
<li>Name/Value Pair: N/A</li>
265+
<li>Query String Parameters: N/A</li>
238266
</ul>
239267
<li>Response:</li>
240268
<ul>
241-
<li>Interrupt Counter2=[Interrupt Counter 2]</li>
269+
<li>:intcount2=[Interrupt Counter 2]</li>
242270
</ul>
243271
<br>
244272
<li>Example Request:</li>
@@ -257,11 +285,11 @@ <h2>HTTP API - GET and POST</h2>
257285
<li>Returns interrupt counter - interruptCounter3 variable</li>
258286
<li>Request:</li>
259287
<ul>
260-
<li>Name/Value Pair: N/A</li>
288+
<li>Query String Parameters: N/A</li>
261289
</ul>
262290
<li>Response:</li>
263291
<ul>
264-
<li>Interrupt Counter=[Interrupt Counter 3]</li>
292+
<li>:intcount3=[Interrupt Counter 3]</li>
265293
</ul>
266294
<br>
267295
<li>Example Request:</li>
@@ -555,7 +583,7 @@ <h2>Future Enhancements</h2>
555583
<ul>
556584
<li>[X] Support multiple interrupt timers (ESP8266TimerInterrupt)</li>
557585
<li>[&nbsp] Support dynamic interval timers on config page</li>
558-
<li>[&nbsp] Support HTTP GET INTERRUPT COUNT with args</li>
586+
<li>[X] Support HTTP GET INTERRUPT COUNT with optional parameters</li>
559587
<li>[&nbsp] Asynch NTP support</li>
560588
<li>[&nbsp] More code comments</li>
561589
<li>[&nbsp] Other?</li>

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,40 +156,56 @@ Extract the folder in each of these zip files and place it in the "library" fold
156156
- `freeHeap=38616`
157157

158158
- HTTP GET INTERRUPT COUNT (TEXT)
159-
- URL: `http://mywidget.local/intcount`
160-
- Returns interrupt counter - interruptCounter1 variable
159+
- URL: `http://mywidget.local/intcount + optional query string parameters`
160+
- Returns interrupt counter(s) based on query string parameters, if no query string parameters, then intcount1
161161
- Request:
162-
- Name/Value Pair: N/A
162+
- Query String Parameters: N/A
163163
- Response:
164-
- Interrupt Counter=[Interrupt Counter]
164+
- `:intcount1=[Interrupt Counter]`
165165
- Example Request:
166166
- `http://mywidget.local/intcount`
167167
- Example Response:
168-
- `Interrupt Counter=4533456`
168+
- `:intcount1=4533456`
169+
170+
- Query string parameters (optional):
171+
- Request:
172+
- 1=true, 2=true, 3=true
173+
- Response::
174+
- Interrupt Counter(s) based on query string
175+
- Name value pairs are delimited using colon ":".
176+
- Name and value fields are delimited using a equal "=" sign.
177+
- Example Request:
178+
- `http://mywidget.local/intcount?1=true&2=true&3=true`
179+
- Example Response:
180+
- `:intcount1=4533456:intcount2=23236:intcount3=98434445`
181+
- Example Request:
182+
- `http://mywidget.local/intcount?3=true`
183+
- Example Response:
184+
- `:intcount3=98434445`
169185

170186
- HTTP GET INTERRUPT COUNT2 (TEXT)
171187
- URL: `http://mywidget.local/intcount2`
172188
- Returns interrupt counter 2 - interruptCounter2 variable
173189
- Request:
174-
- Name/Value Pair: N/A
190+
- Query String Parameters: N/A
175191
- Response:
176-
- Interrupt Counter2=[Interrupt Counter 2]
192+
- `:intcount2=[Interrupt Counter 2]`
177193
- Example Request:
178194
- `http://mywidget.local/intcount2`
179195
- Example Response:
180-
- `Interrupt Counter2=23236`
196+
- `:intcount2=23236`
181197

182198
- HTTP GET INTERRUPT COUNT3 (TEXT)
183199
- URL: `http://mywidget.local/intcount3`
184200
- Returns interrupt counter 3 - interruptCounter3 variable
185201
- Request:
186-
- Name/Value Pair: N/A
202+
- Query String Parameters: N/A
187203
- Response:
188-
- Interrupt Counter3=[Interrupt Counter 3]
204+
- `:intcount3=[Interrupt Counter 3]`
189205
- Example Request:
190206
- `http://mywidget.local/intcount3`
191207
- Example Response:
192-
- `Interrupt Counter3=98434445`
208+
- `:intcount3=98434445`
193209

194210
- HTTP GET STATUS (TEXT)
195211
- URL: `http://mywidget.local/status + query string (name/value pairs)`
@@ -379,7 +395,7 @@ Later I went back and added an HTTP GET with JSON response example without any J
379395

380396
- [X] Support multiple interrupt timers (ESP8266TimerInterrupt)
381397
- [ ] Support dynamic interval timers on config page
382-
- [ ] Support HTTP GET INTERRUPT COUNT with args
398+
- [X] Support HTTP GET INTERRUPT COUNT with optional parameters
383399
- [ ] Asynch NTP support
384400
- [ ] More code comments
385401
- [ ] Other?

0 commit comments

Comments
 (0)