How to get current status of an Arduino pin used by Cayenne?


#1

Hello, I’m new to Cayenne so forgive me if my questions is too basic.
I’m playing with Cayenne and re-did a home control project that took me a few months in a day so I am very impressed and happy with what I saw so far.

Here is my small problem:
I defined output pins on Cayenne to control some relays and some external X10 devices.
I have a buzzer on my arduino and want it to beep briefly on a switch change. For the X10 devices, I am using Virtual Channels so I have a place to add code to detect the change and beep but for the digital pins defined in Cayenne’s interface on the web I have no visible code to detect that a switch was changed and beep, so my question is how to I figure out on the arduino that the switch status changed from the web so I could beep? I thought of using triggers on Cayenne but I wanted to know if I could easily detect the changes in the arduino code itself. Is there a call I can make to know which switch was changed like in a virtual pin?
Thanks in advance.


#2

There are many ways to do it, some are hardware specific.

You can make calls using digitalRead(PIN) to get the current state and compare it to an old state to show it changes, then set the buzzer, and use the time library’s after() function to turn it off after a bit. This requires periodic polling however.

Some Arduinos are AVRs and have hardware ‘interrupt on change’. Thus you can define an ISR function to run when any of your hardware I/O changes state.

See http://playground.arduino.cc/Main/PinChangeInterrupt

Cheers,

Craig


#3

Thank you for the suggestions. I ended yesterday by creating triggers on Cayenne for those 4 relays where I was having trouble just to play and learn better how Cayenne triggers work. Thought about comparing previous and current state of a pin but that would force me to create a table to store those values which I wanted to avoid. If I could somehow get a call from Cayenne as it does on Virtual Pins it would be great. Another alternative for me would be to also use Virtual Pins for those 4 relays but that unfortunately would take the fun out of selecting and setting arruino’s pin from the web interface itself. Any way, I found a solution, not perfect but it works.

Thanks again.
Carlos


#4

Hi @carlos_benjamin

Cayenne trigger functionality is good for deciding when to alert you, etc, but I try to design my systems to make the right decisions independently as you cannot always depend on the internet connection to be there to make the decision on the cloud server.

For instance, lets say you have a water level detector, and the action is to open a drain valve, or turn off a fill valve when full. What would happen if the decision is being made in the cloud to turn off that valve and your connection is lost? The tank will overflow. This is a real world problem. Best to shut it off and set a flag that will trigger an alert to you once the internet connection resumes right?

Cheers,

Craig


#5

Thanks Craig, your are right. For demo and playing purposes I will use the triggers but having the logic locally on the arduino board makes total sense. I’m now getting more familiar with Cayenne so my I expect to rebuild the code in a more robust way.
Thanks again for the recommendation.

Carlos


#6

Hi @carlos_benjamin,

I just don’t want you to burn your house down :wink:

Although, perhaps that is a feature.

Cheers,

Craig