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. General Discussion
  3. Serial Gateway Sketch with si7021

Serial Gateway Sketch with si7021

Scheduled Pinned Locked Moved General Discussion
18 Posts 2 Posters 3.1k Views 2 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.
  • M Offline
    M Offline
    Markus.
    wrote on last edited by
    #6

    the Problem is I cnnaot find any simple Mysensors Sketch to read a SI7021 :-(
    Only with battery Monitoring which I don't Need...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Markus.
      wrote on last edited by Markus.
      #7

      can it work like this?

      // Enable debug prints to serial monitor
      #define MY_DEBUG
      
      
      // Enable and select radio type attached
      #define MY_RADIO_RFM69
      #define MY_RFM69_FREQUENCY RFM69_868MHZ
      
      #include <Wire.h>
      #include <SI7021.h>
      #include <SPI.h>
      #include <RunningAverage.h>
      
      
      #ifdef MY_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 CHILD_ID_TEMP 0
      #define CHILD_ID_HUM 1
      // #define SLEEP_TIME 15000 // 15s for DEBUG
      #define SLEEP_TIME 300000   // 5 min
      #define FORCE_TRANSMIT_CYCLE 36  // 5min*12=1/hour, 5min*36=1/3hour 
      #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 measureCount = 0;
      float lastTemperature = -100;
      int lastHumidity = -100;
      
      RunningAverage raHum(AVERAGES);
      SI7021 humiditySensor;
      
      
      // Enable serial gateway
      #define MY_GATEWAY_SERIAL
      
      // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
      #if F_CPU == 8000000L
      #define MY_BAUD_RATE 38400
      #endif
      
      // Enable inclusion mode
      #define MY_INCLUSION_MODE_FEATURE
      
      // Set inclusion mode duration (in seconds)
      #define MY_INCLUSION_MODE_DURATION 60
      
      // Set blinking period
      #define MY_DEFAULT_LED_BLINK_PERIOD 300
      
      // Inverses the behavior of leds
      //#define MY_WITH_LEDS_BLINKING_INVERSE
      
      // Flash leds on rx/tx/err
      // Uncomment to override default HW configurations
      //#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
      //#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
      //#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED
      
      #include <MySensors.h>
      
      MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP); // Initialize temperature message
      MyMessage msgHum(CHILD_ID_HUM,V_HUM);
      
      
      void setup()
      {
        DEBUG_SERIAL(38400);    // <<<<<<<<<<<<<<<<<<<<<<<<<< Note BAUD_RATE in MySensors.h
        DEBUG_PRINTLN("Serial started");
        delay(500); // Allow time for radio if power useed as reset
        DEBUG_PRINT("Node and "); DEBUG_PRINTLN("2 children presented.");
         raHum.clear();
        
      }
      
      void presentation()
      { 
        sendSketchInfo("Gateway_SI7021", "1.0 161017");
        present(CHILD_ID_TEMP, S_TEMP);   // Present sensor to controller
        present(CHILD_ID_HUM, S_HUM);
      }
      
      
      void loop()
      { 
      
        measureCount ++;
        bool forceTransmit = false;
        
        if (measureCount > FORCE_TRANSMIT_CYCLE) {
          forceTransmit = true; 
        }
        sendTempHumidityMeasurements(forceTransmit);
      /*
        // Read and print internal temp
        float temperature0 = static_cast<float>(static_cast<int>((GetInternalTemp()+0.5) * 10.)) / 10.;
        DEBUG_PRINT("Internal Temp: "); DEBUG_PRINT(temperature0); DEBUG_PRINTLN(" *C");        
      */
       
        sleep(SLEEP_TIME);
      }
      
      /*********************************************
       * * 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;
      
        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));
          lastHumidity = humidity;
          measureCount = 0;
          DEBUG_PRINTLN("H sent!");
        }
      
      }
      
      
      1 Reply Last reply
      0
      • gohanG Offline
        gohanG Offline
        gohan
        Mod
        wrote on last edited by
        #8

        Did you try it?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Markus.
          wrote on last edited by
          #9

          not yet ;-) Will try it later....

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Markus.
            wrote on last edited by
            #10

            tried now the Sketch... Seems that something is wrong with the sensor.

             __  __       ____
            |  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
            | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
            | |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
            |_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
                    |___/                      2.2.0-beta
            
            0;255;3;0;9;53 MCO:BGN:INIT GW,CP=RRNGA---,VER=2.2.0-beta
            0;255;3;0;9;83 TSM:INIT
            0;255;3;0;9;92 TSF:WUR:MS=0
            0;255;3;0;9;100 TSM:INIT:TSP OK
            0;255;3;0;9;108 TSM:INIT:GW MODE
            0;255;3;0;9;118 TSM:READY:ID=0,PAR=0,DIS=0
            0;255;3;0;9;129 MCO:REG:NOT NEEDED
            0;255;3;0;14;Gateway startup complete.
            0;255;0;0;18;2.2.0-beta
            0;255;3;0;11;Gateway_SI7021
            0;255;3;0;12;1.0 161017
            0;0;0;0;6;
            0;1;0;0;7;
            0;255;3;0;9;159 MCO:BGN:STP
            Serial started
            Node and 2 children presented.
            0;255;3;0;9;684 MCO:BGN:INIT OK,TSP=1
            

            Also the issue with the attached sensor blocks completly the Gateway function.Means that the Gateway didn't receive anything. Is it possible to prevent such issue? what I mean, is it possible to seperate both functions in the Sketch in that way If something is wrong with the sensor, the Gateway can still operate?
            THX
            Markus

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

              add some more serial prints around to see where the code stops

              M 1 Reply Last reply
              0
              • gohanG gohan

                add some more serial prints around to see where the code stops

                M Offline
                M Offline
                Markus.
                wrote on last edited by
                #12

                @gohan how can i do this ? :-(

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

                  Look at the "DEBUG_PRINTLN" you have in your code, add some of those where you see there aren't.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Markus.
                    wrote on last edited by
                    #14

                    the Code stops here

                    si7021_env data = humiditySensor.getHumidityAndTemperature();
                    

                    Anything wrong with the sensor I guess. But how can I handle such things?

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

                      did you try an example sketch from the library to see if you can read the values correctly?

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Markus.
                        wrote on last edited by
                        #16

                        the sensor conenction was the Problem at the end. Changed SCD and SCL and now it Looks good so far. At the Moment is the Gateway not connected to a Controller. Hope the sensor will be then also send the values...

                         __  __       ____
                        |  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
                        | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
                        | |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
                        |_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
                                |___/                      2.2.0-beta
                        
                        0;255;3;0;9;53 MCO:BGN:INIT GW,CP=RRNGA---,VER=2.2.0-beta
                        0;255;3;0;9;83 TSM:INIT
                        0;255;3;0;9;92 TSF:WUR:MS=0
                        0;255;3;0;9;100 TSM:INIT:TSP OK
                        0;255;3;0;9;108 TSM:INIT:GW MODE
                        0;255;3;0;9;118 TSM:READY:ID=0,PAR=0,DIS=0
                        0;255;3;0;9;129 MCO:REG:NOT NEEDED
                        0;255;3;0;14;Gateway startup complete.
                        0;255;0;0;18;2.2.0-beta
                        0;255;3;0;11;Gateway_SI7021
                        0;255;3;0;12;1.0 161017
                        0;0;0;0;6;
                        0;1;0;0;7;
                        0;255;3;0;9;159 MCO:BGN:STP
                        Serial started
                        Node and 2 children presented.
                        0;255;3;0;9;684 MCO:BGN:INIT OK,TSP=1
                        Loop1 started
                        T: 21.49
                        TempDiff :121.49
                        0;0;1;0;0;21.5
                        T sent!
                        H: 64
                        HumDiff  :164.00
                        0;1;1;0;1;64
                        H sent!
                        Before first sleep
                        0;255;3;0;9;733 MCO:SLP:MS=300000,SMS=0,I1=255,M1=255,I2=255,M2=255
                        0;255;3;0;9;768 !MCO:SLP:REP
                        0;255;3;0;9;124672 TSF:MSG:READ,106-106-0,s=0,c=1,t=1,pt=7,l=5,sg=0:63.3
                        106;0;1;0;1;63.3
                        0;255;3;0;9;124702 TSF:MSG:READ,106-106-0,s=255,c=3,t=0,pt=1,l=1,sg=0:90
                        106;255;3;0;0;90
                        0;255;3;0;9;124735 TSF:MSG:READ,106-106-0,s=3,c=1,t=38,pt=7,l=5,sg=0:3.71
                        106;3;1;0;38;3.71
                        After first sleep
                        Loop1 started
                        T: 21.64
                        TempDiff :0.15
                        H: 65
                        HumDiff  :0.00
                        Before first sleep
                        0;255;3;0;9;300797 MCO:SLP:MS=300000,SMS=0,I1=255,M1=255,I2=255,M2=255
                        0;255;3;0;9;300830 !MCO:SLP:REP
                        0;255;3;0;9;428443 TSF:MSG:READ,106-106-0,s=255,c=3,t=0,pt=1,l=1,sg=0:90
                        106;255;3;0;0;90
                        0;255;3;0;9;428476 TSF:MSG:READ,106-106-0,s=3,c=1,t=38,pt=7,l=5,sg=0:3.72
                        106;3;1;0;38;3.72
                        

                        But how can I prevent the Situation that a defect or missing sensor on the Gateway blocks also the Gateway function ?

                        Thanks

                        Markus

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

                          You need to make a check in the code that if the sensor read is fails it should keep going

                          M 1 Reply Last reply
                          0
                          • gohanG gohan

                            You need to make a check in the code that if the sensor read is fails it should keep going

                            M Offline
                            M Offline
                            Markus.
                            wrote on last edited by
                            #18

                            @gohan ;-) will try it.... -> Learning leasson... :-)

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


                            25

                            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