Hi,
I used the indications feature of MySensors 2.0 and a webserver to have the gateway provide status information through a web page:
This is how I did it:
In the global section I added this:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
ESP8266WebServer WebServer(80);
unsigned long startTime=millis();
unsigned long gwMsgSent = 0;
unsigned long gwMsgRec = 0;
String WebPage = "<h1>ESP8266 MQTT client for Mysensors</h1>";
void setupWebServer();
void showRootPage();
String readableTimestamp(unsigned long milliseconds);
In the setup() Added this:
Serial.begin(9600);
setupWebServer();
Defined the following functions:
void setupWebServer()
{
WebServer.on("/", HTTP_GET, showRootPage);
WebServer.begin();
Serial.println("WebServer started...");
}
void indication( const indication_t ind )
{
switch (ind)
{
case INDICATION_GW_TX:
gwMsgSent++;
break;
case INDICATION_GW_RX:
gwMsgRec++;
break;
default:
break;
};
}
void showRootPage()
{
unsigned long runningTime = millis() - startTime;
String page = WebPage;
page+="<br>General information</br>";
page+= "<style> table, th, td { border: 1px solid black;}</style>";
page+="<table style=\"width:400\">";
page+="<tr>"; page+= "<th>Item</th>"; page+= "<th>Value</th>"; page+="</tr>";
page+="<tr>"; page+= "<td>Running for</td>"; page+= "<td>"; page += readableTimestamp(runningTime) ; page+= "</td>"; page+="</tr>";
page+="<tr>"; page+= "<td>Gateway messages sent</td>"; page+= "<td>"; page += gwMsgSent; page+= "</td>"; page+="</tr>";
page+="</table>";
page+="<br>MySensors gateway information</br>";
page+= "<style> table, th, td { border: 1px solid black;}</style>";
page+="<table style=\"width:400\">";
page+="<tr>"; page+= "<th>Item</th>"; page+= "<th>Value</th>"; page+="</tr>";
page+="<tr>"; page+= "<td>Gateway messages received</td>"; page+= "<td>"; page += gwMsgRec; page+= "</td>"; page+="</tr>";
page+="<tr>"; page+= "<td>Gateway messages sent</td>"; page+= "<td>"; page += gwMsgSent; page+= "</td>"; page+="</tr>";
page+="</table>";
Serial.println("WS: Send root webpage");
WebServer.send(200, "text/html", page);
}
String readableTimestamp(unsigned long milliseconds)
{
int days = milliseconds / 86400000;
milliseconds=milliseconds % 86400000;
int hours = milliseconds / 3600000;
milliseconds = milliseconds %3600000;
int minutes = milliseconds / 60000;
milliseconds = milliseconds % 60000;
int seconds = milliseconds / 1000;
milliseconds = milliseconds % 1000;
String timeStamp;
timeStamp = days; timeStamp += " days, ";
timeStamp += hours; timeStamp += ":";
timeStamp += minutes ; timeStamp += ":";
timeStamp +=seconds; timeStamp += ".";
timeStamp +=milliseconds;
Serial.println(timeStamp);
return timeStamp;
}
And in loop() added the following:
WebServer.handleClient();
If you have any more idaes on what to add to this page (I am thinking about maintaining a list of registered nodes and sensors) just post a reply!