Turn sensors on and off, is there a way to do it through Millis?


Hello people of Cayenne,

I’m sort of a noob in Cayenne and also not that experienced in C++. I’ve been tinkering with Arduino on a project based on Millis() to monitor a water tank on a 10 minutes loop. The loop follows a logic to turn an HL-69 Soil Electrical Conductivity sensor “On” only while a pH sensor is not reading anything in my Serial Monitor and then the CE turns “Off” while the pH readings are being written. So basically,

if (Millis() < 15000){
if (16000<Millis() < 30000){

During the rest of the loop, the Arduino only waits for the next time threshold to start over again. This avoids the interference of the voltage input of the CE over the pH sensor’s readings and lowers the electrolytic wear off of the CE probe, since I only need readings every 10 minutes.

While trying to transfer this code to Cayenne, i coudn’t find a way to make such loop work, since the readings are made realtime, meaning that my HL-69 probe is working the full 10 minutes on this loop… Is there a way to solve this problem? Can Cayenne make readings on time intervals, like once every 2 or 5 minutes?

Thanks in advance!



@rmbatistic welcome to cayenne community.
i dint get what you are trying to accomplish as whole. but for this

void loop() {

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if (millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, lastMillis);
		//Some examples of other functions you can use to send data.
		//Cayenne.celsiusWrite(1, 22.0);
		//Cayenne.luxWrite(2, 700);
		//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);

this should get you going


Hello @shramiksalgaonkar, thanks for your answer!

Basically what I need is to:

Turn On sensor 1, read sensor 1, turn Off sensor 1, wait a while, read sensor 2, end of loop, repeat. Sensor 1 turns On and Off through the digitalWrite command.

I could do it in Arduino using Millis() in a loop with lots of "if"s conditions based on the currentMillis() function, but I haven’t found a solution to work on Cayenne to do the same loop.


if you can achieve with arduino using mills() then what you want to do with cayenne(like display the value on cayenne dashboard)?


With the Arduino setup I can only upload the Client.prints to a simple HTML page that I’ve set up with an W5100 Ethernet shield, but that gives me no Historical Register of my input data, just a Realtime reading and it is only readable inside my local network(at work only). I think I could theoretically print it as an Excel exportable text file, but I’m not quite sure how to do that…

I really liked the easy and friendly UI of Cayenne, so I thought it would be a nice platform to have this Data saved and it would also allow me to see the measures anywhere, not only here at work!


@rmbatistic when you want to send data to cayenne dashboard to view as line chart/graph follow this steps.
add new > device > custom widget > line chart > fill all the details and select virtual i/o with pin V1.
this will add a line chart to your dashboard.
next you need to program.

  // This command writes the value of x to the Virtual Pin V1.
  Cayenne.virtualWrite(V1, x);

where x stores the value of what you want to send to cayenne dashboard linechart.


@shramiksalgaonkar, yep, I’ve been using this principle already!