Skip to content

Commit

Permalink
MQTT topic to dynamically adjust the STA/LTA threshold
Browse files Browse the repository at this point in the history
Signed-off-by: John Walicki <johnwalicki@gmail.com>
  • Loading branch information
johnwalicki committed Apr 15, 2021
1 parent cb9a563 commit 12b02e9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
33 changes: 29 additions & 4 deletions FIRMWARE.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ This section describes the various commands that can be sent to the OpenEEW firm
#define MQTT_TOPIC_FWCHECK "iot-2/cmd/firmwarecheck/fmt/json"
#define MQTT_TOPIC_SEND10SEC "iot-2/cmd/10secondhistory/fmt/json"
#define MQTT_TOPIC_SENDACCEL "iot-2/cmd/sendacceldata/fmt/json"
#define MQTT_TOPIC_RESTART "iot-2/cmd/forcerestart/fmt/json"
#define MQTT_TOPIC_THRESHOLD "iot-2/cmd/threshold/fmt/json"
```
### ALARM
Expand Down Expand Up @@ -175,7 +177,7 @@ Use this MQTT topic to change the ADXL355 sampling rate.

- Turn off the Accelerometer:

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

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}
Expand All @@ -187,15 +189,15 @@ Use this MQTT topic to change the ADXL355 sampling rate.
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/samplerate/fmt/json -m {SampleRate:31} -i cmd:samplerate

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}
```
```

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

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

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}
```
```

### FWCHECK

Expand Down Expand Up @@ -224,6 +226,29 @@ mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/10secondhistory/fmt/json -m {} -i cm
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 {}
```

#### RESTART

Use this MQTT topic to force a restart on a device that has lost its mind.

```sh
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/forcerestart/fmt/json -m {} -i cmd:restart

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 {}
```

### THRESHOLD

Use this MQTT topic to override and dynamically adjust the STA/LTA threshold.
Some sensors in noisy environments might be too sensitive and might trigger lots of false positives.
The regional administrator might use this to remotely change the STA/LTA algorithm threshold to reduce the frequency of detection events.
The message `{ThresholdOverride:<double>}` will publish a double to the device.

```sh
mosquitto_pub -h 192.168.1.101 -t iot-2/cmd/threshold/fmt/json -m {ThresholdOverride:10.2} -i cmd:threshold

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}
```

### Python Examples

This repository also contains Python examples that can be modified to do the above. `tbd`
Expand Down
43 changes: 29 additions & 14 deletions WatsonIoT/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static char MQTT_ORGID[7]; // Watson IoT 6 character orgid
#define MQTT_TOPIC_SEND10SEC "iot-2/cmd/10secondhistory/fmt/json"
#define MQTT_TOPIC_SENDACCEL "iot-2/cmd/sendacceldata/fmt/json"
#define MQTT_TOPIC_RESTART "iot-2/cmd/forcerestart/fmt/json"
#define MQTT_TOPIC_THRESHOLD "iot-2/cmd/threshold/fmt/json"
char deviceID[13];

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

// --------------------------------------------------------------------------------------------
// STA/LTA Algorithm globals
bool bPossibleEarthQuake = false;
double thresh = 4.0;
double stalta[3] = { 0, 0, 0 };
double sample[3] = { 0, 0, 0 };
double sampleSUM[3] = { 0, 0, 0 };
double ltSUM[3] = { 0, 0, 0 };
double sample1[3] = { 0, 0, 0 };
double LTAsample1[3] = { 0, 0, 0 };
double offset[3] = { 0, 0, 0 };
double sampleABS[3] = { 0, 0, 0 };
double sample1ABS = 0;
double LTAsample1ABS = 0;
double stav[3] = { 0, 0, 0 };
double ltav[3] = { 0, 0, 0 };


// --------------------------------------------------------------------------------------------
void IRAM_ATTR isr_adxl() {
fifoFull = true;
//fifoCount++;
}


void StartADXL355() {
// odr_lpf is a global
adxl355.start();
Expand Down Expand Up @@ -285,6 +305,14 @@ void callback(char* topic, byte* payload, unsigned int length) {
breathedirection = true;
}
jsonMQTTReceiveDoc.clear();
} else if ( strcmp(topic, MQTT_TOPIC_THRESHOLD) == 0 ) {
// Override the `thresh` global
char newthreshmsg[50];
snprintf( newthreshmsg, 49, "Previous STA/LTA Shake Threshold : %5.2f", thresh);
Serial.println(newthreshmsg);
thresh = cmdData["ThresholdOverride"].as<double>();
snprintf( newthreshmsg, 49, "Override STA/LTA Shake Threshold : %5.2f", thresh);
Serial.println(newthreshmsg);
} else if ( strcmp(topic, MQTT_TOPIC_RESTART) == 0 ) {
Serial.println("Restarting Device...");
esp_restart();
Expand Down Expand Up @@ -457,6 +485,7 @@ void Connect2MQTTbroker() {
mqtt.subscribe(MQTT_TOPIC_SEND10SEC);
mqtt.subscribe(MQTT_TOPIC_SENDACCEL);
mqtt.subscribe(MQTT_TOPIC_RESTART);
mqtt.subscribe(MQTT_TOPIC_THRESHOLD);
mqtt.setBufferSize(2000);
mqtt.loop();
} else {
Expand Down Expand Up @@ -796,20 +825,6 @@ void setup() {
digitalWrite(io, LOW); // turn off buzzer
}

bool bPossibleEarthQuake = false;
double thresh = 3.0;
double stalta[3] = { 0, 0, 0 };
double sample[3] = { 0, 0, 0 };
double sampleSUM[3] = { 0, 0, 0 };
double ltSUM[3] = { 0, 0, 0 };
double sample1[3] = { 0, 0, 0 };
double LTAsample1[3] = { 0, 0, 0 };
double offset[3] = { 0, 0, 0 };
double sampleABS[3] = { 0, 0, 0 };
double sample1ABS = 0;
double LTAsample1ABS = 0;
double stav[3] = { 0, 0, 0 };
double ltav[3] = { 0, 0, 0 };

void loop() {
mqtt.loop();
Expand Down

0 comments on commit 12b02e9

Please sign in to comment.