Momentary button to control lights
-
@MasMat said in Momentary button to control lights:
All right. Been banging my head at this all day again.
I seem to be sending the message from the node OK, but the receiving node appears to have intermittent problems.Yes your send message should be ok. You are sending a V_STATUS message from node 7 sensor 3 to node 3 sensor 3
And for some reason, my gateway reboots at some "combinations".
Not sure what could be causing that but you will need to sort that out to get stability. I have not experienced any issues with my gateway using node to node.
I have gone thru the send-node & receive-node code tens of times.
Help & ideas appreciated. Plz excuse the finnish in the codeYou will need to add code to separate the incoming messages from your controller and remotes as they will need to be treated differently. Messages from your controller will have the gateway (node 0) as the sender so this can be used.
You have a lot going on in your receive function so maybe just put the code for the one node you are trying at the moment so it is easier to troubleshoot.
Try something like this
void receive(const MyMessage &message) { if (message.type == V_STATUS) { if (message.sender == 0) { // check if message is from gateway (node 0) switch (message.sensor) { case 3: //incoming message is for WC relay (sensor 3) stateWC = message.getBool(); // get the new state digitalWrite(5, stateWC ? RELAY_ON : RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // save sensor state break; /*-----Add case statements for other realys as needed ----*/ } } else { // message is not from gateway so must be from a remote switch (message.sender) { case 7: //incoming message is from WC remote (node 7) stateWC = !stateWC; // toggle light state digitalWrite(5, stateWC?RELAY_ON:RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // Store state in eeprom send(msgWC.set(stateWC), false); //Msg gateway aware of the change. No ack break; /*-----Add case statements for other remote nodes as needed ----*/ } } // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming change for sensor:"); Serial.println(message.sensor); Serial.print("from node:"); Serial.println(message.sender); Serial.print(" New status: "); Serial.println(message.getBool()); #endif } }@Boots33 So, added some tuff to keep the other relays working.
The issue now is bouncing apparently.
The send-node has delay but seems to fire a bunch of 1's (and 0's for some reason...?).
Here's the lastest on the receiving end:void receive(const MyMessage &message) { if (message.type == V_STATUS) { if (message.sender == 0) { // check if message is from gateway (node 0) switch (message.sensor) { case 3: //incoming message is for WC relay (sensor 3) stateWC = message.getBool(); // get the new state digitalWrite(5, stateWC ? RELAY_ON : RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // save sensor state break; case 7: digitalWrite(RELAY_7, message.getBool()?RELAY_ON:RELAY_OFF); //Write to A0 saveState(message.sensor, message.getBool()); break; case 8: digitalWrite(RELAY_8, message.getBool()?RELAY_ON:RELAY_OFF); //Write to A1 saveState(message.sensor, message.getBool()); break; default: digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); saveState(message.sensor, message.getBool()); break; /*-----Add case statements for other realys as needed ----*/ } } else { // message is not from gateway so must be from a remote switch (message.sender) { case 7: //incoming message is from WC remote (node 7) stateWC = !stateWC; // toggle light state digitalWrite(5, stateWC?RELAY_ON:RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // Store state in eeprom send(msgWC.set(stateWC), false); //Msg gateway aware of the change. No ack break; /*-----Add case statements for other remote nodes as needed ----*/ } } // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming change for sensor:"); Serial.println(message.sensor); Serial.print("from node:"); Serial.println(message.sender); Serial.print(" New status: "); Serial.println(message.getBool()); #endif } }The send-node is still
#define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 #define MY_NODE_ID 7 #include <MySensors.h> //destination specs #define WC_SENSOR_ID 3 #define WC_NODE_ID 3 int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point int oldBatteryPcnt = 0; #define CHILD_ID_BUTTON1 3 #define BUTTON_PIN 2 //pin for button //#define INTERRUPT BUTTON_PIN MyMessage msg(CHILD_ID_BUTTON1, V_STATUS); int sentCounter = 0; int reading; // the current reading from the input pin int previous = HIGH; // the previous reading from the input pin void setup() { // Setup the buttons pinMode(BUTTON_PIN, INPUT); // Activate internal pull-ups digitalWrite(BUTTON_PIN, HIGH); // use the 1.1 V internal reference #if defined(__AVR_ATmega2560__) analogReference(INTERNAL1V1); #else analogReference(INTERNAL); #endif } void presentation() { sendSketchInfo("WCNappiBAT", "v290717"); present(CHILD_ID_BUTTON1, S_BINARY); } void loop() { reading = digitalRead(BUTTON_PIN); if (reading == LOW && previous == HIGH) { send(MyMessage(CHILD_ID_BUTTON1, V_STATUS).setSensor(WC_SENSOR_ID).setDestination(WC_NODE_ID).set("1")); Serial.println("Toggling remote node "); Serial.print(WC_NODE_ID); Serial.print(", sensor "); Serial.println(WC_SENSOR_ID); sentCounter++; } previous = reading; if (sentCounter % 10 == 0) { int sensorValue = analogRead(BATTERY_SENSE_PIN); // 3.44/1023 = Volts per bit = 0.003363075 int batteryPcnt = sensorValue / 10; float batteryV = sensorValue * 0.003363075; Serial.print("Battery Voltage: "); Serial.print(batteryV); Serial.println(" V"); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); } delay (400); // Sleep until something happens with the sensor sleep(0, CHANGE, 0); } -
@Boots33 So, added some tuff to keep the other relays working.
The issue now is bouncing apparently.
The send-node has delay but seems to fire a bunch of 1's (and 0's for some reason...?).
Here's the lastest on the receiving end:void receive(const MyMessage &message) { if (message.type == V_STATUS) { if (message.sender == 0) { // check if message is from gateway (node 0) switch (message.sensor) { case 3: //incoming message is for WC relay (sensor 3) stateWC = message.getBool(); // get the new state digitalWrite(5, stateWC ? RELAY_ON : RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // save sensor state break; case 7: digitalWrite(RELAY_7, message.getBool()?RELAY_ON:RELAY_OFF); //Write to A0 saveState(message.sensor, message.getBool()); break; case 8: digitalWrite(RELAY_8, message.getBool()?RELAY_ON:RELAY_OFF); //Write to A1 saveState(message.sensor, message.getBool()); break; default: digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); saveState(message.sensor, message.getBool()); break; /*-----Add case statements for other realys as needed ----*/ } } else { // message is not from gateway so must be from a remote switch (message.sender) { case 7: //incoming message is from WC remote (node 7) stateWC = !stateWC; // toggle light state digitalWrite(5, stateWC?RELAY_ON:RELAY_OFF); // 5 is RELAY_3 pin saveState(message.sensor, stateWC); // Store state in eeprom send(msgWC.set(stateWC), false); //Msg gateway aware of the change. No ack break; /*-----Add case statements for other remote nodes as needed ----*/ } } // Write some debug info #ifdef MY_DEBUG Serial.print("Incoming change for sensor:"); Serial.println(message.sensor); Serial.print("from node:"); Serial.println(message.sender); Serial.print(" New status: "); Serial.println(message.getBool()); #endif } }The send-node is still
#define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 #define MY_NODE_ID 7 #include <MySensors.h> //destination specs #define WC_SENSOR_ID 3 #define WC_NODE_ID 3 int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point int oldBatteryPcnt = 0; #define CHILD_ID_BUTTON1 3 #define BUTTON_PIN 2 //pin for button //#define INTERRUPT BUTTON_PIN MyMessage msg(CHILD_ID_BUTTON1, V_STATUS); int sentCounter = 0; int reading; // the current reading from the input pin int previous = HIGH; // the previous reading from the input pin void setup() { // Setup the buttons pinMode(BUTTON_PIN, INPUT); // Activate internal pull-ups digitalWrite(BUTTON_PIN, HIGH); // use the 1.1 V internal reference #if defined(__AVR_ATmega2560__) analogReference(INTERNAL1V1); #else analogReference(INTERNAL); #endif } void presentation() { sendSketchInfo("WCNappiBAT", "v290717"); present(CHILD_ID_BUTTON1, S_BINARY); } void loop() { reading = digitalRead(BUTTON_PIN); if (reading == LOW && previous == HIGH) { send(MyMessage(CHILD_ID_BUTTON1, V_STATUS).setSensor(WC_SENSOR_ID).setDestination(WC_NODE_ID).set("1")); Serial.println("Toggling remote node "); Serial.print(WC_NODE_ID); Serial.print(", sensor "); Serial.println(WC_SENSOR_ID); sentCounter++; } previous = reading; if (sentCounter % 10 == 0) { int sensorValue = analogRead(BATTERY_SENSE_PIN); // 3.44/1023 = Volts per bit = 0.003363075 int batteryPcnt = sensorValue / 10; float batteryV = sensorValue * 0.003363075; Serial.print("Battery Voltage: "); Serial.print(batteryV); Serial.println(" V"); Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %"); sendBatteryLevel(batteryPcnt); } delay (400); // Sleep until something happens with the sensor sleep(0, CHANGE, 0); }@MasMat You could try increasing your delay time out to several seconds to see what happens (probably should use wait instead of delay as well) and maybe comment out the sleep line too. This may at least give you an idea of where to start.
perhaps post some of the serial monitor output from the node.
-
@MasMat You could try increasing your delay time out to several seconds to see what happens (probably should use wait instead of delay as well) and maybe comment out the sleep line too. This may at least give you an idea of where to start.
perhaps post some of the serial monitor output from the node.
@Boots33 I increased it already but it didnt help. I can comment out the sleep but eventually I will need it because the sensor is obviously bat powered.
I also tested putting delay before the read to let things stabilize. Tried 50 but same behavior.
Im not really familiar with using the interrupts. Is my syntax correct in the sleep-line? I read somewhere that pin2 =interrupt0? Or use sleep (0, falling, 0)? Instead of "change"? -
@Boots33 I increased it already but it didnt help. I can comment out the sleep but eventually I will need it because the sensor is obviously bat powered.
I also tested putting delay before the read to let things stabilize. Tried 50 but same behavior.
Im not really familiar with using the interrupts. Is my syntax correct in the sleep-line? I read somewhere that pin2 =interrupt0? Or use sleep (0, falling, 0)? Instead of "change"?@MasMat said in Momentary button to control lights:
@Boots33 I increased it already but it didnt help. I can comment out the sleep but eventually I will need it because the sensor is obviously bat powered.
You still need to find the cause of the multiple sends so comment out sleep and see if that is the cause. It is an easy thing to try.
I also tested putting delay before the read to let things stabilize. Tried 50 but same behavior.
Im not really familiar with using the interrupts. Is my syntax correct in the sleep-line? I read somewhere that pin2 =interrupt0? Or use sleep (0, falling, 0)? Instead of "change"?I have no battery powered nodes so probably not the best person to ask on the use of sleep. I would definitely try
sleep(0, FALLING, 0);and see what that brings.
If you comment out sleep and the node works as expected then it may be worth starting a new thread focusing on the sleep problem, at least that may bring the problem to the attention of others who have used sleep before.Again serial output may shed further light on what is going wrong.
-
Fighting with this after a while.
I got the serial output from the sending node, but it appears to send the message only once.
However when I checked the gateway it seems to receive and forward the message three times. Then it reboots. Power issue? I have a NRF24-LA on the gateway so could that be pulling too much juice?Could there be a signalling bounce?
Serial output from when I clicked the button on the remote:
0;255;3;0;9;TSF:MSG:READ,7-7-3,s=3,c=1,t=2,pt=0,l=1,sg=0:1 0;255;3;0;9;TSF:MSG:REL MSG 0;255;3;0;9;TSF:MSG:SEND,7-0-9-3,s=3,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1 0;255;3;0;9;TSF:MSG:READ,7-7-3,s=3,c=1,t=2,pt=0,l=1,sg=0:1 0;255;3;0;9;TSF:MSG:REL MSG 0;255;3;0;9;TSF:MSG:SEND,7-0-9-3,s=3,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=OK:1 0;255;3;0;9;TSF:MSG:READ,7-7-3,s=3,c=1,t=2,pt=0,l=1,sg=0:1 0;255;3;0;9;TSF:MSG:REL MSG 0;255;3;0;9;!TSF:MSG:SEND,7-0-9-3,s=3,c=1,t=2,pt=0,l=1,sg=0,ft=0,st=NACK:1 0;255;3;0;9;MCO:BGN:INIT GW,CP=RNNGA--,VER=2.1.1 0;255;3;0;9;TSM:INIT 0;255;3;0;9;TSF:WUR:MS=0 0;255;3;0;9;TSM:INIT:TSP OK 0;255;3;0;9;TSM:INIT:GW MODE 0;255;3;0;9;TSM:READY:ID=0,PAR=0,DIS=0 0;255;3;0;9;MCO:REG:NOT NEEDED 0;255;3;0;9;TSF:MSG:READ,3-9-0,s=3,c=1,t=2,pt=1,l=1,sg=0:0 IP: 192.168.0.201 0;255;3;0;9;MCO:BGN:STP 0;255;3;0;9;MCO:BGN:INIT OK,TSP=1 IP: 192.168.0.201 0;255;3;0;9;Attempting MQTT connection... 0;255;3;0;9;MQTT connected``` -
Just download the library from github and replace the mysensors folder in the library folder on your pc
-
My gateway is a serial gateway built with an easy newbie PCB and a pro mini.
What I did when I upgraded my 1.4 setup to 2.0, I pulled my pro mini that I had flashed with the 1.4 setup and put a new pro mini in that had 2.0. That way if there were any problems, I could easily swap the 1.4 pro mini back in to easily be back where I started.Likewise when replacing your library, pull the MySensors folder out of your arduino libraries folder to keep as a backup until you know things will work. That way if you have to revert back, you can easily swap back the old MySensors folder. You have to completely remove it from the libraries folder though, you can't just rename it to something else.