Conversion from 1.5.4 to 2.1.1.
-
I am converting all my nodes to 2.1.1 because they are not visible in the Domoticz so I think I have re adjust them all. Is there a central place where the conversion is explained? I found already a lot of info on this forum. Now I have a questions what can I do with the row:
gw.begin(incomingMessage, AUTO, true);
I changed it already in
begin(recieve,AUTO, true);
But get an erro "begin was not declared in the scope.
What must I do? -
Not easy to say without your sketch..
But I think that you may miss the "s" in
#include <MySensors.h>For more infos, you can take a look here:
-
Not easy to say without your sketch..
But I think that you may miss the "s" in
#include <MySensors.h>For more infos, you can take a look here:
@scalz thanks for the link, that was the one I used also. Perhaps you can do something with the whole script in the part void presentation (3rd row)
// 2x binary switch + dallas temp example // Connect button or door/window reed switch between // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND. // Connect dallas temp sensor to digital pin 3 #include <MySensors.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> #define DIGITAL_MOTION_SENSOR 2 #define CHILD_ID_MOTION 6 #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define RELAY_ON 1 // switch around for realy HIGH/LOW state #define RELAY_OFF 0 Bounce motionsDebouncer = Bounce(); int lastMOTION; unsigned long SLEEP_TIME = 420000; //unsigned long SLEEP_TIME = 3000; unsigned long blockMotionTimer = 0; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //#define noRelays 3 //const int relayPin[] = {A1, A4, A5}; // switch around pins to your desire //const int buttonPin[] = {6, 7, 8}; // switch around pins to your desire #define noRelays 1 const int relayPin[] = {A1}; // switch around pins to your desire const int buttonPin[] = {6}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay byte oldValue; // last Values for key (debounce) boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncerRELAY[noRelays]; MyMessage msgRELAY[noRelays]; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; boolean metric = true; MyMessage msg(0,V_TEMP); MyMessage msgMOTION(CHILD_ID_MOTION, V_TRIPPED); void presentation() { sensors.begin(); begin(receive, AUTO, true); sendSketchInfo("Motion/Temp/relay", "1.0"); numSensors = sensors.getDeviceCount(); for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input motionsDebouncer.attach(DIGITAL_MOTION_SENSOR); motionsDebouncer.interval(400); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true); for (int i = 0; i < noRelays; i++) { Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msgRELAY[i].sensor = i; // initialize messages msgRELAY[i].type = V_LIGHT; debouncerRELAY[i] = Bounce(); // initialize debouncer debouncerRELAY[i].attach(buttonPin[i]); debouncerRELAY[i].interval(5); pinMode(Relays[i].buttonPin, INPUT_PULLUP); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = gw.loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status present(i, S_LIGHT); // present sensor to gateway delay(250); } } void loop() { //process(); for (byte i = 0; i < noRelays; i++) { debouncerRELAY[i].update(); byte value = debouncerRELAY[i].read(); if (value != Relays[i].oldValue && value == 0) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msgRELAY[i].set(Relays[i].relayState ? true : false)); saveState( i, Relays[i].relayState ); } // save sensor state in EEPROM (location == sensor number) Relays[i].oldValue = value; } if (blockMotionTimer == 0) { if (motionsDebouncer.update()) { int value = motionsDebouncer.read(); Serial.println( "PIR " + (String)value ); send( msgMOTION.set( value ), true ); // Also it might need inverted value blockMotionTimer = (millis() + SLEEP_TIME); } } else { motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires if (blockMotionTimer < millis()) { blockMotionTimer = 0; } } updateTemperature(30);// update time in seconds } void updateTemperature(int frequency) { static unsigned long lastUpdateTime; if (millis() - lastUpdateTime >= frequency * 1000UL) { sensors.requestTemperatures(); for (int i=0; i<numSensors && i< MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { send(msg.setSensor(i).set(temperature,1)); lastTemperature[i]=temperature; } } lastUpdateTime += frequency * 1000UL; } } //void incomingMessage(const MyMessage &message) void receive(const MyMessage &message){ if (message.type == V_LIGHT) { if (message.sensor < noRelays) // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] { Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } //gw.wait(50); } -
you don't need to use begin() with 2.x. It's explained in the link above ;) Take a look at the examples in case
-
you don't need to use begin() with 2.x. It's explained in the link above ;) Take a look at the examples in case
-
you don't need to use begin() with 2.x. It's explained in the link above ;) Take a look at the examples in case
@scalz I changed everything as mentioned before. I uploaded to my Nano but I see nothing in the serial monitor. I hope it is my last queation:
the code is until now:In// 2x binary switch + dallas temp example // Connect button or door/window reed switch between // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND. // Connect dallas temp sensor to digital pin 3 #include <MySensors.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> #define MY_RADIO_NRF24 #define DIGITAL_MOTION_SENSOR 2 #define CHILD_ID_MOTION 6 #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define RELAY_ON 1 // switch around for realy HIGH/LOW state #define RELAY_OFF 0 Bounce motionsDebouncer = Bounce(); int lastMOTION; unsigned long SLEEP_TIME = 420000; //unsigned long SLEEP_TIME = 3000; unsigned long blockMotionTimer = 0; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //#define noRelays 3 //const int relayPin[] = {A1, A4, A5}; // switch around pins to your desire //const int buttonPin[] = {6, 7, 8}; // switch around pins to your desire #define noRelays 1 const int relayPin[] = {A1}; // switch around pins to your desire const int buttonPin[] = {6}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay byte oldValue; // last Values for key (debounce) boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncerRELAY[noRelays]; MyMessage msgRELAY[noRelays]; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; boolean metric = true; MyMessage msg(0,V_TEMP); MyMessage msgMOTION(CHILD_ID_MOTION, V_TRIPPED); void presentation() { //sensors.begin(); //begin receive, AUTO, true; sendSketchInfo("Motion/Temp/relay", "1.0"); numSensors = sensors.getDeviceCount(); for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input motionsDebouncer.attach(DIGITAL_MOTION_SENSOR); motionsDebouncer.interval(400); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true); for (int i = 0; i < noRelays; i++) { Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msgRELAY[i].sensor = i; // initialize messages msgRELAY[i].type = V_LIGHT; debouncerRELAY[i] = Bounce(); // initialize debouncer debouncerRELAY[i].attach(buttonPin[i]); debouncerRELAY[i].interval(5); pinMode(Relays[i].buttonPin, INPUT_PULLUP); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status present(i, S_LIGHT); // present sensor to gateway delay(250); } } void loop() { for (byte i = 0; i < noRelays; i++) { debouncerRELAY[i].update(); byte value = debouncerRELAY[i].read(); if (value != Relays[i].oldValue && value == 0) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msgRELAY[i].set(Relays[i].relayState ? true : false)); saveState( i, Relays[i].relayState ); } // save sensor state in EEPROM (location == sensor number) Relays[i].oldValue = value; } if (blockMotionTimer == 0) { if (motionsDebouncer.update()) { int value = motionsDebouncer.read(); Serial.println( "PIR " + (String)value ); send( msgMOTION.set( value ), true ); // Also it might need inverted value blockMotionTimer = (millis() + SLEEP_TIME); } } else { motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires if (blockMotionTimer < millis()) { blockMotionTimer = 0; } } updateTemperature(30);// update time in seconds } void updateTemperature(int frequency) { static unsigned long lastUpdateTime; if (millis() - lastUpdateTime >= frequency * 1000UL) { sensors.requestTemperatures(); for (int i=0; i<numSensors && i< MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { send(msg.setSensor(i).set(temperature,1)); lastTemperature[i]=temperature; } } lastUpdateTime += frequency * 1000UL; } } //void incomingMessage(const MyMessage &message) void receive(const MyMessage &message){ if (message.type == V_LIGHT) { if (message.sensor < noRelays) // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] { Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } //gw.wait(50); } ` -
in the link i provided above, at the 2nd line..
"It is important to do these defines before including MySensors.h, otherwise they will go unnoticed when the library evaluates which parts to include."
-
@scalz I changed everything as mentioned before. I uploaded to my Nano but I see nothing in the serial monitor. I hope it is my last queation:
the code is until now:In// 2x binary switch + dallas temp example // Connect button or door/window reed switch between // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND. // Connect dallas temp sensor to digital pin 3 #include <MySensors.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> #define MY_RADIO_NRF24 #define DIGITAL_MOTION_SENSOR 2 #define CHILD_ID_MOTION 6 #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define RELAY_ON 1 // switch around for realy HIGH/LOW state #define RELAY_OFF 0 Bounce motionsDebouncer = Bounce(); int lastMOTION; unsigned long SLEEP_TIME = 420000; //unsigned long SLEEP_TIME = 3000; unsigned long blockMotionTimer = 0; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //#define noRelays 3 //const int relayPin[] = {A1, A4, A5}; // switch around pins to your desire //const int buttonPin[] = {6, 7, 8}; // switch around pins to your desire #define noRelays 1 const int relayPin[] = {A1}; // switch around pins to your desire const int buttonPin[] = {6}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay byte oldValue; // last Values for key (debounce) boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncerRELAY[noRelays]; MyMessage msgRELAY[noRelays]; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; boolean receivedConfig = false; boolean metric = true; MyMessage msg(0,V_TEMP); MyMessage msgMOTION(CHILD_ID_MOTION, V_TRIPPED); void presentation() { //sensors.begin(); //begin receive, AUTO, true; sendSketchInfo("Motion/Temp/relay", "1.0"); numSensors = sensors.getDeviceCount(); for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input motionsDebouncer.attach(DIGITAL_MOTION_SENSOR); motionsDebouncer.interval(400); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true); for (int i = 0; i < noRelays; i++) { Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msgRELAY[i].sensor = i; // initialize messages msgRELAY[i].type = V_LIGHT; debouncerRELAY[i] = Bounce(); // initialize debouncer debouncerRELAY[i].attach(buttonPin[i]); debouncerRELAY[i].interval(5); pinMode(Relays[i].buttonPin, INPUT_PULLUP); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status present(i, S_LIGHT); // present sensor to gateway delay(250); } } void loop() { for (byte i = 0; i < noRelays; i++) { debouncerRELAY[i].update(); byte value = debouncerRELAY[i].read(); if (value != Relays[i].oldValue && value == 0) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msgRELAY[i].set(Relays[i].relayState ? true : false)); saveState( i, Relays[i].relayState ); } // save sensor state in EEPROM (location == sensor number) Relays[i].oldValue = value; } if (blockMotionTimer == 0) { if (motionsDebouncer.update()) { int value = motionsDebouncer.read(); Serial.println( "PIR " + (String)value ); send( msgMOTION.set( value ), true ); // Also it might need inverted value blockMotionTimer = (millis() + SLEEP_TIME); } } else { motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires if (blockMotionTimer < millis()) { blockMotionTimer = 0; } } updateTemperature(30);// update time in seconds } void updateTemperature(int frequency) { static unsigned long lastUpdateTime; if (millis() - lastUpdateTime >= frequency * 1000UL) { sensors.requestTemperatures(); for (int i=0; i<numSensors && i< MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { send(msg.setSensor(i).set(temperature,1)); lastTemperature[i]=temperature; } } lastUpdateTime += frequency * 1000UL; } } //void incomingMessage(const MyMessage &message) void receive(const MyMessage &message){ if (message.type == V_LIGHT) { if (message.sensor < noRelays) // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] { Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } //gw.wait(50); } `@Dick I hope it is the last time to keep you involved, still nothing in the serial Monitor. For a test I loaded an Example and that showed up in the monitor. Any idea?
In// 2x binary switch + dallas temp example // Connect button or door/window reed switch between // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND. // Connect dallas temp sensor to digital pin 3 #define MY_RADIO_NRF24 #define DIGITAL_MOTION_SENSOR 2 #define CHILD_ID_MOTION 6 #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define noRelays 1 #define RELAY_ON 1 // switch around for realy HIGH/LOW state #define RELAY_OFF 0 #include <MySensors.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> Bounce motionsDebouncer = Bounce(); int lastMOTION; unsigned long SLEEP_TIME = 420000; //unsigned long SLEEP_TIME = 3000; unsigned long blockMotionTimer = 0; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //#define noRelays 3 //const int relayPin[] = {A1, A4, A5}; // switch around pins to your desire //const int buttonPin[] = {6, 7, 8}; // switch around pins to your desire const int relayPin[] = {A1}; // switch around pins to your desire const int buttonPin[] = {6}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay byte oldValue; // last Values for key (debounce) boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncerRELAY[noRelays]; MyMessage msgRELAY[noRelays]; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors = 0; boolean receivedConfig = false; boolean metric = true; MyMessage msg(0, V_TEMP); MyMessage msgMOTION(CHILD_ID_MOTION, V_TRIPPED); void presentation() { //sensors.begin(); //begin receive, AUTO, true; sendSketchInfo("Motion/Temp/relay", "1.0"); numSensors = sensors.getDeviceCount(); for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input motionsDebouncer.attach(DIGITAL_MOTION_SENSOR); motionsDebouncer.interval(400); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true); for (int i = 0; i < noRelays; i++) { Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msgRELAY[i].sensor = i; // initialize messages msgRELAY[i].type = V_LIGHT; debouncerRELAY[i] = Bounce(); // initialize debouncer debouncerRELAY[i].attach(buttonPin[i]); debouncerRELAY[i].interval(5); pinMode(Relays[i].buttonPin, INPUT_PULLUP); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status present(i, S_LIGHT); // present sensor to gateway delay(250); } } void loop() { for (byte i = 0; i < noRelays; i++) { debouncerRELAY[i].update(); byte value = debouncerRELAY[i].read(); if (value != Relays[i].oldValue && value == 0) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msgRELAY[i].set(Relays[i].relayState ? true : false)); saveState( i, Relays[i].relayState ); } // save sensor state in EEPROM (location == sensor number) Relays[i].oldValue = value; } if (blockMotionTimer == 0) { if (motionsDebouncer.update()) { int value = motionsDebouncer.read(); Serial.println( "PIR " + (String)value ); send( msgMOTION.set( value ), true ); // Also it might need inverted value blockMotionTimer = (millis() + SLEEP_TIME); } } else { motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires if (blockMotionTimer < millis()) { blockMotionTimer = 0; } } updateTemperature(30);// update time in seconds } void updateTemperature(int frequency) { static unsigned long lastUpdateTime; if (millis() - lastUpdateTime >= frequency * 1000UL) { sensors.requestTemperatures(); for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { send(msg.setSensor(i).set(temperature, 1)); lastTemperature[i] = temperature; } } lastUpdateTime += frequency * 1000UL; } } //void incomingMessage(const MyMessage &message) void receive(const MyMessage &message) { if (message.type == V_LIGHT) { if (message.sensor < noRelays) // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] { Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } //gw.wait(50); } ` -
@Dick I hope it is the last time to keep you involved, still nothing in the serial Monitor. For a test I loaded an Example and that showed up in the monitor. Any idea?
In// 2x binary switch + dallas temp example // Connect button or door/window reed switch between // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND. // Connect dallas temp sensor to digital pin 3 #define MY_RADIO_NRF24 #define DIGITAL_MOTION_SENSOR 2 #define CHILD_ID_MOTION 6 #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 #define noRelays 1 #define RELAY_ON 1 // switch around for realy HIGH/LOW state #define RELAY_OFF 0 #include <MySensors.h> #include <SPI.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> Bounce motionsDebouncer = Bounce(); int lastMOTION; unsigned long SLEEP_TIME = 420000; //unsigned long SLEEP_TIME = 3000; unsigned long blockMotionTimer = 0; OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); //#define noRelays 3 //const int relayPin[] = {A1, A4, A5}; // switch around pins to your desire //const int buttonPin[] = {6, 7, 8}; // switch around pins to your desire const int relayPin[] = {A1}; // switch around pins to your desire const int buttonPin[] = {6}; // switch around pins to your desire class Relay // relay class, store all relevant data (equivalent to struct) { public: int buttonPin; // physical pin number of button int relayPin; // physical pin number of relay byte oldValue; // last Values for key (debounce) boolean relayState; // relay status (also stored in EEPROM) }; Relay Relays[noRelays]; Bounce debouncerRELAY[noRelays]; MyMessage msgRELAY[noRelays]; float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors = 0; boolean receivedConfig = false; boolean metric = true; MyMessage msg(0, V_TEMP); MyMessage msgMOTION(CHILD_ID_MOTION, V_TRIPPED); void presentation() { //sensors.begin(); //begin receive, AUTO, true; sendSketchInfo("Motion/Temp/relay", "1.0"); numSensors = sensors.getDeviceCount(); for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } pinMode(DIGITAL_MOTION_SENSOR, INPUT_PULLUP); // sets the motion sensor digital pin as input motionsDebouncer.attach(DIGITAL_MOTION_SENSOR); motionsDebouncer.interval(400); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_MOTION, S_MOTION, "Motion needs presentation", true); for (int i = 0; i < noRelays; i++) { Relays[i].buttonPin = buttonPin[i]; // assign physical pins Relays[i].relayPin = relayPin[i]; msgRELAY[i].sensor = i; // initialize messages msgRELAY[i].type = V_LIGHT; debouncerRELAY[i] = Bounce(); // initialize debouncer debouncerRELAY[i].attach(buttonPin[i]); debouncerRELAY[i].interval(5); pinMode(Relays[i].buttonPin, INPUT_PULLUP); pinMode(Relays[i].relayPin, OUTPUT); Relays[i].relayState = loadState(i); // retrieve last values from EEPROM digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly send(msgRELAY[i].set(Relays[i].relayState ? true : false)); // make controller aware of last status present(i, S_LIGHT); // present sensor to gateway delay(250); } } void loop() { for (byte i = 0; i < noRelays; i++) { debouncerRELAY[i].update(); byte value = debouncerRELAY[i].read(); if (value != Relays[i].oldValue && value == 0) { Relays[i].relayState = !Relays[i].relayState; digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); send(msgRELAY[i].set(Relays[i].relayState ? true : false)); saveState( i, Relays[i].relayState ); } // save sensor state in EEPROM (location == sensor number) Relays[i].oldValue = value; } if (blockMotionTimer == 0) { if (motionsDebouncer.update()) { int value = motionsDebouncer.read(); Serial.println( "PIR " + (String)value ); send( msgMOTION.set( value ), true ); // Also it might need inverted value blockMotionTimer = (millis() + SLEEP_TIME); } } else { motionsDebouncer.update(); // dummy update to prevent false trigger after timer expires if (blockMotionTimer < millis()) { blockMotionTimer = 0; } } updateTemperature(30);// update time in seconds } void updateTemperature(int frequency) { static unsigned long lastUpdateTime; if (millis() - lastUpdateTime >= frequency * 1000UL) { sensors.requestTemperatures(); for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { send(msg.setSensor(i).set(temperature, 1)); lastTemperature[i] = temperature; } } lastUpdateTime += frequency * 1000UL; } } //void incomingMessage(const MyMessage &message) void receive(const MyMessage &message) { if (message.type == V_LIGHT) { if (message.sensor < noRelays) // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]] { Relays[message.sensor].relayState = message.getBool(); digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number) } } //gw.wait(50); } ` -
@Dick Add the following to the beginning of your sketch to enable serial print:
#define MY_DEBUG
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