Hello Particle Community,
I made a prototype device, using the B404X SoM dev kit, for a customer that simply turns on/off a SSR relay to larger load, and it works perfectly for me, but it stutters on first boot up and on first toggle command for my customer.
My customers device was placed in a much noisier environment than mine, and has lower cellular service than i do, so I was wondering if anyone had similar issues regarding this?
Below is the code that works perfectly for me, but creates a stuttering phenomenon for my customer:
int relay = D4;
int relayState;
unsigned long lastTime = 0;
void setup()
{
pinMode(relay, OUTPUT);
Particle.variable("relay_state", &relayState, INT);
Particle.function("contactor_relay_new", relayToggle);
digitalWrite(relay, LOW);
relayState = 0;
}
void loop()
{
}
int relayToggle(String command) {
// get the current time
unsigned long currentTime = millis();
// check if the function was called less than 5 seconds ago
// if we called this function less than 5 seconds ago, we will ignore this request
if (currentTime - lastTime < 5000)
{
// if it was, return 0 without doing anything
return 0;
}
else
{
// if it was not, update the last time and toggle pin accordingly
lastTime = currentTime;
if (command == "on")
{
digitalWrite(relay, HIGH);
relayState = 1;
return 1;
}
else if (command == "off")
{
digitalWrite(relay, LOW);
relayState = 0;
return 1;
}
else
{
return -1;
}
}
}
You can notice in my code, that I won't even toggle the relay pin if it received the same command less than 5 seconds ago, so it's not an issue of a bunch of toggle commands being stored in a cloud buffer and then being sent to the device in a rapid fashion; this must be something hardware wise, but not sure what exactly could be causing this.
This is what my crude prototype looks like:
The I/O wire (orange) and Relay GND wire (blue) is rather long, so I can see it capturing noise to changing the logic level, but do you think this would enough length to cause such an issue?
This is the problem my customer was having:VID_20230529_190402.mp4 - Google Drive
The video was shot before I implemented the 5 second max frequency toggle, but the same stuttering experienced in the beginning of the video still happened, so that didn't fix it.
Any assistance on this would be greatly appreciated!
Thank you,
Nicolas