It looks like the wires are connected using masking tape, that's absolutely not ideal.. As already mentioned earlier, I think your resets are related to a watchdog reset. Looking at your code, the only thing I can spot that would cause this is the call to "ArduinoCloud.update()" in "loop()". If you get rid of the resets, the problem with the relay coming on and off is no longer an issue.