@petewill Thanks for sharing your knowledge. I am picking this after 2.5 years as this is what I would like to build. i have established my switch is connected to grounds and build a prototype successfully. However, my issue is the code as i am lacking in the software side and appreciate if @petewill or someone else can advise me.
Issue 1: when i press the button, it shows twice in the log and i can see the communicating successfully with the gateway.
Issue 2: when i send the command via mqtt it is not turning on the led.
Thanks for input in advance.
my code is as follows,
#define SKETCH_NAME "Oven Control"
#define SKETCH_VERSION "1.0"
// Enable debug prints to serial monitor
#define MY_DEBUG //MySensors debug messages
//#define LOCAL_DEBUG //Code specific debug messages
// Enable and select radio type attached
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
#define MY_RF24_PA_LEVEL RF24_PA_HIGH //Options: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
#define MY_RF24_CHANNEL 76
#define MY_NODE_ID 4 //Manually set the node ID here. Comment out to auto assign
#include <MySensors.h>
#include <Bounce2.h>
#ifndef BAUD_RATE
#define BAUD_RATE 115200
#endif
#ifdef LOCAL_DEBUG
#define dbg(...) Serial.print(__VA_ARGS__)
#define dbgln(...) Serial.println(__VA_ARGS__)
#else
#define dbg(x)
#define dbgln(x)
#endif
#define DWELL_TIME 50 //value used in all wait calls (in milliseconds) this allows for radio to come back to power after a transmission, ideally 0
#define CHILD_ID_OVEN 0
#define GND_GATE_PIN 3
#define GND_DETECT_PIN 4
#define BUTTON_PRESS_DELAY 100 //The amount of delay used for a button press
//Track button presses
uint8_t gndValuePrev = 1;
//LED button on/off tracking
uint8_t gndLedOn = 0;
unsigned long gndMillis;
// Instantiate a Bounce object
Bounce gndDebouncer = Bounce();
Bounce vccDebouncer = Bounce();
MyMessage msgHeatMode(CHILD_ID_OVEN, V_HVAC_FLOW_STATE);
void setup() {
Serial.begin(115200);
//Setup the pins
pinMode(GND_GATE_PIN, OUTPUT);
pinMode(GND_DETECT_PIN, INPUT_PULLUP);
//Start with all outputs (buttons) not enabled (pressed)
digitalWrite(GND_GATE_PIN, 0);
// After setting up the buttons, setup debouncers
gndDebouncer.attach(GND_DETECT_PIN);
gndDebouncer.interval(50);
}
void presentation()
{
// Send the sketch version information to the gateway
sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_OVEN, S_HEATER);
wait(DWELL_TIME);
//metric = getConfig().isMetric; //This has been removed as it will default to metric if connection to the gateway is not established (bad for imperial users)
//wait(DWELL_TIME);
}
void loop() {
unsigned long currentMillis = millis(); //Get the current millis (used for timers)
// Update the debouncers
gndDebouncer.update();
// Get the update value
uint8_t gndValue = gndDebouncer.read();
if (gndValue != gndValuePrev)
{
if (gndValue == 0)
{
Serial.println(F("Ground Button Pressed"));
if (gndLedOn == 0)
{
//Don't echo the button push if it was turned on by the Arduino
gndMillis = currentMillis + 1000;
gndLedOn = 1;
}
}
gndValuePrev = gndValue;
send(msgHeatMode.set(gndLedOn == 1 ? "HeatOn" : "Off"));
dbgln(gndValue);
}
//Turn on led 1 second after button pressed
if (gndLedOn == 1 && currentMillis > gndMillis)
{
nChannelPress(GND_GATE_PIN);
gndLedOn = 0;
}
}
void nChannelPress(uint8_t buttonPinName)
{
//Simulate a button press
digitalWrite(buttonPinName, 1); //VCC to disable
delay(BUTTON_PRESS_DELAY);
digitalWrite(buttonPinName, 0); //Ground to enable
delay(BUTTON_PRESS_DELAY);
}
The log is as follows,
22:35:51.655 -> 2189 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=11,pt=0,l=12,sg=0,ft=0,st=OK:Oven Control
22:35:51.655 -> 2198 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0
22:35:51.655 -> 2206 TSF:MSG:SEND,4-4-0-0,s=0,c=0,t=14,pt=0,l=0,sg=0,ft=0,st=OK:
22:35:51.724 -> 2262 MCO:REG:REQ
22:35:51.724 -> 2265 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
22:35:51.724 -> 2276 TSF:MSG:READ,0-0-4,s=255,c=3,t=27,pt=1,l=1,sg=0:1
22:35:51.724 -> 2281 MCO:PIM:NODE REG=1
22:35:51.724 -> 2283 MCO:BGN:STP
22:35:51.758 -> 2285 MCO:BGN:INIT OK,TSP=1
22:35:52.541 -> Ground Button Pressed
22:35:52.541 -> 3073 TSF:MSG:SEND,4-4-0-0,s=0,c=1,t=21,pt=0,l=6,sg=0,ft=0,st=OK:HeatOn
22:35:52.745 -> 3291 TSF:MSG:SEND,4-4-0-0,s=0,c=1,t=21,pt=0,l=6,sg=0,ft=0,st=OK:HeatOn
22:39:09.887 -> 200479 TSF:MSG:READ,0-0-4,s=255,c=3,t=6,pt=0,l=0,sg=0:
22:42:23.738 -> 394357 TSF:MSG:READ,0-0-255,s=255,c=3,t=20,pt=0,l=0,sg=0:
22:42:23.738 -> 394362 TSF:MSG:BC
22:42:23.895 -> 394502 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=21,pt=1,l=1,sg=0,ft=0,st=OK:0
22:44:19.647 -> Ground Button Pressed
22:44:19.647 -> 510285 TSF:MSG:SEND,4-4-0-0,s=0,c=1,t=21,pt=0,l=6,sg=0,ft=0,st=OK:HeatOn
22:44:19.958 -> 510600 TSF:MSG:SEND,4-4-0-0,s=0,c=1,t=21,pt=0,l=6,sg=0,ft=0,st=OK:HeatOn```