simple_iot/examples/02_reports.ino

46 lines
1.7 KiB
C++

#include <simple_iot.h>
/**
* This example shows how to use reports.
* Two reports are defined:
* * "free_heap" returns the amount of free heap space. The method is provided as lambda function.
* Since `interval` ist set to 0, it will not be reported via MQTT, making it accessible only
* via HTTP.
* * "uptime" returns the current uptime in seconds. It will be reported via MQTT (as long as you
* keep MQTT enabled) every 10 seconds. Since `cache_only` is set to true, the method won't be
* called any more often than that.
*
* You can access them via HTTP GET request to `/free_heap` or `/uptime`. There's a
* nice-ish website at `/`, listing all registered reports along with (cached) data from them.
* If you kept the suggested hostname, this link should lead you there: http://basic-test/.
*
* If you enabled MQTT, "free_heap" will be published every 10 seconds. Again, if you
* didn't change the topic below, it would be `basic-test-mqtt-topic/free_heap`.
*/
SimpleIOT* iot = NULL;
String uptime() {
return String(millis() / 1000);
}
void setup() {
// Set pin 13 to be an OUTPUT.
pinMode(13, OUTPUT);
// Initialize SimpleIOT, giving it a WiFi SSID and the matching password to connect as well as a hostname to use.
iot = new SimpleIOT("ssid", "password", "basic-test");
// Set data for an MQTT server. You can omit this line if you don't want to use MQTT.
iot->setMQTTData("1.2.3.4", 1883, "basic-test-mqtt-user", "basic-test-mqtt-password", "basic-test-mqtt-topic/");
// Define three actions.
iot->report_on("free_heap", []()->String{ return String(ESP.free_heap()); }, 0, false);
iot->report_on("uptime", uptime, 10000, true);
iot->begin();
}
void loop() {
iot->loop();
}