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. Development
  3. Mysensors si7021 sketch

Mysensors si7021 sketch

Scheduled Pinned Locked Moved Development
11 Posts 5 Posters 2.4k Views 4 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.
  • T Tommas

    Dear All!

    Has anybody a sketch for this temperature and humidity sensor? I would like to use it with mini pro combined easypcb board.

    Thank you in advance!

    sundberg84S Offline
    sundberg84S Offline
    sundberg84
    Hardware Contributor
    wrote on last edited by
    #2

    @Tommas - some resources i found.

    https://forum.mysensors.org/topic/2432/si7021-multisensor-first-try
    https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/SI7021
    https://forum.mysensors.org/topic/6434/solved-problems-with-an-si7021-temp-humidity-sensor/3

    Controller: Proxmox VM - Home Assistant
    MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
    MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
    RFLink GW - Arduino Mega + RFLink Shield, 433mhz

    T 2 Replies Last reply
    0
    • sundberg84S sundberg84

      @Tommas - some resources i found.

      https://forum.mysensors.org/topic/2432/si7021-multisensor-first-try
      https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/SI7021
      https://forum.mysensors.org/topic/6434/solved-problems-with-an-si7021-temp-humidity-sensor/3

      T Offline
      T Offline
      Tommas
      wrote on last edited by
      #3

      Dear @sundberg84

      Thanks for the suggestions!
      Another, which pin should i connect to the si7021? I cant find this info...

      Thanks,

      mfalkviddM 1 Reply Last reply
      0
      • T Tommas

        Dear @sundberg84

        Thanks for the suggestions!
        Another, which pin should i connect to the si7021? I cant find this info...

        Thanks,

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

        @Tommas the si7021 uses i2c so you can use the wiring described at https://www.mysensors.org/build/light-bh1750 which also is an i2c device.

        T 1 Reply Last reply
        1
        • mfalkviddM mfalkvidd

          @Tommas the si7021 uses i2c so you can use the wiring described at https://www.mysensors.org/build/light-bh1750 which also is an i2c device.

          T Offline
          T Offline
          Tommas
          wrote on last edited by Tommas
          #5

          Dear @mfalkvidd !

          Thank you, and to everyone for the help!
          I got it work!
          If the sketch will be completed i will upload that!

          1 Reply Last reply
          1
          • sundberg84S sundberg84

            @Tommas - some resources i found.

            https://forum.mysensors.org/topic/2432/si7021-multisensor-first-try
            https://github.com/mysensors/MySensorsArduinoExamples/tree/master/libraries/SI7021
            https://forum.mysensors.org/topic/6434/solved-problems-with-an-si7021-temp-humidity-sensor/3

            T Offline
            T Offline
            Tommas
            wrote on last edited by
            #6

            @sundberg84

            What is the problem with my sketch? It is in developement phase, but the reading stops at this line, but sometimes it works for 3-4 loop...

            " DEBUG_PRINTLN("Send TEMP and HUM started"); "

            /**
             * 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.
             *
             *******************************
             *
             * REVISION HISTORY
             * Version 1.0: Henrik EKblad
             * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
             * 
             * DESCRIPTION
             * This sketch provides an example of how to implement a humidity/temperature
             * sensor using a DHT11/DHT-22.
             *  
             * For more information, please visit:
             * http://www.mysensors.org/build/humidity
             * 
             */
            #define MY_NODE_ID 4
            // Enable debug prints
            #define MY_DEBUG
            
            // Enable and select radio type attached 
            #define MY_RADIO_NRF24
            //#define MY_RADIO_RFM69
            //#define MY_RS485
            /* Sketch with Si7021 and battery monitoring.
            by m26872, 20151109 
            */
            #include <MySensors.h>  
            #include <Wire.h>
            #include <SI7021.h>
            #include <SPI.h>
            #include <RunningAverage.h>
            
            #define DEBUG
            
            #ifdef DEBUG
            #define DEBUG_SERIAL(x) Serial.begin(x)
            #define DEBUG_PRINT(x) Serial.print(x)
            #define DEBUG_PRINTLN(x) Serial.println(x)
            #else
            #define DEBUG_SERIAL(x)
            #define DEBUG_PRINT(x) 
            #define DEBUG_PRINTLN(x) 
            #endif
            
            #define NODE_ID 4             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
            #define CHILD_ID_TEMP 0
            #define CHILD_ID_HUM 1
            //#define SLEEP_TIME 15000 // 15s for DEBUG
            #define SLEEP_TIME 10000   // 5 min
            //#define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
            #define FORCE_TRANSMIT_CYCLE 1  // 5min*12=1/hour, 5min*36=1/3hour 
            //#define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
            //#define VMIN 1900
            //#define VMAX 3300
            #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
            #define TEMP_TRANSMIT_THRESHOLD 0.5
            #define AVERAGES 2
            
            //int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
            int measureCount = 0;
            float lastTemperature = -100;
            int lastHumidity = -100;
            
            RunningAverage raHum(AVERAGES);
            SI7021 humiditySensor;
            
            //MySensor gw;
            MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
            MyMessage msgHum(CHILD_ID_HUM,V_HUM);
            
            void presentation(){
            
            sendSketchInfo("Node 4, Temp,Hum sensor", "1.1"); 
              present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
              present(CHILD_ID_HUM, S_HUM);
              
            }
            
            
            void setup() {
              DEBUG_SERIAL(115200);    
              DEBUG_PRINTLN("Serial started");
              
              //DEBUG_PRINT("Voltage: ");
              //DEBUG_PRINT(readVcc()); 
              //DEBUG_PRINTLN(" mV");
            /*
              delay(500);
              DEBUG_PRINT("Internal temp: ");
              DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
              DEBUG_PRINTLN(" *C");
            */  
              delay(500); // Allow time for radio if power useed as reset
              //begin(NULL,NODE_ID);
              
              //DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
              
              raHum.clear();
              
            }
            
            void loop() {
              DEBUG_PRINTLN("Loop started"); 
              measureCount ++;
              
              bool forceTransmit = true;
              
              //if (measureCount > FORCE_TRANSMIT_CYCLE) {
              //forceTransmit = true; 
              //}
              sleep(SLEEP_TIME);
              sendTempHumidityMeasurements(forceTransmit);
              DEBUG_PRINTLN("Loop ended"); 
              }
            /*********************************************
             * * Sends temperature and humidity from Si7021 sensor
             * Parameters
             * - force : Forces transmission of a value (even if it's the same as previous measurement)
             *********************************************/
            void sendTempHumidityMeasurements(bool force) {
              bool tx = force;
              DEBUG_PRINTLN("Send TEMP and HUM started"); 
              si7021_env data = humiditySensor.getHumidityAndTemperature();
              float temperature = data.celsiusHundredths / 100.0;
              DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
              float diffTemp = abs(lastTemperature - temperature);
              DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
              //if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
              send(msgTemp.set(temperature, 1));
              //lastTemperature = temperature;
              //measureCount = 0;
              DEBUG_PRINTLN("T sent!");
              //}
              
              int humidity = data.humidityPercent;
              DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
              raHum.addValue(humidity);
              humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
              float diffHum = abs(lastHumidity - humidity);  
              DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
              //if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
              send(msgHum.set(humidity, 1));
              //lastHumidity = humidity;
              //measureCount = 0;
              DEBUG_PRINTLN("H sent!");
              //}
            
            }
            
            sundberg84S 1 Reply Last reply
            0
            • T Tommas

              @sundberg84

              What is the problem with my sketch? It is in developement phase, but the reading stops at this line, but sometimes it works for 3-4 loop...

              " DEBUG_PRINTLN("Send TEMP and HUM started"); "

              /**
               * 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.
               *
               *******************************
               *
               * REVISION HISTORY
               * Version 1.0: Henrik EKblad
               * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
               * 
               * DESCRIPTION
               * This sketch provides an example of how to implement a humidity/temperature
               * sensor using a DHT11/DHT-22.
               *  
               * For more information, please visit:
               * http://www.mysensors.org/build/humidity
               * 
               */
              #define MY_NODE_ID 4
              // Enable debug prints
              #define MY_DEBUG
              
              // Enable and select radio type attached 
              #define MY_RADIO_NRF24
              //#define MY_RADIO_RFM69
              //#define MY_RS485
              /* Sketch with Si7021 and battery monitoring.
              by m26872, 20151109 
              */
              #include <MySensors.h>  
              #include <Wire.h>
              #include <SI7021.h>
              #include <SPI.h>
              #include <RunningAverage.h>
              
              #define DEBUG
              
              #ifdef DEBUG
              #define DEBUG_SERIAL(x) Serial.begin(x)
              #define DEBUG_PRINT(x) Serial.print(x)
              #define DEBUG_PRINTLN(x) Serial.println(x)
              #else
              #define DEBUG_SERIAL(x)
              #define DEBUG_PRINT(x) 
              #define DEBUG_PRINTLN(x) 
              #endif
              
              #define NODE_ID 4             // <<<<<<<<<<<<<<<<<<<<<<<<<<<   Enter Node_ID
              #define CHILD_ID_TEMP 0
              #define CHILD_ID_HUM 1
              //#define SLEEP_TIME 15000 // 15s for DEBUG
              #define SLEEP_TIME 10000   // 5 min
              //#define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
              #define FORCE_TRANSMIT_CYCLE 1  // 5min*12=1/hour, 5min*36=1/3hour 
              //#define BATTERY_REPORT_CYCLE 2880   // Once per 5min   =>   12*24*7 = 2016 (one report/week)
              //#define VMIN 1900
              //#define VMAX 3300
              #define HUMI_TRANSMIT_THRESHOLD 3.0  // THRESHOLD tells how much the value should have changed since last time it was transmitted.
              #define TEMP_TRANSMIT_THRESHOLD 0.5
              #define AVERAGES 2
              
              //int batteryReportCounter = BATTERY_REPORT_CYCLE - 1;  // to make it report the first time.
              int measureCount = 0;
              float lastTemperature = -100;
              int lastHumidity = -100;
              
              RunningAverage raHum(AVERAGES);
              SI7021 humiditySensor;
              
              //MySensor gw;
              MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
              MyMessage msgHum(CHILD_ID_HUM,V_HUM);
              
              void presentation(){
              
              sendSketchInfo("Node 4, Temp,Hum sensor", "1.1"); 
                present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
                present(CHILD_ID_HUM, S_HUM);
                
              }
              
              
              void setup() {
                DEBUG_SERIAL(115200);    
                DEBUG_PRINTLN("Serial started");
                
                //DEBUG_PRINT("Voltage: ");
                //DEBUG_PRINT(readVcc()); 
                //DEBUG_PRINTLN(" mV");
              /*
                delay(500);
                DEBUG_PRINT("Internal temp: ");
                DEBUG_PRINT(GetInternalTemp()); // Probably not calibrated. Just to print something.
                DEBUG_PRINTLN(" *C");
              */  
                delay(500); // Allow time for radio if power useed as reset
                //begin(NULL,NODE_ID);
                
                //DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
                
                raHum.clear();
                
              }
              
              void loop() {
                DEBUG_PRINTLN("Loop started"); 
                measureCount ++;
                
                bool forceTransmit = true;
                
                //if (measureCount > FORCE_TRANSMIT_CYCLE) {
                //forceTransmit = true; 
                //}
                sleep(SLEEP_TIME);
                sendTempHumidityMeasurements(forceTransmit);
                DEBUG_PRINTLN("Loop ended"); 
                }
              /*********************************************
               * * Sends temperature and humidity from Si7021 sensor
               * Parameters
               * - force : Forces transmission of a value (even if it's the same as previous measurement)
               *********************************************/
              void sendTempHumidityMeasurements(bool force) {
                bool tx = force;
                DEBUG_PRINTLN("Send TEMP and HUM started"); 
                si7021_env data = humiditySensor.getHumidityAndTemperature();
                float temperature = data.celsiusHundredths / 100.0;
                DEBUG_PRINT("T: ");DEBUG_PRINTLN(temperature);
                float diffTemp = abs(lastTemperature - temperature);
                DEBUG_PRINT(F("TempDiff :"));DEBUG_PRINTLN(diffTemp);
                //if (diffTemp > TEMP_TRANSMIT_THRESHOLD || tx) {
                send(msgTemp.set(temperature, 1));
                //lastTemperature = temperature;
                //measureCount = 0;
                DEBUG_PRINTLN("T sent!");
                //}
                
                int humidity = data.humidityPercent;
                DEBUG_PRINT("H: ");DEBUG_PRINTLN(humidity);
                raHum.addValue(humidity);
                humidity = raHum.getAverage();  // MA sample imply reasonable fast sample frequency
                float diffHum = abs(lastHumidity - humidity);  
                DEBUG_PRINT(F("HumDiff  :"));DEBUG_PRINTLN(diffHum); 
                //if (diffHum > HUMI_TRANSMIT_THRESHOLD || tx) {
                send(msgHum.set(humidity, 1));
                //lastHumidity = humidity;
                //measureCount = 0;
                DEBUG_PRINTLN("H sent!");
                //}
              
              }
              
              sundberg84S Offline
              sundberg84S Offline
              sundberg84
              Hardware Contributor
              wrote on last edited by
              #7

              @Tommas - sorry, im not the code expert...
              If it stops without any indication why it can be a power issue as well.

              Controller: Proxmox VM - Home Assistant
              MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
              MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
              RFLink GW - Arduino Mega + RFLink Shield, 433mhz

              T 1 Reply Last reply
              0
              • sundberg84S sundberg84

                @Tommas - sorry, im not the code expert...
                If it stops without any indication why it can be a power issue as well.

                T Offline
                T Offline
                Tommas
                wrote on last edited by
                #8

                @sundberg84

                There arent any indication. Sometimes it works.... i tried another si7021 and same. Voltage is Ok. A connected the sda - A4 scl - A5 on easypcb board

                Any idea?

                YveauxY 1 Reply Last reply
                0
                • T Tommas

                  @sundberg84

                  There arent any indication. Sometimes it works.... i tried another si7021 and same. Voltage is Ok. A connected the sda - A4 scl - A5 on easypcb board

                  Any idea?

                  YveauxY Offline
                  YveauxY Offline
                  Yveaux
                  Mod
                  wrote on last edited by
                  #9

                  @Tommas do you have pullups (around 10k) at sda & scl lines?

                  http://yveaux.blogspot.nl

                  T 1 Reply Last reply
                  0
                  • YveauxY Yveaux

                    @Tommas do you have pullups (around 10k) at sda & scl lines?

                    T Offline
                    T Offline
                    Tommas
                    wrote on last edited by Tommas
                    #10

                    Dear @Yveaux

                    I think that yes. The si7021 module has an smd 103 part on it. Maybe it is at the sda and scl but im not sure.

                    https://www.ebay.com/i/401364416322

                    Thanks

                    1 Reply Last reply
                    0
                    • Nca78N Offline
                      Nca78N Offline
                      Nca78
                      Hardware Contributor
                      wrote on last edited by
                      #11

                      Hello,

                      you are missing the sensor.begin() to initialize the sensor. Return value will tell you if initialization succeeded or not.

                      if (!sensor.begin()) {
                      #ifdef MY_DEBUG
                          Serial.println("FAILED TO INIT SENSOR !!!!");
                      #endif
                      ...
                        }
                      
                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      12

                      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