diff --git a/protobufs b/protobufs index 6320d6795..dc28ae3d1 160000 --- a/protobufs +++ b/protobufs @@ -1 +1 @@ -Subproject commit 6320d6795e5462da38ab0bdc3769163a0fac37c6 +Subproject commit dc28ae3d128b76707c0b87b6f3b2514c7f8514bd diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 924d98698..196874d48 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -248,6 +248,11 @@ void NodeDB::installDefaultModuleConfig() moduleConfig.has_neighbor_info = true; moduleConfig.neighbor_info.enabled = false; + moduleConfig.has_detection_sensor = true; + moduleConfig.detection_sensor.enabled = false; + moduleConfig.detection_sensor.detection_triggered_high = true; + moduleConfig.detection_sensor.minimum_broadcast_secs = 45; + initModuleConfigIntervals(); } diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index cd230cb1c..c91f2e815 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -283,6 +283,10 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) fromRadioScratch.moduleConfig.which_payload_variant = meshtastic_ModuleConfig_neighbor_info_tag; fromRadioScratch.moduleConfig.payload_variant.neighbor_info = moduleConfig.neighbor_info; break; + case meshtastic_ModuleConfig_detection_sensor_tag: + fromRadioScratch.moduleConfig.which_payload_variant = meshtastic_ModuleConfig_detection_sensor_tag; + fromRadioScratch.moduleConfig.payload_variant.detection_sensor = moduleConfig.detection_sensor; + break; default: LOG_ERROR("Unknown module config type %d\n", config_state); } diff --git a/src/mesh/generated/meshtastic/admin.pb.h b/src/mesh/generated/meshtastic/admin.pb.h index 89e1fde0c..38248d94a 100644 --- a/src/mesh/generated/meshtastic/admin.pb.h +++ b/src/mesh/generated/meshtastic/admin.pb.h @@ -57,7 +57,9 @@ typedef enum _meshtastic_AdminMessage_ModuleConfigType { /* TODO: REPLACE */ meshtastic_AdminMessage_ModuleConfigType_NEIGHBORINFO_CONFIG = 9, /* TODO: REPLACE */ - meshtastic_AdminMessage_ModuleConfigType_AMBIENTLIGHTING_CONFIG = 10 + meshtastic_AdminMessage_ModuleConfigType_AMBIENTLIGHTING_CONFIG = 10, + /* TODO: REPLACE */ + meshtastic_AdminMessage_ModuleConfigType_DETECTIONSENSOR_CONFIG = 11 } meshtastic_AdminMessage_ModuleConfigType; /* Struct definitions */ @@ -176,8 +178,8 @@ extern "C" { #define _meshtastic_AdminMessage_ConfigType_ARRAYSIZE ((meshtastic_AdminMessage_ConfigType)(meshtastic_AdminMessage_ConfigType_BLUETOOTH_CONFIG+1)) #define _meshtastic_AdminMessage_ModuleConfigType_MIN meshtastic_AdminMessage_ModuleConfigType_MQTT_CONFIG -#define _meshtastic_AdminMessage_ModuleConfigType_MAX meshtastic_AdminMessage_ModuleConfigType_AMBIENTLIGHTING_CONFIG -#define _meshtastic_AdminMessage_ModuleConfigType_ARRAYSIZE ((meshtastic_AdminMessage_ModuleConfigType)(meshtastic_AdminMessage_ModuleConfigType_AMBIENTLIGHTING_CONFIG+1)) +#define _meshtastic_AdminMessage_ModuleConfigType_MAX meshtastic_AdminMessage_ModuleConfigType_DETECTIONSENSOR_CONFIG +#define _meshtastic_AdminMessage_ModuleConfigType_ARRAYSIZE ((meshtastic_AdminMessage_ModuleConfigType)(meshtastic_AdminMessage_ModuleConfigType_DETECTIONSENSOR_CONFIG+1)) #define meshtastic_AdminMessage_payload_variant_get_config_request_ENUMTYPE meshtastic_AdminMessage_ConfigType #define meshtastic_AdminMessage_payload_variant_get_module_config_request_ENUMTYPE meshtastic_AdminMessage_ModuleConfigType diff --git a/src/mesh/generated/meshtastic/config.pb.h b/src/mesh/generated/meshtastic/config.pb.h index a02b41a76..ef9d09a19 100644 --- a/src/mesh/generated/meshtastic/config.pb.h +++ b/src/mesh/generated/meshtastic/config.pb.h @@ -269,7 +269,7 @@ typedef struct _meshtastic_Config_PositionConfig { uint32_t tx_gpio; /* The minimum distance in meters traveled (since the last send) before we can send a position to the mesh if position_broadcast_smart_enabled */ uint32_t broadcast_smart_minimum_distance; - /* The minumum number of seconds (since the last send) before we can send a position to the mesh if position_broadcast_smart_enabled */ + /* The minimum number of seconds (since the last send) before we can send a position to the mesh if position_broadcast_smart_enabled */ uint32_t broadcast_smart_minimum_interval_secs; } meshtastic_Config_PositionConfig; diff --git a/src/mesh/generated/meshtastic/deviceonly.pb.h b/src/mesh/generated/meshtastic/deviceonly.pb.h index 998b02bcc..143bd4482 100644 --- a/src/mesh/generated/meshtastic/deviceonly.pb.h +++ b/src/mesh/generated/meshtastic/deviceonly.pb.h @@ -316,7 +316,7 @@ extern const pb_msgdesc_t meshtastic_NodeRemoteHardwarePin_msg; #define meshtastic_DeviceState_size 16854 #define meshtastic_NodeInfoLite_size 151 #define meshtastic_NodeRemoteHardwarePin_size 29 -#define meshtastic_OEMStore_size 3148 +#define meshtastic_OEMStore_size 3210 #define meshtastic_PositionLite_size 28 #ifdef __cplusplus diff --git a/src/mesh/generated/meshtastic/localonly.pb.h b/src/mesh/generated/meshtastic/localonly.pb.h index 4c27e0b67..471074948 100644 --- a/src/mesh/generated/meshtastic/localonly.pb.h +++ b/src/mesh/generated/meshtastic/localonly.pb.h @@ -75,6 +75,12 @@ typedef struct _meshtastic_LocalModuleConfig { /* The part of the config that is specific to the Neighbor Info module */ bool has_neighbor_info; meshtastic_ModuleConfig_NeighborInfoConfig neighbor_info; + /* The part of the config that is specific to the Ambient Lighting module */ + bool has_ambient_lighting; + meshtastic_ModuleConfig_AmbientLightingConfig ambient_lighting; + /* The part of the config that is specific to the Detection Sensor module */ + bool has_detection_sensor; + meshtastic_ModuleConfig_DetectionSensorConfig detection_sensor; } meshtastic_LocalModuleConfig; @@ -84,9 +90,9 @@ extern "C" { /* Initializer values for message structs */ #define meshtastic_LocalConfig_init_default {false, meshtastic_Config_DeviceConfig_init_default, false, meshtastic_Config_PositionConfig_init_default, false, meshtastic_Config_PowerConfig_init_default, false, meshtastic_Config_NetworkConfig_init_default, false, meshtastic_Config_DisplayConfig_init_default, false, meshtastic_Config_LoRaConfig_init_default, false, meshtastic_Config_BluetoothConfig_init_default, 0} -#define meshtastic_LocalModuleConfig_init_default {false, meshtastic_ModuleConfig_MQTTConfig_init_default, false, meshtastic_ModuleConfig_SerialConfig_init_default, false, meshtastic_ModuleConfig_ExternalNotificationConfig_init_default, false, meshtastic_ModuleConfig_StoreForwardConfig_init_default, false, meshtastic_ModuleConfig_RangeTestConfig_init_default, false, meshtastic_ModuleConfig_TelemetryConfig_init_default, false, meshtastic_ModuleConfig_CannedMessageConfig_init_default, 0, false, meshtastic_ModuleConfig_AudioConfig_init_default, false, meshtastic_ModuleConfig_RemoteHardwareConfig_init_default, false, meshtastic_ModuleConfig_NeighborInfoConfig_init_default} +#define meshtastic_LocalModuleConfig_init_default {false, meshtastic_ModuleConfig_MQTTConfig_init_default, false, meshtastic_ModuleConfig_SerialConfig_init_default, false, meshtastic_ModuleConfig_ExternalNotificationConfig_init_default, false, meshtastic_ModuleConfig_StoreForwardConfig_init_default, false, meshtastic_ModuleConfig_RangeTestConfig_init_default, false, meshtastic_ModuleConfig_TelemetryConfig_init_default, false, meshtastic_ModuleConfig_CannedMessageConfig_init_default, 0, false, meshtastic_ModuleConfig_AudioConfig_init_default, false, meshtastic_ModuleConfig_RemoteHardwareConfig_init_default, false, meshtastic_ModuleConfig_NeighborInfoConfig_init_default, false, meshtastic_ModuleConfig_AmbientLightingConfig_init_default, false, meshtastic_ModuleConfig_DetectionSensorConfig_init_default} #define meshtastic_LocalConfig_init_zero {false, meshtastic_Config_DeviceConfig_init_zero, false, meshtastic_Config_PositionConfig_init_zero, false, meshtastic_Config_PowerConfig_init_zero, false, meshtastic_Config_NetworkConfig_init_zero, false, meshtastic_Config_DisplayConfig_init_zero, false, meshtastic_Config_LoRaConfig_init_zero, false, meshtastic_Config_BluetoothConfig_init_zero, 0} -#define meshtastic_LocalModuleConfig_init_zero {false, meshtastic_ModuleConfig_MQTTConfig_init_zero, false, meshtastic_ModuleConfig_SerialConfig_init_zero, false, meshtastic_ModuleConfig_ExternalNotificationConfig_init_zero, false, meshtastic_ModuleConfig_StoreForwardConfig_init_zero, false, meshtastic_ModuleConfig_RangeTestConfig_init_zero, false, meshtastic_ModuleConfig_TelemetryConfig_init_zero, false, meshtastic_ModuleConfig_CannedMessageConfig_init_zero, 0, false, meshtastic_ModuleConfig_AudioConfig_init_zero, false, meshtastic_ModuleConfig_RemoteHardwareConfig_init_zero, false, meshtastic_ModuleConfig_NeighborInfoConfig_init_zero} +#define meshtastic_LocalModuleConfig_init_zero {false, meshtastic_ModuleConfig_MQTTConfig_init_zero, false, meshtastic_ModuleConfig_SerialConfig_init_zero, false, meshtastic_ModuleConfig_ExternalNotificationConfig_init_zero, false, meshtastic_ModuleConfig_StoreForwardConfig_init_zero, false, meshtastic_ModuleConfig_RangeTestConfig_init_zero, false, meshtastic_ModuleConfig_TelemetryConfig_init_zero, false, meshtastic_ModuleConfig_CannedMessageConfig_init_zero, 0, false, meshtastic_ModuleConfig_AudioConfig_init_zero, false, meshtastic_ModuleConfig_RemoteHardwareConfig_init_zero, false, meshtastic_ModuleConfig_NeighborInfoConfig_init_zero, false, meshtastic_ModuleConfig_AmbientLightingConfig_init_zero, false, meshtastic_ModuleConfig_DetectionSensorConfig_init_zero} /* Field tags (for use in manual encoding/decoding) */ #define meshtastic_LocalConfig_device_tag 1 @@ -108,6 +114,8 @@ extern "C" { #define meshtastic_LocalModuleConfig_audio_tag 9 #define meshtastic_LocalModuleConfig_remote_hardware_tag 10 #define meshtastic_LocalModuleConfig_neighbor_info_tag 11 +#define meshtastic_LocalModuleConfig_ambient_lighting_tag 12 +#define meshtastic_LocalModuleConfig_detection_sensor_tag 13 /* Struct field encoding specification for nanopb */ #define meshtastic_LocalConfig_FIELDLIST(X, a) \ @@ -140,7 +148,9 @@ X(a, STATIC, OPTIONAL, MESSAGE, canned_message, 7) \ X(a, STATIC, SINGULAR, UINT32, version, 8) \ X(a, STATIC, OPTIONAL, MESSAGE, audio, 9) \ X(a, STATIC, OPTIONAL, MESSAGE, remote_hardware, 10) \ -X(a, STATIC, OPTIONAL, MESSAGE, neighbor_info, 11) +X(a, STATIC, OPTIONAL, MESSAGE, neighbor_info, 11) \ +X(a, STATIC, OPTIONAL, MESSAGE, ambient_lighting, 12) \ +X(a, STATIC, OPTIONAL, MESSAGE, detection_sensor, 13) #define meshtastic_LocalModuleConfig_CALLBACK NULL #define meshtastic_LocalModuleConfig_DEFAULT NULL #define meshtastic_LocalModuleConfig_mqtt_MSGTYPE meshtastic_ModuleConfig_MQTTConfig @@ -153,6 +163,8 @@ X(a, STATIC, OPTIONAL, MESSAGE, neighbor_info, 11) #define meshtastic_LocalModuleConfig_audio_MSGTYPE meshtastic_ModuleConfig_AudioConfig #define meshtastic_LocalModuleConfig_remote_hardware_MSGTYPE meshtastic_ModuleConfig_RemoteHardwareConfig #define meshtastic_LocalModuleConfig_neighbor_info_MSGTYPE meshtastic_ModuleConfig_NeighborInfoConfig +#define meshtastic_LocalModuleConfig_ambient_lighting_MSGTYPE meshtastic_ModuleConfig_AmbientLightingConfig +#define meshtastic_LocalModuleConfig_detection_sensor_MSGTYPE meshtastic_ModuleConfig_DetectionSensorConfig extern const pb_msgdesc_t meshtastic_LocalConfig_msg; extern const pb_msgdesc_t meshtastic_LocalModuleConfig_msg; @@ -163,7 +175,7 @@ extern const pb_msgdesc_t meshtastic_LocalModuleConfig_msg; /* Maximum encoded size of messages (where known) */ #define meshtastic_LocalConfig_size 455 -#define meshtastic_LocalModuleConfig_size 547 +#define meshtastic_LocalModuleConfig_size 609 #ifdef __cplusplus } /* extern "C" */ diff --git a/src/mesh/generated/meshtastic/mesh.pb.h b/src/mesh/generated/meshtastic/mesh.pb.h index 060c18fe1..f319c58b9 100644 --- a/src/mesh/generated/meshtastic/mesh.pb.h +++ b/src/mesh/generated/meshtastic/mesh.pb.h @@ -153,7 +153,7 @@ typedef enum _meshtastic_CriticalErrorCode { /* Radio transmit hardware failure. We sent data to the radio chip, but it didn't reply with an interrupt. */ meshtastic_CriticalErrorCode_TRANSMIT_FAILED = 8, - /* We detected that the main CPU voltage dropped below the minumum acceptable value */ + /* We detected that the main CPU voltage dropped below the minimum acceptable value */ meshtastic_CriticalErrorCode_BROWNOUT = 9, /* Selftest of SX1262 radio chip failed */ meshtastic_CriticalErrorCode_SX1262_FAILURE = 10, diff --git a/src/mesh/generated/meshtastic/module_config.pb.c b/src/mesh/generated/meshtastic/module_config.pb.c index 86614d18c..7318d34f7 100644 --- a/src/mesh/generated/meshtastic/module_config.pb.c +++ b/src/mesh/generated/meshtastic/module_config.pb.c @@ -18,6 +18,9 @@ PB_BIND(meshtastic_ModuleConfig_RemoteHardwareConfig, meshtastic_ModuleConfig_Re PB_BIND(meshtastic_ModuleConfig_NeighborInfoConfig, meshtastic_ModuleConfig_NeighborInfoConfig, AUTO) +PB_BIND(meshtastic_ModuleConfig_DetectionSensorConfig, meshtastic_ModuleConfig_DetectionSensorConfig, AUTO) + + PB_BIND(meshtastic_ModuleConfig_AudioConfig, meshtastic_ModuleConfig_AudioConfig, AUTO) diff --git a/src/mesh/generated/meshtastic/module_config.pb.h b/src/mesh/generated/meshtastic/module_config.pb.h index 43b330b04..828a44cc7 100644 --- a/src/mesh/generated/meshtastic/module_config.pb.h +++ b/src/mesh/generated/meshtastic/module_config.pb.h @@ -125,6 +125,33 @@ typedef struct _meshtastic_ModuleConfig_NeighborInfoConfig { uint32_t update_interval; } meshtastic_ModuleConfig_NeighborInfoConfig; +/* Detection Sensor Module Config */ +typedef struct _meshtastic_ModuleConfig_DetectionSensorConfig { + /* Whether the Module is enabled */ + bool enabled; + /* Interval in seconds of how often we can send a message to the mesh when a state change is detected */ + uint32_t minimum_broadcast_secs; + /* Interval in seconds of how often we should send a message to the mesh with the current state regardless of changes + When set to 0, only state changes will be broadcasted + Works as a sort of status heartbeat for peace of mind */ + uint32_t state_broadcast_secs; + /* Send ASCII bell with alert message + Useful for triggering ext. notification on bell */ + bool send_bell; + /* Friendly name used to format message sent to mesh + Example: A name "Motion" would result in a message "Motion detected" + Maximum length of 20 characters */ + char name[20]; + /* GPIO pin to monitor for state changes */ + uint8_t monitor_pin; + /* Whether or not the GPIO pin state detection is triggered on HIGH (1) + Otherwise LOW (0) */ + bool detection_triggered_high; + /* Whether or not use INPUT_PULLUP mode for GPIO pin + Only applicable if the board uses pull-up resistors on the pin */ + bool use_pullup; +} meshtastic_ModuleConfig_DetectionSensorConfig; + /* Audio Config for codec2 voice */ typedef struct _meshtastic_ModuleConfig_AudioConfig { /* Whether Audio is enabled */ @@ -342,6 +369,8 @@ typedef struct _meshtastic_ModuleConfig { meshtastic_ModuleConfig_NeighborInfoConfig neighbor_info; /* TODO: REPLACE */ meshtastic_ModuleConfig_AmbientLightingConfig ambient_lighting; + /* TODO: REPLACE */ + meshtastic_ModuleConfig_DetectionSensorConfig detection_sensor; } payload_variant; } meshtastic_ModuleConfig; @@ -375,6 +404,7 @@ extern "C" { + #define meshtastic_ModuleConfig_AudioConfig_bitrate_ENUMTYPE meshtastic_ModuleConfig_AudioConfig_Audio_Baud #define meshtastic_ModuleConfig_SerialConfig_baud_ENUMTYPE meshtastic_ModuleConfig_SerialConfig_Serial_Baud @@ -397,6 +427,7 @@ extern "C" { #define meshtastic_ModuleConfig_MQTTConfig_init_default {0, "", "", "", 0, 0, 0, "", 0} #define meshtastic_ModuleConfig_RemoteHardwareConfig_init_default {0, 0, 0, {meshtastic_RemoteHardwarePin_init_default, meshtastic_RemoteHardwarePin_init_default, meshtastic_RemoteHardwarePin_init_default, meshtastic_RemoteHardwarePin_init_default}} #define meshtastic_ModuleConfig_NeighborInfoConfig_init_default {0, 0} +#define meshtastic_ModuleConfig_DetectionSensorConfig_init_default {0, 0, 0, 0, "", 0, 0, 0} #define meshtastic_ModuleConfig_AudioConfig_init_default {0, 0, _meshtastic_ModuleConfig_AudioConfig_Audio_Baud_MIN, 0, 0, 0, 0} #define meshtastic_ModuleConfig_SerialConfig_init_default {0, 0, 0, 0, _meshtastic_ModuleConfig_SerialConfig_Serial_Baud_MIN, 0, _meshtastic_ModuleConfig_SerialConfig_Serial_Mode_MIN, 0} #define meshtastic_ModuleConfig_ExternalNotificationConfig_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} @@ -410,6 +441,7 @@ extern "C" { #define meshtastic_ModuleConfig_MQTTConfig_init_zero {0, "", "", "", 0, 0, 0, "", 0} #define meshtastic_ModuleConfig_RemoteHardwareConfig_init_zero {0, 0, 0, {meshtastic_RemoteHardwarePin_init_zero, meshtastic_RemoteHardwarePin_init_zero, meshtastic_RemoteHardwarePin_init_zero, meshtastic_RemoteHardwarePin_init_zero}} #define meshtastic_ModuleConfig_NeighborInfoConfig_init_zero {0, 0} +#define meshtastic_ModuleConfig_DetectionSensorConfig_init_zero {0, 0, 0, 0, "", 0, 0, 0} #define meshtastic_ModuleConfig_AudioConfig_init_zero {0, 0, _meshtastic_ModuleConfig_AudioConfig_Audio_Baud_MIN, 0, 0, 0, 0} #define meshtastic_ModuleConfig_SerialConfig_init_zero {0, 0, 0, 0, _meshtastic_ModuleConfig_SerialConfig_Serial_Baud_MIN, 0, _meshtastic_ModuleConfig_SerialConfig_Serial_Mode_MIN, 0} #define meshtastic_ModuleConfig_ExternalNotificationConfig_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} @@ -432,6 +464,14 @@ extern "C" { #define meshtastic_ModuleConfig_MQTTConfig_proxy_to_client_enabled_tag 9 #define meshtastic_ModuleConfig_NeighborInfoConfig_enabled_tag 1 #define meshtastic_ModuleConfig_NeighborInfoConfig_update_interval_tag 2 +#define meshtastic_ModuleConfig_DetectionSensorConfig_enabled_tag 1 +#define meshtastic_ModuleConfig_DetectionSensorConfig_minimum_broadcast_secs_tag 2 +#define meshtastic_ModuleConfig_DetectionSensorConfig_state_broadcast_secs_tag 3 +#define meshtastic_ModuleConfig_DetectionSensorConfig_send_bell_tag 4 +#define meshtastic_ModuleConfig_DetectionSensorConfig_name_tag 5 +#define meshtastic_ModuleConfig_DetectionSensorConfig_monitor_pin_tag 6 +#define meshtastic_ModuleConfig_DetectionSensorConfig_detection_triggered_high_tag 7 +#define meshtastic_ModuleConfig_DetectionSensorConfig_use_pullup_tag 8 #define meshtastic_ModuleConfig_AudioConfig_codec2_enabled_tag 1 #define meshtastic_ModuleConfig_AudioConfig_ptt_pin_tag 2 #define meshtastic_ModuleConfig_AudioConfig_bitrate_tag 3 @@ -509,6 +549,7 @@ extern "C" { #define meshtastic_ModuleConfig_remote_hardware_tag 9 #define meshtastic_ModuleConfig_neighbor_info_tag 10 #define meshtastic_ModuleConfig_ambient_lighting_tag 11 +#define meshtastic_ModuleConfig_detection_sensor_tag 12 /* Struct field encoding specification for nanopb */ #define meshtastic_ModuleConfig_FIELDLIST(X, a) \ @@ -522,7 +563,8 @@ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,canned_message,payload_varia X(a, STATIC, ONEOF, MESSAGE, (payload_variant,audio,payload_variant.audio), 8) \ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,remote_hardware,payload_variant.remote_hardware), 9) \ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,neighbor_info,payload_variant.neighbor_info), 10) \ -X(a, STATIC, ONEOF, MESSAGE, (payload_variant,ambient_lighting,payload_variant.ambient_lighting), 11) +X(a, STATIC, ONEOF, MESSAGE, (payload_variant,ambient_lighting,payload_variant.ambient_lighting), 11) \ +X(a, STATIC, ONEOF, MESSAGE, (payload_variant,detection_sensor,payload_variant.detection_sensor), 12) #define meshtastic_ModuleConfig_CALLBACK NULL #define meshtastic_ModuleConfig_DEFAULT NULL #define meshtastic_ModuleConfig_payload_variant_mqtt_MSGTYPE meshtastic_ModuleConfig_MQTTConfig @@ -536,6 +578,7 @@ X(a, STATIC, ONEOF, MESSAGE, (payload_variant,ambient_lighting,payload_var #define meshtastic_ModuleConfig_payload_variant_remote_hardware_MSGTYPE meshtastic_ModuleConfig_RemoteHardwareConfig #define meshtastic_ModuleConfig_payload_variant_neighbor_info_MSGTYPE meshtastic_ModuleConfig_NeighborInfoConfig #define meshtastic_ModuleConfig_payload_variant_ambient_lighting_MSGTYPE meshtastic_ModuleConfig_AmbientLightingConfig +#define meshtastic_ModuleConfig_payload_variant_detection_sensor_MSGTYPE meshtastic_ModuleConfig_DetectionSensorConfig #define meshtastic_ModuleConfig_MQTTConfig_FIELDLIST(X, a) \ X(a, STATIC, SINGULAR, BOOL, enabled, 1) \ @@ -564,6 +607,18 @@ X(a, STATIC, SINGULAR, UINT32, update_interval, 2) #define meshtastic_ModuleConfig_NeighborInfoConfig_CALLBACK NULL #define meshtastic_ModuleConfig_NeighborInfoConfig_DEFAULT NULL +#define meshtastic_ModuleConfig_DetectionSensorConfig_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, BOOL, enabled, 1) \ +X(a, STATIC, SINGULAR, UINT32, minimum_broadcast_secs, 2) \ +X(a, STATIC, SINGULAR, UINT32, state_broadcast_secs, 3) \ +X(a, STATIC, SINGULAR, BOOL, send_bell, 4) \ +X(a, STATIC, SINGULAR, STRING, name, 5) \ +X(a, STATIC, SINGULAR, UINT32, monitor_pin, 6) \ +X(a, STATIC, SINGULAR, BOOL, detection_triggered_high, 7) \ +X(a, STATIC, SINGULAR, BOOL, use_pullup, 8) +#define meshtastic_ModuleConfig_DetectionSensorConfig_CALLBACK NULL +#define meshtastic_ModuleConfig_DetectionSensorConfig_DEFAULT NULL + #define meshtastic_ModuleConfig_AudioConfig_FIELDLIST(X, a) \ X(a, STATIC, SINGULAR, BOOL, codec2_enabled, 1) \ X(a, STATIC, SINGULAR, UINT32, ptt_pin, 2) \ @@ -667,6 +722,7 @@ extern const pb_msgdesc_t meshtastic_ModuleConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_MQTTConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_RemoteHardwareConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_NeighborInfoConfig_msg; +extern const pb_msgdesc_t meshtastic_ModuleConfig_DetectionSensorConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_AudioConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_SerialConfig_msg; extern const pb_msgdesc_t meshtastic_ModuleConfig_ExternalNotificationConfig_msg; @@ -682,6 +738,7 @@ extern const pb_msgdesc_t meshtastic_RemoteHardwarePin_msg; #define meshtastic_ModuleConfig_MQTTConfig_fields &meshtastic_ModuleConfig_MQTTConfig_msg #define meshtastic_ModuleConfig_RemoteHardwareConfig_fields &meshtastic_ModuleConfig_RemoteHardwareConfig_msg #define meshtastic_ModuleConfig_NeighborInfoConfig_fields &meshtastic_ModuleConfig_NeighborInfoConfig_msg +#define meshtastic_ModuleConfig_DetectionSensorConfig_fields &meshtastic_ModuleConfig_DetectionSensorConfig_msg #define meshtastic_ModuleConfig_AudioConfig_fields &meshtastic_ModuleConfig_AudioConfig_msg #define meshtastic_ModuleConfig_SerialConfig_fields &meshtastic_ModuleConfig_SerialConfig_msg #define meshtastic_ModuleConfig_ExternalNotificationConfig_fields &meshtastic_ModuleConfig_ExternalNotificationConfig_msg @@ -696,6 +753,7 @@ extern const pb_msgdesc_t meshtastic_RemoteHardwarePin_msg; #define meshtastic_ModuleConfig_AmbientLightingConfig_size 14 #define meshtastic_ModuleConfig_AudioConfig_size 19 #define meshtastic_ModuleConfig_CannedMessageConfig_size 49 +#define meshtastic_ModuleConfig_DetectionSensorConfig_size 44 #define meshtastic_ModuleConfig_ExternalNotificationConfig_size 40 #define meshtastic_ModuleConfig_MQTTConfig_size 222 #define meshtastic_ModuleConfig_NeighborInfoConfig_size 8 diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 283375b23..27b539f38 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -373,6 +373,11 @@ void AdminModule::handleSetModuleConfig(const meshtastic_ModuleConfig &c) moduleConfig.has_neighbor_info = true; moduleConfig.neighbor_info = c.payload_variant.neighbor_info; break; + case meshtastic_ModuleConfig_detection_sensor_tag: + LOG_INFO("Setting module config: Detection Sensor\n"); + moduleConfig.has_detection_sensor = true; + moduleConfig.detection_sensor = c.payload_variant.detection_sensor; + break; } saveChanges(SEGMENT_MODULECONFIG); @@ -513,6 +518,11 @@ void AdminModule::handleGetModuleConfig(const meshtastic_MeshPacket &req, const res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_neighbor_info_tag; res.get_module_config_response.payload_variant.neighbor_info = moduleConfig.neighbor_info; break; + case meshtastic_AdminMessage_ModuleConfigType_DETECTIONSENSOR_CONFIG: + LOG_INFO("Getting module config: Detection Sensor\n"); + res.get_module_config_response.which_payload_variant = meshtastic_ModuleConfig_detection_sensor_tag; + res.get_module_config_response.payload_variant.detection_sensor = moduleConfig.detection_sensor; + break; } // NOTE: The phone app needs to know the ls_secsvalue so it can properly expect sleep behavior. diff --git a/src/modules/DetectionSensorModule.cpp b/src/modules/DetectionSensorModule.cpp new file mode 100644 index 000000000..8050c209e --- /dev/null +++ b/src/modules/DetectionSensorModule.cpp @@ -0,0 +1,99 @@ +#include "DetectionSensorModule.h" +#include "MeshService.h" +#include "NodeDB.h" +#include "PowerFSM.h" +#include "configuration.h" +#include "main.h" + +DetectionSensorModule *detectionSensorModule; + +#define GPIO_POLLING_INTERVAL 100 +#define DELAYED_INTERVAL 1000 + +int32_t DetectionSensorModule::runOnce() +{ + /* + Uncomment the preferences below if you want to use the module + without having to configure it from the PythonAPI or WebUI. + */ + // moduleConfig.detection_sensor.enabled = true; + // moduleConfig.detection_sensor.monitor_pin = 10; // WisBlock PIR IO6 + // moduleConfig.detection_sensor.minimum_broadcast_secs = 60; + // moduleConfig.detection_sensor.state_broadcast_secs = 120; + // moduleConfig.detection_sensor.detection_triggered_high = false; + // strcpy(moduleConfig.detection_sensor.name, "Motion"); + + if (moduleConfig.detection_sensor.enabled == false) + return disable(); + + if (firstTime) { + // This is the first time the OSThread library has called this function, so do some setup + firstTime = false; + if (moduleConfig.detection_sensor.monitor_pin > 0) { + pinMode(moduleConfig.detection_sensor.monitor_pin, moduleConfig.detection_sensor.use_pullup ? INPUT_PULLUP : INPUT); + } else { + LOG_WARN("Detection Sensor Module: Set to enabled but no monitor pin is set. Disabling module...\n"); + return disable(); + } + LOG_INFO("Detection Sensor Module: Initializing\n"); + + return DELAYED_INTERVAL; + } + + if ((millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.minimum_broadcast_secs) && + hasDetectionEvent()) { + sendDetectionMessage(); + return DELAYED_INTERVAL; + } + // Even if we haven't detected an event, broadcast our current state to the mesh on the scheduled interval as a sort + // of heartbeat. We only do this if the minimum broadcast interval is greater than zero, otherwise we'll only broadcast state + // change detections. + else if (moduleConfig.detection_sensor.state_broadcast_secs > 0 && + (millis() - lastSentToMesh) >= getConfiguredOrDefaultMs(moduleConfig.detection_sensor.state_broadcast_secs)) { + sendCurrentStateMessage(); + return DELAYED_INTERVAL; + } + return GPIO_POLLING_INTERVAL; +} + +void DetectionSensorModule::sendDetectionMessage() +{ + LOG_DEBUG("Detected event observed. Sending message\n"); + char *message = new char[40]; + sprintf(message, "%s detected", moduleConfig.detection_sensor.name); + meshtastic_MeshPacket *p = allocDataPacket(); + p->want_ack = false; + p->decoded.payload.size = strlen(message); + memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size); + if (moduleConfig.detection_sensor.send_bell && p->decoded.payload.size < meshtastic_Constants_DATA_PAYLOAD_LEN) { + p->decoded.payload.bytes[p->decoded.payload.size] = 7; // Bell character + p->decoded.payload.bytes[p->decoded.payload.size + 1] = '\0'; // Bell character + p->decoded.payload.size++; + } + LOG_INFO("Sending message id=%d, dest=%x, msg=%.*s\n", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes); + lastSentToMesh = millis(); + service.sendToMesh(p); + delete[] message; +} + +void DetectionSensorModule::sendCurrentStateMessage() +{ + char *message = new char[40]; + sprintf(message, "%s state: %i", moduleConfig.detection_sensor.name, hasDetectionEvent()); + + meshtastic_MeshPacket *p = allocDataPacket(); + p->want_ack = false; + p->decoded.payload.size = strlen(message); + memcpy(p->decoded.payload.bytes, message, p->decoded.payload.size); + LOG_INFO("Sending message id=%d, dest=%x, msg=%.*s\n", p->id, p->to, p->decoded.payload.size, p->decoded.payload.bytes); + lastSentToMesh = millis(); + service.sendToMesh(p); + delete[] message; +} + +bool DetectionSensorModule::hasDetectionEvent() +{ + bool currentState = digitalRead(moduleConfig.detection_sensor.monitor_pin); + // LOG_DEBUG("Detection Sensor Module: Current state: %i\n", currentState); + return moduleConfig.detection_sensor.detection_triggered_high ? currentState : !currentState; +} \ No newline at end of file diff --git a/src/modules/DetectionSensorModule.h b/src/modules/DetectionSensorModule.h new file mode 100644 index 000000000..bcc0b9419 --- /dev/null +++ b/src/modules/DetectionSensorModule.h @@ -0,0 +1,23 @@ +#pragma once +#include "SinglePortModule.h" + +class DetectionSensorModule : public SinglePortModule, private concurrency::OSThread +{ + public: + DetectionSensorModule() + : SinglePortModule("detection", meshtastic_PortNum_TEXT_MESSAGE_APP), OSThread("DetectionSensorModule") + { + } + + protected: + virtual int32_t runOnce() override; + + private: + bool firstTime = true; + uint32_t lastSentToMesh = 0; + void sendDetectionMessage(); + void sendCurrentStateMessage(); + bool hasDetectionEvent(); +}; + +extern DetectionSensorModule *detectionSensorModule; \ No newline at end of file diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index 9c216ff72..bcb33c640 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -7,6 +7,7 @@ #include "input/kbMatrixImpl.h" #include "modules/AdminModule.h" #include "modules/CannedMessageModule.h" +#include "modules/DetectionSensorModule.h" #include "modules/NeighborInfoModule.h" #include "modules/NodeInfoModule.h" #include "modules/PositionModule.h" @@ -50,6 +51,7 @@ void setupModules() textMessageModule = new TextMessageModule(); traceRouteModule = new TraceRouteModule(); neighborInfoModule = new NeighborInfoModule(); + detectionSensorModule = new DetectionSensorModule(); // Note: if the rest of meshtastic doesn't need to explicitly use your module, you do not need to assign the instance // to a global variable.