Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. 2 dallas temp + 4 relays

2 dallas temp + 4 relays

Scheduled Pinned Locked Moved Troubleshooting
39 Posts 6 Posters 3.2k Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • dzjrD dzjr

    @mghaff my sketch is working good, dallas and moisture readings are good, and i can control my relays.

    rejoe2R Offline
    rejoe2R Offline
    rejoe2
    wrote on last edited by
    #5

    @dzjr Didn't test that to be honest, but nevertheless, imho it is not a state of the art code design to use wait() nor is it best practice to use much hard-coded steps to do the same thing over and over. Rather use millis() for non-blocking coding and for-loops over comparable stuff, write to and read results from arrays. This keeps coding much easier to read and understand once you got the idea how it works.
    But what in the end counts: It works...

    Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

    dzjrD 1 Reply Last reply
    0
    • dzjrD dzjr

      @mghaff my sketch is working good, dallas and moisture readings are good, and i can control my relays.

      MGHaffM Offline
      MGHaffM Offline
      MGHaff
      wrote on last edited by
      #6

      @dzjr I tried to use your sketch to get mine to work and it does not. I might just rebuild it all with new everything and try it again.

      1 Reply Last reply
      0
      • MGHaffM Offline
        MGHaffM Offline
        MGHaff
        wrote on last edited by
        #7

        Would this be my blocking loop?

        void presentation() {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("Fireplace interface", "1.1");
          //wait(LONG_WAIT);
        
          // 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+5, S_TEMP,"Fireplace Temp");
          //wait(SHORT_WAIT);
          }  
          for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
           // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
          //wait(SHORT_WAIT);
          }
        }
        
        rejoe2R 1 Reply Last reply
        0
        • MGHaffM MGHaff

          Would this be my blocking loop?

          void presentation() {
            // Send the sketch version information to the gateway and Controller
            sendSketchInfo("Fireplace interface", "1.1");
            //wait(LONG_WAIT);
          
            // 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+5, S_TEMP,"Fireplace Temp");
            //wait(SHORT_WAIT);
            }  
            for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
             // Register all sensors to gw (they will be created as child devices)
              present(sensor, S_BINARY);
            //wait(SHORT_WAIT);
            }
          }
          
          rejoe2R Offline
          rejoe2R Offline
          rejoe2
          wrote on last edited by
          #8

          @mghaff Looks quite ok in the presentation() function.
          You do not need the "pin" variable in presentation, so you might shorten that to:

          for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) {
             // Register all sensors to gw (they will be created as child devices)
              present(sensor, S_BINARY);
           }
          

          The "pin++" is needed in case you want to switch also the relays to a specific state. If you also want a completely flexible pin-assignment see sketch in https://forum.mysensors.org/topic/4847/multi-button-relay-sketch/33# as a very good example. There also the corresponding PIN is read from an predefined array (has some quite interesting button features also).

          Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

          1 Reply Last reply
          0
          • MGHaffM Offline
            MGHaffM Offline
            MGHaff
            wrote on last edited by MGHaff
            #9

            I tried to combine a couple other sketches and the closest I got was this one. But it still does not give me values for my Dallas temp. if I try each sketch separate they work fine and present fine with readings. I am a newbie and have luck, yes i call it luck cause my other sensors work just fine.

            My debug is


            | / |_ / | ___ _ __ ___ ___ _ __ ___
            | |/| | | | _
            \ / _ \ _ \/ __|/ _ \|
            _/ __|
            | | | | |
            | || | / | | _ \ _ | | _
            |
            | |
            |_
            , |/ ___|| ||/_/|| |/
            |
            __/ 2.3.1

            16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1
            26 MCO:BGN:BFR
            120 TSM:INIT
            121 TSF:WUR:MS=0
            129 TSM:INIT:TSP OK
            130 TSM:INIT:STATID=8
            136 TSF:SID:OK,ID=8
            138 TSM:FPAR
            174 TSF:MSG:SEND,8-8-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
            448 TSF:MSG:READ,0-0-8,s=255,c=3,t=8,pt=1,l=1,sg=0:0
            452 TSF:MSG:FPAR OK,ID=0,D=1
            2181 TSM:FPAR:OK
            2182 TSM:ID
            2183 TSM:ID:OK
            2185 TSM:UPL
            2188 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
            2195 TSF:MSG:READ,0-0-8,s=255,c=3,t=25,pt=1,l=1,sg=0:1
            2200 TSF:MSG:PONG RECV,HP=1
            2203 TSM:UPL:OK
            2204 TSM:READY:ID=8,PAR=0,DIS=1
            2209 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
            2217 TSF:MSG:READ,0-0-8,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
            2225 TSF:MSG:SEND,8-8-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
            2233 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
            2250 TSF:MSG:READ,0-0-8,s=255,c=3,t=6,pt=0,l=1,sg=0:M
            2257 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
            2268 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.2
            2276 TSF:MSG:SEND,8-8-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2283 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2290 TSF:MSG:SEND,8-8-0-0,s=2,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2298 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2306 TSF:MSG:SEND,8-8-0-0,s=3,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2314 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2321 TSF:MSG:SEND,8-8-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2328 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2336 TSF:MSG:SEND,8-8-0-0,s=2,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2343 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2352 TSF:MSG:SEND,8-8-0-0,s=3,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
            2359 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
            2365 MCO:REG:REQ
            2368 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
            2374 TSF:MSG:READ,0-0-8,s=255,c=3,t=27,pt=1,l=1,sg=0:1
            2379 MCO:PIM:NODE REG=1
            2382 MCO:BGN:STP
            Hello world, I am a sensor.
            2383 MCO:BGN:INIT OK,TSP=1
            Starting new measurement(s)
            Sensor #0 says it is -127.00 degrees
            Sensor #1 says it is -127.00 degrees
            zzzzZZZZzzzzZZZZzzzz

            /**
               The MySensors Arduino library handles the wireless radio link and protocol
               between your home built sensors/actuators and HA controller of choice.
               The sensors forms a self healing radio network with optional repeaters. Each
               repeater and gateway builds a routing tables in EEPROM which keeps track of the
               network topology allowing messages to be routed to nodes.
            
               Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
               Copyright (C) 2013-2015 Sensnology AB
               Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
            
               Documentation: http://www.mysensors.org
               Support Forum: http://forum.mysensors.org
            
               This program is free software; you can redistribute it and/or
               modify it under the terms of the GNU General Public License
               version 2 as published by the Free Software Foundation.
            
             *******************************
            
               DESCRIPTION
            
               Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
               http://www.mysensors.org/build/temp
            
               The cool thing about this temperature sensor (pun intended) is thay you can attach multiple Dallas temperature sensors outputs to the same arduino pin. They will all automatically be recognised as separate sensors.
            
               At the moment of writing (februari 2017) you need older versions of the Dallas and OneWire libraries. Please check the website or forum to see if this is still the case.
            
               Modifications by anonymous user so that it can now simultaneously function as a MySensors repeater.
            */
            
            
            // Enable debug prints to serial monitor
            #define MY_DEBUG
            
            // Enable and select radio type attached
            #define MY_RADIO_RF24                            // A 2.4Ghz transmitter and receiver, often used with MySensors.
            //#define MY_RF24_PA_LEVEL RF24_PA_MIN              // This sets a low-power mode for the radio. Useful if you use the verison with the bigger antenna, but don' want to power that from a separate source. It can also fix problems with fake Chinese versions of the radio.
            //#define MY_RADIO_RFM69                          // 433Mhz transmitter and reveiver.
            #define MY_NODE_ID 8
            // Choose if you want this sensor to also be a repeater.
            // #define MY_REPEATER_FEATURE                    // Just remove the two slashes at the beginning of this line to also enable this sensor to act as a repeater for other sensors. If this node is on battery power, you probably shouldn't enable this.
            // Are you using this sensor on battery power?
            //#define BATTERY_POWERED                   // Just remove the two slashes at the beginning of this line if your node is battery powered. It will then go into deep sleep as much as possible. But when it' sleeping it can' work as a repeater.
            
            #include <SPI.h>
            #include <MySensors.h>
            #include <DallasTemperature.h>
            #include <OneWire.h>
            
            
            // These defines and variables can be changed:
            #define COMPARE_TEMP 1                            // Send temperature only if changed? 1 = Yes 0 = No. Can save battery.
            #define ONE_WIRE_BUS 3                            // Pin where Dallas sensor(s) is/are connected.
            #define MAX_ATTACHED_DS18B20 16                   // Maximum amount of teperature sensors you can connect to this arduino (16).
            #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
            #define NUMBER_OF_RELAYS 3 // Total number of attached relays
            #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
            
            
            unsigned long measurementInterval = 30000;        // Time to wait between reads (in milliseconds).
            
            // You should not change these:
            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];      // creates an array to hold the previous temperature measurements for each possible sensor.
            int numSensors = 0;                               // variable to contain the number of found attached sensors.
            boolean metric = true;                            // old Mysensors??
            unsigned long measurementSleepTime = 0;           // variable to store the Sleep time if the node is battery powered.
            
            
            // Mysensors settings
            MyMessage msg(0, V_TEMP);                         // Sets up the message format that we'l be sending to the MySensors gateway later.
            
            
            void before()
            {
              sensors.begin();                              // Startup up the OneWire library. It allows multiple sensors to talk over one wire (one pin).
              for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
                // Then set relay pins in output mode
                pinMode(pin, OUTPUT);
                // Set relay to last known state (using eeprom storage)
                digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF);
              }
            }
            void setup()
            {
              for (int i = 0; i < MAX_ATTACHED_DS18B20; i++) {
                lastTemperature[i] = 0;  //Pre-filling array with 0's.
              }
              sensors.setWaitForConversion(false);            // requestTemperatures() will not block current thread
            
            #ifdef BATTERY_POWERED
              measurementSleepTime = measurementInterval;
              measurementInterval = 1;                    // We'll let Sleep take over the scheduling. When the arduino is asleep, millis doesn't increment anymore (time stops as it were). To fix this, we'l set the measurement interval time to 1, so that when the arduino wakes up it will immediately try to measure again.
            #endif
            
              Serial.begin(115200);                           // for serial debugging.
              Serial.print("Hello world, I am a sensor. \n ");
            
            }
            
            void presentation()
            {
              sendSketchInfo("Temperature Sensor", "1.2");    // Send the sketch version information to the gateway and Controller
              numSensors = sensors.getDeviceCount();          // Fetch the number of attached temperature sensors
              for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++)
              for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
                // Register all sensors to gw (they will be created as child devices)
                present(sensor, S_BINARY);
                present(i+21, S_TEMP);                          // Present all sensors to controller (16 maximum).
              }
            }
            
            void loop()
            {
            
              // You should not change these variables:
              static boolean isMeasuring = true;                // Used to indicate when the time is right for a new measurement to be made.
              static boolean isCalculating = false;                     // Used to bridge the time that is needed to calculate the temperature values by the Dallas library.
              static unsigned long currentMillis = 0;                   // The millisecond clock in the main loop.
              static unsigned long previousMeasurementMillis = 0;       // Used to remember the time of the last temperature measurement.
              static int16_t conversionTime = 0;                        // Used to store the time needed to calculate the temperature from measurements.
            
              currentMillis = millis(); // The time since the sensor started, counted in milliseconds. This script tries to avoid using the Sleep function, so that it could at the same time be a MySensors repeater.
            
              // Let's measure the temperature
              if (isMeasuring == true && currentMillis - previousMeasurementMillis >= measurementInterval) { // If we're not calculating, and enough time has passed, we'll start again.
                isMeasuring = false; // We're measuring, so let's take it off our to-do list.
                Serial.print("Starting new measurement(s)\n");
                previousMeasurementMillis = currentMillis; // Mark the time of the initialiation of this measurement.
            
                // Fetch temperatures from Dallas sensors
                sensors.requestTemperatures();
            
                // query conversion time. Apparently it takes a while to calculate.
                //CONVERSION_TIME = sensors.millisToWaitForConversion(sensors.getResolution());
                conversionTime = millisToWaitForConversion(sensors.getResolution());
                isCalculating = true; //Next step is to re-calculate the temperature again.
              }
            
            
              // Next, let's calculate and send the temperature
              if (isCalculating == true && currentMillis > previousMeasurementMillis + conversionTime ) {
                isCalculating = false; // check calculating off the to-do list too.
            
                for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { // Loop through all the attached temperatur sensors.
                  float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.; // Fetch and round temperature to one decimal
                  Serial.print("Sensor #");
                  Serial.print(i);
                  Serial.print(" says it is ");
                  Serial.print(temperature);
                  Serial.print(" degrees\n");
                  if (temperature != -127.00 && temperature != 85.00) { // avoid working with measurement errors.
                    if (COMPARE_TEMP == 1 && lastTemperature[i] == temperature) {
                      Serial.print("Not sending it though, because it's the same temperature as before.\n");
                    } else {
                      Serial.print("Sending the temperature to the gateway.\n");
                      send(msg.setSensor(i).set(temperature, 1));
                      lastTemperature[i] = temperature; // Save new temperatures to be able to compare in the next round.
                    }
                  }
                }
            
                // Both tasks are done. Time to wait until we should measure again.
                Serial.print("zzzzZZZZzzzzZZZZzzzz\n");
            
            #ifdef BATTERY_POWERED
                unsigned long quicktimecheck = millis(); // check how much time has passed during the measurement (can be up to 750 milliseconds), and then calculate from that how long to sleep until the next intended measuring time.
                unsigned long sleeptime = measurementSleepTime - (quicktimecheck - previousMeasurementMillis); //How much time has passed already during the calculating? Subtract that from the intended interval time.
                sleep (sleeptime);
            #endif
            
                isMeasuring = true;
              }
            }
            
            
            // This function helps to avoid a problem with the latest Dallas temperature library.
            int16_t millisToWaitForConversion(uint8_t bitResolution)
            {
              switch (bitResolution) {
                case 9:
                  return 94;
                case 10:
                  return 188;
                case 11:
                  return 375;
                default:
                  return 750;
              }
            }
            
            void receive(const MyMessage &message)
            {
              // We only expect one type of message from controller. But we better check anyway.
              if (message.type == V_STATUS) {
                // Change relay state
                digitalWrite(message.sensor - 1 + RELAY_PIN, message.getBool() ? RELAY_ON : RELAY_OFF);
                // Store state in eeprom
                saveState(message.sensor, message.getBool());
                // Write some debug info
                Serial.print("Incoming change for sensor:");
                Serial.print(message.sensor);
                Serial.print(", New status: ");
                Serial.println(message.getBool());
              }
            }
            
            /*Last note : celsius/farenheit in dallatemperature lib is not handled by the metric variable. To get the conversion you need to use C or F functions as below. I've not checked how you handle this in sketch.
            
                // returns temperature in degrees C
                float getTempC(const uint8_t*);
            
                // returns temperature in degrees F
                float getTempF(const uint8_t*);
            
                // Get temperature for device index (slow)
                float getTempCByIndex(uint8_t);
            
                // Get temperature for device index (slow)
              float getTempFByIndex(uint8_t);
            */
            dzjrD 1 Reply Last reply
            0
            • rejoe2R Offline
              rejoe2R Offline
              rejoe2
              wrote on last edited by
              #10

              Have a look at at least 2 things in the sketch:
              The for-loop(s) in presentation should be seperate ones for temp and relays.
              Imo you try to send out temperature using a different ChildID than in presentation().

              Additionally, -127 degrees indicates some wiring problems.

              Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

              MGHaffM 2 Replies Last reply
              0
              • rejoe2R rejoe2

                @mghaff: please avoid double postings. It would have been nice if you'd at least have made a link to this post in the other one you did on that topic here: https://forum.mysensors.org/post/95894.

                @both:
                Imo you should implement a "non-blocking" code, nut just add a sleep or wait at the end of loop or just delete that. Otherwise most likely either your DS18B20 will not deliver any meaningfull measurements or you will not be able to controll your relays...
                Please have a look at https://github.com/mysensors/MySensors/blob/master/examples/EnergyMeterPulseSensor/EnergyMeterPulseSensor.ino as a base example how to implement non-blocking code (path: sleepmode false) or have a look in the sketch I linked to in the answer to the other posting: https://forum.mysensors.org/post/95901

                dzjrD Offline
                dzjrD Offline
                dzjr
                wrote on last edited by dzjr
                #11

                @rejoe2 said in 2 dallas temp + 4 relays:

                ost in the other on

                thank you for your tip to adjust the sketch, I am now working with a node for the water meter (partly based on your example of GITHUB) and I think I understand now what you mean.

                I will adjust the sketch of the example!

                I still have a wait (3000); in "void before" to start just after the gateway, because I use an RS-485 network.

                rejoe2R 1 Reply Last reply
                0
                • rejoe2R rejoe2

                  @dzjr Didn't test that to be honest, but nevertheless, imho it is not a state of the art code design to use wait() nor is it best practice to use much hard-coded steps to do the same thing over and over. Rather use millis() for non-blocking coding and for-loops over comparable stuff, write to and read results from arrays. This keeps coding much easier to read and understand once you got the idea how it works.
                  But what in the end counts: It works...

                  dzjrD Offline
                  dzjrD Offline
                  dzjr
                  wrote on last edited by
                  #12

                  @rejoe2

                  To be honest I had to experiment a bit to get it working, but that is probably because I have little "experience" with Arduino code writing.

                  But fortunately here at MySensors we get a lot of help and tips at this forum.

                  1 Reply Last reply
                  0
                  • MGHaffM MGHaff

                    I tried to combine a couple other sketches and the closest I got was this one. But it still does not give me values for my Dallas temp. if I try each sketch separate they work fine and present fine with readings. I am a newbie and have luck, yes i call it luck cause my other sensors work just fine.

                    My debug is


                    | / |_ / | ___ _ __ ___ ___ _ __ ___
                    | |/| | | | _
                    \ / _ \ _ \/ __|/ _ \|
                    _/ __|
                    | | | | |
                    | || | / | | _ \ _ | | _
                    |
                    | |
                    |_
                    , |/ ___|| ||/_/|| |/
                    |
                    __/ 2.3.1

                    16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1
                    26 MCO:BGN:BFR
                    120 TSM:INIT
                    121 TSF:WUR:MS=0
                    129 TSM:INIT:TSP OK
                    130 TSM:INIT:STATID=8
                    136 TSF:SID:OK,ID=8
                    138 TSM:FPAR
                    174 TSF:MSG:SEND,8-8-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
                    448 TSF:MSG:READ,0-0-8,s=255,c=3,t=8,pt=1,l=1,sg=0:0
                    452 TSF:MSG:FPAR OK,ID=0,D=1
                    2181 TSM:FPAR:OK
                    2182 TSM:ID
                    2183 TSM:ID:OK
                    2185 TSM:UPL
                    2188 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
                    2195 TSF:MSG:READ,0-0-8,s=255,c=3,t=25,pt=1,l=1,sg=0:1
                    2200 TSF:MSG:PONG RECV,HP=1
                    2203 TSM:UPL:OK
                    2204 TSM:READY:ID=8,PAR=0,DIS=1
                    2209 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
                    2217 TSF:MSG:READ,0-0-8,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
                    2225 TSF:MSG:SEND,8-8-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
                    2233 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
                    2250 TSF:MSG:READ,0-0-8,s=255,c=3,t=6,pt=0,l=1,sg=0:M
                    2257 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
                    2268 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.2
                    2276 TSF:MSG:SEND,8-8-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2283 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2290 TSF:MSG:SEND,8-8-0-0,s=2,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2298 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2306 TSF:MSG:SEND,8-8-0-0,s=3,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2314 TSF:MSG:SEND,8-8-0-0,s=21,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2321 TSF:MSG:SEND,8-8-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2328 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2336 TSF:MSG:SEND,8-8-0-0,s=2,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2343 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2352 TSF:MSG:SEND,8-8-0-0,s=3,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK:
                    2359 TSF:MSG:SEND,8-8-0-0,s=22,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
                    2365 MCO:REG:REQ
                    2368 TSF:MSG:SEND,8-8-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
                    2374 TSF:MSG:READ,0-0-8,s=255,c=3,t=27,pt=1,l=1,sg=0:1
                    2379 MCO:PIM:NODE REG=1
                    2382 MCO:BGN:STP
                    Hello world, I am a sensor.
                    2383 MCO:BGN:INIT OK,TSP=1
                    Starting new measurement(s)
                    Sensor #0 says it is -127.00 degrees
                    Sensor #1 says it is -127.00 degrees
                    zzzzZZZZzzzzZZZZzzzz

                    /**
                       The MySensors Arduino library handles the wireless radio link and protocol
                       between your home built sensors/actuators and HA controller of choice.
                       The sensors forms a self healing radio network with optional repeaters. Each
                       repeater and gateway builds a routing tables in EEPROM which keeps track of the
                       network topology allowing messages to be routed to nodes.
                    
                       Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                       Copyright (C) 2013-2015 Sensnology AB
                       Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                    
                       Documentation: http://www.mysensors.org
                       Support Forum: http://forum.mysensors.org
                    
                       This program is free software; you can redistribute it and/or
                       modify it under the terms of the GNU General Public License
                       version 2 as published by the Free Software Foundation.
                    
                     *******************************
                    
                       DESCRIPTION
                    
                       Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
                       http://www.mysensors.org/build/temp
                    
                       The cool thing about this temperature sensor (pun intended) is thay you can attach multiple Dallas temperature sensors outputs to the same arduino pin. They will all automatically be recognised as separate sensors.
                    
                       At the moment of writing (februari 2017) you need older versions of the Dallas and OneWire libraries. Please check the website or forum to see if this is still the case.
                    
                       Modifications by anonymous user so that it can now simultaneously function as a MySensors repeater.
                    */
                    
                    
                    // Enable debug prints to serial monitor
                    #define MY_DEBUG
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_RF24                            // A 2.4Ghz transmitter and receiver, often used with MySensors.
                    //#define MY_RF24_PA_LEVEL RF24_PA_MIN              // This sets a low-power mode for the radio. Useful if you use the verison with the bigger antenna, but don' want to power that from a separate source. It can also fix problems with fake Chinese versions of the radio.
                    //#define MY_RADIO_RFM69                          // 433Mhz transmitter and reveiver.
                    #define MY_NODE_ID 8
                    // Choose if you want this sensor to also be a repeater.
                    // #define MY_REPEATER_FEATURE                    // Just remove the two slashes at the beginning of this line to also enable this sensor to act as a repeater for other sensors. If this node is on battery power, you probably shouldn't enable this.
                    // Are you using this sensor on battery power?
                    //#define BATTERY_POWERED                   // Just remove the two slashes at the beginning of this line if your node is battery powered. It will then go into deep sleep as much as possible. But when it' sleeping it can' work as a repeater.
                    
                    #include <SPI.h>
                    #include <MySensors.h>
                    #include <DallasTemperature.h>
                    #include <OneWire.h>
                    
                    
                    // These defines and variables can be changed:
                    #define COMPARE_TEMP 1                            // Send temperature only if changed? 1 = Yes 0 = No. Can save battery.
                    #define ONE_WIRE_BUS 3                            // Pin where Dallas sensor(s) is/are connected.
                    #define MAX_ATTACHED_DS18B20 16                   // Maximum amount of teperature sensors you can connect to this arduino (16).
                    #define RELAY_PIN 4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                    #define NUMBER_OF_RELAYS 3 // Total number of attached relays
                    #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
                    
                    
                    unsigned long measurementInterval = 30000;        // Time to wait between reads (in milliseconds).
                    
                    // You should not change these:
                    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];      // creates an array to hold the previous temperature measurements for each possible sensor.
                    int numSensors = 0;                               // variable to contain the number of found attached sensors.
                    boolean metric = true;                            // old Mysensors??
                    unsigned long measurementSleepTime = 0;           // variable to store the Sleep time if the node is battery powered.
                    
                    
                    // Mysensors settings
                    MyMessage msg(0, V_TEMP);                         // Sets up the message format that we'l be sending to the MySensors gateway later.
                    
                    
                    void before()
                    {
                      sensors.begin();                              // Startup up the OneWire library. It allows multiple sensors to talk over one wire (one pin).
                      for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
                        // Then set relay pins in output mode
                        pinMode(pin, OUTPUT);
                        // Set relay to last known state (using eeprom storage)
                        digitalWrite(pin, loadState(sensor) ? RELAY_ON : RELAY_OFF);
                      }
                    }
                    void setup()
                    {
                      for (int i = 0; i < MAX_ATTACHED_DS18B20; i++) {
                        lastTemperature[i] = 0;  //Pre-filling array with 0's.
                      }
                      sensors.setWaitForConversion(false);            // requestTemperatures() will not block current thread
                    
                    #ifdef BATTERY_POWERED
                      measurementSleepTime = measurementInterval;
                      measurementInterval = 1;                    // We'll let Sleep take over the scheduling. When the arduino is asleep, millis doesn't increment anymore (time stops as it were). To fix this, we'l set the measurement interval time to 1, so that when the arduino wakes up it will immediately try to measure again.
                    #endif
                    
                      Serial.begin(115200);                           // for serial debugging.
                      Serial.print("Hello world, I am a sensor. \n ");
                    
                    }
                    
                    void presentation()
                    {
                      sendSketchInfo("Temperature Sensor", "1.2");    // Send the sketch version information to the gateway and Controller
                      numSensors = sensors.getDeviceCount();          // Fetch the number of attached temperature sensors
                      for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++)
                      for (int sensor = 1, pin = RELAY_PIN; sensor <= NUMBER_OF_RELAYS; sensor++, pin++) {
                        // Register all sensors to gw (they will be created as child devices)
                        present(sensor, S_BINARY);
                        present(i+21, S_TEMP);                          // Present all sensors to controller (16 maximum).
                      }
                    }
                    
                    void loop()
                    {
                    
                      // You should not change these variables:
                      static boolean isMeasuring = true;                // Used to indicate when the time is right for a new measurement to be made.
                      static boolean isCalculating = false;                     // Used to bridge the time that is needed to calculate the temperature values by the Dallas library.
                      static unsigned long currentMillis = 0;                   // The millisecond clock in the main loop.
                      static unsigned long previousMeasurementMillis = 0;       // Used to remember the time of the last temperature measurement.
                      static int16_t conversionTime = 0;                        // Used to store the time needed to calculate the temperature from measurements.
                    
                      currentMillis = millis(); // The time since the sensor started, counted in milliseconds. This script tries to avoid using the Sleep function, so that it could at the same time be a MySensors repeater.
                    
                      // Let's measure the temperature
                      if (isMeasuring == true && currentMillis - previousMeasurementMillis >= measurementInterval) { // If we're not calculating, and enough time has passed, we'll start again.
                        isMeasuring = false; // We're measuring, so let's take it off our to-do list.
                        Serial.print("Starting new measurement(s)\n");
                        previousMeasurementMillis = currentMillis; // Mark the time of the initialiation of this measurement.
                    
                        // Fetch temperatures from Dallas sensors
                        sensors.requestTemperatures();
                    
                        // query conversion time. Apparently it takes a while to calculate.
                        //CONVERSION_TIME = sensors.millisToWaitForConversion(sensors.getResolution());
                        conversionTime = millisToWaitForConversion(sensors.getResolution());
                        isCalculating = true; //Next step is to re-calculate the temperature again.
                      }
                    
                    
                      // Next, let's calculate and send the temperature
                      if (isCalculating == true && currentMillis > previousMeasurementMillis + conversionTime ) {
                        isCalculating = false; // check calculating off the to-do list too.
                    
                        for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) { // Loop through all the attached temperatur sensors.
                          float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.; // Fetch and round temperature to one decimal
                          Serial.print("Sensor #");
                          Serial.print(i);
                          Serial.print(" says it is ");
                          Serial.print(temperature);
                          Serial.print(" degrees\n");
                          if (temperature != -127.00 && temperature != 85.00) { // avoid working with measurement errors.
                            if (COMPARE_TEMP == 1 && lastTemperature[i] == temperature) {
                              Serial.print("Not sending it though, because it's the same temperature as before.\n");
                            } else {
                              Serial.print("Sending the temperature to the gateway.\n");
                              send(msg.setSensor(i).set(temperature, 1));
                              lastTemperature[i] = temperature; // Save new temperatures to be able to compare in the next round.
                            }
                          }
                        }
                    
                        // Both tasks are done. Time to wait until we should measure again.
                        Serial.print("zzzzZZZZzzzzZZZZzzzz\n");
                    
                    #ifdef BATTERY_POWERED
                        unsigned long quicktimecheck = millis(); // check how much time has passed during the measurement (can be up to 750 milliseconds), and then calculate from that how long to sleep until the next intended measuring time.
                        unsigned long sleeptime = measurementSleepTime - (quicktimecheck - previousMeasurementMillis); //How much time has passed already during the calculating? Subtract that from the intended interval time.
                        sleep (sleeptime);
                    #endif
                    
                        isMeasuring = true;
                      }
                    }
                    
                    
                    // This function helps to avoid a problem with the latest Dallas temperature library.
                    int16_t millisToWaitForConversion(uint8_t bitResolution)
                    {
                      switch (bitResolution) {
                        case 9:
                          return 94;
                        case 10:
                          return 188;
                        case 11:
                          return 375;
                        default:
                          return 750;
                      }
                    }
                    
                    void receive(const MyMessage &message)
                    {
                      // We only expect one type of message from controller. But we better check anyway.
                      if (message.type == V_STATUS) {
                        // Change relay state
                        digitalWrite(message.sensor - 1 + RELAY_PIN, message.getBool() ? RELAY_ON : RELAY_OFF);
                        // Store state in eeprom
                        saveState(message.sensor, message.getBool());
                        // Write some debug info
                        Serial.print("Incoming change for sensor:");
                        Serial.print(message.sensor);
                        Serial.print(", New status: ");
                        Serial.println(message.getBool());
                      }
                    }
                    
                    /*Last note : celsius/farenheit in dallatemperature lib is not handled by the metric variable. To get the conversion you need to use C or F functions as below. I've not checked how you handle this in sketch.
                    
                        // returns temperature in degrees C
                        float getTempC(const uint8_t*);
                    
                        // returns temperature in degrees F
                        float getTempF(const uint8_t*);
                    
                        // Get temperature for device index (slow)
                        float getTempCByIndex(uint8_t);
                    
                        // Get temperature for device index (slow)
                      float getTempFByIndex(uint8_t);
                    */
                    dzjrD Offline
                    dzjrD Offline
                    dzjr
                    wrote on last edited by
                    #13

                    @mghaff

                    in the Dallas temperature library there is also a test sketch, with that you can check of the Dallas sensors are working.

                    My experience with multiple dallas sensors (more than 4) is that de 4K7 resistor not the right choise is, beter to use a 2K2 resisitor.

                    maybe you can look at this forum post for that

                    MGHaffM 1 Reply Last reply
                    0
                    • dzjrD dzjr

                      @rejoe2 said in 2 dallas temp + 4 relays:

                      ost in the other on

                      thank you for your tip to adjust the sketch, I am now working with a node for the water meter (partly based on your example of GITHUB) and I think I understand now what you mean.

                      I will adjust the sketch of the example!

                      I still have a wait (3000); in "void before" to start just after the gateway, because I use an RS-485 network.

                      rejoe2R Offline
                      rejoe2R Offline
                      rejoe2
                      wrote on last edited by
                      #14

                      @dzjr Good to hear your positive feedback, it's not always easy to decide whether to make some critical remarks or not.
                      Imo, the wait() in before() might only be needed because of the transport-wait-flag, this is not related to RS485 as transport layer.
                      I also use a 3.3kOhm resistor, the right value depends on some more variables than just the number of sensors.

                      Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                      dzjrD 1 Reply Last reply
                      0
                      • rejoe2R rejoe2

                        Have a look at at least 2 things in the sketch:
                        The for-loop(s) in presentation should be seperate ones for temp and relays.
                        Imo you try to send out temperature using a different ChildID than in presentation().

                        Additionally, -127 degrees indicates some wiring problems.

                        MGHaffM Offline
                        MGHaffM Offline
                        MGHaff
                        wrote on last edited by
                        #15
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • dzjrD dzjr

                          @mghaff

                          in the Dallas temperature library there is also a test sketch, with that you can check of the Dallas sensors are working.

                          My experience with multiple dallas sensors (more than 4) is that de 4K7 resistor not the right choise is, beter to use a 2K2 resisitor.

                          maybe you can look at this forum post for that

                          MGHaffM Offline
                          MGHaffM Offline
                          MGHaff
                          wrote on last edited by
                          #16

                          @dzjr If i use the mysensors dallas temp that i use for my pool the sensors work just fine. If i use the relay sketch it works fine Alone. Whenever i combine these two sketches is when i have problems. I have been trying sketches that are working for others with no progress myself. I have even rewired twice. I am no programmer i took 2 classes in college but they r not helping me either.

                          1 Reply Last reply
                          0
                          • rejoe2R rejoe2

                            Have a look at at least 2 things in the sketch:
                            The for-loop(s) in presentation should be seperate ones for temp and relays.
                            Imo you try to send out temperature using a different ChildID than in presentation().

                            Additionally, -127 degrees indicates some wiring problems.

                            MGHaffM Offline
                            MGHaffM Offline
                            MGHaff
                            wrote on last edited by
                            #17

                            @rejoe2 When i tried the for loops separate they don't show up at all in my serial. That was the only way to get them to present. I'm trying another sketch that looks more promising for me to build from but it as well doesn't show my temp. I get presentation but no reading.

                            BTW thanks for the help.

                            1 Reply Last reply
                            0
                            • rejoe2R rejoe2

                              @dzjr Good to hear your positive feedback, it's not always easy to decide whether to make some critical remarks or not.
                              Imo, the wait() in before() might only be needed because of the transport-wait-flag, this is not related to RS485 as transport layer.
                              I also use a 3.3kOhm resistor, the right value depends on some more variables than just the number of sensors.

                              dzjrD Offline
                              dzjrD Offline
                              dzjr
                              wrote on last edited by dzjr
                              #18

                              @rejoe2

                              I have again put a wait (500) in the loop, otherwise the sketch does not read any temperature .....
                              between

                              {
                                  //Dallas temperature sensoren
                                  // Fetch temperatures from Dallas sensors 
                                  sensors.requestTemperatures();
                                  // query conversion time and sleep until conversion completed
                                  int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
                                  
                               
                                  wait(500);
                              
                                  // Read temperatures and send them to controller 
                                  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
                              
                              1 Reply Last reply
                              0
                              • TheoLT Online
                                TheoLT Online
                                TheoL
                                Contest Winner
                                wrote on last edited by
                                #19

                                You calculate the conversion time, but you don't use it. So it makes sense that you won't get any readings without a wait, because the sensors don't get anytime to read. You should wait for the conversionTime before you read the sensors. Which is I'm guessing shorter than 500 ms, but I don't use Dallas temp sensors. So no expert on that.

                                rejoe2R 1 Reply Last reply
                                1
                                • TheoLT TheoL

                                  You calculate the conversion time, but you don't use it. So it makes sense that you won't get any readings without a wait, because the sensors don't get anytime to read. You should wait for the conversionTime before you read the sensors. Which is I'm guessing shorter than 500 ms, but I don't use Dallas temp sensors. So no expert on that.

                                  rejoe2R Offline
                                  rejoe2R Offline
                                  rejoe2
                                  wrote on last edited by
                                  #20

                                  @theol said in 2 dallas temp + 4 relays:

                                  You calculate the conversion time, but you don't use it. So it makes sense that you won't get any readings without a wait, because the sensors don't get anytime to read. You should wait for the conversionTime before you read the sensors. Which is I'm guessing shorter than 500 ms, but I don't use Dallas temp sensors. So no expert on that.

                                  Absolutely right.
                                  Conversion time depends on resolution, max is 750ms according to data sheet.

                                  Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                                  TheoLT 1 Reply Last reply
                                  0
                                  • rejoe2R rejoe2

                                    @theol said in 2 dallas temp + 4 relays:

                                    You calculate the conversion time, but you don't use it. So it makes sense that you won't get any readings without a wait, because the sensors don't get anytime to read. You should wait for the conversionTime before you read the sensors. Which is I'm guessing shorter than 500 ms, but I don't use Dallas temp sensors. So no expert on that.

                                    Absolutely right.
                                    Conversion time depends on resolution, max is 750ms according to data sheet.

                                    TheoLT Online
                                    TheoLT Online
                                    TheoL
                                    Contest Winner
                                    wrote on last edited by
                                    #21

                                    @rejoe2 changing
                                    wait(500);

                                    to

                                    wait( millisToWaitForConversion );

                                    Should do the trick.
                                    But wait has an unsigned long as parameter and your conversionTime is a signed int16_t, you might need to cast it. Best to update the millisWaitForConversion to unsigned long as well.

                                    // This function helps to avoid a problem with the latest Dallas temperature library.
                                    unsigned long millisToWaitForConversion(uint8_t bitResolution)
                                    {
                                      switch (bitResolution) {
                                        case 9:
                                          return 94;
                                        case 10:
                                          return 188;
                                        case 11:
                                          return 375;
                                        default:
                                          return 750;
                                      }
                                    }
                                    

                                    But it's hard to help with only a fragment of the code. I'm guessing that the resolution doesn't change when your sketch is running. In that case I'd declare a global unsigned long conversionWaitDuration and initialize it in the setup. I honestly would never ever use a wait in a sketchs main loop though. I always use a none blocking wait which uses millis().

                                    But again hard to help with coding when you can only see a fragment of the code.

                                    dzjrD 1 Reply Last reply
                                    0
                                    • TheoLT TheoL

                                      @rejoe2 changing
                                      wait(500);

                                      to

                                      wait( millisToWaitForConversion );

                                      Should do the trick.
                                      But wait has an unsigned long as parameter and your conversionTime is a signed int16_t, you might need to cast it. Best to update the millisWaitForConversion to unsigned long as well.

                                      // This function helps to avoid a problem with the latest Dallas temperature library.
                                      unsigned long millisToWaitForConversion(uint8_t bitResolution)
                                      {
                                        switch (bitResolution) {
                                          case 9:
                                            return 94;
                                          case 10:
                                            return 188;
                                          case 11:
                                            return 375;
                                          default:
                                            return 750;
                                        }
                                      }
                                      

                                      But it's hard to help with only a fragment of the code. I'm guessing that the resolution doesn't change when your sketch is running. In that case I'd declare a global unsigned long conversionWaitDuration and initialize it in the setup. I honestly would never ever use a wait in a sketchs main loop though. I always use a none blocking wait which uses millis().

                                      But again hard to help with coding when you can only see a fragment of the code.

                                      dzjrD Offline
                                      dzjrD Offline
                                      dzjr
                                      wrote on last edited by
                                      #22

                                      @theol

                                      i have uploaded the complete sketch at Github at here: link url)

                                      TheoLT 2 Replies Last reply
                                      0
                                      • dzjrD dzjr

                                        @theol

                                        i have uploaded the complete sketch at Github at here: link url)

                                        TheoLT Online
                                        TheoLT Online
                                        TheoL
                                        Contest Winner
                                        wrote on last edited by TheoL
                                        #23

                                        @dzjr Would be so much easier to discuss it in Dutch :)

                                        What is the problem you encounter at the moment?

                                        I'm looking at the code, and just asking why you use so many unneeded blocks. e.g.

                                        { if (flow != oldflow) { // Theo <--- Removed the bracelet at the start of the line?
                                        ...

                                        That first starting bracelet ({) is not needed because the part that is being executed if the if statement evaluates to true is after the last bracelet. I'll see if I can clean up the code a bit for you. I'll add comments with what I've changed.

                                        1 Reply Last reply
                                        0
                                        • dzjrD dzjr

                                          @theol

                                          i have uploaded the complete sketch at Github at here: link url)

                                          TheoLT Online
                                          TheoLT Online
                                          TheoL
                                          Contest Winner
                                          wrote on last edited by TheoL
                                          #24

                                          @dzjr here is the code with my comments. It compiles but I can't test it I use NRF24L01 and don't have any dallas temp sensors. I'll be cleaning up the code more like removing unused variables. Let's use that one for debugging purposes. Don't add anything else, because there's already a lot in it and it'll be harder to find the bug.

                                          /******************************************************************
                                            Created with PROGRAMINO IDE for Arduino - 28.12.2018 12:45:52
                                            Project     :MySensors Watermeter Node RS485
                                            Libraries   :MySensors, Dallas temp etc
                                            Author      :MySensors community & dzjr
                                            Description :Mysensors Watermeter node & leiding temperatuur
                                                        met lekkage detectie en optie voor waterdruk
                                          ******************************************************************/
                                          
                                          #define MY_DEBUG
                                          
                                          //RS 485 transport zaken
                                          #define MY_RS485                  //RS-485 transport
                                          #define MY_RS485_DE_PIN 11        //De DE pin benoemen, normaal is dit PIN-3
                                          #define MY_RS485_BAUD_RATE 9600
                                          //#define MY_RS485_MAX_MESSAGE_LENGTH 40
                                          //#define MY_RS485_SOH_COUNT 3 // gebruiken bij Collision op de bus
                                          
                                          //eerst wat administratie en dergelijk
                                          #define SN "PWN_Node" //Software Naam
                                          #define SV "1.01"      //Software versie
                                          #define MY_NODE_ID 20 //Node nummer
                                          #define MY_TRANSPORT_WAIT_READY_MS 3000    //wachten tot de gateway is opgestart
                                          
                                          
                                          //libaries activeren
                                          #include <MySensors.h>
                                          #include <DallasTemperature.h>
                                          #include <OneWire.h>
                                          #include <Bounce2.h>
                                          
                                          //Dallas settings
                                          #define COMPARE_TEMP 0 // 1= zenden alleen bij verandering 0= direct zenden
                                          #define ONE_WIRE_BUS 7
                                          #define TEMPERATURE_PRECISION 10 // 10 = 0,25°C 
                                          #define MAX_ATTACHED_DS18B20 4
                                          unsigned long SLEEP_TIME = 3000; //slaaptijd tussen twee metingen in ms
                                          OneWire oneWire(ONE_WIRE_BUS); //Een oneWire-exemplaar instellen om te communiceren met alle OneWire-apparaten
                                          DallasTemperature sensors(&oneWire); //OneWire naar Dallas
                                          float lastTemperature[MAX_ATTACHED_DS18B20];
                                          int numSensors = 0;
                                          bool receivedConfig = false;
                                          //bool metric = true;
                                          
                                          DeviceAddress Probe03 = { 0x28, 0x80, 0x43, 0x77, 0x91, 0x0F, 0x02, 0x5A  }; // leiding temp
                                          DeviceAddress Probe02 = { 0x28, 0xFF, 0x64, 0x1D, 0xF9, 0x9D, 0xDC, 0x5E  }; // op print1
                                          DeviceAddress Probe01 = { 0x28, 0xFF, 0x64, 0x1D, 0xF8, 0x4F, 0x3A, 0x08  }; // op print2
                                          
                                          
                                          MyMessage msgTemp(0, V_TEMP);
                                          
                                          #define CHILD_ID_PWN 11                 //Child ID Sensor watermeter
                                          #define CHILD_ID_LEKKAGE 21             //Child ID Lekkage sensor
                                          #define CHILD_ID_DRUK 22                //Child ID Water druk sensor
                                          #define PWN_SENSOR 2                    //Pin waar sensor watermeter op is aangesloten
                                          #define LEKKAGE_PIN A0                  //Pin waarop de lekkage sensor op is aangesloten
                                          #define WATERDRUK_PIN A1                //Pin waarom de druksensor is aangesloten
                                          #define SENSOR_INTERRUPT PWN_SENSOR-2   //Usually the interrupt = pin -2 
                                          #define PULSE_FACTOR 1000               //Pulsen per m³ water 1000=één rotatie per 10 liter
                                          #define MAX_FLOW 60ul                     //flitert meetfouten eruit (40 van @RBisschops en 100 van @Rejoe) <-- Theo: ul toegevoegd hoef je niet te casten
                                          #define wachtTime 5000ul              // Theo: added ul which makes it an unsigned long also mixing dutch and english in names is very confusing. At least to me.
                                          
                                          MyMessage flowmsg (CHILD_ID_PWN, V_FLOW);
                                          MyMessage volumemsg (CHILD_ID_PWN, V_VOLUME);
                                          MyMessage lastcountermsg (CHILD_ID_PWN, V_VAR1);
                                          MyMessage lekmsg (CHILD_ID_LEKKAGE, V_TEXT);
                                          MyMessage drukmsg (CHILD_ID_DRUK, V_PRESSURE);
                                          
                                          
                                          //unsigned long SEND_FREQUENCY = 30000; // Minimum time between send (in milliseconds). We don't want to spam the gateway.
                                          #define SEND_FREQUENCY 30000ul // Theo it's a constant 
                                          
                                          double ppl = ((double)PULSE_FACTOR) / 1000;      // Pulses per liter
                                          volatile unsigned long pulseCount = 0;
                                          volatile unsigned long lastBlink = 0;
                                          volatile double flow = 0;
                                          boolean pcReceived = false;
                                          unsigned long oldPulseCount = 0;
                                          unsigned long newBlink = 0;
                                          double oldflow = 0;
                                          double volume = 0;
                                          double oldvolume = 0;
                                          unsigned long lastSend = 0;
                                          unsigned long lastPulse = 0;
                                          #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
                                          unsigned long maxTimeDryRun = 30000;
                                          unsigned long startTimeDryRun = 0;
                                          volatile unsigned long dryPulseCount = 0;
                                          
                                          int lastlekValue = 0;
                                          int lastdrukValue = 0;
                                          
                                          // lowest and highest sensor readings:
                                          const int sensorMin = 0;     // sensor minimum
                                          const int sensorMax = 600;  // sensor maximum
                                          
                                          
                                          void before() {
                                            wait(wachtTime); // Theo: Since this is only used one, a constant is not really needed. You can keep it for better readability
                                            sensors.begin(); // Theo: I always use meaningfull names. To me sensors is to vague, I'd use dallasSensors or temperatureSensors.
                                          }
                                          
                                          void setup() {
                                            sensors.setWaitForConversion(false);
                                            pinMode(PWN_SENSOR, INPUT_PULLUP);
                                            sensors.setResolution(TEMPERATURE_PRECISION);
                                            pulseCount = oldPulseCount = 0;
                                            attachInterrupt(SENSOR_INTERRUPT, onPulse , FALLING );
                                          }
                                          
                                          void presentation() {
                                            sendSketchInfo(SN, SV);
                                          
                                            numSensors = sensors.getDeviceCount();
                                          
                                            for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
                                              //  metric = getControllerConfig().isMetric;
                                              // Registreer alle sensors naar de Gateway
                                              present(i + 0, S_TEMP, "temp sensors");
                                              //present(i+1, S_TEMP, "MK Node temp");
                                              //present(i+2, S_TEMP, "PWN Leiding temp");
                                              present(CHILD_ID_PWN, S_WATER, "PWN_Meter");
                                              request(CHILD_ID_PWN, V_VAR1, "Req PWN data" );
                                              present(CHILD_ID_LEKKAGE, S_INFO, "Lekkage MK");
                                              present(CHILD_ID_DRUK, S_BARO, "PWN Waterdruk");
                                            }
                                          }
                                          
                                          unsigned long currentTime; // Theo: <--- removed declaration from loop. Not necessary but a good practice to make large variables global. That way heap gets less fragmented
                                          
                                          void loop() {
                                            currentTime = millis();
                                          
                                            //Watermeter sensor
                                            //WaterPulse counter
                                            // Only send values at a maximum frequency or woken up from sleep
                                            if (currentTime - lastSend > SEND_FREQUENCY) {
                                              lastSend = currentTime;
                                          
                                              if (flow != oldflow) { // Theo <--- Removed the bracelet at the start of the line?
                                                oldflow = flow;
                                          #ifdef MY_DEBUG_LOCAL
                                                Serial.print("l/min:");
                                                Serial.println(flow);
                                          #endif
                                                // Check that we dont get unresonable large flow value.
                                                // could hapen when long wraps or false interrupt triggered
                                                if (flow < MAX_FLOW ) { // Theo: <-- cast (unsigned long) removed. No longer needed constant defined is ul (unsigned long) saves a bit of cpu time.
                                                  send(flowmsg.set(flow, 2 ) ); // Send flow value to gw
                                                }
                                              }
                                          
                                              // No Pulse count received in 2min
                                              if (currentTime - lastPulse > 120000) {
                                                flow = 0;
                                              }
                                          
                                              // Pulse count has changed
                                              if (pulseCount != oldPulseCount) {
                                                oldPulseCount = pulseCount;
                                          #ifdef MY_DEBUG
                                                Serial.print(F("pulsecnt:"));
                                                Serial.println(pulseCount);
                                          #endif
                                                if (!pcReceived) {
                                                  request(CHILD_ID_PWN, V_VAR1);
                                                }
                                                send(flowmsg.set(pulseCount));                  // Send  pulsecount value to gw in VAR1
                                          
                                                double volume = ((double)pulseCount / ((double)PULSE_FACTOR));
                                                if (volume != oldvolume) {
                                                  oldvolume = volume;
                                          #ifdef MY_DEBUG
                                                  Serial.print(F("vol:"));
                                                  Serial.println(volume, 3);
                                          #endif
                                                  send(volumemsg.set(volume, 3));               // Send volume value to gw
                                                }
                                              }
                                          
                                          
                                              //    { Theo
                                              //Waterdruk meten
                                              int Waterdruk = (analogRead(WATERDRUK_PIN)) * 10;
                                              if (Waterdruk != lastdrukValue) {
                                                send (drukmsg.set(Waterdruk));
                                                lastdrukValue = Waterdruk;
                                              }
                                          
                                          
                                              //waterlekkage sensor
                                              int lekLevel = map(analogRead(LEKKAGE_PIN), sensorMin, sensorMax, 0, 3);
                                          
                                              switch (lekLevel) {
                                                case 0:    // Sensor is droog
                                                  send(lekmsg.set("Geen lekkage"));
                                                  break;
                                                case 1:    // Sensor raakt nat
                                                  send(lekmsg.set("Vochtig"));
                                                  break;
                                                case 2:   // Sensor is volledig bedekt met water
                                                  send(lekmsg.set("Lekkage"));
                                                  break;
                                              }
                                              //    } Theo
                                          
                                              //    } Theo
                                          
                                          
                                          
                                          
                                          
                                          
                                          
                                              //    { Theo
                                              //Dallas temperature sensoren
                                              // Fetch temperatures from Dallas sensors
                                              sensors.requestTemperatures();
                                              // query conversion time and sleep until conversion completed
                                          //    int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
                                          
                                          
                                          //    wait(500);
                                          
                                              wait( sensors.millisToWaitForConversion( sensors.getResolution() ) );
                                          
                                              // 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.;
                                                float temperature;
                                                // voor geadreseerde sensoren
                                                switch (i)  {
                                                  case 0:
                                                    temperature = sensors.getTempC(Probe01);
                                                    break;
                                                  case 1:
                                                    temperature = sensors.getTempC(Probe02);
                                                    break;
                                                  case 2:
                                                    temperature = sensors.getTempC(Probe03);
                                                    break;
                                                  default:
                                                    temperature = sensors.getTempCByIndex(Probe03);
                                                    break;
                                                }
                                          
                                          
                                                // 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) { // Theo: COMPARE_TEMP looks like a constant with value 0 so the #if looks unneeded to me.??
                                                  //#endif
                                          
                                                  // Send in the new temperature
                                                  send( msgTemp.setSensor( i ).set( temperature, 1 ) );
                                                  // Save new temperatures for next compare
                                                  lastTemperature[i] = temperature;
                                                }
                                                //      } Theo
                                              }
                                            }
                                          }
                                          
                                          void onPulse() {
                                            if ( !SLEEP_MODE )  {
                                              unsigned long newBlink = micros();
                                              unsigned long interval = newBlink - lastBlink;
                                          
                                              if (interval != 0) {
                                                lastPulse = millis();
                                                if (interval < 1000000L) {
                                                  // Sometimes we get interrupt on RISING,  1000000 = 1sek debounce ( max 60 l/min)
                                                  return;
                                                }
                                                flow = (60000000.0 / interval) / ppl;
                                              }
                                              lastBlink = newBlink;
                                            }
                                            pulseCount++;
                                            dryPulseCount++;
                                          }
                                          

                                          Another MAX_ATTACHED_DS18B20 is 4. You only have 3 probes. This means that the 3rd and 4th will always report the same values.

                                          1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          14

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular