Format for CAYENNE_IN (slider)

I am not reading the value of the slider in my Cayenne project.

I am using Arduino IDE with a Wemos D1 mini esp8266 dev board. My device is a soil moisture sensor and I use
ESP.deepSleep (SleepTime); at the end of the loop to put the device to sleep between readings to save battery power.

Sleeptime = 100000 * sleeptimesec and sleeptimesec is defined as a Long variable.

I created a slider at V8 from 5-300 and set it at 10 (the default value of sleeptimesec is 5).

I have tried placing

CAYENNE_IN(V8){
sleeptimesec = getValue.asLong();
}

In various places, but none of them alter the value of sleeptimesec. from 5 to 10.

Everything else works nicely, but I would like to be able to change the sleep time in the field.

My full sketch is here:

//set sleep time
long sleeptimesec = 5;
long SleepTime = 1000000 * sleeptimesec;

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

// Set Soil reading
#define SoilMoistureADMax  850                                             // Max Analog Value
#define SoilMoistureADMin  0                                                // Min Analog Value


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

// Cayenne authentication info. 
char username[] = "xxx";
char password[] = "xxxx";
char clientID[] = "xxx";


int moist = 0;


//Values sent/error booleans
bool CayenneSent = false;
bool ReadError = false;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  CayenneSent = false;
  ReadError = true;
  pinMode(12,OUTPUT);
  pinMode(D0, WAKEUP_PULLUP);
             }
             
CAYENNE_IN(V8){
  sleeptimesec = getValue.asLong();
              }

void loop() {
	Cayenne.loop();
// Print sleeptimesec
Serial.print ("sleeptime is ");
Serial.println (sleeptimesec);
// read soil moisture after powering sensor from pin 12
for (int i=0; i <= 4; i++){
      digitalWrite (12,HIGH);
      delay (1000);
      moist = analogRead(A0);
      moist = map(moist, SoilMoistureADMin, SoilMoistureADMax, 0, 100);
//send moist to Cayenne      
      Cayenne.virtualWrite(1, moist, "soil_moist", "p");
      Cayenne.virtualWrite(0, millis () /1000);
      Serial.println (analogRead(A0));
      delay (1000);
                          }     
 //turn off sensor
 digitalWrite(12,LOW); 

 //go to sleep
 ESP.deepSleep (SleepTime);
            }

I have discovered a few things wrong with the sketch. I changed the channel used to 20 on both the sketch and the dashboard. I moved the CAYENNE_IN function to the end, outside the loop.

If I comment out the deepSleep instruction, sleeptimesec changes in real time. However, if I put deepSleep back in, it reverts to the default and doen’t change.

So, my current question is, can I call the CAYENNE_IN function from inside the loop, so it reads the slider value while it is awake?

so you want the device to wait to get the slider value and then sleep? Set a variable true when you receive value from the slider and if it is true then only sleep the device.

Nice. I was starting to think along this line but hadn’t gotten there yet.

Is the slider value received only after it is changed on the dashboard or is it after every completion of the loop or is it timed?

it is received only after it is changed on the dashboard.

OK, here’s my problem:

The default sleeptime is 300 sec. If I want to change it to 5 sec. for a while, I move the slider to 5 sec. Most likely, the device will be asleep. When it wakes up, it will start cayenne. Will it receive the change to 5? After a while, it will sleep. When it wakes up (restarts), it will be at the default again. If the slider is still at 5, will it receive that update? Is there a way that I can query the slider from the device?

save the sleep time in EEPROM. so every time you arduino restart it gets the value from the EEPROM.

I didn’t know how to do that. Thanks!


Very handy.


There have been talks of enabling the MQTT retain flag which would solve your problem. @acedeno any news on that?

@adam currently that is not in our roadmap, but we will revisit in the near future. Thanks!