Combining relay and temperature sketch
-
I use one temp sensor. Ok i try correct but i dont know how correct it for send...please help me for sending...
And please help me with this:
Combining relays, debounced buttons and sleep() most likely will cause problems. Better use a non-blocking loop as described in post #45 and following./** DESCRIPTION Sketch for 2x relay with buttons monostable. After back power all relays set OFF and send correct status OFF to controller. */ // Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_RFM69 #define MY_RFM69_FREQUENCY RF69_868MHZ #define MY_IS_RFM69HW // Enabled repeater feature for this node #define MY_REPEATER_FEATURE // Node id defaults to AUTO (tries to fetch id from controller) #define MY_NODE_ID AUTO #include <SPI.h> #include <MySensors.h> #include <Bounce2.h> #include <DallasTemperature.h> #include <OneWire.h> // Define Relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay // Define Sensor ID's #define SSR_A_ID 1 // Id of the sensor child #define SSR_B_ID 2 // Id of the sensor child // Define buttons and relays const int buttonPinA = 3; const int buttonPinB = 4; const int relayPinA = 5; const int relayPinB = 6; // Define Variables int oldValueA = 0; int oldValueB = 0; bool stateA = false; bool stateB = false; int trigger = 0; // Define Sensors Temperature #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No #define ONE_WIRE_BUS 7 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; bool receivedConfig = false; bool metric = true; Bounce debouncerA = Bounce(); Bounce debouncerB = Bounce(); MyMessage msgA(SSR_A_ID, V_STATUS); MyMessage msgB(SSR_B_ID, V_STATUS); // Initialize temperature message MyMessage msg(0,V_TEMP); void before() { // Startup up the OneWire library sensors.begin(); } void setup() { pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up // After setting up the buttons, setup debouncer debouncerA.attach(buttonPinA); debouncerA.interval(5); debouncerB.attach(buttonPinB); debouncerB.interval(5); // Make sure relays are off when starting up digitalWrite(relayPinA, RELAY_OFF); digitalWrite(relayPinB, RELAY_OFF); // Then set relay pins in output mode pinMode(relayPinA, OUTPUT); pinMode(relayPinB, OUTPUT); // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("2xRelay monostable + temp. DS18b20", "2.1.1"); // Register all sensors to gw (they will be created as child devices) present(SSR_A_ID, S_LIGHT); present(SSR_B_ID, S_LIGHT); // Fetch the number of attached temperature sensors numSensors = sensors.getDeviceCount(); // Present all sensors to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i+10, S_TEMP); } } void loop() { if (trigger == 0){ send(msgA.set(false)); // Send off state for relayA to ensure controller knows the switch is off send(msgB.set(false)); // Send off state for relayB to ensure controller knows the switch is off trigger = 1; } debouncerA.update(); // Get the update value int valueA = debouncerA.read(); if (valueA != oldValueA && valueA == 0) { send(msgA.set(stateA ? false : true), true); // Send new state and request ack back } oldValueA = valueA; debouncerB.update(); // Get the update value int valueB = debouncerB.read(); if (valueB != oldValueB && valueB == 0) { send(msgB.set(stateB ? false : true), true); // Send new state and request ack back } oldValueB = valueB; // Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // query conversion time and sleep until conversion completed int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution()); // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater) sleep(conversionTime); // Read temperatures and send them to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; // Only send data if temperature has changed and no error #if COMPARE_TEMP == 1 if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { #else if (temperature != -127.00 && temperature != 85.00) { #endif // Send in the new temperature send(msg.setSensor(i).set(temperature,1)); // Save new temperatures for next compare lastTemperature[i]=temperature; } } sleep(SLEEP_TIME); } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type == V_STATUS) { switch (message.sensor) { case 1: stateA = message.getBool(); digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF); break; case 2: stateB = message.getBool(); digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF); break; } // Write some debug info 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()); } }``` -
- Wrt sending: your controller should show one temperature sensor with Child ID 10. As a consequence, the temp value should also be reported using this ID. So you should adopt this line appropriately, just use the same logic as in presentation() :grin: .
send(msg.setSensor(i).set(temperature,1));- For the non-blocking loop: As already mentionned, within this thread you just should follow @bluezr1 steps and some of my remarks :grinning: .
The sketch in #63 should be a working solution, but when using dallas temp instead of the DHT, you have also to change the sleep(conversionTime) to wait().
-
@pepson Especially as a beginner you really should try to follow the necessary steps to develop combined sketches - it's easier than you may think by now.
Otherwise you may run into the next unexpected trouble without any idea, how things fit together and - even more important - how to debug (or even where to attach what). So asking for ready sketches is not the best idea.If you are still convinced about the need of a ready sketch: There's - amongst others - a sketch with #101 using DS18B20 and 4 relays in my repo (link is already available in this thread). , BUT: this is work in progress, may use a modified version of the dallas lib and reports temperatures and relays under the same ChildID - my controller (FHEM) seems not to care about that...
As I changed communication to RS485 last weekend, I most likely will not do any update, if there are issues or further enhancements on the code (I plan to do some). -
@rejoe2 said in Combining relay and temperature sketch:
#10
I try also ready sketch for only dallas fom mysensors.rg but it also not working:
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_RFM69 #define MY_RFM69_FREQUENCY RF69_868MHZ #define MY_IS_RFM69HW // Enabled repeater feature for this node #define MY_REPEATER_FEATURE // Node id defaults to AUTO (tries to fetch id from controller) #define MY_NODE_ID AUTO #include <SPI.h> #include <MySensors.h> #include <DallasTemperature.h> #include <OneWire.h> #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected #define MAX_ATTACHED_DS18B20 16 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=0; bool receivedConfig = false; bool metric = true; // Initialize temperature message MyMessage msg(0,V_TEMP); void before() { // Startup up the OneWire library sensors.begin(); } void setup() { // requestTemperatures() will not block current thread sensors.setWaitForConversion(false); } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Temperature Sensor", "1.1"); // Fetch the number of attached temperature sensors numSensors = sensors.getDeviceCount(); // Present all sensors to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { present(i, S_TEMP); } } void loop() { // Fetch temperatures from Dallas sensors sensors.requestTemperatures(); // query conversion time and sleep until conversion completed int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution()); // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater) sleep(conversionTime); // Read temperatures and send them to controller for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { // Fetch and round temperature to one decimal float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; // Only send data if temperature has changed and no error #if COMPARE_TEMP == 1 if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) { #else if (temperature != -127.00 && temperature != 85.00) { #endif // Send in the new temperature send(msg.setSensor(i).set(temperature,1)); // Save new temperatures for next compare lastTemperature[i]=temperature; } } sleep(SLEEP_TIME); }``` -
@pepson
Imo there are several things to look at:-
Pin 3 seems to be used for button a + 1-wire? -> use a different one
-
How many DS18B20 do you have attached?
a) In case it's up to three, you try to report Relay and Temp info partly under the same Child_ID (1+2). Imo, it's better to chose a different starting Child ID, something like "present(i+10, S_TEMP)" or use a variable like "FIRST_TEMP_CHILD" (also use the same scheme for sending).
b) You may also suffer from a wiring problem, if you are able to compile and flash this sketch. Just for testing you could add a debugg-message to serial with the numSensors -
Combining relays, debounced buttons and sleep() most likely will cause problems. Better use a non-blocking loop as described in post #45 and following.
@rejoe2 said in Combining relay and temperature sketch:
b) You may also suffer from a wiring problem, if you are able to compile and flash this sketch. Just for testing you could add a debugg-message to serial with the numSensors
Did you follow the instructions as described in the "build" section, attached the data line to PIN3 and used an appropriate resistor (measure it's value) ? Then it's most likely a damaged sensor...
-
-
@rejoe2 said in Combining relay and temperature sketch:
b) You may also suffer from a wiring problem, if you are able to compile and flash this sketch. Just for testing you could add a debugg-message to serial with the numSensors
Did you follow the instructions as described in the "build" section, attached the data line to PIN3 and used an appropriate resistor (measure it's value) ? Then it's most likely a damaged sensor...
-
@rejoe2
Yes i connected my sensors to PIN 3 and use resistor 4.7
But in Domoticz on gateway not show any CHILDS for this NODE.@pepson Seems like the sensor is not detected, so the presentation() will have zero sensors to present...
If you do not want to follow the proposal to print the numSensors to serial (most likely: 0) you should try to test the DS18B20-Hardware by using an example sketch (e.g. Simple.pde) from the dallas temperature lib (without the MySensors-framework) and have a look on the serial console of the IDE. Be aware: "as is", it uses PIN2 and a different baudrate.
If there are no temperature values printed, it's either still a wiring problem or the sensor's just broken. -
Has anyone had any progress on sensor node with mysensors + multiple dallas temp sensors and multiple relays, combining the two codes? Any help would be greatly appreciated. I have been playing with this for a little more than a week, have tried everything suggested but still not working. A push in the right direction would be great!
-
@mghaff said in Combining relay and temperature sketch:
Has anyone had any progress on sensor node with mysensors + multiple dallas temp sensors and multiple relays, combining the two codes?
Some older code; some rework might be necessary: https://github.com/rejoe2/MySensors_Small/blob/master/MyS101/MyS101.ino
There are some more DS18B20 examples in the repo, just have a look around... -
@mghaff said in Combining relay and temperature sketch:
Has anyone had any progress on sensor node with mysensors + multiple dallas temp sensors and multiple relays, combining the two codes?
Some older code; some rework might be necessary: https://github.com/rejoe2/MySensors_Small/blob/master/MyS101/MyS101.ino
There are some more DS18B20 examples in the repo, just have a look around...