Sending a lot of SMS alert and email

Hello,

I hope I’m in the right place for my question,

I created a project and alerts, the problem is that several tens of sms are sent per second until the state has changed.

The number of sms create problems with my wives, lol.

can we manage the number of sending of notification?

Thank you for your really great application!

this is known bug and team is working on solving it. till then I can provide a workaround, so can you post the code you are using.

Ok thanks for the return

Here are:

I am not professional the code may not be the best :wink:

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

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

#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
#define VIRTUAL_CHANNELA 2
int  Steps2Take = 0;

int HumiditeTerre;
#define CapteurTerre 0
#define VIRTUAL_CHANNEL 1

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  myStepper.setSpeed(60);
  pinMode(CapteurTerre, INPUT);
  
}

void loop() {
  Cayenne.loop();
  int HumiditeTerre = analogRead(CapteurTerre);
  Serial.print("HumiditeTerre : ");
  Serial.println(HumiditeTerre);
  Cayenne.virtualWrite(1, HumiditeTerre, "soil_moist", "p");
  Steps2Take  = 1024;
 
}
CAYENNE_IN(VIRTUAL_CHANNELA)
{

    myStepper.step(Steps2Take);

}

changed the main part of the code to avoid trigger flooding, but you need to set the trigger value of the humidity in the code.

int y;
int x;
int a;

int trigger = ;// you need to set this value
unsigned long lastMillis = 0;

void loop() {
  Cayenne.loop();
  int HumiditeTerre = analogRead(CapteurTerre);
  Serial.print("HumiditeTerre : ");
  Serial.println(HumiditeTerre);
  send_data();
  trigger();
}
void send_data()
{
  if (HumiditeTerre > trigger)
  {
    a = 1;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      Cayenne.virtualWrite(1, HumiditeTerre, "soil_moist", "p");
    }
  }
}
void trigger()
{
  if (HumiditeTerre <= trigger)
  {
    if (millis() - lastMillis > 10000)
    {
      lastMillis = millis();
      Cayenne.virtualWrite(1, HumiditeTerre, "soil_moist", "p");
    }
    if ( a == 1)
    {
      Serial.println("Sending trigger");
      Cayenne.virtualWrite(3, HumiditeTerre, "soil_moist", "p");
      a = 0;
    }
  }
} 

Upload this code and set a trigger for channel 3.

1 Like

The code is clever and solves the problem
thank you,
:+1:

1 Like

Hi Thanks for pointing me to this from my question. This looks something similar to what i was doing with blynk, i.e setting flags and so i understand how something like this works from a code level. However don’t completely understand how this code actually activates a trigger. As far as i can tell if the codes trigger value is true then it VirtualWrites to channel 3? but that seems to be another sensor value? Would this code work if you did a virtualWrite to a custom widget that would just ‘show its on/off state’ thereby sending 1 notification?
thanks for your time

yes that can work too. give it a try and let me know.

ok. and what about my questions regarding your code? ‘’ However don’t completely understand how this code actually activates a trigger. As far as i can tell if the codes trigger value is true then it VirtualWrites to channel 3? but that seems to be another sensor value? ‘’

sorry just trying to understand why your doing it that way. Im new to coding…

you have to set the trigger value. eg if humidity is less than 35;

int trigger 35= ;// you need to set this value

I have two functions which are called from the main loop.

send_data();
 trigger();

this function sends data to channel 2 if humidity is greater than the trigger and set a = 1;

void send_data()  
{
  if (HumiditeTerre > trigger)
  {
    a = 1;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      Cayenne.virtualWrite(1, HumiditeTerre, "soil_moist", "p");
    }
  }
}

this function sends data to channel 2 when humidity is less than the trigger and also send data to channel 3 and sets a =0. so till humidity does not rise higher than the trigger, it won’t send data to channel 3.

void trigger()
{
  if (HumiditeTerre <= trigger)
  {
    if (millis() - lastMillis > 10000)
    {
      lastMillis = millis();
      Cayenne.virtualWrite(1, HumiditeTerre, "soil_moist", "p");
    }
    if ( a == 1)
    {
      Serial.println("Sending trigger");
      Cayenne.virtualWrite(3, HumiditeTerre, "soil_moist", "p");
      a = 0;
    }
  }
} 

Now that you have sent data to channel 3 only once, you set a trigger on the cayenne dashboard to channel 3. this will avoid the trigger flooding issue.
this is the best i can explain the code working , i hope it helps you out. revert back if any problem.

3 Likes

hi… shramiksalgaonkarCommunity Manager

can u explain reed swicth to this clever code like this … i want use esp8266 or arduino to use reed sensor … i use some basiccode to display 2 state on dash board… its give me too many sms alert … n some time its stop sms to me … pls share the reed exmple like this… thanks

can you share the the reed switch code you are using to display a 2 state?

here my code

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

int sensorPin = A0;   
int sensorValue = 0;  // value for photoresistor


#define VIRTUAL_CHANNEL 2
const int REED_PIN = D0 ; 
int  buttonstate;


#define VIRTUAL_CHANNEL 1
#define VIRTUAL_CHANNEL 4
#define VIRTUAL_CHANNEL 5
#define ACTUATOR_PIN  D1
#define VIRTUAL_CHANNEL 6
#define VIRTUAL_CHANNEL 7

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

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


void setup() {
	Serial.begin(115200);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(REED_PIN,INPUT);
  pinMode(sensorPin, INPUT);
  pinMode(ACTUATOR_PIN, OUTPUT);
  }




void loop() {
	Cayenne.loop();
  send_data();
  trigger();  
   
  sensorValue = analogRead(sensorPin);   
  Serial.print("Light Intensity                 = ");
  Serial.println(sensorValue);
  Cayenne.virtualWrite(V1, sensorValue, "lum",  "lux");
  
   int  buttonstate = digitalRead(REED_PIN);
  if(buttonstate == HIGH) {
  Cayenne.virtualWrite(V4, LOW, "digital_sensor",   "d");
  Serial.println("MAIN DOOR CLOSED");
}else {
  Cayenne.virtualWrite(V4, HIGH, "digital_sensor",   "d");
  Serial.println("MAIN DOOR Opened");
       }
   }   
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(V6)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN, value);
      }
}

for your case you can try using this code:

int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
	unsigned long currentMillis = millis();
	// Check sensor data every 250 milliseconds
	if (currentMillis - previousMillis >= 250) {
		// Check the sensor state and send data when it changes.
		currentState = digitalRead(SENSOR_PIN);
		if (currentState != previousState) {
      Serial.println("changed");
			Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState, "digital_sensor",   "d");
			previousState = currentState;
		}
        previousMillis = currentMillis;
	}
}

ur provided code not work for me … here i create new code but its still sms a lot … i need 1 single sms when door is open …this is my Code

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>
#define CAYENNE_DEBUG
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = " ";
char password = " ";
char clientID = " ";

#define VIRTUAL_CHANNEL 4
#define SENSOR_PIN D0 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
// WiFi network info.
char ssid = “”;
char wifiPassword = “”;

void setup()
{
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
// Read data from the sensor and send it to the virtual channel here.
// For example, to send a digital value you can use the following:
int value = digitalRead(SENSOR_PIN);
Cayenne.virtualWrite(V4, value, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
}

i cannot see my code in your above code.

Hallo @shramik_salgaonkar as Community Manager
Can you help me with my code to not continuously send a notification when the ultrasonic sensor read the limit, thank you

indent preformatted text by 4 spaces

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>
#include <Ultrasonic.h>

#define VIRTUAL_PIN V2
#define VIRTUAL_PIN V7

#define ACTUATOR_PIN 0

#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO



// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char username[] = " ";
char mqtt_password[] = " ";
char client_id[] = " ";
// Your network name and password.
char ssid[] = " ";
char password[] = " ";

Ultrasonic ultrasonic(5, 4);

void setup() {
  
  Serial.begin (9600);
  Cayenne.begin(username, mqtt_password, client_id, ssid, password);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  pinMode(ACTUATOR_PIN, OUTPUT);
  digitalWrite(ACTUATOR_PIN, HIGH);
  
}

void loop() 
{
    Cayenne.loop();
  // do something
}



// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V2)


{

  long duration; 
  int distance;
  
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(1); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(1); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = duration/29.1/ 2;
  if(distance <= 0){
    Serial.println("Out of Range");
  }
  else{
  Serial.print(distance);
  Serial.println("cm");
  delay(1);
  Cayenne.virtualWrite(V2, distance);   
}}
CAYENNE_IN(V7)
{
  
  int value = getValue.asInt();
   if (value == HIGH){
    digitalWrite(ACTUATOR_PIN, LOW);
  }
  else if (value == LOW){
    digitalWrite(ACTUATOR_PIN, HIGH);
  }
}

i have explained it here how to do it. give it a try and will help if you are stuck somewhere.

Okay I have success with my program… thanks for your brilliant code sir
this is my code :

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>
#include <Ultrasonic.h>

#define VIRTUAL_PIN V2
#define VIRTUAL_PIN V7
#define VIRTUAL_PIN V5

#define ACTUATOR_PIN 0
#define RELAY 2

#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO



// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char username[] = " ";
char mqtt_password[] = " ";
char client_id[] = " ";
// Your network name and password.
char ssid[] = " ";
char password[] = " ";

Ultrasonic ultrasonic(5, 4);

int y;
int x;
int a;
int trigger1 = 50;
unsigned long lastMillis = 0;

  int distance;
  long duration; 
void setup() {
  
  Serial.begin (9600);
  Cayenne.begin(username, mqtt_password, client_id, ssid, password);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
  pinMode(ACTUATOR_PIN, OUTPUT);
  pinMode(RELAY,OUTPUT);
  digitalWrite(ACTUATOR_PIN, HIGH);
  
}

void loop() 
{
    Cayenne.loop();
    send_data();
    trigger();
  // do something
}
void send_data()
{
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(1); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(1); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = duration/29.1/ 2;  
  if(distance > trigger1)
  {
    a = 1;
    if(millis() - lastMillis > 10000){
      lastMillis = millis();
      Cayenne.virtualWrite(V6,distance);
    }
  }
}
void trigger()
{
  if(distance <= trigger1){
    if(millis() - lastMillis > 1000){
      lastMillis = millis();
      Cayenne.virtualWrite(V6,distance);
    }
    if(a==1){
      Cayenne.virtualWrite(V2,distance);
      a = 0;
    }
  }
}
CAYENNE_IN(V7)
{
  
  int value = getValue.asInt();
   if (value == HIGH){
    digitalWrite(ACTUATOR_PIN, LOW);
  }
  else if (value == LOW){
    digitalWrite(ACTUATOR_PIN, HIGH);
  }
}

CAYENNE_IN(V5)
{
  
  int indicator = getValue.asInt();
   if (indicator == HIGH){
    digitalWrite(RELAY, LOW);
  }
  else if (indicator == LOW){
    digitalWrite(RELAY, HIGH);
  }
}
2 Likes

hello I turned on the trigger to send e-mail to the action of the channel, the first half hour while I was trying the emails arrived,but for some reason then the emails stopped coming, whatever I did

you might hit trigger limit. you need the trigger limit code from above.