In my Home automation system, a wemos reboots my raspberry in case of crash.
I made a plugin which pings an IP address.
I use the ESP8266Ping library. https://github.com/dancol90/ESP8266Ping
Here the plugin :
Code: Select all
//#######################################################################################################
//#################################### Plugin 085: Ping ################################################
//#######################################################################################################
#define PLUGIN_085
#define PLUGIN_ID_085 85
#define PLUGIN_NAME_085 "Ping Device"
#define PLUGIN_VALUENAME1_085 "Ping"
#include <ESP8266Ping.h>
boolean Plugin_085(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
static byte switchstate[TASKS_MAX];
static byte outputstate[TASKS_MAX];
char deviceTemplate[1][41]; // variable for saving the subscription topics
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_085;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].ValueCount = 1;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_085);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_085));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
LoadCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
addFormTextBox(string, String(F("IP ")), String(F("Plugin_085_template")), deviceTemplate[0], 40);
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String argName;
argName = F("Plugin_085_template");
strncpy(deviceTemplate[0], WebServer.arg(argName).c_str(), sizeof(deviceTemplate[0]));
SaveCustomTaskSettings(event->TaskIndex, (byte*)&deviceTemplate, sizeof(deviceTemplate));
success = true;
break;
}
case PLUGIN_READ:
{
char deviceTemplate[1][41];
String AdrIp;
// Loop over all tasks looking for a 085 instance
for (byte y = 0; y < TASKS_MAX; y++)
{
if (Settings.TaskDeviceNumber[y] == PLUGIN_ID_085)
{
LoadCustomTaskSettings(y, (byte*)&deviceTemplate, sizeof(deviceTemplate));
// Now loop over all import variables and subscribe to those that are not blank
AdrIp = deviceTemplate[0];
}
}
int Parts[4] = {0,0,0,0};
int Part = 0;
for ( int i=0; i<AdrIp.length(); i++ )
{
char c = AdrIp[i];
if ( c == '.' )
{
Part++;
continue;
}
Parts[Part] *= 10;
Parts[Part] += c - '0';
}
IPAddress ip( Parts[0], Parts[1], Parts[2], Parts[3] ); // The remote ip to ping
String log = F("Ping ");
bool ret = Ping.ping(ip);
log += AdrIp;
log += " state ";
log += ret;
addLog(LOG_LEVEL_INFO,log);
UserVar[event->BaseVarIndex] = ret;
event->sensorType = SENSOR_TYPE_SWITCH;
success = true;
break;
}
}
return success;
}
