Skip to content

Commit

Permalink
Support HTTP GET INTERRUPT COUNT with optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
onewithhammer committed Feb 18, 2021
1 parent 6f3bd3c commit 62d9ae1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 34 deletions.
45 changes: 34 additions & 11 deletions ESP8266-MyWidget-Demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Version: 1.1.0
Version: 1.1.1
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
Expand Down Expand Up @@ -61,7 +61,6 @@ Version: 1.1.0
const char* ssid = "YOUR-SSID";
const char* password = "YOUR-SSID-PASSWORD";


// Your MQTT broker address here
#define MQTT_HOST IPAddress(192, 168, 0, 100)

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

// global LED state
bool ledState1 = 0;
// global counter
// global MQTT publish counter
int counter = 1;

// interrupt variables
volatile unsigned long interruptCounter1 = 0;
volatile unsigned long interruptCounter2 = 0;
volatile unsigned long interruptCounter3 = 0;

// timer intervals
unsigned long interruptInterval1 = INT_SLOW;
unsigned long interruptInterval2 = INT_FAST;
unsigned long interruptInterval3 = INT_FASTEST;
Expand All @@ -87,7 +87,6 @@ int timer1_idx=0; // ledTimerISR()
int timer2_idx=0; // counterTimer1ISR()
int timer3_idx=0; // counterTimer2ISR()


// init ESP8266 timer
ESP8266Timer ITimer;
ESP8266_ISR_Timer ISR_Timer;
Expand Down Expand Up @@ -417,6 +416,33 @@ void handleStatusJson(AsyncWebServerRequest* request)
json += "}";
request->send(200, "application/json", json);
} // handleStatusJson()
//
// Based on args, return interrupt counters
//
void handleIntCounter(AsyncWebServerRequest* request)
{
String buf;
Serial.println("GET intcount");

if(request->params() != 0) {
if (request->arg("1") == "true") {
Serial.println("intcount1");
buf += ":intcount1=" + String(interruptCounter1);
}
if (request->arg("2") == "true") {
Serial.println("intcount2");
buf += ":intcount2=" + String(interruptCounter2);
}
if (request->arg("3") == "true") {
Serial.println("intcount3");
buf += ":intcount3=" + String(interruptCounter3);
}

} else {
buf += ":intcount1=" + String(interruptCounter1);
}
request->send(200, "text/plain", buf);
} // handleIntCounter()

void handleStatus(AsyncWebServerRequest* request)
{
Expand Down Expand Up @@ -480,21 +506,19 @@ void initWeb() {
Serial.println("GET heap");
request->send(200, "text/plain", "freeHeap=" + String(ESP.getFreeHeap()));
});

// get intcount (TEXT response)
// get intcount with optional parameters (TEXT response)
webServer.on("/intcount", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("GET intcount");
request->send(200, "text/plain", "Interrupt Counter=" + String(interruptCounter1));
handleIntCounter(request);
});
// get intcount2 (TEXT response)
webServer.on("/intcount2", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("GET intcount2");
request->send(200, "text/plain", "Interrupt Counter2=" + String(interruptCounter2));
request->send(200, "text/plain", ":intcount2=" + String(interruptCounter2));
});
// get intcount3 (TEXT response)
webServer.on("/intcount3", HTTP_GET, [](AsyncWebServerRequest *request) {
Serial.println("GET intcount3");
request->send(200, "text/plain", "Interrupt Counter3=" + String(interruptCounter3));
request->send(200, "text/plain", ":intcount3=" + String(interruptCounter3));
});
// get status (TEXT response)
webServer.on("/status", HTTP_GET, [](AsyncWebServerRequest* request) {
Expand All @@ -504,7 +528,6 @@ void initWeb() {
webServer.on("/status-json", HTTP_GET, [](AsyncWebServerRequest* request) {
handleStatusJson(request);
});

// post counter (TEXT response)
webServer.on("/counter", HTTP_POST, [](AsyncWebServerRequest *request){
String message;
Expand Down
50 changes: 39 additions & 11 deletions README.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,53 @@ <h2>HTTP API - GET and POST</h2>
<br>
<li>HTTP GET INTERRUPT COUNT (TEXT)</li>
<ul>
<li>URL: http://mywidget.local/intcount</li>
<li>Returns interrupt counter - interruptCounter1 variable</li>
<li>URL: http://mywidget.local/intcount + optional query string parameters</li>
<li>Returns interrupt counter(s) based on query string parameters, if no query string parameters, then intcount1</li>
<li>Request:</li>
<ul>
<li>Name/Value Pair: N/A</li>
<li>Query String Parameters (optional): N/A</li>
</ul>
<li>Response:</li>
<ul>
<li>Interrupt Counter=[Interrupt Counter]</li>
<li>:intcount1=[Interrupt Counter1]</li>
</ul>
<br>
<li>Example Request:</li>
<ul>
<li>http://mywidget.local/intcount</li>
</ul>
<li>Example Response:</li>
<ul>
<li>Interrupt Counter=4533456</li>
<li>:intcount=4533456</li>
</ul>
<li>Query string parameters (optional):</li>
<ul>
<li>Request:</li>
<ul>
<li>1=true, 2=true, 3=true</li>
</ul>
<li>Response:</li>
<ul>
<li>Interrupt Counter(s) based on query string</li>
<li>Name value pairs are delimited using colon ":".</li>
<li>Name and value fields are delimited using a equal "=" sign.</li>
</ul>
<li>Example Request:</li>
<ul>
<li>http://mywidget.local/intcount?1=true&2=true&3=true</li>
</ul>
<li>Example Response:</li>
<ul>
<li>:intcount1=4533456:intcount2=23236:intcount3=98434445</li>
</ul>
<li>Example Request:</li>
<ul>
<li>http://mywidget.local/intcount?3=true</li>
</ul>
<li>Example Response:</li>
<ul>
<li>:intcount3=98434445</li>
</ul>
</ul>
</ul>
<br>
<li>HTTP GET INTERRUPT COUNT2 (TEXT)</li>
Expand All @@ -234,11 +262,11 @@ <h2>HTTP API - GET and POST</h2>
<li>Returns interrupt counter - interruptCounter2 variable</li>
<li>Request:</li>
<ul>
<li>Name/Value Pair: N/A</li>
<li>Query String Parameters: N/A</li>
</ul>
<li>Response:</li>
<ul>
<li>Interrupt Counter2=[Interrupt Counter 2]</li>
<li>:intcount2=[Interrupt Counter 2]</li>
</ul>
<br>
<li>Example Request:</li>
Expand All @@ -257,11 +285,11 @@ <h2>HTTP API - GET and POST</h2>
<li>Returns interrupt counter - interruptCounter3 variable</li>
<li>Request:</li>
<ul>
<li>Name/Value Pair: N/A</li>
<li>Query String Parameters: N/A</li>
</ul>
<li>Response:</li>
<ul>
<li>Interrupt Counter=[Interrupt Counter 3]</li>
<li>:intcount3=[Interrupt Counter 3]</li>
</ul>
<br>
<li>Example Request:</li>
Expand Down Expand Up @@ -555,7 +583,7 @@ <h2>Future Enhancements</h2>
<ul>
<li>[X] Support multiple interrupt timers (ESP8266TimerInterrupt)</li>
<li>[&nbsp] Support dynamic interval timers on config page</li>
<li>[&nbsp] Support HTTP GET INTERRUPT COUNT with args</li>
<li>[X] Support HTTP GET INTERRUPT COUNT with optional parameters</li>
<li>[&nbsp] Asynch NTP support</li>
<li>[&nbsp] More code comments</li>
<li>[&nbsp] Other?</li>
Expand Down
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,40 +156,56 @@ Extract the folder in each of these zip files and place it in the "library" fold
- `freeHeap=38616`

- HTTP GET INTERRUPT COUNT (TEXT)
- URL: `http://mywidget.local/intcount`
- Returns interrupt counter - interruptCounter1 variable
- URL: `http://mywidget.local/intcount + optional query string parameters`
- Returns interrupt counter(s) based on query string parameters, if no query string parameters, then intcount1
- Request:
- Name/Value Pair: N/A
- Query String Parameters: N/A
- Response:
- Interrupt Counter=[Interrupt Counter]
- `:intcount1=[Interrupt Counter]`
- Example Request:
- `http://mywidget.local/intcount`
- Example Response:
- `Interrupt Counter=4533456`
- `:intcount1=4533456`
- Query string parameters (optional):
- Request:
- 1=true, 2=true, 3=true
- Response::
- Interrupt Counter(s) based on query string
- Name value pairs are delimited using colon ":".
- Name and value fields are delimited using a equal "=" sign.
- Example Request:
- `http://mywidget.local/intcount?1=true&2=true&3=true`
- Example Response:
- `:intcount1=4533456:intcount2=23236:intcount3=98434445`
- Example Request:
- `http://mywidget.local/intcount?3=true`
- Example Response:
- `:intcount3=98434445`

- HTTP GET INTERRUPT COUNT2 (TEXT)
- URL: `http://mywidget.local/intcount2`
- Returns interrupt counter 2 - interruptCounter2 variable
- Request:
- Name/Value Pair: N/A
- Query String Parameters: N/A
- Response:
- Interrupt Counter2=[Interrupt Counter 2]
- `:intcount2=[Interrupt Counter 2]`
- Example Request:
- `http://mywidget.local/intcount2`
- Example Response:
- `Interrupt Counter2=23236`
- `:intcount2=23236`

- HTTP GET INTERRUPT COUNT3 (TEXT)
- URL: `http://mywidget.local/intcount3`
- Returns interrupt counter 3 - interruptCounter3 variable
- Request:
- Name/Value Pair: N/A
- Query String Parameters: N/A
- Response:
- Interrupt Counter3=[Interrupt Counter 3]
- `:intcount3=[Interrupt Counter 3]`
- Example Request:
- `http://mywidget.local/intcount3`
- Example Response:
- `Interrupt Counter3=98434445`
- `:intcount3=98434445`

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

- [X] Support multiple interrupt timers (ESP8266TimerInterrupt)
- [ ] Support dynamic interval timers on config page
- [ ] Support HTTP GET INTERRUPT COUNT with args
- [X] Support HTTP GET INTERRUPT COUNT with optional parameters
- [ ] Asynch NTP support
- [ ] More code comments
- [ ] Other?
Expand Down

0 comments on commit 62d9ae1

Please sign in to comment.