Skip to content

Commit 12b02e9

Browse files
committed
MQTT topic to dynamically adjust the STA/LTA threshold
Signed-off-by: John Walicki <[email protected]>
1 parent cb9a563 commit 12b02e9

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

FIRMWARE.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ This section describes the various commands that can be sent to the OpenEEW firm
116116
#define MQTT_TOPIC_FWCHECK "iot-2/cmd/firmwarecheck/fmt/json"
117117
#define MQTT_TOPIC_SEND10SEC "iot-2/cmd/10secondhistory/fmt/json"
118118
#define MQTT_TOPIC_SENDACCEL "iot-2/cmd/sendacceldata/fmt/json"
119+
#define MQTT_TOPIC_RESTART "iot-2/cmd/forcerestart/fmt/json"
120+
#define MQTT_TOPIC_THRESHOLD "iot-2/cmd/threshold/fmt/json"
119121
```
120122
121123
### ALARM
@@ -175,7 +177,7 @@ Use this MQTT topic to change the ADXL355 sampling rate.
175177

176178
- Turn off the Accelerometer:
177179

178-
```sh
180+
```sh
179181
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/samplerate/fmt/json -m {SampleRate:0} -i cmd:samplerate
180182

181183
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i "a:OrgID:mosquitto" -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/samplerate/fmt/json -m {SampleRate:0}
@@ -187,15 +189,15 @@ Use this MQTT topic to change the ADXL355 sampling rate.
187189
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/samplerate/fmt/json -m {SampleRate:31} -i cmd:samplerate
188190

189191
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i "a:OrgID:mosquitto" -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/samplerate/fmt/json -m {SampleRate:31}
190-
```
192+
```
191193

192194
- 125 samples per second (a firehose that eats bandwidth)
193195

194-
```sh
196+
```sh
195197
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/samplerate/fmt/json -m {SampleRate:125} -i cmd:samplerate
196198

197199
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i "a:OrgID:mosquitto" -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/samplerate/fmt/json -m {SampleRate:125}
198-
```
200+
```
199201

200202
### FWCHECK
201203

@@ -224,6 +226,29 @@ mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/10secondhistory/fmt/json -m {} -i cm
224226
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i "a:OrgID:mosquitto" -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/10secondhistory/fmt/json -m {}
225227
```
226228

229+
#### RESTART
230+
231+
Use this MQTT topic to force a restart on a device that has lost its mind.
232+
233+
```sh
234+
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/forcerestart/fmt/json -m {} -i cmd:restart
235+
236+
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i "a:OrgID:mosquitto" -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/forcerestart/fmt/json -m {}
237+
```
238+
239+
### THRESHOLD
240+
241+
Use this MQTT topic to override and dynamically adjust the STA/LTA threshold.
242+
Some sensors in noisy environments might be too sensitive and might trigger lots of false positives.
243+
The regional administrator might use this to remotely change the STA/LTA algorithm threshold to reduce the frequency of detection events.
244+
The message `{ThresholdOverride:<double>}` will publish a double to the device.
245+
246+
```sh
247+
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/threshold/fmt/json -m {ThresholdOverride:10.2} -i cmd:threshold
248+
249+
mosquitto_pub -h OrgID.messaging.internetofthings.ibmcloud.com -p 8883 --cafile messaging.pem -u $WIOTP_APIKEY -P $WIOTP_TOKEN -i a:OrgID:mosquitto -t iot-2/type/OpenEEW/id/A8032A4DD5F0/cmd/threshold/fmt/json -m {ThresholdOverride:10.2}
250+
```
251+
227252
### Python Examples
228253

229254
This repository also contains Python examples that can be modified to do the above. `tbd`

WatsonIoT/src/main.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static char MQTT_ORGID[7]; // Watson IoT 6 character orgid
3434
#define MQTT_TOPIC_SEND10SEC "iot-2/cmd/10secondhistory/fmt/json"
3535
#define MQTT_TOPIC_SENDACCEL "iot-2/cmd/sendacceldata/fmt/json"
3636
#define MQTT_TOPIC_RESTART "iot-2/cmd/forcerestart/fmt/json"
37+
#define MQTT_TOPIC_THRESHOLD "iot-2/cmd/threshold/fmt/json"
3738
char deviceID[13];
3839

3940
// Store the Download Server PEM and Digicert CA and Root CA in SPIFFS
@@ -169,12 +170,31 @@ int channel = 0;
169170
int resolution = 8;
170171
int io = 5;
171172

173+
// --------------------------------------------------------------------------------------------
174+
// STA/LTA Algorithm globals
175+
bool bPossibleEarthQuake = false;
176+
double thresh = 4.0;
177+
double stalta[3] = { 0, 0, 0 };
178+
double sample[3] = { 0, 0, 0 };
179+
double sampleSUM[3] = { 0, 0, 0 };
180+
double ltSUM[3] = { 0, 0, 0 };
181+
double sample1[3] = { 0, 0, 0 };
182+
double LTAsample1[3] = { 0, 0, 0 };
183+
double offset[3] = { 0, 0, 0 };
184+
double sampleABS[3] = { 0, 0, 0 };
185+
double sample1ABS = 0;
186+
double LTAsample1ABS = 0;
187+
double stav[3] = { 0, 0, 0 };
188+
double ltav[3] = { 0, 0, 0 };
189+
190+
172191
// --------------------------------------------------------------------------------------------
173192
void IRAM_ATTR isr_adxl() {
174193
fifoFull = true;
175194
//fifoCount++;
176195
}
177196

197+
178198
void StartADXL355() {
179199
// odr_lpf is a global
180200
adxl355.start();
@@ -285,6 +305,14 @@ void callback(char* topic, byte* payload, unsigned int length) {
285305
breathedirection = true;
286306
}
287307
jsonMQTTReceiveDoc.clear();
308+
} else if ( strcmp(topic, MQTT_TOPIC_THRESHOLD) == 0 ) {
309+
// Override the `thresh` global
310+
char newthreshmsg[50];
311+
snprintf( newthreshmsg, 49, "Previous STA/LTA Shake Threshold : %5.2f", thresh);
312+
Serial.println(newthreshmsg);
313+
thresh = cmdData["ThresholdOverride"].as<double>();
314+
snprintf( newthreshmsg, 49, "Override STA/LTA Shake Threshold : %5.2f", thresh);
315+
Serial.println(newthreshmsg);
288316
} else if ( strcmp(topic, MQTT_TOPIC_RESTART) == 0 ) {
289317
Serial.println("Restarting Device...");
290318
esp_restart();
@@ -457,6 +485,7 @@ void Connect2MQTTbroker() {
457485
mqtt.subscribe(MQTT_TOPIC_SEND10SEC);
458486
mqtt.subscribe(MQTT_TOPIC_SENDACCEL);
459487
mqtt.subscribe(MQTT_TOPIC_RESTART);
488+
mqtt.subscribe(MQTT_TOPIC_THRESHOLD);
460489
mqtt.setBufferSize(2000);
461490
mqtt.loop();
462491
} else {
@@ -796,20 +825,6 @@ void setup() {
796825
digitalWrite(io, LOW); // turn off buzzer
797826
}
798827

799-
bool bPossibleEarthQuake = false;
800-
double thresh = 3.0;
801-
double stalta[3] = { 0, 0, 0 };
802-
double sample[3] = { 0, 0, 0 };
803-
double sampleSUM[3] = { 0, 0, 0 };
804-
double ltSUM[3] = { 0, 0, 0 };
805-
double sample1[3] = { 0, 0, 0 };
806-
double LTAsample1[3] = { 0, 0, 0 };
807-
double offset[3] = { 0, 0, 0 };
808-
double sampleABS[3] = { 0, 0, 0 };
809-
double sample1ABS = 0;
810-
double LTAsample1ABS = 0;
811-
double stav[3] = { 0, 0, 0 };
812-
double ltav[3] = { 0, 0, 0 };
813828

814829
void loop() {
815830
mqtt.loop();

0 commit comments

Comments
 (0)