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. Announcements
  3. 💬 Temperature Sensor

💬 Temperature Sensor

Scheduled Pinned Locked Moved Announcements
171 Posts 40 Posters 54.8k Views 36 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.
  • G Offline
    G Offline
    gewoonandre
    wrote on last edited by
    #44

    Is it also possible to upload the example code to a Arduino Nano, and connect it via USB to a Windows PC with Domoticz?

    Or will it only work with the radio modules (and extra gateway?)

    1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #45

      I think you can use the serial gateway sketch and the sensor code in setup and loop

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CurtisMack
        wrote on last edited by
        #46

        Thank you for posting this project!

        Could like to suggest a small change to the sketch. There are several posts out there about this not running with the latest DallasTemperature library. The call to sensors.millisToWaitForConversion won't compile because the method is not public. From another post I learned that this method is very simple and could be included in the sketch. I would suggest changing the line

        int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());

        to read

        int16_t conversionTime = millisToWaitForConversion(sensors.getResolution());

        and add this function to the sketch:

        int16_t millisToWaitForConversion(uint8_t bitResolution)
        {
        switch (bitResolution)
        {
        case 9:
        return 94;
        case 10:
        return 188;
        case 11:
        return 375;
        default:
        return 750;
        }
        }

        In any case, this might help the next person using this project.

        1 Reply Last reply
        1
        • WoekaW Offline
          WoekaW Offline
          Woeka
          wrote on last edited by
          #47

          I had some problems with Domoticz triggering an event based on a certain temperature. When using multiple DS18B20 it is a good idea to use the ID of the sensors to always get the same order of sensors. If one is not read, for example B of A, B, C then C becomes B. See: https://forum.mysensors.org/topic/4143/about-ds18b20-onewire for more info. You maybe need to adjust the sketch a bit for your number of sensors.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rene046
            wrote on last edited by
            #48

            @CurtisMack

            could you explain how to solve this ?
            I'm no pro, and wanted to use 1 single DS1820 in a sketch and also got same message about :
            int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());

            Mysensors is hard hard nut to learn, specially because of a lot of changes in the newer version 2.1.0 ...(and im still learning to work with arduino software , i keep running often against old sketches i like to try as sample to use them in other sketches , (my way of learning) but than they dont work with latest version. because something changed.
            but i love the mysensors functions.

            p.s sorry for my bad english....

            K 1 Reply Last reply
            0
            • R Rene046

              @CurtisMack

              could you explain how to solve this ?
              I'm no pro, and wanted to use 1 single DS1820 in a sketch and also got same message about :
              int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());

              Mysensors is hard hard nut to learn, specially because of a lot of changes in the newer version 2.1.0 ...(and im still learning to work with arduino software , i keep running often against old sketches i like to try as sample to use them in other sketches , (my way of learning) but than they dont work with latest version. because something changed.
              but i love the mysensors functions.

              p.s sorry for my bad english....

              K Offline
              K Offline
              kontrollable
              wrote on last edited by
              #49

              @Rene046 The solution is here:https://forum.mysensors.org/topic/4828/temperature-sensor/46

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rene046
                wrote on last edited by
                #50

                I sofar got the sketch running and giving good data on serial and into domoticz

                i only could use some help with how to sent my Temperature in Celsius to domoticz to
                i tried to use:
                send(msgTemp.set(insideThermometer ,2));
                and
                send(msgTemp.set(tempC,2));

                both without luck, the first did transmit some data but wrong numbers
                second did nothing think because with this i done i does not know what address the DS1820 is on.

                Im learning every day something new....

                Maybe someone could help,

                G 1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rene046
                  wrote on last edited by
                  #51

                  forgot the sketch

                  // Enable debug prints to serial monitor
                  #define MY_DEBUG
                  
                  // Enable and select radio type attached
                  #define MY_RADIO_NRF24
                  //#define MY_RADIO_RFM69
                  
                  #include <SPI.h>
                  #include <MySensors.h>  
                  #include <BH1750.h>
                  #include <Wire.h>
                  #include <OneWire.h>
                  #include <DallasTemperature.h>
                  
                  // Data wire is plugged into port 3 on the Arduino
                  #define ONE_WIRE_BUS 3
                  
                  // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
                  OneWire oneWire(ONE_WIRE_BUS);
                  
                  // Pass our oneWire reference to Dallas Temperature. 
                  DallasTemperature sensors(&oneWire);
                  
                  // arrays to hold device address
                  DeviceAddress insideThermometer;
                  
                  
                  unsigned long SLEEP_TIME = 180000; // Sleep time between reads (in milliseconds)
                  int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                  int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
                  #define CHILD_ID_BATTERY 4
                  #define CHILD_ID_SOLAR 6
                  #define CHILD_ID_LIGHT 1
                  #define CHILD_ID_TEMP1 2
                  
                  BH1750 lightSensor;
                  
                  int oldBatteryPcnt = 0;
                  MyMessage msgTemp(CHILD_ID_TEMP1,V_TEMP);
                  MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                  MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
                  MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                  uint16_t lastlux;  //lux
                  
                  void setup(void)
                  {
                  // use the 1.1 V internal reference
                    #if defined(__AVR_ATmega2560__)
                    analogReference(INTERNAL1V1);
                    #else
                    analogReference(INTERNAL);
                    #endif
                  
                    
                  // Send the sketch version information to the gateway and Controller
                    sendSketchInfo("Battery Meter", "2.0");
                    present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                   
                    //Serial.begin(9600); // start serial port
                    Serial.print("Locating devices..."); // locate devices on the bus
                    sensors.begin(); // start reading sensor
                    Serial.print("Found ");
                    Serial.print(sensors.getDeviceCount(), DEC);
                    Serial.println(" devices.");
                    Serial.print("Parasite power is: "); // report parasite power requirements
                    
                    if (sensors.isParasitePowerMode()) Serial.println("ON");
                    else Serial.println("OFF"); 
                  // assign address manually.  the addresses below will beed to be changed
                    // to valid device addresses on your bus.  device address can be retrieved
                    // by using either oneWire.search(deviceAddress) or individually via
                    // sensors.getAddress(deviceAddress, index)
                    //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
                  
                    // Method 1:
                    // search for devices on the bus and assign based on an index.  ideally,
                    // you would do this to initially discover addresses on the bus and then 
                    // use those addresses and manually assign them (see above) once you know 
                    // the devices on your bus (and assuming they don't change).
                    if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
                    
                    // method 2: search()
                    // search() looks for the next device. Returns 1 if a new address has been
                    // returned. A zero might mean that the bus is shorted, there are no devices, 
                    // or you have already retrieved all of them.  It might be a good idea to 
                    // check the CRC to make sure you didn't get garbage.  The order is 
                    // deterministic. You will always get the same devices in the same order
                    //
                    // Must be called before search()
                    //oneWire.reset_search();
                    // assigns the first address found to insideThermometer
                    //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
                  
                    // show the addresses we found on the bus
                    Serial.print("Device 0 Address: ");
                    printAddress(insideThermometer);
                    Serial.println();
                  
                    // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
                    sensors.setResolution(insideThermometer, 9);
                   
                    Serial.print("Device 0 Resolution: ");
                    Serial.print(sensors.getResolution(insideThermometer), DEC); 
                    Serial.println();  
                  }
                  
                  // function to print the temperature for a device
                  void printTemperature(DeviceAddress deviceAddress)
                  {
                    // method 1 - slower
                    //Serial.print("Temp C: ");
                    //Serial.print(sensors.getTempC(deviceAddress));
                    //Serial.print(" Temp F: ");
                    //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
                  
                    // method 2 - faster
                    float tempC = sensors.getTempC(deviceAddress);
                    Serial.print("Temp C: ");
                    Serial.print(tempC);
                    Serial.print(" Temp F: ");
                    Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
                  }
                  
                  
                  
                  void loop(void)
                  {
                    // call sensors.requestTemperatures() to issue a global temperature 
                    // request to all devices on the bus
                    Serial.print("Requesting temperatures...");
                    sensors.requestTemperatures(); // Send the command to get temperatures
                    Serial.println("DONE");
                    
                    
                    // It responds almost immediately. Let's print out the data
                    printTemperature(insideThermometer); // Use a simple function to print out the data
                  {
                  
                   // get the battery Voltage
                    int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
                    delay(1000);   
                    
                    int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
                    delay(100);
                    
                    int sensorValue = analogRead(BATTERY_SENSE_PIN);
                    delay(1000);
                      
                    int sensorValueS = analogRead(SOLAR_SENSE_PIN);
                    delay(1000);
                   
                  #ifdef MY_DEBUG
                    Serial.print("Battery Voltage2: ");
                    Serial.println(sensorValue2);
                    Serial.print("Battery Voltage1: ");
                    Serial.println(sensorValue1);
                    Serial.print("Battery Voltage: ");
                    Serial.println(sensorValue);
                    Serial.print("Solar Voltage: ");
                    Serial.println(sensorValueS);
                  #endif
                  
                    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                    // 3.44/1023 = Volts per bit = 0.003363075
                    const float vRef = 4.200/ 1.05/ 1023 ;  
                    int batteryPcnt = sensorValue / 10;
                    float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
                    float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
                    
                  #ifdef MY_DEBUG
                    Serial.print("Battery Voltage: ");
                    Serial.print(batteryV);
                    Serial.println(" V");
                  send(msgbatt.set(batteryV ,2));
                  
                    Serial.print("Solar Voltage: ");
                    Serial.print(batteryS);
                    Serial.println(" V");
                  send(msgsolar.set(batteryS ,2));
                  
                    Serial.print("Battery percent: ");
                    Serial.print(batteryPcnt);
                    Serial.println(" %");
                  #endif
                  
                     if (oldBatteryPcnt != batteryPcnt) {
                      // Power up radio after sleep
                      sendBatteryLevel(batteryPcnt);
                      oldBatteryPcnt = batteryPcnt;
                    }
                        
                    uint16_t lux = lightSensor.readLightLevel();// Get Lux value
                    Serial.println(lux);
                    if (lux != lastlux) {
                        send(msg.set(lux));
                        lastlux = lux;
                    }
                    sleep(SLEEP_TIME);
                  }}
                  
                  // function to print a device address
                  void printAddress(DeviceAddress deviceAddress)
                  {
                    for (uint8_t i = 0; i < 8; i++)
                    {
                      if (deviceAddress[i] < 16) Serial.print("0");
                      Serial.print(deviceAddress[i], HEX);
                    }
                  }
                    
                  
                  
                  
                  
                  C 1 Reply Last reply
                  0
                  • R Rene046

                    forgot the sketch

                    // Enable debug prints to serial monitor
                    #define MY_DEBUG
                    
                    // Enable and select radio type attached
                    #define MY_RADIO_NRF24
                    //#define MY_RADIO_RFM69
                    
                    #include <SPI.h>
                    #include <MySensors.h>  
                    #include <BH1750.h>
                    #include <Wire.h>
                    #include <OneWire.h>
                    #include <DallasTemperature.h>
                    
                    // Data wire is plugged into port 3 on the Arduino
                    #define ONE_WIRE_BUS 3
                    
                    // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
                    OneWire oneWire(ONE_WIRE_BUS);
                    
                    // Pass our oneWire reference to Dallas Temperature. 
                    DallasTemperature sensors(&oneWire);
                    
                    // arrays to hold device address
                    DeviceAddress insideThermometer;
                    
                    
                    unsigned long SLEEP_TIME = 180000; // Sleep time between reads (in milliseconds)
                    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                    int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
                    #define CHILD_ID_BATTERY 4
                    #define CHILD_ID_SOLAR 6
                    #define CHILD_ID_LIGHT 1
                    #define CHILD_ID_TEMP1 2
                    
                    BH1750 lightSensor;
                    
                    int oldBatteryPcnt = 0;
                    MyMessage msgTemp(CHILD_ID_TEMP1,V_TEMP);
                    MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                    MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
                    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                    uint16_t lastlux;  //lux
                    
                    void setup(void)
                    {
                    // use the 1.1 V internal reference
                      #if defined(__AVR_ATmega2560__)
                      analogReference(INTERNAL1V1);
                      #else
                      analogReference(INTERNAL);
                      #endif
                    
                      
                    // Send the sketch version information to the gateway and Controller
                      sendSketchInfo("Battery Meter", "2.0");
                      present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                     
                      //Serial.begin(9600); // start serial port
                      Serial.print("Locating devices..."); // locate devices on the bus
                      sensors.begin(); // start reading sensor
                      Serial.print("Found ");
                      Serial.print(sensors.getDeviceCount(), DEC);
                      Serial.println(" devices.");
                      Serial.print("Parasite power is: "); // report parasite power requirements
                      
                      if (sensors.isParasitePowerMode()) Serial.println("ON");
                      else Serial.println("OFF"); 
                    // assign address manually.  the addresses below will beed to be changed
                      // to valid device addresses on your bus.  device address can be retrieved
                      // by using either oneWire.search(deviceAddress) or individually via
                      // sensors.getAddress(deviceAddress, index)
                      //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
                    
                      // Method 1:
                      // search for devices on the bus and assign based on an index.  ideally,
                      // you would do this to initially discover addresses on the bus and then 
                      // use those addresses and manually assign them (see above) once you know 
                      // the devices on your bus (and assuming they don't change).
                      if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
                      
                      // method 2: search()
                      // search() looks for the next device. Returns 1 if a new address has been
                      // returned. A zero might mean that the bus is shorted, there are no devices, 
                      // or you have already retrieved all of them.  It might be a good idea to 
                      // check the CRC to make sure you didn't get garbage.  The order is 
                      // deterministic. You will always get the same devices in the same order
                      //
                      // Must be called before search()
                      //oneWire.reset_search();
                      // assigns the first address found to insideThermometer
                      //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
                    
                      // show the addresses we found on the bus
                      Serial.print("Device 0 Address: ");
                      printAddress(insideThermometer);
                      Serial.println();
                    
                      // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
                      sensors.setResolution(insideThermometer, 9);
                     
                      Serial.print("Device 0 Resolution: ");
                      Serial.print(sensors.getResolution(insideThermometer), DEC); 
                      Serial.println();  
                    }
                    
                    // function to print the temperature for a device
                    void printTemperature(DeviceAddress deviceAddress)
                    {
                      // method 1 - slower
                      //Serial.print("Temp C: ");
                      //Serial.print(sensors.getTempC(deviceAddress));
                      //Serial.print(" Temp F: ");
                      //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
                    
                      // method 2 - faster
                      float tempC = sensors.getTempC(deviceAddress);
                      Serial.print("Temp C: ");
                      Serial.print(tempC);
                      Serial.print(" Temp F: ");
                      Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
                    }
                    
                    
                    
                    void loop(void)
                    {
                      // call sensors.requestTemperatures() to issue a global temperature 
                      // request to all devices on the bus
                      Serial.print("Requesting temperatures...");
                      sensors.requestTemperatures(); // Send the command to get temperatures
                      Serial.println("DONE");
                      
                      
                      // It responds almost immediately. Let's print out the data
                      printTemperature(insideThermometer); // Use a simple function to print out the data
                    {
                    
                     // get the battery Voltage
                      int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
                      delay(1000);   
                      
                      int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
                      delay(100);
                      
                      int sensorValue = analogRead(BATTERY_SENSE_PIN);
                      delay(1000);
                        
                      int sensorValueS = analogRead(SOLAR_SENSE_PIN);
                      delay(1000);
                     
                    #ifdef MY_DEBUG
                      Serial.print("Battery Voltage2: ");
                      Serial.println(sensorValue2);
                      Serial.print("Battery Voltage1: ");
                      Serial.println(sensorValue1);
                      Serial.print("Battery Voltage: ");
                      Serial.println(sensorValue);
                      Serial.print("Solar Voltage: ");
                      Serial.println(sensorValueS);
                    #endif
                    
                      // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                      // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                      // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                      // 3.44/1023 = Volts per bit = 0.003363075
                      const float vRef = 4.200/ 1.05/ 1023 ;  
                      int batteryPcnt = sensorValue / 10;
                      float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
                      float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
                      
                    #ifdef MY_DEBUG
                      Serial.print("Battery Voltage: ");
                      Serial.print(batteryV);
                      Serial.println(" V");
                    send(msgbatt.set(batteryV ,2));
                    
                      Serial.print("Solar Voltage: ");
                      Serial.print(batteryS);
                      Serial.println(" V");
                    send(msgsolar.set(batteryS ,2));
                    
                      Serial.print("Battery percent: ");
                      Serial.print(batteryPcnt);
                      Serial.println(" %");
                    #endif
                    
                       if (oldBatteryPcnt != batteryPcnt) {
                        // Power up radio after sleep
                        sendBatteryLevel(batteryPcnt);
                        oldBatteryPcnt = batteryPcnt;
                      }
                          
                      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
                      Serial.println(lux);
                      if (lux != lastlux) {
                          send(msg.set(lux));
                          lastlux = lux;
                      }
                      sleep(SLEEP_TIME);
                    }}
                    
                    // function to print a device address
                    void printAddress(DeviceAddress deviceAddress)
                    {
                      for (uint8_t i = 0; i < 8; i++)
                      {
                        if (deviceAddress[i] < 16) Serial.print("0");
                        Serial.print(deviceAddress[i], HEX);
                      }
                    }
                      
                    
                    
                    
                    
                    C Offline
                    C Offline
                    CurtisMack
                    wrote on last edited by
                    #52

                    @Rene046 Your sketch doesn't show either of the send commands you say you are attempting. This makes it difficult to determine why it wouldn't be working. I am relative new to all of this as well, and use Vera not a Domoticz. On the first version that is sending a message, what data is getting through? I would try putting the "send" command someplace in the code where msgTemp has already been set. Then just call send(msgTemp).

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rene046
                      wrote on last edited by
                      #53

                      hi

                      Then i would have placed this in the loop>
                      The error i get then is :'TempC' was not declared in this scope

                      #ifdef MY_DEBUG
                      Serial.print("DS1820 Temperature: ");
                        Serial.print(tempC);
                        Serial.println(" C");
                      send(msgTemp.set(tempC ,2));
                        
                        Serial.print("Battery Voltage: ");
                        Serial.print(batteryV);
                        Serial.println(" V");
                      send(msgbatt.set(batteryV ,2));```
                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        Rene046
                        wrote on last edited by
                        #54

                        so far my result.

                        had to use float TempC = 50.5 at the start
                        but in my serial i get the right temperature,
                        but using it in the loop gives me still wrong number 50.5 so gets not updated.

                        // Enable debug prints to serial monitor
                        #define MY_DEBUG
                        
                        // Enable and select radio type attached
                        #define MY_RADIO_NRF24
                        //#define MY_RADIO_RFM69
                        
                        #include <SPI.h>
                        #include <MySensors.h>  
                        #include <BH1750.h>
                        #include <Wire.h>
                        #include <OneWire.h>
                        #include <DallasTemperature.h>
                        
                        // Data wire is plugged into port 3 on the Arduino
                        #define ONE_WIRE_BUS 3
                        
                        // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
                        OneWire oneWire(ONE_WIRE_BUS);
                        
                        // Pass our oneWire reference to Dallas Temperature. 
                        DallasTemperature sensors(&oneWire);
                        
                        // arrays to hold device address
                        DeviceAddress insideThermometer;
                        
                        
                        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
                        int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                        int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
                        #define CHILD_ID_BATTERY 4
                        #define CHILD_ID_SOLAR 6
                        #define CHILD_ID_LIGHT 1
                        #define CHILD_ID_TEMP1 2
                        
                        BH1750 lightSensor;
                        float TempC = 50.5;
                        int oldBatteryPcnt = 0;
                        MyMessage msgTemp(CHILD_ID_TEMP1,V_TEMP);
                        MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                        MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
                        MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                        uint16_t lastlux;  //lux
                        
                        void setup(void)
                        {
                        // use the 1.1 V internal reference
                          #if defined(__AVR_ATmega2560__)
                          analogReference(INTERNAL1V1);
                          #else
                          analogReference(INTERNAL);
                          #endif
                        
                          
                        // Send the sketch version information to the gateway and Controller
                          sendSketchInfo("Battery Meter", "2.0");
                          present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                         
                          //Serial.begin(9600); // start serial port
                          Serial.print("Locating devices..."); // locate devices on the bus
                          sensors.begin(); // start reading sensor
                          Serial.print("Found ");
                          Serial.print(sensors.getDeviceCount(), DEC);
                          Serial.println(" devices.");
                          Serial.print("Parasite power is: "); // report parasite power requirements
                          
                          if (sensors.isParasitePowerMode()) Serial.println("ON");
                          else Serial.println("OFF"); 
                        // assign address manually.  the addresses below will beed to be changed
                          // to valid device addresses on your bus.  device address can be retrieved
                          // by using either oneWire.search(deviceAddress) or individually via
                          // sensors.getAddress(deviceAddress, index)
                          //insideThermometer = { 0x10, 0x9C, 0x04, 0x26, 0x01, 0x08, 0x0, 0x8C };
                        
                        
                          // Method 1:
                          // search for devices on the bus and assign based on an index.  ideally,
                          // you would do this to initially discover addresses on the bus and then 
                          // use those addresses and manually assign them (see above) once you know 
                          // the devices on your bus (and assuming they don't change).
                          if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
                          
                          // method 2: search()
                          // search() looks for the next device. Returns 1 if a new address has been
                          // returned. A zero might mean that the bus is shorted, there are no devices, 
                          // or you have already retrieved all of them.  It might be a good idea to 
                          // check the CRC to make sure you didn't get garbage.  The order is 
                          // deterministic. You will always get the same devices in the same order
                          //
                          // Must be called before search()
                          //oneWire.reset_search();
                          // assigns the first address found to insideThermometer
                          //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
                        
                          // show the addresses we found on the bus
                          Serial.print("Device 0 Address: ");
                          printAddress(insideThermometer);
                          Serial.println();
                        
                          // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
                          sensors.setResolution(insideThermometer, 9);
                         
                          Serial.print("Device 0 Resolution: ");
                          Serial.print(sensors.getResolution(insideThermometer), DEC); 
                          Serial.println();  
                        }
                        
                        // function to print the temperature for a device
                        void printTemperature(DeviceAddress deviceAddress)
                        {
                          // method 1 - slower
                          //Serial.print("Temp C: ");
                          //Serial.print(sensors.getTempC(deviceAddress));
                          //Serial.print(" Temp F: ");
                          //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
                        
                          // method 2 - faster
                          float TempC = sensors.getTempC(deviceAddress);
                          Serial.print("Temp C: ");
                          Serial.print(TempC);
                          Serial.print(" Temp F: ");
                          Serial.println(DallasTemperature::toFahrenheit(TempC)); // Converts tempC to Fahrenheit
                        }
                        
                        
                        
                        void loop(void)
                        {
                          // call sensors.requestTemperatures() to issue a global temperature 
                          // request to all devices on the bus
                          Serial.print("Requesting temperatures...");
                          sensors.requestTemperatures(); // Send the command to get temperatures
                          Serial.println("DONE");
                          
                          
                          // It responds almost immediately. Let's print out the data
                          printTemperature(insideThermometer); // Use a simple function to print out the data
                        {
                        
                         // get the battery Voltage
                          int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
                          delay(1000);   
                          
                          int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
                          delay(100);
                          
                          int sensorValue = analogRead(BATTERY_SENSE_PIN);
                          delay(1000);
                            
                          int sensorValueS = analogRead(SOLAR_SENSE_PIN);
                          delay(1000);
                         
                        #ifdef MY_DEBUG
                          Serial.print("Battery Voltage2: ");
                          Serial.println(sensorValue2);
                          Serial.print("Battery Voltage1: ");
                          Serial.println(sensorValue1);
                          Serial.print("Battery Voltage: ");
                          Serial.println(sensorValue);
                          Serial.print("Solar Voltage: ");
                          Serial.println(sensorValueS);
                        #endif
                        
                          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                          // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                          // 3.44/1023 = Volts per bit = 0.003363075
                          const float vRef = 4.200/ 1.05/ 1023 ;  
                          int batteryPcnt = sensorValue / 10;
                          float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
                          float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
                        
                        
                        #ifdef MY_DEBUG
                        Serial.print("DS1820 Temperature: ");
                          Serial.print(TempC);
                          Serial.println(" C");
                        send(msgTemp.set(TempC ,2));
                        
                          
                          Serial.print("Battery Voltage: ");
                          Serial.print(batteryV);
                          Serial.println(" V");
                        send(msgbatt.set(batteryV ,2));
                        
                          Serial.print("Solar Voltage: ");
                          Serial.print(batteryS);
                          Serial.println(" V");
                        send(msgsolar.set(batteryS ,2));
                        
                          Serial.print("Battery percent: ");
                          Serial.print(batteryPcnt);
                          Serial.println(" %");
                        #endif
                        
                           if (oldBatteryPcnt != batteryPcnt) {
                            // Power up radio after sleep
                            sendBatteryLevel(batteryPcnt);
                            oldBatteryPcnt = batteryPcnt;
                          }
                              
                          uint16_t lux = lightSensor.readLightLevel();// Get Lux value
                          Serial.println(lux);
                          if (lux != lastlux) {
                              send(msg.set(lux));
                              lastlux = lux;
                          }
                          sleep(SLEEP_TIME);
                        }}
                        
                        // function to print a device address
                        void printAddress(DeviceAddress deviceAddress)
                        {
                          for (uint8_t i = 0; i < 8; i++)
                          {
                            if (deviceAddress[i] < 16) Serial.print("0");
                            Serial.print(deviceAddress[i], HEX);
                          }
                        }
                          
                        
                        
                        
                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Rene046
                          wrote on last edited by
                          #55

                          here is my serial result.

                          45651 MCO:SLP:WUP=-1
                          Requesting temperatures...DONE
                          Temp C: 17.87 Temp F: 64.18
                          Battery Voltage2: 931
                          Battery Voltage1: 926
                          Battery Voltage: 927
                          Solar Voltage: 393
                          DS1820 Temperature: 50.50 C
                          49545 TSF:MSG:SEND,2-2-0-0,s=2,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:50.50
                          Battery Voltage: 3.95 V
                          49557 TSF:MSG:SEND,2-2-0-0,s=4,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.95
                          Solar Voltage: 4.03 V
                          49569 TSF:MSG:SEND,2-2-0-0,s=6,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:4.03
                          Battery percent: 92 %
                          1
                          49577 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
                          49584 MCO:SLP:TPD

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            Rene046
                            wrote on last edited by
                            #56

                            i dont know how but i got it now running perfect, and cleaned up a bid.
                            for if anyone could use ....

                            // Enable debug prints to serial monitor
                            #define MY_DEBUG
                            
                            // Enable and select radio type attached
                            #define MY_RADIO_NRF24
                            //#define MY_RADIO_RFM69
                            
                            #include <SPI.h>
                            #include <MySensors.h>  
                            #include <BH1750.h>
                            #include <Wire.h>
                            #include <OneWire.h>
                            #include <DallasTemperature.h>
                            
                            // Data wire is plugged into port 3 on the Arduino
                            #define ONE_WIRE_BUS 3
                            
                            // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
                            OneWire oneWire(ONE_WIRE_BUS);
                            
                            // Pass our oneWire reference to Dallas Temperature. 
                            DallasTemperature sensors(&oneWire);
                            
                            // arrays to hold device address
                            DeviceAddress insideThermometer;
                            
                            unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
                            int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                            int SOLAR_SENSE_PIN = A1;  // select the input pin for the solar sense point
                            #define CHILD_ID_BATTERY 4
                            #define CHILD_ID_SOLAR 6
                            #define CHILD_ID_LIGHT 1
                            #define CHILD_ID_TEMP1 2
                            
                            BH1750 lightSensor;
                            float TempC = 50.5;
                            int oldBatteryPcnt = 0;
                            MyMessage msgTempC(CHILD_ID_TEMP1,V_TEMP);
                            MyMessage msgbatt(CHILD_ID_BATTERY, V_VOLTAGE);
                            MyMessage msgsolar(CHILD_ID_SOLAR, V_VOLTAGE);
                            MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                            uint16_t lastlux;  //lux
                            
                            void setup(void)
                            {
                            // use the 1.1 V internal reference
                              #if defined(__AVR_ATmega2560__)
                              analogReference(INTERNAL1V1);
                              #else
                              analogReference(INTERNAL);
                              #endif
                              
                            // Send the sketch version information to the gateway and Controller
                              sendSketchInfo("Battery Meter", "2.0");
                              present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                               
                              sensors.begin(); // start reading sensor
                              
                              // search for devices on the bus and assign based on an index.  ideally,
                              if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
                              
                              // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
                              //sensors.setResolution(insideThermometer, 9);
                            }
                            
                            void loop(void)
                            {
                              
                             // get the battery Voltage
                              int sensorValue2 = analogRead(BATTERY_SENSE_PIN);
                              delay(1000);   
                              
                              int sensorValue1 = analogRead(BATTERY_SENSE_PIN);
                              delay(100);
                              
                              int sensorValue = analogRead(BATTERY_SENSE_PIN);
                              delay(1000);
                                
                              int sensorValueS = analogRead(SOLAR_SENSE_PIN);
                              delay(1000);
                             
                            #ifdef MY_DEBUG
                              Serial.print("Battery Voltage2: ");
                              Serial.println(sensorValue2);
                              Serial.print("Battery Voltage1: ");
                              Serial.println(sensorValue1);
                              Serial.print("Battery Voltage: ");
                              Serial.println(sensorValue);
                              Serial.print("Solar Voltage: ");
                              Serial.println(sensorValueS);
                            #endif
                            
                              // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                              // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                              // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                              // 3.44/1023 = Volts per bit = 0.003363075
                              const float vRef = 4.200/ 1.05/ 1023 ;  
                              int batteryPcnt = sensorValue / 10;
                              float batteryV  = sensorValue * 0.0042598 ; // 0.0038952294568380753114792412093 max 4,2 volt
                              float batteryS  = sensorValueS * 0.0102459 ; // 0.0038952294568380753114792412093 max 10 volt
                              float TempC = sensors.getTempC(insideThermometer);
                            
                            #ifdef MY_DEBUG
                              Serial.print("DS1820 Temperature: ");
                              Serial.print(TempC);
                              Serial.println(" C");
                            send(msgTempC.set(TempC ,2));
                              
                              Serial.print("Battery Voltage: ");
                              Serial.print(batteryV);
                              Serial.println(" V");
                            send(msgbatt.set(batteryV ,2));
                            
                              Serial.print("Solar Voltage: ");
                              Serial.print(batteryS);
                              Serial.println(" V");
                            send(msgsolar.set(batteryS ,2));
                            
                              Serial.print("Battery percent: ");
                              Serial.print(batteryPcnt);
                              Serial.println(" %");
                            #endif
                            
                               if (oldBatteryPcnt != batteryPcnt) {
                                // Power up radio after sleep
                                sendBatteryLevel(batteryPcnt);
                                oldBatteryPcnt = batteryPcnt;
                              }
                                  
                              uint16_t lux = lightSensor.readLightLevel();// Get Lux value
                              Serial.println(lux);
                              if (lux != lastlux) {
                                  send(msg.set(lux));
                                  lastlux = lux;
                              }
                              sleep(SLEEP_TIME);
                            }
                            
                            
                            
                            
                            1 Reply Last reply
                            0
                            • CptSlowC Offline
                              CptSlowC Offline
                              CptSlow
                              wrote on last edited by
                              #57

                              Hi,

                              Can someone push me into the right direction.
                              I followed the "tutorial" and setup a Serial Gateway. This seems to be running just fine.

                              Now i want to add a node with a temperature sensor.

                              Both are using the NRF24L radio.

                              On my node i get an errors in the serial monitor.
                              What did i do wrong?

                              0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
                              3 MCO:BGN:BFR
                              63 TSM:INIT
                              64 TSF:WUR:MS=0
                              71 !TSM:INIT:TSP FAIL
                              72 TSM:FAIL:CNT=1
                              74 TSM:FAIL:PDT
                              10077 TSM:FAIL:RE-INIT
                              10079 TSM:INIT
                              10085 !TSM:INIT:TSP FAIL
                              10088 TSM:FAIL:CNT=2
                              10089 TSM:FAIL:PDT
                              
                              1 Reply Last reply
                              0
                              • gohanG Offline
                                gohanG Offline
                                gohan
                                Mod
                                wrote on last edited by
                                #58

                                have you checked https://www.mysensors.org/build/debug ?

                                1 Reply Last reply
                                0
                                • CptSlowC Offline
                                  CptSlowC Offline
                                  CptSlow
                                  wrote on last edited by
                                  #59

                                  Hi Gohan,

                                  I did not know that page. Thanks for the push :+1:

                                  I think i have crappy radio tho, because i switched it with anotherone i have and it started working....
                                  Thanks!

                                  1 Reply Last reply
                                  1
                                  • gohanG Offline
                                    gohanG Offline
                                    gohan
                                    Mod
                                    wrote on last edited by
                                    #60

                                    don't tell me about crappy nrf24... I got a bunch of really bad ones that work but only for 5 meters max.

                                    1 Reply Last reply
                                    0
                                    • skywatchS Offline
                                      skywatchS Offline
                                      skywatch
                                      wrote on last edited by
                                      #61

                                      I just tried this today but get an error. Here's the output.....

                                      Arduino: 1.8.3 (Windows 10), Board: "Arduino Pro or Pro Mini, ATmega328 (3.3V, 8 MHz)"

                                      In file included from C:\Users\captain\Documents\Arduino\MYS-HW-CH\MYS-HW-CH.ino:37:0:

                                      C:\Users\captain\Documents\Arduino\libraries\DallasTemperature/DallasTemperature.h: In function 'void loop()':

                                      C:\Users\captain\Documents\Arduino\libraries\DallasTemperature/DallasTemperature.h:252:13: error: 'int16_t DallasTemperature::millisToWaitForConversion(uint8_t)' is private

                                       int16_t millisToWaitForConversion(uint8_t);
                                      
                                               ^
                                      

                                      MYS-HW-CH:85: error: within this context

                                      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());

                                                                                                                       ^
                                      

                                      exit status 1
                                      within this context

                                      This report would have more information with
                                      "Show verbose output during compilation"
                                      option enabled in File -> Preferences.

                                      THis is MYS 2.1.1 installed so a bit baffled as to what is causing the problem, it should just work, right?

                                      Anyone with any insight please let me know! :)

                                      mfalkviddM 1 Reply Last reply
                                      0
                                      • skywatchS skywatch

                                        I just tried this today but get an error. Here's the output.....

                                        Arduino: 1.8.3 (Windows 10), Board: "Arduino Pro or Pro Mini, ATmega328 (3.3V, 8 MHz)"

                                        In file included from C:\Users\captain\Documents\Arduino\MYS-HW-CH\MYS-HW-CH.ino:37:0:

                                        C:\Users\captain\Documents\Arduino\libraries\DallasTemperature/DallasTemperature.h: In function 'void loop()':

                                        C:\Users\captain\Documents\Arduino\libraries\DallasTemperature/DallasTemperature.h:252:13: error: 'int16_t DallasTemperature::millisToWaitForConversion(uint8_t)' is private

                                         int16_t millisToWaitForConversion(uint8_t);
                                        
                                                 ^
                                        

                                        MYS-HW-CH:85: error: within this context

                                        int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());

                                                                                                                         ^
                                        

                                        exit status 1
                                        within this context

                                        This report would have more information with
                                        "Show verbose output during compilation"
                                        option enabled in File -> Preferences.

                                        THis is MYS 2.1.1 installed so a bit baffled as to what is causing the problem, it should just work, right?

                                        Anyone with any insight please let me know! :)

                                        mfalkviddM Offline
                                        mfalkviddM Offline
                                        mfalkvidd
                                        Mod
                                        wrote on last edited by mfalkvidd
                                        #62

                                        @skywatch just follow the instructions on the build page:

                                        This example uses a modified version of the external DTH library, which is included in the MySensors external examples. Please install it and restart the Arduino IDE before trying to compile.

                                        1 Reply Last reply
                                        0
                                        • skywatchS Offline
                                          skywatchS Offline
                                          skywatch
                                          wrote on last edited by
                                          #63

                                          Thank you - that did it.

                                          But it would be good to have that info in the file header too, just to call attention to it. Either that or a structured step-by-step tutorial so that things like this don't get left out.

                                          At least it works now though, kul!

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


                                          21

                                          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