Trigger reminder

Hi Shramik & all,
I’m trying to add a resend of the trigger notification every hour as a reminder. Thought just by resetting the widget back to 0 would do it. Everything works but the reset. Is there a better way? Any pointers?
Thanks,
John

/*

This example is to be used when using Cayenne triggers.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:

  1. Set the trigger and threshold values in the sketch below.
  2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  3. Compile and upload the sketch.
  4. Two temporary widget (DATA_CHANNEL and TRIGGER_CHANNEL) will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
  5. Add trigger on your cayenne dashboard for the TRIGGER_CHANNEL widget when it becomes 1.
    */
    #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #include <CayenneMQTTESP8266.h>
    #define ONE_WIRE_BUS 2
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    DeviceAddress tempSensorA = { 0x28, 0xFF, 0x09, 0x61, 0x51, 0x17, 0x04, 0x97};

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char ssid = “__”;
char wifiPassword = “00”;
char username = “000000000000”;
char password = “0000000000”;
char clientID = “000000000”;

#define DATA_CHANNEL 0 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL 1 //Virtual channel for publishing the trigger value.
#define THRESHOLD 75 //Threshold for the trigger.
unsigned long lastMillis = 0;

bool sendBelowThreshold = false; ///// Set to true if the trigger should happen when the data value is below the threshold,
///// false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;
int previousState1 = -1;
int currentState1 = -1;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.setResolution(tempSensorA, 10);
sensors.begin();

}

void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
if (!crossedThreshold) {
Cayenne.virtualWrite(channel, 1, “digital_sensor”, “d”); //set trigger two-state widget to 1
crossedThreshold = true;
}
}
else
{
Cayenne.virtualWrite(channel, 0, “digital_sensor”, “d”); //set trigger two-state widget to 0
crossedThreshold = false;
}
}

void loop() {
Cayenne.loop();

//Publish data every 5 minutes (300000 milliseconds).
if (millis() - lastMillis > 60000)
{
lastMillis = millis();

sensors.requestTemperatures();

int sensor_value1 = sensors.getTempF(tempSensorA);
sendTriggerValue(TRIGGER_CHANNEL, sensor_value1, THRESHOLD, sendBelowThreshold);
Cayenne.virtualWrite(DATA_CHANNEL, sensor_value1 , “temp”, “f”);

long rssi = WiFi.RSSI();
Cayenne.virtualWrite(3, rssi, "rssi", "dbm");  
}

//  Problem here>>>>>>>
      // Reset trigger every 1 hour  (3600000 milliseconds).
     if (millis() - lastMillis > 120000) 
     {
      lastMillis = millis();

    Cayenne.virtualWrite(1, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
      
 }

}

you have two lastMillis = millis() thus, it never reaches your required time duration.

Thanks for the reply Shramik,
I got the sketch to do what I wanted, but reseting the widget to 0 doesn’t help for reasons now obvious to me. So I’m back to nothing on having a sketch resend a trigger at a pre-set time when the alarm stays in. This is harder than I thought it be. :frowning:
Any ideas on this? Maybe it’s not possible?
John

is a digital sensor widget or an digital actuator widget you are trying to change on the dashboard?

Back to the basic idea. Using your “send only on change” sketch with a 2 state widget, add a trigger, works good. But you only get 1 email. What I’d like to add is where something would re-trigger the notification on a pre-set delay (1hour) if the widget is still high to remind you of the situation or if you missed the first email. My intention is then do the same for the threshold analog sketch. I’m not aware of anybody in the community that has done anything like this yet.
Thanks,
John

int previousState = -1;
int currentState = -1;

void setup()
{
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();

// Check the sensor state and send data when it changes.
currentState = digitalRead(1);
if (currentState != previousState) {
Cayenne.virtualWrite(1, currentState);
previousState = currentState;
}
}

something like this should work:

/*
  This example is to be used when using Cayenne triggers.
  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
  Steps:
  1. Set the trigger and threshold values in the sketch below.
  2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  3. Compile and upload the sketch.
  4. Two temporary widget (DATA_CHANNEL and TRIGGER_CHANNEL) will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
  5. Add trigger on your cayenne dashboard for the TRIGGER_CHANNEL widget when it becomes 1.
*/

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h> // Change this to use a different communication device. See Communications examples.

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

#define DATA_CHANNEL 0 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL 1 //Virtual channel for publishing the trigger value.
#define THRESHOLD 200 //Threshold for the trigger.
bool sendBelowThreshold = true; //Set to true if the trigger should happen when the data value is below the threshold,
//false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;
bool sendAgain = false;
unsigned long lastMillis = 0;


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
}

void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
  if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
    if (!crossedThreshold) {
      Cayenne.virtualWrite(channel, 1, "digital_sensor", "d"); //set trigger two-state widget to 1
      crossedThreshold = true;
      sendAgain = true;
      lastMillis = millis();
    }
  }
  else
  {
    Cayenne.virtualWrite(channel, 0, "digital_sensor", "d"); //set trigger two-state widget to 0
    crossedThreshold = false;
    sendAgain = false;
  }
}

void loop() {
  Cayenne.loop();
  if ((millis() - lastMillis > 60000) && sendAgain) // change the time to whaterver you want.
  {
    Cayenne.virtualWrite(TRIGGER_CHANNEL, 1, "digital_sensor", "d"); //set trigger two-state widget to 1
    lastMillis = millis();
  }
}
CAYENNE_OUT_DEFAULT()
{
  int sensor_value = analogRead(A0);
  sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold);
  Cayenne.virtualWrite(DATA_CHANNEL, sensor_value , "analog_sensor", "null"); //widget to display sensor data.
}
1 Like

@shramik_salgaonkar
Shramik,
Works perfect! So the widget needed to be resent as a 1 instead of being set to 0. Wish I
thought of that. Here is the sketch modified to work with a ESP8266 and DS18B20 temp probe.
Thank You!!!
John

/*
This example is to be used when using Cayenne triggers.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:

  1. Set the trigger and threshold values in the sketch below.
  2. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  3. Compile and upload the sketch.
  4. Two temporary widget (DATA_CHANNEL and TRIGGER_CHANNEL) will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
  5. Add trigger on your cayenne dashboard for the TRIGGER_CHANNEL widget when it becomes 1.
    */

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <DallasTemperature.h>
#include <CayenneMQTTESP8266.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempSensorA = { 0x28, 0xFF, 0x09, 0x61, 0x51, 0x17, 0x04, 0x97};

// WiFi network info.
char ssid = “ssid”;
char wifiPassword = “wifiPassword”;
char username = “MQTT_USERNAME”;
char password = “MQTT_PASSWORD”;
char clientID = “CLIENT_ID”;

#define DATA_CHANNEL 0 //Virtual channel for publishing sensor data.
#define TRIGGER_CHANNEL 1 //Virtual channel for publishing the trigger value.
#define THRESHOLD 75 //Threshold for the trigger.

bool sendBelowThreshold = false; //Set to true if the trigger should happen when the data value is below the threshold,
//false if it should happen when the data value is above or equal to the threshold.
bool crossedThreshold = false;
bool sendAgain = false;
unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
sensors.setResolution(tempSensorA, 10);
sensors.begin();
}

void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
if (!crossedThreshold) {
Cayenne.virtualWrite(channel, 1, “digital_sensor”, “d”); //set trigger two-state widget to 1
crossedThreshold = true;
sendAgain = true;
lastMillis = millis();
}
}
else
{
Cayenne.virtualWrite(channel, 0, “digital_sensor”, “d”); //set trigger two-state widget to 0
crossedThreshold = false;
sendAgain = false;
}
}

void loop() {
Cayenne.loop();
if ((millis() - lastMillis > 600000) && sendAgain) // 10 min, change the time to whaterver you want.
{
Cayenne.virtualWrite(TRIGGER_CHANNEL, 1, “digital_sensor”, “d”); //set trigger two-state widget to 1
lastMillis = millis();
}
}
CAYENNE_OUT_DEFAULT()
{

sensors.requestTemperatures();
int sensor_value1 = sensors.getTempF(tempSensorA);
sendTriggerValue(TRIGGER_CHANNEL, sensor_value1, THRESHOLD, sendBelowThreshold);
Cayenne.virtualWrite(DATA_CHANNEL, sensor_value1 , “temp”, “f”);

}

that is the not what the code does. the code is all around the variable sendAgain