Sending MQTT messages within notification limit

notification trigger (SMS and email) are currently limited to 90 per day. the trigger activates every time the device publish a message above the set threshold. To avoid publish message flood and causing the trigger to run continuously and hit the 90 limit quickly use the following code for sending only once notification trigger when the threshold is crossed. For actuator trigger, there is no limit and there is no need to use the code but it is good practice to use as it.

Using Cayenne-MQTT-Arduino library.
https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Basics/SendDataOnTrigger/SendDataOnTrigger.ino

Code explanation:
the main point here is to only publish data once when the trigger threshold is crossed.

#define DATA_CHANNEL 0 
#define TRIGGER_CHANNEL 1 
#define THRESHOLD 200
bool sendBelowThreshold = true; ,

where

  1. DATA_CHANNEL is the MQTT channel used to publish sensor data at a 15-second interval.

  2. TRIGGER_CHANNEL is the MQTT channel which will publish value 1 when the sensor value has crossed the threshold and publish 0 when the sensor value is below the threshold.

  3. THRESHOLD is the threshold value you need to set for the trigger to activate.

  4. sendBelowThreshold is the boolean value which when set to true the trigger is set for value below the threshold and if false the trigger is set for values above or equal the threshold.

you only need to set this values correctly for your trigger to send notification only once. rest the code handles everything.

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

the line sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold); calls the function sendTriggerValue() with all the arguments.

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

this function handles publishing data only once.

when you upload the code with correct values set, there will two temporary green widgets populated on your dashboard i.e. one with DATA_CHANNEL as a generic analog sensor value widget and other with TRIGGER_CHANNEL as a two-state digital sensor widget. Add them by clicking on + top right of each widget. Now add a trigger to TRIGGER_CHANNEL widget when it becomes 1 as the code only publish data 1 once the trigger threshold is crossed.

the same principle applies when using Cayenne MQTT Python Library
https://github.com/myDevicesIoT/Cayenne-MQTT-Python/blob/master/examples/Example-04-SendDataOnTrigger.py or any other library you are using.

2 Likes

Fantasticā€¦ Let me try thatā€¦

Hi, thanks for the reply,
but i have three temperature sensors in my project, how should i change my code for three trigger and which values i need to set?

thanks

you just need it set this for all three sensor.

How do we know the trigger channel

Trigger Channel is the channel number of the digital two state widget which you want to set the trigger on the cayenne dashboard,

how about the Threshold value

Am working on aproject to send alert when humidity exceeds the 50
how should i prevent the triger from floading too many times

CAYENNE_OUT(2)
{
float hum=dht.readHumidity();
Serial.println(ā€œHumi isā€);
Serial.println(hum);
Cayenne.virtualWrite(2, hum );
}

so the threshold value will be 50

triger value?

It is clearly explained above all the things and how to do it. Please go through it again.

What is the result expected from running the above code ?

i guess the topic says it all.

If i have so many triggers how do i know which trigger is activated

First of all, have you understood the above code? and are you familiar with a little bit of coding?

Yes am familiar with coding in arduino
but this one is abit challenging
maybe if you can tell me how to deal with it may help

can you create a new topic, as i dont want this topic to be loaded with coding lesson.