This example code and webhook will particle.publish every 20s, and on receiving the subscribed webhook response, it will particle.publish “received” to be viewed in the Console.
But after activating the particle.function RST from the Console resulting in System.reset(), when the device gets online again it no longer publish “received” to the console and seem to have lost it’s webhook response subscription.
When uploading slightly changed code, (ex. add/remove a delay(1)), webhook reception will start again.
This is on B523 with OS2.0.1, but likely the same with Boron.
Am I missing something?
Code:
SYSTEM_MODE(SEMI_AUTOMATIC)
SYSTEM_THREAD(ENABLED);
SerialLogHandler logHandler;
static bool SystemResetNow = false;
static unsigned long lastUploadTime = 0-5000;
const pin_t WATCHDOG_PIN = D22;
void setup() {
pinMode(WATCHDOG_PIN, OUTPUT);
SystemResetNow = false;
lastUploadTime = millis();
Particle.function("RST", startReset);
Particle.subscribe(System.deviceID() + "/hook-response/upload/", uploadResponseHandler);
Particle.process();
Particle.connect();
}
void loop() {
if (Particle.connected() && Time.isValid()){
if (((millis() - lastUploadTime) >= 20*1000)){
lastUploadTime = millis();
delay(1); // to re-enable webhook response reception after RST, include/exclude this line and upload
Particle.publish("upload", "{\"T\":\"2021-02-02T12:51:03Z\",\"R\":-72,\"I\":60,\"D\":\"{}\"}", 60, PRIVATE, WITH_ACK);
}
}
if(SystemResetNow == true){
SystemResetNow = false;
delay(3000);
System.reset();
}
digitalWrite(WATCHDOG_PIN, !digitalRead(WATCHDOG_PIN));
}
int startReset(String input) {
Log.info("startReset()");
SystemResetNow = true;
return 1;
}
void uploadResponseHandler(const char *event, const char *data) {
Log.info("Response: OK");
if(Particle.connected()) Particle.publish("log", "Received", PRIVATE, NO_ACK);
}
(The webhook is meaningless - just gets the demo going)
{
"event": "upload",
"responseTopic": "{{PARTICLE_DEVICE_ID}}/hook-response/{{PARTICLE_EVENT_NAME}}",
"url": "https://www.google.com",
"requestType": "GET",
"noDefaults": false,
"rejectUnauthorized": true,
"responseTemplate": "V"
}