[SOLVED] Multiple devices demonstrating erratic behavior
-
@hek I have an 8 relay board and I'm using only 4. The board is powered off the arduino 5V pin.
-
This is probably the cause of your problems. The Arduino 5V pin cannot provide the necessary power needed.
You have to power them separately.
@hek Thank you - it worked without the addition of the NRF24L01+ so I'm guessing that pushed it over the edge. I'll try without it and let you know, thanks!
-
This is probably the cause of your problems. The Arduino 5V pin cannot provide the necessary power needed.
You have to power them separately.
@hek ok, I think I solved some of this. The extra sensors were coming from previous trials and errors. I removed the device and added it again, and bingo, no extra relays.
As for the relays not responding, I found an error in the code AND I think the motion sensor is getting stuck in a loop. When I activate all sensors there is still enough power for the relays to go. The problem is when I uncomment the code monitoring the motion sensor, everything becomes stuck again. There's a problem with the code to continuously monitor the motion sensor:
float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }and here is the current version of the code:
#include <SPI.h> #include <MySensor.h> #include <DHT.h> //Temp #define CHILD_ID_HUM 10 #define CHILD_ID_TEMP 20 #define HUMIDITY_SENSOR_DIGITAL_PIN 7 //Motion #define DIGITAL_INPUT_SENSOR A0 #define CHILD_ID_PIR 30 float lastMo; // Relay #define RELAY_1 3 #define RELAY_2 4 #define RELAY_3 5 #define RELAY_4 6 #define RELAY_ON 0 #define RELAY_OFF 1 // LED int red = A5; int blue = A4; int green = A3; // MySensor gw; DHT dht; float lastTemp; float lastHum; //Timer long previousMillis1 = 0; long interval1 = 60000; long previousMillis2 = 0; long interval2 = 5000; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED); void setup() { //Still unsure on conditions for this: gw.begin(incomingMessage, AUTO, false); //start temp sensor dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Arduino 2", "1.0"); // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); // gw.present(CHILD_ID_PIR, S_MOTION); //LED pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); // Register Relays gw.present(RELAY_1, S_LIGHT); digitalWrite(RELAY_1,RELAY_OFF); pinMode(RELAY_1, OUTPUT); gw.present(RELAY_2, S_LIGHT); digitalWrite(RELAY_2,RELAY_OFF); pinMode(RELAY_2, OUTPUT); gw.present(RELAY_3, S_LIGHT); digitalWrite(RELAY_3,RELAY_OFF); pinMode(RELAY_3, OUTPUT); gw.present(RELAY_4, S_LIGHT); digitalWrite(RELAY_4,RELAY_OFF); pinMode(RELAY_4, OUTPUT); digitalWrite(red, HIGH); digitalWrite(green, LOW); digitalWrite(blue, LOW); } void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis1 > interval1) { sendTemp(); delay(500); sendHumid(); previousMillis1 = currentMillis; } /* float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }*/ gw.process(); } void sendTemp(){ float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; temperature = dht.toFahrenheit(temperature); gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } } void sendHumid(){ float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; gw.send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, message.getBool()); Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } } -
@hek ok, I think I solved some of this. The extra sensors were coming from previous trials and errors. I removed the device and added it again, and bingo, no extra relays.
As for the relays not responding, I found an error in the code AND I think the motion sensor is getting stuck in a loop. When I activate all sensors there is still enough power for the relays to go. The problem is when I uncomment the code monitoring the motion sensor, everything becomes stuck again. There's a problem with the code to continuously monitor the motion sensor:
float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }and here is the current version of the code:
#include <SPI.h> #include <MySensor.h> #include <DHT.h> //Temp #define CHILD_ID_HUM 10 #define CHILD_ID_TEMP 20 #define HUMIDITY_SENSOR_DIGITAL_PIN 7 //Motion #define DIGITAL_INPUT_SENSOR A0 #define CHILD_ID_PIR 30 float lastMo; // Relay #define RELAY_1 3 #define RELAY_2 4 #define RELAY_3 5 #define RELAY_4 6 #define RELAY_ON 0 #define RELAY_OFF 1 // LED int red = A5; int blue = A4; int green = A3; // MySensor gw; DHT dht; float lastTemp; float lastHum; //Timer long previousMillis1 = 0; long interval1 = 60000; long previousMillis2 = 0; long interval2 = 5000; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED); void setup() { //Still unsure on conditions for this: gw.begin(incomingMessage, AUTO, false); //start temp sensor dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Arduino 2", "1.0"); // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); // gw.present(CHILD_ID_PIR, S_MOTION); //LED pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); // Register Relays gw.present(RELAY_1, S_LIGHT); digitalWrite(RELAY_1,RELAY_OFF); pinMode(RELAY_1, OUTPUT); gw.present(RELAY_2, S_LIGHT); digitalWrite(RELAY_2,RELAY_OFF); pinMode(RELAY_2, OUTPUT); gw.present(RELAY_3, S_LIGHT); digitalWrite(RELAY_3,RELAY_OFF); pinMode(RELAY_3, OUTPUT); gw.present(RELAY_4, S_LIGHT); digitalWrite(RELAY_4,RELAY_OFF); pinMode(RELAY_4, OUTPUT); digitalWrite(red, HIGH); digitalWrite(green, LOW); digitalWrite(blue, LOW); } void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis1 > interval1) { sendTemp(); delay(500); sendHumid(); previousMillis1 = currentMillis; } /* float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }*/ gw.process(); } void sendTemp(){ float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; temperature = dht.toFahrenheit(temperature); gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } } void sendHumid(){ float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; gw.send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, message.getBool()); Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }@thermistor1 What is the Serial Monitor outputting?
-
@thermistor1 said:
float lastMo;
/* float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }*/if anything is a binary device it is a motion sensor... total possible states = 2
You then introduce all of the problems of arduino's lousy float precision to your sketch?
try instead to declare lastMo as a byte or int.
byte lastMo;oh... and state too....
byte state = digitalRead(DIGITAL_INPUT_SENSOR); -
@thermistor1 said:
float lastMo;
/* float state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } previousMillis2 = currentMillis; }*/if anything is a binary device it is a motion sensor... total possible states = 2
You then introduce all of the problems of arduino's lousy float precision to your sketch?
try instead to declare lastMo as a byte or int.
byte lastMo;oh... and state too....
byte state = digitalRead(DIGITAL_INPUT_SENSOR);@BulldogLowell Thank you! I'm not a programmer, can you tell?
I'll implement that. Got the rest of it working and will share the code as well. -
So for those who may be wondering, here are a couple things I learned working with multiple sensors + relays:
- All radios need capacitors because the 3.3V on the arduino is not sufficient
- Extra devices and arduinos that appear in your device list can be cleared out - remove the device and re-scan, and it should sort those problems out. I had mysterious sensors appear and refuse to leave until I did this.
- Keep track of and double check your pin numbers.
- (Clearly) don't use delay(), instead use millis()
- I did some of this piece by piece instead of repeating a function because I was trying to diagnose problems. It could probably be cleaned up but it successfully has seven different devices being controlled and functioning properly.
#include <SPI.h> #include <MySensor.h> #include <DHT.h> //Temp #define CHILD_ID_HUM 10 #define CHILD_ID_TEMP 20 #define HUMIDITY_SENSOR_DIGITAL_PIN 7 //Motion #define DIGITAL_INPUT_SENSOR A0 #define CHILD_ID_PIR 30 byte lastMo; // Relay #define RELAY_1 3 #define RELAY_2 4 #define RELAY_3 5 #define RELAY_4 6 #define RELAY_ON 0 #define RELAY_OFF 1 // LED int red = A5; int blue = A4; int green = A3; // MySensor gw; DHT dht; float lastTemp; float lastHum; //Timer long previousMillis1 = 0; long interval1 = 60000; long previousMillis2 = 0; long interval2 = 5000; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED); void setup() { //Still unsure on conditions for this: gw.begin(incomingMessage, AUTO, false); //start temp sensor dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Arduino 2", "0.9"); // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); gw.present(CHILD_ID_PIR, S_MOTION); //LED pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); // Register Relays gw.present(RELAY_1, S_LIGHT); digitalWrite(RELAY_1,RELAY_OFF); pinMode(RELAY_1, OUTPUT); gw.present(RELAY_2, S_LIGHT); digitalWrite(RELAY_2,RELAY_OFF); pinMode(RELAY_2, OUTPUT); gw.present(RELAY_3, S_LIGHT); digitalWrite(RELAY_3,RELAY_OFF); pinMode(RELAY_3, OUTPUT); gw.present(RELAY_4, S_LIGHT); digitalWrite(RELAY_4,RELAY_OFF); pinMode(RELAY_4, OUTPUT); digitalWrite(red, HIGH); digitalWrite(green, LOW); digitalWrite(blue, LOW); } void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis1 > interval1) { sendTemp(); delay(500); sendHumid(); previousMillis1 = currentMillis; } byte state = digitalRead(DIGITAL_INPUT_SENSOR); if (state == HIGH) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } } if (state == LOW) { if (state != lastMo) { gw.send(msgPIR.set(state?"1":"0")); lastMo = state; } } previousMillis2 = currentMillis; gw.process(); } void sendTemp(){ float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else { lastTemp = temperature; temperature = dht.toFahrenheit(temperature); gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } } void sendHumid(){ float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else { lastHum = humidity; gw.send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } } void incomingMessage(const MyMessage &message) { if (message.type==V_LIGHT) { if (message.sensor==3){ digitalWrite(RELAY_1,RELAY_ON); digitalWrite(red, HIGH); digitalWrite(blue, HIGH); digitalWrite(green, LOW); } else if (message.sensor==4){ digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, message.getBool()); } else if (message.sensor==5){ digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, message.getBool()); } else if (message.sensor==6){ digitalWrite(message.sensor, message.getBool()?RELAY_ON:RELAY_OFF); gw.saveState(message.sensor, message.getBool()); if((digitalRead(RELAY_4))==LOW){ //If relay 4 is on: digitalWrite(red, LOW); digitalWrite(blue, HIGH); digitalWrite(green, HIGH); } if((digitalRead(RELAY_4))==HIGH){ //If relay 4 is off and the thing is on: digitalWrite(red, HIGH); digitalWrite(blue, LOW); digitalWrite(green, LOW); } } } } -
@BulldogLowell Thank you! I'm not a programmer, can you tell?
I'll implement that. Got the rest of it working and will share the code as well.So... all working OK now?
-
So... all working OK now?
@BulldogLowell Yes, all is working, thanks. If you want to use this code for reference it is fully functional.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login