add name set, beacon commands
This commit is contained in:
34
src/main.cpp
34
src/main.cpp
@@ -41,6 +41,7 @@ static bool g_hall_alert = false;
|
|||||||
static bool g_motion_alert = false;
|
static bool g_motion_alert = false;
|
||||||
static bool g_relay2_state = false;
|
static bool g_relay2_state = false;
|
||||||
static bool g_temp_requested = false;
|
static bool g_temp_requested = false;
|
||||||
|
static char g_device_name[32] = {0};
|
||||||
static unsigned long g_temp_last_request = 0;
|
static unsigned long g_temp_last_request = 0;
|
||||||
static float g_last_temperature = -127.0f;
|
static float g_last_temperature = -127.0f;
|
||||||
static unsigned long g_last_hall_alert = 0;
|
static unsigned long g_last_hall_alert = 0;
|
||||||
@@ -87,6 +88,23 @@ bool loadSecurityConfig() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void saveDeviceName() {
|
||||||
|
File f = InternalFS.open(NAME_FILE, FILE_O_WRITE);
|
||||||
|
if (!f) return;
|
||||||
|
f.write((const uint8_t*)g_device_name, sizeof(g_device_name));
|
||||||
|
f.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadDeviceName() {
|
||||||
|
File f = InternalFS.open(NAME_FILE, FILE_O_READ);
|
||||||
|
if (!f) return;
|
||||||
|
if (f.read((uint8_t*)g_device_name, sizeof(g_device_name)) != sizeof(g_device_name)) {
|
||||||
|
memset(g_device_name, 0, sizeof(g_device_name));
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
g_device_name[sizeof(g_device_name) - 1] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
class SecurityMesh : public mesh::Mesh {
|
class SecurityMesh : public mesh::Mesh {
|
||||||
public:
|
public:
|
||||||
mesh::GroupChannel mesh_channel;
|
mesh::GroupChannel mesh_channel;
|
||||||
@@ -170,7 +188,7 @@ public:
|
|||||||
while (*cmd == ' ') cmd++;
|
while (*cmd == ' ') cmd++;
|
||||||
|
|
||||||
if (strcmp(cmd, "help") == 0) {
|
if (strcmp(cmd, "help") == 0) {
|
||||||
defer_response("help:ohrana on,ohrana off,relay1 on,relay1 off,relay2 on,relay2 off,relay,status,ver,scan,snr,uptime,kontrol,time,reboot");
|
defer_response("help:ohrana on,ohrana off,relay1 on,relay1 off,relay2 on,relay2 off,relay,status,ver,scan,snr,name,name set,beacon,uptime,kontrol,time,reboot");
|
||||||
} else if (strcmp(cmd, "ohrana on") == 0) {
|
} else if (strcmp(cmd, "ohrana on") == 0) {
|
||||||
g_armed = true; digitalWrite(PIN_RELAY_1, LOW); saveSecurityConfig(); defer_response("ohrana on ok");
|
g_armed = true; digitalWrite(PIN_RELAY_1, LOW); saveSecurityConfig(); defer_response("ohrana on ok");
|
||||||
} else if (strcmp(cmd, "ohrana off") == 0) {
|
} else if (strcmp(cmd, "ohrana off") == 0) {
|
||||||
@@ -267,6 +285,19 @@ public:
|
|||||||
} else if (strcmp(cmd, "snr") == 0) {
|
} else if (strcmp(cmd, "snr") == 0) {
|
||||||
char r[48]; snprintf(r, sizeof(r), "rssi:%.0fdBm snr:%.1fdB", last_rssi, last_snr);
|
char r[48]; snprintf(r, sizeof(r), "rssi:%.0fdBm snr:%.1fdB", last_rssi, last_snr);
|
||||||
defer_response(r);
|
defer_response(r);
|
||||||
|
} else if (strcmp(cmd, "name") == 0) {
|
||||||
|
if (g_device_name[0]) defer_response(g_device_name);
|
||||||
|
else defer_response("name not set");
|
||||||
|
} else if (strncmp(cmd, "name set ", 9) == 0) {
|
||||||
|
const char* n = cmd + 9;
|
||||||
|
int l = strlen(n); if (l > 31) l = 31;
|
||||||
|
memcpy(g_device_name, n, l); g_device_name[l] = 0;
|
||||||
|
saveDeviceName();
|
||||||
|
defer_response(g_device_name);
|
||||||
|
} else if (strcmp(cmd, "beacon") == 0) {
|
||||||
|
auto pkt = createAdvert(self_id, (const uint8_t*)g_device_name, strlen(g_device_name));
|
||||||
|
if (pkt) { sendFlood(pkt); defer_response("beacon ok"); }
|
||||||
|
else defer_response("beacon fail");
|
||||||
} else if (strcmp(cmd, "reboot") == 0) {
|
} else if (strcmp(cmd, "reboot") == 0) {
|
||||||
send_text("rebooting");
|
send_text("rebooting");
|
||||||
delay(100);
|
delay(100);
|
||||||
@@ -309,6 +340,7 @@ void setup() {
|
|||||||
|
|
||||||
InternalFS.begin();
|
InternalFS.begin();
|
||||||
loadSecurityConfig();
|
loadSecurityConfig();
|
||||||
|
loadDeviceName();
|
||||||
if (g_armed) digitalWrite(PIN_RELAY_1, LOW); else digitalWrite(PIN_RELAY_1, HIGH);
|
if (g_armed) digitalWrite(PIN_RELAY_1, LOW); else digitalWrite(PIN_RELAY_1, HIGH);
|
||||||
digitalWrite(PIN_RELAY_2, g_relay2_state ? LOW : HIGH);
|
digitalWrite(PIN_RELAY_2, g_relay2_state ? LOW : HIGH);
|
||||||
|
|
||||||
|
|||||||
@@ -115,6 +115,7 @@
|
|||||||
|
|
||||||
#define CONFIG_FILE "/security.cfg"
|
#define CONFIG_FILE "/security.cfg"
|
||||||
#define CHANNEL_FILE "/channel.cfg"
|
#define CHANNEL_FILE "/channel.cfg"
|
||||||
|
#define NAME_FILE "/name.cfg"
|
||||||
#define WDT_TIMEOUT_MS 10000
|
#define WDT_TIMEOUT_MS 10000
|
||||||
|
|
||||||
#include <RadioLib.h>
|
#include <RadioLib.h>
|
||||||
|
|||||||
Reference in New Issue
Block a user