About DS18B20 onewire.



    1. Anyone know how many ds18b20 you can connect to a MySensors sketch?
      This sketch saying maximum 16, https://www.mysensors.org/build/temp.

    2. Lets say you have 3 sensors and sending them to e.g. Domoticz.
      Sensor A=15 °C
      Sensor B=25 °C
      Sensor C=35 °C
      If I understand the sketch correct, it will first search for connected sensors and then send one by one to Controller.
      I will create a Device called "A" in Controller and will receive value from sensor A to Device A, same for B and C.
      What happen if sensor B fails or I disconnect it.
      Will sensor C be the new sensor B?



  • I have now tested this and the answer is YES, if B disconnects C will be the new B



  • I have modify the sketch so now if sensor B is disconnected A will be A and C will be C, as soon as B is connected again it will send the values again.

    /**
     * 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
     */
    
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 0 // 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 = 3000; // 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. 
    
    byte D[3][8] = {
    { 0x28, 0xB1, 0x47, 0xB4, 0x04, 0x00, 0x00, 0x97 },
    { 0x28, 0xFF, 0x53, 0x78, 0x63, 0x15, 0x02, 0xC9 },
    { 0x28, 0xFF, 0xCD, 0x06, 0x52, 0x04, 0x00, 0x80 }
    };
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    void setup()  
    { 
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.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<3 && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      
    Serial.println(millis()); 
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // 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)
      gw.sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<3 && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
     //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
          float temperature = sensors.getTempC(D[i]);
        // 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
          gw.send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
      //gw.sleep(SLEEP_TIME);
    }
    

    first you need to use this sketch to get the correct addresses for all your DS18B20

    #include <OneWire.h>
    
    // OneWire DS18S20, DS18B20, DS1822 Temperature Example
    //
    // http://www.pjrc.com/teensy/td_libs_OneWire.html
    //
    // The DallasTemperature library can do all this work for you!
    // http://milesburton.com/Dallas_Temperature_Control_Library
    
    OneWire  ds(3);  // on pin 10 (a 4.7K resistor is necessary)
    int count=0;
    void setup(void) {
      Serial.begin(9600);
    }
    
    void loop(void) {
      byte i;
      byte present = 0;
      byte type_s;
      byte data[12];
      byte addr[8];
      float celsius;
      
      if ( !ds.search(addr)) {
        //Serial.println("No more addresses.");
        Serial.println();
        ds.reset_search();
        delay(250);
        Serial.println(count);
        Serial.println();
        count=0;
        return;
      }
      
      //Serial.print("ROM =");
      Serial.print("{");
      for( i = 0; i < 8; i++) {
      Serial.print(" 0x");
        //Serial.write(' ');
        //int a = (addr[i], DEC);
        //Serial.print(a);
        if (addr[i] <= 15){
        
          Serial.print("0");
        }
        
        Serial.print(addr[i], HEX);
        if (i < 7){
          Serial.print(",");
        }
        else 
        Serial.print(" },");
      }
    
      if (OneWire::crc8(addr, 7) != addr[7]) {
          Serial.println("CRC is not valid!");
          return;
      }
      Serial.println();
     
      // the first ROM byte indicates which chip
      switch (addr[0]) {
        case 0x10:
          //Serial.println("  Chip = DS18S20");  // or old DS1820
          type_s = 1;
          break;
        case 0x28:
          //Serial.println("  Chip = DS18B20");
          type_s = 0;
          break;
        case 0x22:
          //Serial.println("  Chip = DS1822");
          type_s = 0;
          break;
        default:
          //Serial.println("Device is not a DS18x20 family device.");
          return;
      } 
    
      ds.reset();
      ds.select(addr);
      ds.write(0x44, 1);        // start conversion, with parasite power on at the end
      
      delay(1000);     // maybe 750ms is enough, maybe not
      // we might do a ds.depower() here, but the reset will take care of it.
      
      present = ds.reset();
      ds.select(addr);    
      ds.write(0xBE);         // Read Scratchpad
    
      //Serial.print("  Data = ");
      //Serial.print(present, HEX);
      //Serial.print(" ");
      for ( i = 0; i < 9; i++) {           // we need 9 bytes
        data[i] = ds.read();
        //Serial.print(data[i], HEX);
        //Serial.print(" ");
      }
      //Serial.print(" CRC=");
      //Serial.print(OneWire::crc8(data, 8), HEX);
      //Serial.println();
    
      // Convert the data to actual temperature
      // because the result is a 16 bit signed integer, it should
      // be stored to an "int16_t" type, which is always 16 bits
      // even when compiled on a 32 bit processor.
      int16_t raw = (data[1] << 8) | data[0];
      if (type_s) {
        raw = raw << 3; // 9 bit resolution default
        if (data[7] == 0x10) {
          // "count remain" gives full 12 bit resolution
          raw = (raw & 0xFFF0) + 12 - data[6];
        }
      } else {
        byte cfg = (data[4] & 0x60);
        // at lower res, the low bits are undefined, so let's zero them
        if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
        else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
        else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
        //// default is 12 bit resolution, 750 ms conversion time
      }
      celsius = (float)raw / 16.0;
    
      //Serial.print("  Temperature = ");
      //Serial.print(celsius);
      //Serial.println(" Celsius, ");
    
      count++;
    }
    

    you will get the address like this format

    { 0x28, 0xB1, 0x47, 0xB4, 0x04, 0x00, 0x00, 0x97 }



  • it takes about 80 ms for each sensor that you add



  • My test has been running now for 4 days now and it works perfect. Of course it is the same sketch that MySensors is using but I have modified it a little bit.



  • First test with 3 temps is still OK.
    Second test is with 9 temps is still OK.



  • I have now 2 nodes, one with 27 temps, second with 17 temps.

    working with out problems since I started them 1 weeks ago

    I had some problem when adding more sensors, some of them seems not to work together that is why I have 2 nodes



  • How can I get this to work with 2.0 or 2.1.1
    I would so much have to help because I am a beginner



  • @tomasandersson Did some work wrt. this some time ago, sketches for MySensors 2.0+ are available here: https://github.com/rejoe2/MySensors-Dallas-Address-ChildID-Consistency.

    Easiest to use may be the "stored id"-Version, based on a concept of leodesigner. This is designed für max. 20 DS18x20.



  • @rejoe2
    Thanks
    I have now tried the stored ID "version and it works much better than the previous.
    I still get the wrong values sometimes. Wonder if there is control domoticz which mixes the ID.

    0_1490085476002_Skärmavbild 2017-03-21 kl. 09.34.34.png



  • @tomasandersson I really doubt the controller to mix things up, nor the node doing so.
    What are the absolute values of the measured temperatures? The "faulty" values seem to be more or less identical, so this could indicate powering problems or may be related to cabeling or resistor size. Did you stick to the 30 seconds intervall or something else?
    Normally 750ms for conversion should be enought, but official (according to datasheet) seem to be a little longer, so trying to change line 69 may help: conversionTime = 1000 / (1 << (12 - resolution));).



  • @rejoe2
    I will try to change.
    Here is a picture where you see that the sensor ID picks temperature of another sensor0_1490096020397_Skärmavbild 2017-03-21 kl. 12.31.12.png



  • @tomasandersson This really looks strange.
    Some additional remarks:

    • Some values represent also failure codes, beside -127°, 85° (C), which are already filtered out by code I also suspect 0° to be one of these. This is the backgound of me asking above for absolute figures.
    • There seem to be also fake chips on the market; they may have slightly different operation conditions esp. wrt. to timing (if they work at all; I don't have own experience on this).
    • How are your cabeling conditions? I had some troubles when using nRF+PA+LNA modules together with the new (post 2.0.1-) MySensors-libs and more than 5 DS18B20 on one line (non-parasitic mode). This seems to be solved by the use of a small delay between temp-sends (wait(50) or so). With a lot of sensors and long lines and/or star topology, you may have to lower the pullup resistor value (e.g. 3.3k).


  • @rejoe2

    Thanks so much for your help.
    I think you're a little ahead of me in programming. Interesting with time delay between temp sends. How do I create it?



  • @tomasandersson You are welcome!
    I am more feeling as a beginner also in most programming aspects. This is just some sort of copy/pasting in addition with some thoughts on how things may work and reading technical documentation and examples :simple_smile:; most of this is based on leodesigners work here somewhere in the forum (fride monitor). I had a hard time to find solutions working for me, so I'm happy if someone can learn from this.

    Back to the answer: just add a wait(50); as new line 120 (somewhere within the reading/sending for-loop).



  • @rejoe2

    Now I have tried both of the programs. It works much the same. It pops up a fault sometimes.
    Is it difficult to enter a line in the program, says that if the temperature has changed system than - + 5 degrees so it shall not be reported



  • @tomasandersson To me, this really seems to be hardware related, so triple check your wirings first.
    If you really believe this to be a software problem, you may add an additional condition in line 119 with another "&&", (from memory something like abs(lastTemperature[i] - temperature < 5). I would have to test it myself and don't find this reasonable (s.o.), so feel free to finish this part of the job yourself.



  • @rejoe2
    Thank you for helping me in the right direction
    I do not think there is anything wrong with the hardware because I have two identical sets. One to a spa and a heat pump. Maybe I'll try it with less resistance. Now i have 4.8kohm

    I'm now trying with more or less distinction from the former transient temp.
    I will reply when I got it to work.

    Tomas



  • @tomasandersson Just 2 DS18B20 on each line should never cause trouble wrt current draw using a 4.8k resistor and 30 sec. intervals and no additional sensors drawing current from the arduino. If not already in place I would anyhow recommend using 3 wires for operation instead of parasitic mode.
    Anyhow: Best way to identify these chips is using the addresses. Most likely you now are also prepared to do some small editing in sketches?
    "My" array-sketch-version also prints the ID's as array information you need to serial. You may test it and then disable this feature once you have the info for both of your sketches (begin the respecting line with the #define with a "//"). Doing so will definitely eliminate any smallest risk of mixing data...



  • Is any chance to connect more than 16 dallas ? By change value 16 to example 20 ?



  • @flopp

    Hi
    i try use your sketch but it is not working... I add in this to use serial gateway but also i get error why ?

    /**
     * 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
     */
    
    #define MY_GATEWAY_SERIAL
    
    #include <MySensors.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 0 // 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 = 3000; // 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. 
    
    byte D[3][8] = {
    { 0x28, 0xC6, 0xCA, 0xA0, 0x05, 0x00, 0x00, 0x30 }
    };
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    void setup()  
    { 
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.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<3 && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      
    Serial.println(millis()); 
      // Process incoming messages (like config from server)
      gw.process(); 
    
      // 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)
      gw.sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<3 && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
     //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
          float temperature = sensors.getTempC(D[i]);
        // 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
          gw.send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
      //gw.sleep(SLEEP_TIME);
    }
    

    But when try compile i get error:

    dallas_with_address_ok:45:1: error: 'MySensor' does not name a type
    
     MySensor gw;
    
     ^
    
    C:\Users\Piotrek\Desktop\Arduino\dallas_with_address_ok\dallas_with_address_ok.ino: In function 'void setup()':
    
    dallas_with_address_ok:61:3: error: 'gw' was not declared in this scope
    
       gw.begin();
    
       ^
    
    C:\Users\Piotrek\Desktop\Arduino\dallas_with_address_ok\dallas_with_address_ok.ino: In function 'void loop()':
    
    dallas_with_address_ok:81:3: error: 'gw' was not declared in this scope
    
       gw.process(); 
    
       ^
    
    exit status 1
    'MySensor' does not name a type
    

    Please help me...


  • Mod

    @pepson that sketch is for MySensors 1.x. It needs to be updated to work with 2.x. There is a guide, see if you can find it by searching (I’m on mobile now so I can’t search easily)



  • I correct this sketch But for me dont read temp.....
    standard sketch from mysensors looks similary like this read with no problem.
    https://www.mysensors.org/build/temp

    /**
     * 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
     */
    
    #define MY_GATEWAY_SERIAL
    
    #include <MySensors.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define COMPARE_TEMP 0 // 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 = 3000; // 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. 
    
    byte D[3][8] = {
    { 0x28, 0xFB, 0x8F, 0x77, 0x91, 0x15, 0x02, 0x32 },
    { 0x28, 0xFF, 0x37, 0x77, 0x91, 0x18, 0x02, 0x16 }
    };
    //MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean 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);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
    //  begin();
    }
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temp. Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<3 && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      
    Serial.println(millis()); 
      // Process incoming messages (like config from server)
      //process(); 
    
      // 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<3 && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
     //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
          float temperature = sensors.getTempC(D[i]);
        // 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;
        }
      }
      //gw.sleep(SLEEP_TIME);
    }
    


  • @mfalkvidd
    Hi
    I am not programmer and i dont know how. Is any chance to you can help me convert it to working with MySensors 2.x ?
    This sketch looks similary as standard sketch from mysensors:
    https://www.mysensors.org/build/temp

    Standard sketch works but i want use sketch with ID Dallas to write in my Home assistant to specify number child.... and when dallas destroy i replace ID in sketch write to arduino and in Home Assistant automaticaly replace it and show still correct temp to my room.



  • @pepson said in About DS18B20 onewire.:

    #define MAX_ATTACHED_DS18B20 16
    

    this should be how many DS18B20 you have attached



  • @pepson said in About DS18B20 onewire.:

    byte D[3][8] = {
    { 0x28, 0xFB, 0x8F, 0x77, 0x91, 0x15, 0x02, 0x32 },
    { 0x28, 0xFF, 0x37, 0x77, 0x91, 0x18, 0x02, 0x16 }
    };
    

    D[3] means 3 attached DS18B20, but you only have address for 2



  • I have a sketch where I have attached 40 pcs mixed both DS18B20 and DS18S20, works perfect, except that sometimes I recevied 0 as value for some of them. I am polling every minute, so I don't care if the value is 0 for a couple of minutes, it is not that very important



  • @flopp

    But for what is number 8 in this ?
    byte D[3][8] = {

    On what MySensors you use it ?
    Previous sketch your in this thread i can not compile. I convert it as i show. You mean that sketch with 2 pcs dallas should look like this:

    /**
     * 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
     */
    
    #define MY_GATEWAY_SERIAL
    
    #include <MySensors.h>  
    #include <SPI.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 = 3000; // 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. 
    
    byte D[2][8] = {
    { 0x28, 0xFB, 0x8F, 0x77, 0x91, 0x15, 0x02, 0x32 },
    { 0x28, 0xC5, 0xBF, 0x77, 0x91, 0x16, 0x02, 0x1D }
    };
    //MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean 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);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
    //  begin();
    }
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temp. Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<3 && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      }
    }
    
    
    void loop()     
    {     
      
    Serial.println(millis()); 
      // Process incoming messages (like config from server)
      //process(); 
    
      // 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<3 && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
        //float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(D[i]):sensors.getTempFByIndex(D[i])) * 10.)) / 10.;
        //float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
        float temperature = sensors.getTempC(D[i]);
        // 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);
    }
    

    And what command in sketch should be use:

    1. float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(D[i]):sensors.getTempFByIndex(D[i])) * 10.)) / 10.;
    2. float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
    3. float temperature = sensors.getTempC(D[i]);

    In original sketch from Mysensors he use this :
    float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;

    And please share me sketch which you use with MySensors 2.x and with how system controller you use it ? I want use with Home Assistant.



  • /**
     * 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
     */
     
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    #define MY_NODE_ID 14
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DallasTemperature.h>
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define ATTACHED_DS18B20 40
    unsigned long SLEEP_TIME = 60000; // 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. 
    
    byte D[ATTACHED_DS18B20][8] = {
    { 0x10, 0x04, 0xB8, 0x3F, 0x02, 0x08, 0x00, 0xBD }, //KökTbx
    { 0x10, 0xF4, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0xB1 }, //UppTbx
    { 0x10, 0x92, 0x9F, 0x3E, 0x02, 0x08, 0x00, 0x98 }, //KökUt
    { 0x10, 0x4E, 0xE4, 0x3E, 0x02, 0x08, 0x00, 0x3C }, //ToaUt
    { 0x10, 0x1E, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x33 }, //EfterPump
    { 0x10, 0x09, 0xA1, 0x3E, 0x02, 0x08, 0x00, 0xF7 }, //HallTbx
    { 0x10, 0x59, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xFA }, //UppUt
    { 0x10, 0x07, 0x1B, 0x3F, 0x02, 0x08, 0x00, 0x99 }, //ToaTbx
    { 0x28, 0xFF, 0xB1, 0xAA, 0x63, 0x15, 0x03, 0xC8 }, //Kök 
    { 0x10, 0x80, 0xB6, 0x3F, 0x02, 0x08, 0x00, 0x29 }, //Panna komp
    { 0x10, 0x3C, 0x9C, 0x3E, 0x02, 0x08, 0x00, 0x99 }, //Hall ut
    { 0x10, 0x42, 0xE0, 0x3F, 0x02, 0x08, 0x00, 0xD1 }, //Tvätt
    { 0x10, 0xCE, 0xE9, 0x3E, 0x02, 0x08, 0x00, 0x3A }, //T12
    { 0x10, 0x99, 0xAC, 0x3F, 0x02, 0x08, 0x00, 0x25 }, //V mellan
    { 0x10, 0x15, 0xDB, 0x3E, 0x02, 0x08, 0x00, 0x01 }, //Hallen
    { 0x10, 0xF3, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0x34 }, //Panna El
    { 0x10, 0xFB, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xC8 }, //Toa Nere
    { 0x10, 0x97, 0x23, 0x3F, 0x02, 0x08, 0x00, 0x92 }, //Varmvatten
    { 0x10, 0x0F, 0xE6, 0x3E, 0x02, 0x08, 0x00, 0xFD }, //Carport
    { 0x10, 0xF3, 0xC6, 0x3F, 0x02, 0x08, 0x00, 0x85 }, //Förrådet
    { 0x10, 0x9C, 0x3B, 0x52, 0x02, 0x08, 0x00, 0x25 }, //Mark 60
    { 0x10, 0xF2, 0x24, 0x3F, 0x02, 0x08, 0x00, 0xEB }, //Vrum V
    { 0x10, 0x6A, 0x5E, 0x52, 0x02, 0x08, 0x00, 0x26 }, //Mark 30
    { 0x10, 0x76, 0xCE, 0x3F, 0x02, 0x08, 0x00, 0xBA }, //Plattan
    { 0x10, 0x0E, 0x7B, 0x13, 0x02, 0x08, 0x00, 0x7C }, //Utetemp
    { 0x10, 0xEE, 0xEB, 0x3E, 0x02, 0x08, 0x00, 0x0F }, //Uterum
    { 0x28, 0x8C, 0x2D, 0xB4, 0x04, 0x00, 0x00, 0x9C }, //Kyl uppe
    { 0x28, 0xFF, 0xB2, 0x74, 0x63, 0x15, 0x02, 0xCC }, //Kyl mitten
    { 0x28, 0xFF, 0x08, 0x07, 0x52, 0x04, 0x00, 0xFF }, //Kyl nere
    { 0x28, 0xFF, 0xE6, 0x06, 0x52, 0x04, 0x00, 0x08 }, //Frys uppe
    { 0x28, 0xFF, 0xB4, 0x07, 0x55, 0x04, 0x00, 0xEB }, //Kyl bakom
    { 0x10, 0x1C, 0xA8, 0x3F, 0x02, 0x08, 0x00, 0x3B }, //Lucas
    { 0x10, 0x83, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x06 }, //Allrum
    { 0x10, 0xA3, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x7E }, //Theo
    { 0x10, 0x33, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x01 }, //Sovrum
    { 0x28, 0xFF, 0xFB, 0x3D, 0xC3, 0x16, 0x03, 0x58 }, //Panna luft in(varm)
    { 0x28, 0xFF, 0xD4, 0x1C, 0x00, 0x17, 0x03, 0x43 }, //Dränering
    { 0x28, 0xFF, 0x97, 0x11, 0x01, 0x15, 0x04, 0xC0 } // Poolvatten
    
    };
    
    float lastTemperature[ATTACHED_DS18B20];
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    MyMessage heat(0,V_STATUS);
    
    void setup()  
    { 
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    }
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("OneWire Temp+Heating LED", "20180709");
      
      // Fetch the number of attached temperature sensors  
      //numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      }
      for (int i=ATTACHED_DS18B20; i<ATTACHED_DS18B20+5; i++) {   
         present(i, S_HEATER);
      }
    }
    
    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<ATTACHED_DS18B20; i++) {
    
      //Serial.println(sensors.getResolution(D[i]), DEC); 
        // Fetch and round temperature to one decimal
     //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
          float temperature = sensors.getTempC(D[i]);
        // 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;
        }
      }
    
       for (int i=0; i<5;i++){
        int value = analogRead(i);
        /*Serial.print("Pin");
        Serial.print(i);
        Serial.print("=");
        Serial.println(value);
        */
        int j=i+ATTACHED_DS18B20;
        if (value>150) {
          send(heat.setSensor(j).set(1));
        }
        else {
          send(heat.setSensor(j).set(0));
        }
      }
      
      sleep(SLEEP_TIME);
    }
    


  • And you dont use library onewire.h ?



  • @pepson said in About DS18B20 onewire.:

    And you dont use library onewire.h ?

    I think it will be included in DallasTemperature.h.

    Doesn’t my sketch work for you?



  • @flopp
    Probably tommorow I test your sketch and send you an info...

    And what you mean in sketch write Heat?

    And is any chance to modify this sketch and add value name "Description" to each sensor Dallas to send info to controller like Home Assistant to user can easily identify sensors in controller? What is what....



  • @flopp
    Hi
    Tell me... is any chance to add in sketch position to write description for sensor and to send this description to Home Assistant....? It is ok write in other sketch for relay and it works perfect:
    https://github.com/lkankowski/arduino-multi-relay/blob/master/arduino-multi-relay.ino

    It show in Home Assisnatn this information in description example: Ł2 - kinkiet [C10]
    0_1555504238931_PART_1555431841808.jpeg


  • Mod

    @pepson yes. Just change the present call, just like in your sketch.

    Example: change

    present(i, S_HEATER);
    

    to

    present(i, S_HEATER, "Description goes here");
    


  • @mfalkvidd
    OK but also must add defined description to all address. But how add also description?



  • @pepson said in About DS18B20 onewire.:

    @mfalkvidd
    OK but also must add defined description to all address. But how add also description?

    Don't know, if that really works, because I never tested it since my controller sw supports comments: https://github.com/rejoe2/MySensors-Dallas-Address-ChildID-Consistency/blob/master/DallasTemperatureSimple/DallasTemperatureSimple.ino



  • @rejoe2
    But where in sketch you define address and description ?



  • @pepson That's not necessary: The sketch reads the bus to get all the necessary info and will just send whatever is found.

    If you want a "hardcoded version", you'd have to addopt the sketch for something in between these variants: if you use an array for the 1-wire addresses like https://github.com/rejoe2/MySensors-Dallas-Address-ChildID-Consistency/blob/master/Dallas_Addresses_Array_Solution/Dallas_Addresses_Array_Solution.ino does, just built the comment char-array based on that info.



  • @rejoe2 said in About DS18B20 onewire.:

    If you want a "hardcoded version", you'd have to addopt the sketch for something in between these variants: if you use an array for the 1-wire addresses like https://github.com/rejoe2/MySensors-Dallas-Address-ChildID-Consistency/blob/master/Dallas_Addresses_Array_Solution/Dallas_Addresses_Array_Solution.ino does, just built the comment char-array based on that info.

    Ok i know how add to present description , but still dont know how add description to each sensors.... as in sketch for relay as i show as example.



  • @pepson I'd suggest you make a test with the "simple" variant of the sketches. Then we could lateron discuss how to deal with an array containing the hardcoded addresses.
    Pls. make also a test with the hardcoded version. Afai remember, it will print the needed array to serial, as long as #define PRINT_ARRAY isn't commented.



  • @mfalkvidd Can you help me... or change it ?



  • @flopp said in About DS18B20 onewire.:

    /**
     * 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
     */
     
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    #define MY_NODE_ID 14
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DallasTemperature.h>
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define ATTACHED_DS18B20 40
    unsigned long SLEEP_TIME = 60000; // 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. 
    
    byte D[ATTACHED_DS18B20][8] = {
    { 0x10, 0x04, 0xB8, 0x3F, 0x02, 0x08, 0x00, 0xBD }, //KökTbx
    { 0x10, 0xF4, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0xB1 }, //UppTbx
    { 0x10, 0x92, 0x9F, 0x3E, 0x02, 0x08, 0x00, 0x98 }, //KökUt
    { 0x10, 0x4E, 0xE4, 0x3E, 0x02, 0x08, 0x00, 0x3C }, //ToaUt
    { 0x10, 0x1E, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x33 }, //EfterPump
    { 0x10, 0x09, 0xA1, 0x3E, 0x02, 0x08, 0x00, 0xF7 }, //HallTbx
    { 0x10, 0x59, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xFA }, //UppUt
    { 0x10, 0x07, 0x1B, 0x3F, 0x02, 0x08, 0x00, 0x99 }, //ToaTbx
    { 0x28, 0xFF, 0xB1, 0xAA, 0x63, 0x15, 0x03, 0xC8 }, //Kök 
    { 0x10, 0x80, 0xB6, 0x3F, 0x02, 0x08, 0x00, 0x29 }, //Panna komp
    { 0x10, 0x3C, 0x9C, 0x3E, 0x02, 0x08, 0x00, 0x99 }, //Hall ut
    { 0x10, 0x42, 0xE0, 0x3F, 0x02, 0x08, 0x00, 0xD1 }, //Tvätt
    { 0x10, 0xCE, 0xE9, 0x3E, 0x02, 0x08, 0x00, 0x3A }, //T12
    { 0x10, 0x99, 0xAC, 0x3F, 0x02, 0x08, 0x00, 0x25 }, //V mellan
    { 0x10, 0x15, 0xDB, 0x3E, 0x02, 0x08, 0x00, 0x01 }, //Hallen
    { 0x10, 0xF3, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0x34 }, //Panna El
    { 0x10, 0xFB, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xC8 }, //Toa Nere
    { 0x10, 0x97, 0x23, 0x3F, 0x02, 0x08, 0x00, 0x92 }, //Varmvatten
    { 0x10, 0x0F, 0xE6, 0x3E, 0x02, 0x08, 0x00, 0xFD }, //Carport
    { 0x10, 0xF3, 0xC6, 0x3F, 0x02, 0x08, 0x00, 0x85 }, //Förrådet
    { 0x10, 0x9C, 0x3B, 0x52, 0x02, 0x08, 0x00, 0x25 }, //Mark 60
    { 0x10, 0xF2, 0x24, 0x3F, 0x02, 0x08, 0x00, 0xEB }, //Vrum V
    { 0x10, 0x6A, 0x5E, 0x52, 0x02, 0x08, 0x00, 0x26 }, //Mark 30
    { 0x10, 0x76, 0xCE, 0x3F, 0x02, 0x08, 0x00, 0xBA }, //Plattan
    { 0x10, 0x0E, 0x7B, 0x13, 0x02, 0x08, 0x00, 0x7C }, //Utetemp
    { 0x10, 0xEE, 0xEB, 0x3E, 0x02, 0x08, 0x00, 0x0F }, //Uterum
    { 0x28, 0x8C, 0x2D, 0xB4, 0x04, 0x00, 0x00, 0x9C }, //Kyl uppe
    { 0x28, 0xFF, 0xB2, 0x74, 0x63, 0x15, 0x02, 0xCC }, //Kyl mitten
    { 0x28, 0xFF, 0x08, 0x07, 0x52, 0x04, 0x00, 0xFF }, //Kyl nere
    { 0x28, 0xFF, 0xE6, 0x06, 0x52, 0x04, 0x00, 0x08 }, //Frys uppe
    { 0x28, 0xFF, 0xB4, 0x07, 0x55, 0x04, 0x00, 0xEB }, //Kyl bakom
    { 0x10, 0x1C, 0xA8, 0x3F, 0x02, 0x08, 0x00, 0x3B }, //Lucas
    { 0x10, 0x83, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x06 }, //Allrum
    { 0x10, 0xA3, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x7E }, //Theo
    { 0x10, 0x33, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x01 }, //Sovrum
    { 0x28, 0xFF, 0xFB, 0x3D, 0xC3, 0x16, 0x03, 0x58 }, //Panna luft in(varm)
    { 0x28, 0xFF, 0xD4, 0x1C, 0x00, 0x17, 0x03, 0x43 }, //Dränering
    { 0x28, 0xFF, 0x97, 0x11, 0x01, 0x15, 0x04, 0xC0 } // Poolvatten
    
    };
    
    float lastTemperature[ATTACHED_DS18B20];
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    MyMessage heat(0,V_STATUS);
    
    void setup()  
    { 
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    }
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("OneWire Temp+Heating LED", "20180709");
      
      // Fetch the number of attached temperature sensors  
      //numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      }
      for (int i=ATTACHED_DS18B20; i<ATTACHED_DS18B20+5; i++) {   
         present(i, S_HEATER);
      }
    }
    
    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<ATTACHED_DS18B20; i++) {
    
      //Serial.println(sensors.getResolution(D[i]), DEC); 
        // Fetch and round temperature to one decimal
     //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
          float temperature = sensors.getTempC(D[i]);
        // 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;
        }
      }
    
       for (int i=0; i<5;i++){
        int value = analogRead(i);
        /*Serial.print("Pin");
        Serial.print(i);
        Serial.print("=");
        Serial.println(value);
        */
        int j=i+ATTACHED_DS18B20;
        if (value>150) {
          send(heat.setSensor(j).set(1));
        }
        else {
          send(heat.setSensor(j).set(0));
        }
      }
      
      sleep(SLEEP_TIME);
    }
    

    When i try compile this sketch it get me error:

    C:\Users\Piotrek\Desktop\Temp dallas\Arduino_dallas_18B20_with_desc\Arduino_dallas_18B20_with_desc.ino: In function 'void presentation()':

    Arduino_dallas_18B20_with_desc:117:3: error: expected initializer before 'sensors'

    sensors.requestTemperatures();

    ^

    exit status 1
    expected initializer before 'sensors'


  • Mod

    @pepson you have a stray } after setWaitForConversion

    Tip: use ctrl+t in the editor to easier see this type of mistake



  • @mfalkvidd
    Still is problem after use CTRL+T

    /**
       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
    */
    
    #define MY_GATEWAY_SERIAL
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    #define MY_NODE_ID 14
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <DallasTemperature.h>
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define ATTACHED_DS18B20 40
    unsigned long SLEEP_TIME = 60000; // 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.
    
    byte D[ATTACHED_DS18B20][8] = {
      { 0x10, 0x04, 0xB8, 0x3F, 0x02, 0x08, 0x00, 0xBD }, //KökTbx
      { 0x10, 0xF4, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0xB1 }, //UppTbx
      { 0x10, 0x92, 0x9F, 0x3E, 0x02, 0x08, 0x00, 0x98 }, //KökUt
      { 0x10, 0x4E, 0xE4, 0x3E, 0x02, 0x08, 0x00, 0x3C }, //ToaUt
      { 0x10, 0x1E, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x33 }, //EfterPump
      { 0x10, 0x09, 0xA1, 0x3E, 0x02, 0x08, 0x00, 0xF7 }, //HallTbx
      { 0x10, 0x59, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xFA }, //UppUt
      { 0x10, 0x07, 0x1B, 0x3F, 0x02, 0x08, 0x00, 0x99 }, //ToaTbx
      { 0x28, 0xFF, 0xB1, 0xAA, 0x63, 0x15, 0x03, 0xC8 }, //Kök
      { 0x10, 0x80, 0xB6, 0x3F, 0x02, 0x08, 0x00, 0x29 }, //Panna komp
      { 0x10, 0x3C, 0x9C, 0x3E, 0x02, 0x08, 0x00, 0x99 }, //Hall ut
      { 0x10, 0x42, 0xE0, 0x3F, 0x02, 0x08, 0x00, 0xD1 }, //Tvätt
      { 0x10, 0xCE, 0xE9, 0x3E, 0x02, 0x08, 0x00, 0x3A }, //T12
      { 0x10, 0x99, 0xAC, 0x3F, 0x02, 0x08, 0x00, 0x25 }, //V mellan
      { 0x10, 0x15, 0xDB, 0x3E, 0x02, 0x08, 0x00, 0x01 }, //Hallen
      { 0x10, 0xF3, 0xD7, 0x3F, 0x02, 0x08, 0x00, 0x34 }, //Panna El
      { 0x10, 0xFB, 0xCB, 0x3F, 0x02, 0x08, 0x00, 0xC8 }, //Toa Nere
      { 0x10, 0x97, 0x23, 0x3F, 0x02, 0x08, 0x00, 0x92 }, //Varmvatten
      { 0x10, 0x0F, 0xE6, 0x3E, 0x02, 0x08, 0x00, 0xFD }, //Carport
      { 0x10, 0xF3, 0xC6, 0x3F, 0x02, 0x08, 0x00, 0x85 }, //Förrådet
      { 0x10, 0x9C, 0x3B, 0x52, 0x02, 0x08, 0x00, 0x25 }, //Mark 60
      { 0x10, 0xF2, 0x24, 0x3F, 0x02, 0x08, 0x00, 0xEB }, //Vrum V
      { 0x10, 0x6A, 0x5E, 0x52, 0x02, 0x08, 0x00, 0x26 }, //Mark 30
      { 0x10, 0x76, 0xCE, 0x3F, 0x02, 0x08, 0x00, 0xBA }, //Plattan
      { 0x10, 0x0E, 0x7B, 0x13, 0x02, 0x08, 0x00, 0x7C }, //Utetemp
      { 0x10, 0xEE, 0xEB, 0x3E, 0x02, 0x08, 0x00, 0x0F }, //Uterum
      { 0x28, 0x8C, 0x2D, 0xB4, 0x04, 0x00, 0x00, 0x9C }, //Kyl uppe
      { 0x28, 0xFF, 0xB2, 0x74, 0x63, 0x15, 0x02, 0xCC }, //Kyl mitten
      { 0x28, 0xFF, 0x08, 0x07, 0x52, 0x04, 0x00, 0xFF }, //Kyl nere
      { 0x28, 0xFF, 0xE6, 0x06, 0x52, 0x04, 0x00, 0x08 }, //Frys uppe
      { 0x28, 0xFF, 0xB4, 0x07, 0x55, 0x04, 0x00, 0xEB }, //Kyl bakom
      { 0x10, 0x1C, 0xA8, 0x3F, 0x02, 0x08, 0x00, 0x3B }, //Lucas
      { 0x10, 0x83, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x06 }, //Allrum
      { 0x10, 0xA3, 0xE8, 0x3E, 0x02, 0x08, 0x00, 0x7E }, //Theo
      { 0x10, 0x33, 0x3C, 0x3F, 0x02, 0x08, 0x00, 0x01 }, //Sovrum
      { 0x28, 0xFF, 0xFB, 0x3D, 0xC3, 0x16, 0x03, 0x58 }, //Panna luft in(varm)
      { 0x28, 0xFF, 0xD4, 0x1C, 0x00, 0x17, 0x03, 0x43 }, //Dränering
      { 0x28, 0xFF, 0x97, 0x11, 0x01, 0x15, 0x04, 0xC0 } // Poolvatten
    
    };
    
    float lastTemperature[ATTACHED_DS18B20];
    // Initialize temperature message
    MyMessage msg(0, V_TEMP);
    MyMessage heat(0, V_STATUS);
    
    void setup()
    {
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    }
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("OneWire Temp+Heating LED", "20180709");
    
      // Fetch the number of attached temperature sensors
      //numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i = 0; i < ATTACHED_DS18B20; i++) {
        present(i, S_TEMP);
      }
      for (int i = ATTACHED_DS18B20; i < ATTACHED_DS18B20 + 5; i++) {
        present(i, S_HEATER);
      }
    
      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 < ATTACHED_DS18B20; i++) {
    
        //Serial.println(sensors.getResolution(D[i]), DEC);
        // Fetch and round temperature to one decimal
        //   float temperature = static_cast<float>(static_cast<int>((sensors.requestTemperaturesByAddress(D[i])) * 10.)) / 10.;
        float temperature = sensors.getTempC(D[i]);
        // 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;
        }
      }
    
      for (int i = 0; i < 5; i++) {
        int value = analogRead(i);
        /*Serial.print("Pin");
          Serial.print(i);
          Serial.print("=");
          Serial.println(value);
        */
        int j = i + ATTACHED_DS18B20;
        if (value > 150) {
          send(heat.setSensor(j).set(1));
        }
        else {
          send(heat.setSensor(j).set(0));
        }
      }
    
      sleep(SLEEP_TIME);
    }
    

    C:\Users\Piotrek\Desktop\Temp dallas\Arduino_dallas_18B20_with_desc\Arduino_dallas_18B20_with_desc.ino: In function 'void presentation()':

    Arduino_dallas_18B20_with_desc:117:3: error: expected initializer before 'sensors'

    sensors.requestTemperatures();

    ^

    exit status 1
    expected initializer before 'sensors'


  • Mod

    @pepson you’re missing a } before start of loop.



  • @mfalkvidd
    Where ? please show me more.... 😞 i am beginner



  • @pepson ok fixed it 🙂



  • But.... Can you also help me to add to this sketch option to send to example to Home Assistant also DESCRIPTION to each sensors ? And description will be get from sketch from name about address sensor ?

    As in this sketch which i have for relay:

    #define USE_EXPANDER
    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    
    #define MY_GATEWAY_SERIAL
    
    #include <MySensors.h>
    #include <Bounce2.h>
    #ifdef USE_EXPANDER
      #include <Wire.h>    // Required for I2C communication
      #include "PCF8574.h"
      uint8_t expanderAddresses[] = {0x20};
      const int numberOfExpanders = sizeof(expanderAddresses);
      PCF8574 expander[numberOfExpanders];
      #define E(expanderNo, ExpanderPin) (((expanderNo+1)<<8) | (ExpanderPin))
    #endif
    
    // No Button Constant
    #define NOB -1
    #define MULTI_RELAY_VERSION 9
    #define RELAY_STATE_STORAGE 1
    
    const uint8_t RELAY_TRIGGER_LOW  = 0;
    const uint8_t RELAY_TRIGGER_HIGH = 1;
    const uint8_t RELAY_STARTUP_ON   = 2;
    const uint8_t RELAY_STARTUP_OFF  = 4;
    const uint8_t RELAY_STARTUP_MASK = RELAY_STARTUP_ON | RELAY_STARTUP_OFF;
    
    enum ButtonType {
      MONO_STABLE = 0,
      BI_STABLE = 1,
      DING_DONG = 2 // HIGH state immediatly after push, LOW state after release
    };
    
    typedef struct {
      int sensorId;
      int relay;
      int button;
      uint8_t relayOptions;
      ButtonType buttonType;
      const char * relayDescription;
    } RelayButton;
    
    // CONFIGURE ONLY THIS ARRAY!
    // Row params: sensor ID - sensor ID reported on MySensor Gateway
    //             relay pin - Expander supported
    //             button pin - <0 for virtual buttons (only available in MySensor Gateway); no support for Expander
    //             relay options - [RELAY_TRIGGER_LOW|RELAY_TRIGGER_HIGH] {RELAY_STARTUP_ON|RELAY_STARTUP_OFF}
    //             button type - [MONO_STABLE|BI_STABLE|DING_DONG]
    //             relay description - reported on MySensor Gateway, can help identify device on initial configuration in Home Automation App, can be empty ("")
    RelayButton myRelayButtons[] = {
      {0, 2, A0, RELAY_TRIGGER_LOW, MONO_STABLE, "Ł2 - kinkiet [C10]"},  // WŁ: Ł2
      {1, 16, A1, RELAY_TRIGGER_LOW, BI_STABLE, "Salon 2 [A9]"},  // WŁ: Salon 2
      {2, 15, A2, RELAY_TRIGGER_LOW, BI_STABLE, "Salon 1 [A10]"},  // WŁ: Salon 1
      {3, E(0,1), A3, RELAY_TRIGGER_LOW | RELAY_STARTUP_OFF, BI_STABLE, "Halogen - Taras [B8]"},  // WŁ: Taras
      {4, 22, A4, RELAY_TRIGGER_LOW, BI_STABLE, "Kuchnia [B2]"},  // WŁ: Kuchnia 1
      {5, 23, A5, RELAY_TRIGGER_LOW, BI_STABLE, "Kuchnia - Kinkiet [B3]"},  // WŁ: Kuchnia 2
      {6, 28, A6, RELAY_TRIGGER_LOW, BI_STABLE, "Jadalnia 2 [A4]"},  // WŁ: Hall I/Jadalnia prawy
      {17, 17, A7, RELAY_TRIGGER_LOW, BI_STABLE, "Ł1 - Kinkiet [A11]"},  // WŁ: Hall I/Ł1 prawy
      {8, 31, A8, RELAY_TRIGGER_LOW, MONO_STABLE, "Garaż [A7]"},  // WŁ: Kotłownia/Garaż
      {8, 31, A9, RELAY_TRIGGER_LOW, MONO_STABLE, "Garaż [A7]"},  // WŁ: Garaż
      {10, 14, A10, RELAY_TRIGGER_LOW | RELAY_STARTUP_ON, BI_STABLE, "Halogen - wejście [B4]"},  // WŁ: Drzwi wejściowe
      {11, E(0,7), A11, RELAY_TRIGGER_LOW, DING_DONG, "Dzwonek [?]"},  // WŁ: Dzwonek
      {12, 29, A12, RELAY_TRIGGER_LOW, BI_STABLE, "Hall 1 [A5]"},  // WŁ: Hall I/Jadalnia lewy
      {12, 29, A13, RELAY_TRIGGER_LOW, BI_STABLE, "Hall 1 [A5]"},  // WŁ: Hall I/Wiatrołap
      {14, 32, A14, RELAY_TRIGGER_LOW, BI_STABLE, "Wiatrołap [A8]"},  // WŁ: Wiatrołap/Hall I
      {15, 19, A15, RELAY_TRIGGER_LOW, MONO_STABLE, "Kotłownia [B1]"},  // WŁ: Kotłownia/Hall I
      {16, 24, 53, RELAY_TRIGGER_LOW, BI_STABLE, "Ł1 - Taśma LED [C1]"},  // WŁ: Hall I/Ł1 środek
      {17, 17, 52, RELAY_TRIGGER_LOW, MONO_STABLE, "Ł1 - Kinkiet [A11]"},  // WŁ: Ł1
      {18, 18, 51, RELAY_TRIGGER_LOW, BI_STABLE, "Ł1 [A12]"},  // WŁ: Hall I/Ł1 lewy
      {19, 6, 50, RELAY_TRIGGER_LOW, BI_STABLE, "Klatka Schodowa [B7]"},  // WŁ: Hall I/Schody 1
      {12, 29, 49, RELAY_TRIGGER_LOW, BI_STABLE, "Hall 1 [A5]"},  // WŁ: Hall I/Schody 2
      {21, 26, 48, RELAY_TRIGGER_LOW, BI_STABLE, "Gabinet [A2]"},  // WŁ: Gabinet
      {22, 7, 47, RELAY_TRIGGER_LOW, BI_STABLE, "Hall 2 [B5]"},  // WŁ: Hall II/Schody 1
      {19, 6, 46, RELAY_TRIGGER_LOW, BI_STABLE, "Klatka Schodowa [B7]"},  // WŁ: Hall II/Schody 2
      {24, 10, 45, RELAY_TRIGGER_LOW, BI_STABLE, "Garderoba [C12]"},  // WŁ: Garderoba
      {25, 4, 44, RELAY_TRIGGER_LOW, MONO_STABLE, "Pok. nad kuchnią 2 [B10]"},  // WŁ: Pok. nad kuchnią 2
      {26, 5, 43, RELAY_TRIGGER_LOW, BI_STABLE, "Pok. nad kuchnią 1 [B9]"},  // WŁ: Pok. nad kuchnią 1
      {27, 8, 42, RELAY_TRIGGER_LOW, BI_STABLE, "Pok. nad salonem 2 [B12]"},  // WŁ: Pok. nad salonem 2
      {28, 9, 41, RELAY_TRIGGER_LOW, MONO_STABLE, "Pok. nad salonem 1 [B11]"},  // WŁ: Pok. nad salonem 1
      {29, 3, 40, RELAY_TRIGGER_LOW, BI_STABLE, "Ł2 [C7]"},  // WŁ: Hall II/Ł2 1
      {30, E(0,3), 39, RELAY_TRIGGER_LOW, BI_STABLE, "Ł2 - Taśma LED [?]"},  // WŁ: Hall II/Ł2 2
      {22, 7, 38, RELAY_TRIGGER_LOW, BI_STABLE, "Hall 2 [B5]"},  // WŁ: Hall II/Sypialnia
      {32, 11, 37, RELAY_TRIGGER_LOW, BI_STABLE, "Sypialnia 2 [C9]"},  // WŁ: Sypialnia 2
      {33, 12, 36, RELAY_TRIGGER_LOW, BI_STABLE, "Sypialnia 1 [C8]"},  // WŁ: Sypialnia 1
      {34, 25, -1, RELAY_TRIGGER_LOW | RELAY_STARTUP_ON, MONO_STABLE, "Halogen - Garaż [A1]"},  // WŁ: Virtual Button 1
      {35, 30, -2, RELAY_TRIGGER_LOW | RELAY_STARTUP_OFF, MONO_STABLE, "Ł1 - Wentylator [A3]"},  // WŁ: Virtual Button 2
      {36, E(0,2), -3, RELAY_TRIGGER_LOW | RELAY_STARTUP_OFF, MONO_STABLE, "Halogen - wschód [B6]"},  // WŁ: Virtual Button 3
      {37, E(0,4), -4, RELAY_TRIGGER_LOW, MONO_STABLE, "Lampki schodowe [C6]"},  // WŁ: Virtual Button 4
      {38, E(0,5), -5, RELAY_TRIGGER_LOW, MONO_STABLE, "Lampki podłogowe I [C4]"},  // WŁ: Virtual Button 5
      {39, E(0,6), -6, RELAY_TRIGGER_LOW, MONO_STABLE, "Lampki podłogowe II [C2]"},  // WŁ: Virtual Button 6
      {40, E(0,0), -7, RELAY_TRIGGER_LOW | RELAY_STARTUP_OFF, MONO_STABLE, "Ł2 - wentylator [C11]"},  // WŁ: Virtual Button 7
    };
    
    const int numberOfRelayButtons = sizeof(myRelayButtons) / sizeof(RelayButton);
    
    typedef struct {
      int firstButton;
      int nextButton;
    } RelayMultiButtons;
    
    RelayMultiButtons relayMultiButtons[numberOfRelayButtons];
    uint8_t myRelayState[numberOfRelayButtons];
    
    // MySensors - Sending Data
    // To send data you have to create a MyMessage container to hold the information.
    MyMessage msgs[numberOfRelayButtons];
    
    Bounce myButtonDebouncer[numberOfRelayButtons];
    
    //Function Declaration
    uint8_t loadRelayState(int relayNum, uint8_t forceEeprom = 0);
    void saveRelayState(int relayNum, uint8_t state, uint8_t useEeprom);
    void saveRelayState(int relayNum, uint8_t state);
    void changeRelayState(int relayNum, uint8_t relayState);
    
    
    
    // MySensors - This will execute before MySensors starts up
    void before() {
      Serial.begin(115200);
      
      #ifdef USE_EXPANDER
        /* Start I2C bus and PCF8574 instance */
        for(int i = 0; i < numberOfExpanders; i++) {
          expander[i].begin(expanderAddresses[i]);
        }
      #endif
      
      // initialize multiple buttons list structure
      for (int i = 0; i < numberOfRelayButtons; i++) {
        relayMultiButtons[i].firstButton = -1;
        relayMultiButtons[i].nextButton = -1;
      }
      // find multiple buttons for the same relay (uni-directional list)
      for (int i = 0; i < numberOfRelayButtons-1; i++) {
        if (relayMultiButtons[i].firstButton == -1) {
          int prevRelayButton = i;
          for (int j = i+1; j < numberOfRelayButtons; j++) {
            if (myRelayButtons[i].relay == myRelayButtons[j].relay) {
              relayMultiButtons[prevRelayButton].firstButton = i;
              relayMultiButtons[prevRelayButton].nextButton = j;
              relayMultiButtons[j].firstButton = i;
              prevRelayButton = j;
            }
          }
        }
      }
      
      // if version has changed, reset state of all relays
      int versionChangeResetState = (MULTI_RELAY_VERSION == loadState(0) ) ? 0 : 1;
      
      for (int i = 0; i < numberOfRelayButtons; i++) {
        // if this relay has multiple buttons, load only first
        if (relayMultiButtons[i].firstButton == -1 || relayMultiButtons[i].firstButton == i) {
          // Then set relay pins in output mode
          #ifdef USE_EXPANDER
            if ( myRelayButtons[i].relay & 0xff00 ) {
              // EXPANDER
              int expanderNo = (myRelayButtons[i].relay >> 8) - 1;
              int expanderPin = myRelayButtons[i].relay & 0xff;
              expander[expanderNo].pinMode(expanderPin, OUTPUT);
            } else {
          #endif
              pinMode(myRelayButtons[i].relay, OUTPUT);
          #ifdef USE_EXPANDER
            }
          #endif
          
          uint8_t isTurnedOn = 0;
          
          if (myRelayButtons[i].relayOptions & RELAY_STARTUP_ON) {
            isTurnedOn = 1;
          } else if (myRelayButtons[i].relayOptions & RELAY_STARTUP_OFF) {
          } else {
            // Set relay to last known state (using eeprom storage)
            isTurnedOn = loadRelayState(i, 1); // 1 - true, 0 - false
            if (versionChangeResetState && isTurnedOn) {
              saveRelayState(i, 0, 1);
              isTurnedOn = 0;
            }
          }
    
          changeRelayState(i, isTurnedOn);
          myRelayState[i] = isTurnedOn;
        }
      }
      if (versionChangeResetState) {
        // version has changed, so store new version in eeporom
        saveState(0, MULTI_RELAY_VERSION);
      }
    }
    
    // executed AFTER mysensors has been initialised
    void setup() {
      for(int i = 0; i < numberOfRelayButtons; i++) {
        if (myRelayButtons[i].button >= 0) {
          // No Expander support for buttons (de-bouncing)
          pinMode(myRelayButtons[i].button, INPUT_PULLUP); // HIGH state when button is not pushed
        }
      }
      // Setup locally attached sensors
      delay(5000);
      // Send state to MySensor Gateway
      for(int i = 0; i < numberOfRelayButtons; i++) {
        // if this relay has multiple buttons, send only first
        if (relayMultiButtons[i].firstButton == -1 || relayMultiButtons[i].firstButton == i) {
          msgs[i] = MyMessage(myRelayButtons[i].sensorId, V_LIGHT);
          uint8_t relayState;
          if (myRelayButtons[i].relayOptions & RELAY_STARTUP_ON) {
            relayState = 1;
          } else if (myRelayButtons[i].relayOptions & RELAY_STARTUP_OFF) {
            relayState = 0;
          } else {
            relayState = loadRelayState(i);
          }
          send(msgs[i].set(relayState)); // send current state
        }
      }
      // Setup buttons
      for(int i = 0; i < numberOfRelayButtons; i++) {
        if (myRelayButtons[i].button >= 0) {
          // setup debouncer
          myButtonDebouncer[i] = Bounce();
          myButtonDebouncer[i].attach(myRelayButtons[i].button);
          myButtonDebouncer[i].interval(50);
        }
      }
    }
    
    void loop() {
      for(int i = 0; i < numberOfRelayButtons; i++) {
        if (myRelayButtons[i].button >= 0 && myButtonDebouncer[i].update()) {
          int buttonState = myButtonDebouncer[i].read();
          #ifdef MY_DEBUG
            Serial.print("# Button ");
            Serial.print(i);
            Serial.print(" changed to: ");
            Serial.println(buttonState);
          #endif
          
          int relayNum = (relayMultiButtons[i].firstButton == -1) ? i : relayMultiButtons[i].firstButton;
          
          if (myRelayButtons[i].buttonType == DING_DONG) {
            if (buttonState == LOW) { // button pressed
              changeRelayState(relayNum, 1);
              send(msgs[relayNum].set(1));
            } else { // button released
              changeRelayState(relayNum, 0);
              send(msgs[relayNum].set(0));
            }
          } else if (myRelayButtons[i].buttonType == BI_STABLE || buttonState == HIGH) {
            // If button type is BI_STABLE, any change will toggle relay state
            // For MONO_STABLE, button must be pushed and released (HIGH)
            uint8_t isTurnedOn = ! loadRelayState(relayNum); // 1 - true, 0 - false
            changeRelayState(relayNum, isTurnedOn);
            send(msgs[relayNum].set(isTurnedOn));
            saveRelayState(relayNum, isTurnedOn);
          }
        }
      }
    }
    
    
    
    // MySensors - Presentation
    // Your sensor must first present itself to the controller.
    // The presentation is a hint to allow controller prepare for the sensor data that eventually will come.
    // Executed after "before()" and before "setup()" in: _begin (MySensorsCore.cpp) > gatewayTransportInit() > presentNode()
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Multi Relay", "1.2");
      
      // Register every relay as separate sensor
      for (int i = 0; i < numberOfRelayButtons; i++) {
        // if this relay has multiple buttons, register only first
        if (relayMultiButtons[i].firstButton == -1 || relayMultiButtons[i].firstButton == i) {
          // Register all sensors to gw (they will be created as child devices)
          // void present(uint8_t childSensorId, uint8_t sensorType, const char *description, bool ack);
          //   childSensorId - The unique child id you want to choose for the sensor connected to this Arduino. Range 0-254.
          //   sensorType - The sensor type you want to create.
          //   description An optional textual description of the attached sensor.
          //   ack - Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
          present(myRelayButtons[i].sensorId, S_BINARY, myRelayButtons[i].relayDescription);
        }
      }
    }
    
    
    // MySensors - Handling incoming messages
    // Nodes that expects incoming data, such as an actuator or repeating nodes,
    // must implement the receive() - function to handle the incoming messages.
    // Do not sleep a node where you expect incoming data or you will lose messages.
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        uint8_t isTurnedOn = message.getBool(); // 1 - true, 0 - false
        changeRelayState(message.sensor, isTurnedOn);
        // Store state in eeprom if changed
        if (loadRelayState(message.sensor) != isTurnedOn) {
          saveRelayState(message.sensor, isTurnedOn);
        }
        send(msgs[message.sensor].set(isTurnedOn)); // support for OPTIMISTIC=FALSE (Home Asistant)
        #ifdef MY_DEBUG
          // Write some debug info
          Serial.print("# Incoming change for sensor: " + message.sensor);
          Serial.println(", New status: " + isTurnedOn);
        #endif
      }
    }
    
    uint8_t loadRelayState(int relayNum, uint8_t forceEeprom) {
      uint8_t relayState;
      if (forceEeprom) {
        relayState = loadState(RELAY_STATE_STORAGE + relayNum);
      } else {
        relayState = myRelayState[relayNum];
      }
      #ifdef MY_DEBUG
        Serial.print("# loadRelayState: ");
        Serial.print(relayNum);
        if (forceEeprom) {
          Serial.print("(byte ");
          Serial.print(RELAY_STATE_STORAGE + relayNum);
          Serial.print(")");
        }
        Serial.print(" = ");
        Serial.println(relayState);
      #endif
      return(relayState);
    }
    
    void saveRelayState(int relayNum, uint8_t state, uint8_t useEeprom) {
      
      int mainRelayNum = (relayMultiButtons[relayNum].firstButton == -1) ? relayNum : relayMultiButtons[relayNum].firstButton;
      
      myRelayState[mainRelayNum] = state;
      if (useEeprom && (relayNum == mainRelayNum)) {
        saveState(RELAY_STATE_STORAGE + mainRelayNum, state);
      }
      
      int nextButton = mainRelayNum;
      // update all buttons
      while ((nextButton = relayMultiButtons[nextButton].nextButton) != -1) {
        myRelayState[nextButton] = state;
      };
    }
    
    void saveRelayState(int relayNum, uint8_t state) {
      uint8_t useEeprom = ((myRelayButtons[relayNum].relayOptions & RELAY_STARTUP_MASK) == 0);
      saveRelayState(relayNum, state, useEeprom);
    }
    
    void changeRelayState(int relayNum, uint8_t relayState) {
      
      uint8_t relayTrigger = myRelayButtons[relayNum].relayOptions & RELAY_TRIGGER_HIGH;
      uint8_t digitalOutState = relayState ? relayTrigger : ! relayTrigger;
      
      #ifdef USE_EXPANDER
        if ( myRelayButtons[relayNum].relay & 0xff00 ) {
          int expanderNo = (myRelayButtons[relayNum].relay >> 8) - 1;
          int expanderPin = myRelayButtons[relayNum].relay & 0xff;
          expander[expanderNo].digitalWrite(expanderPin, digitalOutState);
        } else {
      #endif
        digitalWrite(myRelayButtons[relayNum].relay, digitalOutState);
      #ifdef USE_EXPANDER
        }
      #endif
    }
    

    And in Home assistant show example decription: Ł2 - kinkiet [C10] or for second Salon 2 [A9] etc.
    Please help me... because it will be good to identify sensors when add to Controller.



Suggested Topics

  • 2
  • 7
  • 1
  • 4
  • 7
  • 2
  • 1
  • 3

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts