Sensor values take too much to update

Hello!I’m trying to improve my smart home.So I want to add some conditions for my lights.I add an SR 04 sensor and a photoresistor.When I try the serial output everything works fine.BUT when I add cayenne connection I get long delays between the readings.For instance my sr 04 makes 16 seconds to update it’s value.Thing is that this amount of time is too high when you are trying to make motion sensors.I can provide my code,but I see nothing wrong there.maybe it’s a cayenne loop delay function??any ideas would be very much appreciated…

Are you using CAYENNE_OUT it is set to run at an interval of 15 sec. Sending MQTT messages within rate limits

1 Like

that’s great and you are right!(THANKS SO SO SO MUCH for this…I ve been wondering for hours…)
So I m trying to make the last example.(send data when status changed).And again,I m doing something wrong.
I m going to post my code just in case anyone has an idea,because I m stuck and (to be honest) I m having headache,so I may missing something easy enough that I won’t see now!
Again,ANY help would be very much appreciated…
I also want to ask.Could I update the values on my dashboard EVEN changes the sr-04 data OR pResistor??

#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;


// Defines pins numbers for the HC-SR04 connections.
#define TRIGGER_PIN 13
#define ECHO_PIN 5
const int pResistor = A0;
int const ledpin = 2;

//timer data
int previousState = -1;
int currentState = -1;

// Defines variables for storing the calculated values.
long duration;
int distance;
int value;

void setup() {
pinMode(ledpin, OUTPUT); // led pin is output to control buzzering
pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)

Cayenne.begin(username, password, clientID);

void loop() {

currentState = digitalRead(5);
if (currentState != previousState) {
Cayenne.virtualWrite(1, currentState);
previousState = currentState;


// This function is called at intervals to send HC-SR04 sensor data to Cayenne.
digitalWrite(TRIGGER_PIN, LOW); // Clears the trigger pin

digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigger pin on HIGH state for 10 micro seconds
digitalWrite(TRIGGER_PIN, LOW);

duration = pulseIn(ECHO_PIN, HIGH); // Reads the echo pin, returns the sound wave travel time in microseconds

distance = duration * 0.034 / 2; // Calculating the distance.

// Send the distance value to Cayenne on proximity widget in centimeter.
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, “prox”, “cm”);


value = analogRead(pResistor);

you want to send the data when the photoresistor state changes or the ultrasonic sensor?

basically I want to measure both.but I want data to be send when ultrasonic state changes.but if possible to update both values (pResistor and ultrasonic) at that I would not exceed the limit.(or update the pResistor every 15 seconds)

just to be ultrasonic and photoresistor cant be a state change as it an a analog value.
So you can do something like this:

if( (ultrasonic_value < U_value) && (photoresistor_value < P_value))
  Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, “prox”, “cm”);
  Cayenne.virtualWrite(PRESISTOR_VIRTUAL_CHANNEL, value);

so when both the conditions are true, it will send the data. You need to set the required U_value and P_value and whether you want it to be less than or greater than or equal.
You can also have the CAYENNE_OUT() function alongside so that it sends data at regular intervals and above if statement sends only when triggered.

hello again!!
I should try this,inside my void loop(),right??

void loop() {
if( (distance < 68)&&(distance > 10) && (value < 180))
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, “prox”, “cm”);

or try another function,right after cayenne.loop.
Am I getting it wrong?
Also,most of the times,my ultrasonic sensor has a value of 0 with the code I ve send you.any ideas??

yes, you can do something like that.
for ultrasonic value 0, check your connection between arduino and sensor.

Hello once again!!
I have this problem with value 0 I mentioned before…
So,let me explain the error,and maybe someone sees the problem!
When I try simply an “example” code (no difference if I modify it to achieve my goals),like this everything works fine.I get my results and everything.
BUT when I try this I get correctly the photoresistant sensor,BUT I get 0 for the ultrasonic,even on my serial output.
The problem dissapears when I comment out Cayenne.Begin.(Cayenne doesn’t work,but I get the value on my serial)
Any ideas??It seems like I m missing something.Maybe an incompatibility with Cayenne and ultrasonic?I have absolutely no idea but I suppose they could work together…

use pins for ultrasonic other than 9 10 11 12 13. There are used by the arduino for communicating to the ethernet shield.