distance + relay node
-
Hello, I'm trying to aggregate 2 sketches : distance and relay actuator node ...
But I think I'm going the wrong way because it doesn't work ...
Here is my code :
// Sketch pour commander un relai en mode impultionnel. #include <MySensor.h> #include <SPI.h> #include <NewPing.h> // relais #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay int node_id = 9; // distance #define CHILD_ID 1 #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) MySensor gw; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage msg(CHILD_ID, V_DISTANCE); int lastDist; boolean metric = true; void setup() { // Initialize library and add callback for incoming messages //gw.begin(incomingMessage, AUTO, true); gw.begin(NULL, node_id); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelaisPulDist", "1.1"); // Fetch relay status // Register all sensors to gw (they will be created as child devices) gw.present(RELAY_1, S_LIGHT); gw.present(CHILD_ID, S_DISTANCE); boolean metric = gw.getConfig().isMetric; // Then set relay pins in output mode pinMode(RELAY_1, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(RELAY_1, RELAY_OFF); } void loop() { // Alway process incoming messages whenever possible gw.process(); int dist = metric?sonar.ping_cm():sonar.ping_in(); Serial.print("Ping: "); Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); if (dist != lastDist) { send(msg.set(dist)); lastDist = dist; } } void incomingMessage(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_LIGHT) { // Change relay state digitalWrite(RELAY_1, RELAY_ON); delay(500); digitalWrite(RELAY_1, RELAY_OFF); } } }Can you see what's going wrong ?
thanls ! -
@mfalkvidd said:
Could you share any details on "it doesn't work"?
The distance stays at 0 cm on my controller, and the relay actuator isn't triggered when I click it on my controller (I use my sensors with Jeedom)
-
@mfalkvidd said:
Could you share any details on "it doesn't work"?
The distance stays at 0 cm on my controller, and the relay actuator isn't triggered when I click it on my controller (I use my sensors with Jeedom)
@carmelo24 this is my motion and distance sketch:
https://github.com/sundberg84/MySensors-2.0/blob/master/Padd13/MotionDistNoSleepAs.inoNote its for 2.0 and not using sleep()
0 distance is (for me) an error value or fail to read the distance, i have used
if (dist == 0){ return; }To force the distance senor to do a new calculation and it works for me.
It could be a issue with your sensor, power or hardware if you get it all the time.Look at your serial output and see for the node.
-
So this is my sketch right now. The relay works ok, but I have only 0cm or 1cm with for the distance (and I can hear a sound coming from the distance sensor ...) :
// Sketch pour commander un relai en mode impultionnel. #include <MySensor.h> #include <SPI.h> #include <NewPing.h> #define node_id 66 #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay #define CHILD_ID 1 #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) MySensor gw; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage msg(CHILD_ID, V_DISTANCE); //int lastDist; boolean metric = true; void setup() { // Initialize library and add callback for incoming messages gw.begin(incomingMessage, node_id, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistance", "1.1"); // Fetch relay status // Register all sensors to gw (they will be created as child devices) gw.present(RELAY_1, S_LIGHT); gw.present(CHILD_ID, S_DISTANCE); boolean metric = gw.getConfig().isMetric; // Then set relay pins in output mode pinMode(RELAY_1, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(RELAY_1, RELAY_OFF); } void loop() { // Alway process incoming messages whenever possible gw.process(); int dist = metric?sonar.ping_cm():sonar.ping_in(); Serial.print("Ping: "); Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); // if (dist != lastDist) { // gw.send(msg.set(dist)); // lastDist = dist; // } } void incomingMessage(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_LIGHT) { // Change relay state digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); } } -
So this is my sketch right now. The relay works ok, but I have only 0cm or 1cm with for the distance (and I can hear a sound coming from the distance sensor ...) :
// Sketch pour commander un relai en mode impultionnel. #include <MySensor.h> #include <SPI.h> #include <NewPing.h> #define node_id 66 #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay #define CHILD_ID 1 #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds) MySensor gw; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage msg(CHILD_ID, V_DISTANCE); //int lastDist; boolean metric = true; void setup() { // Initialize library and add callback for incoming messages gw.begin(incomingMessage, node_id, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistance", "1.1"); // Fetch relay status // Register all sensors to gw (they will be created as child devices) gw.present(RELAY_1, S_LIGHT); gw.present(CHILD_ID, S_DISTANCE); boolean metric = gw.getConfig().isMetric; // Then set relay pins in output mode pinMode(RELAY_1, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(RELAY_1, RELAY_OFF); } void loop() { // Alway process incoming messages whenever possible gw.process(); int dist = metric?sonar.ping_cm():sonar.ping_in(); Serial.print("Ping: "); Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range) Serial.println(metric?" cm":" in"); // if (dist != lastDist) { // gw.send(msg.set(dist)); // lastDist = dist; // } } void incomingMessage(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_LIGHT) { // Change relay state digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); } }@carmelo24 it's ok now :)
here is my sketch :
// Sketch pour commander un relai en mode impultionnel. #include <MySensor.h> #include <SPI.h> #include <NewPing.h> #define node_id 66 #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define RELAY_ON 1 // GPIO value to write to turn on attached relay #define RELAY_OFF 0 // GPIO value to write to turn off attached relay #define CHILD_ID 1 #define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. unsigned long SLEEP_TIME = 500; // Sleep time between reads (in milliseconds) MySensor gw; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. MyMessage msg(CHILD_ID, V_DISTANCE); boolean metric = true; long temps; void setup() { // Initialize library and add callback for incoming messages gw.begin(incomingMessage, node_id, true); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("RelayDistance", "1.1"); // Fetch relay status // Register all sensors to gw (they will be created as child devices) gw.present(RELAY_1, S_LIGHT); gw.present(CHILD_ID, S_DISTANCE); boolean metric = gw.getConfig().isMetric; // Then set relay pins in output mode pinMode(RELAY_1, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(RELAY_1, RELAY_OFF); temps = millis(); } void loop() { // Alway process incoming messages whenever possible gw.process(); // mesure de la distance si ça fait plus de 10 secondes qu'on ne l'a pas fait if((millis() - temps) > 10000) { int dist = metric?sonar.ping_cm():sonar.ping_in(); gw.send(msg.set(dist)); Serial.print(dist); Serial.print("toto"); temps = millis(); } } void incomingMessage(const MyMessage &message) { // action du relais (avec impulsion, pour action comme un bouton poussoir) if (message.type==V_LIGHT) { // Change relay state digitalWrite(RELAY_1, RELAY_ON); delay(100); digitalWrite(RELAY_1, RELAY_OFF); delay(100); } }
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