Skip to content

Commit

Permalink
Fix "watch GPIOs" feature of Remote Hardware module (meshtastic#3047)
Browse files Browse the repository at this point in the history
* Fix watch GPIO feature of Remote Hardware

* Add Remote Hardware messages to JSON output

* Add curly brackets

---------

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
  • Loading branch information
GUVWAF and thebentern authored Jan 3, 2024
1 parent 4577646 commit 943367e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/modules/RemoteHardwareModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
LOG_INFO("Received RemoteHardware type=%d\n", p.type);

switch (p.type) {
case meshtastic_HardwareMessage_Type_WRITE_GPIOS:
case meshtastic_HardwareMessage_Type_WRITE_GPIOS: {
// Print notification to LCD screen
screen->print("Write GPIOs\n");

Expand All @@ -69,6 +69,7 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
pinModes(p.gpio_mask, OUTPUT);

break;
}

case meshtastic_HardwareMessage_Type_READ_GPIOS: {
// Print notification to LCD screen
Expand All @@ -92,8 +93,9 @@ bool RemoteHardwareModule::handleReceivedProtobuf(const meshtastic_MeshPacket &r
watchGpios = p.gpio_mask;
lastWatchMsec = 0; // Force a new publish soon
previousWatch =
~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
~watchGpios; // generate a 'previous' value which is guaranteed to not match (to force an initial publish)
enabled = true; // Let our thread run at least once
setInterval(2000); // Set a new interval so we'll run soon
LOG_INFO("Now watching GPIOs 0x%llx\n", watchGpios);
break;
}
Expand All @@ -118,6 +120,7 @@ int32_t RemoteHardwareModule::runOnce()

if (now - lastWatchMsec >= WATCH_INTERVAL_MSEC) {
uint64_t curVal = digitalReads(watchGpios);
lastWatchMsec = now;

if (curVal != previousWatch) {
previousWatch = curVal;
Expand All @@ -136,5 +139,5 @@ int32_t RemoteHardwareModule::runOnce()
return disable();
}

return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg)
}
return 2000; // Poll our GPIOs every 2000ms
}
23 changes: 23 additions & 0 deletions src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#if defined(ARCH_ESP32)
#include "../mesh/generated/meshtastic/paxcount.pb.h"
#endif
#include "mesh/generated/meshtastic/remote_hardware.pb.h"
#include "sleep.h"
#if HAS_WIFI
#include "mesh/wifi/WiFiAPClient.h"
Expand Down Expand Up @@ -747,6 +748,28 @@ std::string MQTT::meshPacketToJson(meshtastic_MeshPacket *mp)
break;
}
#endif
case meshtastic_PortNum_REMOTE_HARDWARE_APP: {
meshtastic_HardwareMessage scratch;
meshtastic_HardwareMessage *decoded = NULL;
memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_HardwareMessage_msg,
&scratch)) {
decoded = &scratch;
if (decoded->type == meshtastic_HardwareMessage_Type_GPIOS_CHANGED) {
msgType = "gpios_changed";
msgPayload["gpio_value"] = new JSONValue((uint)decoded->gpio_value);
jsonObj["payload"] = new JSONValue(msgPayload);
} else if (decoded->type == meshtastic_HardwareMessage_Type_READ_GPIOS_REPLY) {
msgType = "gpios_read_reply";
msgPayload["gpio_value"] = new JSONValue((uint)decoded->gpio_value);
msgPayload["gpio_mask"] = new JSONValue((uint)decoded->gpio_mask);
jsonObj["payload"] = new JSONValue(msgPayload);
}
} else {
LOG_ERROR("Error decoding protobuf for RemoteHardware message!\n");
}
break;
}
// add more packet types here if needed
default:
break;
Expand Down

0 comments on commit 943367e

Please sign in to comment.