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. Controllers
  3. Domoticz
  4. Help needed on a sketch...

Help needed on a sketch...

Scheduled Pinned Locked Moved Domoticz
16 Posts 2 Posters 4.9k 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.
  • B Offline
    B Offline
    Bram81
    wrote on last edited by
    #1

    Hi,

    I'm running a serial gateway on a rpi running Domoticz with two moisture sensores connected. Since I want to feed the nodes by batteries I'm trying to only let the nodes send their info a couple of times a day, starting with a two hours interval for now.
    The nodes are seen by domoticz and their info is updated after two hours only once, after that no new data is send/received until I've restarted the nodes. Domoticz and the gateway are running fine for the entire time. It seems that the nodes don't wake up after sleeping for two hours for the second time. This is the sketch I use:

    Insert Code Here
    #include <SPI.h>
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define BATTERY_SENSE_PIN  A1  // select the input pin for the battery sense point
    #define MIN 300  // Liquid
    #define MAX 700 // Air
    
    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor
    
    
    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    int lastLightLevel;
    int oldBatteryPcnt = 0;
    int battLoop =0;
    
    void setup(){
    
    Serial.begin(9600);
    
    gw.begin();
    
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Mobile2", "1.16");
    
    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop(){
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    int lastsensorValue = sensorValue;
     
    Serial.println(sensorValue);
    gw.send(msg.set(constrain(map(sensorValue, 1023, 250, 0, 100),0,100)));  
    gw.sleep(1800000);
    }
     
    void check_batt()
    {
      // get the battery Voltage
      int batteryValue = analogRead(BATTERY_SENSE_PIN);
      Serial.println(batteryValue);
      
      // 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
      // vout = (sensorvalue x 3.3) / 1023
      //vin = vout /  (R2/(R1+R2)); 
     
      float batteryV  = ( batteryValue * 0.003225806 ) / 0.3; // divide R2/(R1+R2)
      int batteryPcnt = batteryValue / 10 ;
      if ( batteryPcnt > 100 )
      batteryPcnt = 100;
      
      Serial.print("Battery Voltage: ");
      Serial.print(batteryV);
      Serial.println(" V");
      Serial.print("Battery percent: ");
      Serial.print(batteryPcnt);
      Serial.println(" %");
    
      if (oldBatteryPcnt != batteryPcnt)
      {
        // Power up radio after sleep
        gw.sendBatteryLevel(batteryPcnt);
        oldBatteryPcnt = batteryPcnt;
      }
    
    }```
    

    I'm still a newbie when it comes to coding, so maybe there is just a obvious error that I just miss?
    BTW, the both nodes are fed through USB for now, so I'm sure it's not because of low battery levels. Is there anyone that can help me solve this?

    1 Reply Last reply
    0
    • rvendrameR Offline
      rvendrameR Offline
      rvendrame
      Hero Member
      wrote on last edited by
      #2

      Did you try to remove the constrain() and move map() out of the equation?
      Like this:

        int sendValue =  map(sensorValue, 1023, 250, 0, 100); 
        gw.send( msg.set( sendValue ) );  
      

      And am I wrong or gw.sleep(1800000) = 20min?

      And finally, can you keep the note connected to serial monitor, so you would see the output after 2h (or 20min ;-) )?

      Home Assistant / Vera Plus UI7
      ESP8266 GW + mySensors 2.3.2
      Alexa / Google Home

      B 1 Reply Last reply
      0
      • rvendrameR rvendrame

        Did you try to remove the constrain() and move map() out of the equation?
        Like this:

          int sendValue =  map(sensorValue, 1023, 250, 0, 100); 
          gw.send( msg.set( sendValue ) );  
        

        And am I wrong or gw.sleep(1800000) = 20min?

        And finally, can you keep the note connected to serial monitor, so you would see the output after 2h (or 20min ;-) )?

        B Offline
        B Offline
        Bram81
        wrote on last edited by
        #3

        @rvendrame Hi! Just changed the code as you suggested. That 1800000 was still from an old test, I'm sure that the sleeptime on the nodes was set for 2 hrs. It's set in miliseconds right? In that case it wasn't 20 but 30 minutes.. :wink:

        For the test I've set it to 600000 - do we agree on this being 10 min..? Since I'm using a Pro Trinket which can only use the serial monitor via FDTI I had to connect a breadbord to get some extra GND pins.. Node are running now.. Knowing more within the next half hour...

        1 Reply Last reply
        0
        • rvendrameR Offline
          rvendrameR Offline
          rvendrame
          Hero Member
          wrote on last edited by
          #4

          Right, 1800000 = 30min, sorry.

          Home Assistant / Vera Plus UI7
          ESP8266 GW + mySensors 2.3.2
          Alexa / Google Home

          B 1 Reply Last reply
          0
          • rvendrameR rvendrame

            Right, 1800000 = 30min, sorry.

            B Offline
            B Offline
            Bram81
            wrote on last edited by
            #5

            @rvendrame Hi, It seems to work now... THANKS! Another question, I'm trying to measure the level of a 9v battery in the same sketch as well, using this:

            Insert Code Here
            ``#include <SPI.h>
            #include <MySensor.h>
            
            #define CHILD_ID_LIGHT 0
            #define LIGHT_SENSOR_ANALOG_PIN 1
            #define BATTERY_SENSE_PIN  A1  // select the input pin for the battery sense point
            #define MIN 300  // Liquid
            #define MAX 700 // Air
            
            int sensorPin = A0; // select the input pin for the potentiometer
            int sensorValue = 0; // variable to store the value coming from the sensor
            
            boolean metric = true;
            MySensor gw;
            MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
            int lastLightLevel;
            int oldBatteryPcnt = 0;
            int battLoop =0;
            
            void setup(){
            
            Serial.begin(9600);
            
            gw.begin();
            
            // Send the sketch version information to the gateway and Controller
            gw.sendSketchInfo("Mobile1", "1.16");
            
            // Register all sensors to gateway (they will be created as child devices)
            gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
            
            }
            
            void loop(){
            // read the value from the sensor:
            sensorValue = analogRead(sensorPin);
            delay(1000);
            int lastsensorValue = sensorValue;
             
            Serial.println(sensorValue);
            int sendValue =  map(sensorValue, 1023, 250, 0, 100); 
              gw.send( msg.set( sendValue ) );  
            
            
              // get the battery Voltage
            int batteryValue = analogRead(BATTERY_SENSE_PIN);
              Serial.println(batteryValue);
              
              // 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
              // vout = (sensorvalue x 3.3) / 1023
              //vin = vout /  (R2/(R1+R2)); 
             
              float batteryV  = ( batteryValue * 0.0088 ) / 0.8; // divide R2/(R1+R2)
              int batteryPcnt = batteryValue / 10 ;
                if ( batteryPcnt > 100 )
              batteryPcnt = 100;
              
              Serial.print("Battery Voltage: ");
              Serial.print(batteryV);
              Serial.println(" V");
              Serial.print("Battery percent: ");
              Serial.print(batteryPcnt);
              Serial.println(" %");
            
              if (oldBatteryPcnt != batteryPcnt)
              {
                // Power up radio after sleep
                gw.sendBatteryLevel(batteryPcnt);
                oldBatteryPcnt = batteryPcnt;
            gw.sleep(300000);
              }
            
            
            

            Using this sketch causes the battery check into some kind of loop and not sending it back correctly to domoticz. Log shows:
            Battery Voltage: 1.68 V
            Battery percent: 15 %
            384
            send: 1-1-0-0 s=0,c=1,t=23,pt=2,l=2,st=ok:82
            153
            Battery Voltage: 1.68 V
            Battery percent: 15 %
            385
            send: 1-1-0-0 s=0,c=1,t=23,pt=2,l=2,st=ok:82
            153
            Battery Voltage: 1.68 V
            Battery percent: 15 %
            384

            Any ideas on this?

            1 Reply Last reply
            0
            • rvendrameR Offline
              rvendrameR Offline
              rvendrame
              Hero Member
              wrote on last edited by
              #6

              Just put the gw.sleep out of the if {}

              Home Assistant / Vera Plus UI7
              ESP8266 GW + mySensors 2.3.2
              Alexa / Google Home

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bram81
                wrote on last edited by
                #7

                Ok, I'm almost where I want to be... Feeding my pro tinket 5v on a 9v battery I've managed to get the correct voltage reading. Which is aprox 9.10v on a new battery. The only thing I can't get to work is the % which I need to send to Domoticz .

                I'm using this code, being pretty sure that the formula is correct but whatever I try, the battery % is shown as 0 on every voltage level. Am I missing something... ?

                // number of analog samples to take per reading
                #define NUM_SAMPLES 10
                
                int sum = 0;                    // sum of samples taken
                unsigned char sample_count = 0; // current sample number
                float voltage = 0.0;            // calculated voltage
                
                #include <SPI.h>
                #include <MySensor.h>
                
                #define CHILD_ID_LIGHT 0
                #define LIGHT_SENSOR_ANALOG_PIN 1
                #define BATTERY_SENSE_PIN  A1  // select the input pin for the battery sense point
                #define MIN 300  // Liquid
                #define MAX 700 // Air
                // number of analog samples to take per reading
                #define NUM_SAMPLES 10
                
                
                int sensorPin = A0; // select the input pin for the potentiometer
                int sensorValue = 0; // variable to store the value coming from the sensor
                
                
                MySensor gw;
                MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                
                void setup(){
                
                Serial.begin(9600);
                
                gw.begin();
                
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("Mobile1", "1.16");
                
                // Register all sensors to gateway (they will be created as child devices)
                gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                
                }
                
                void loop(){
                // read the value from the sensor:
                sensorValue = analogRead(sensorPin);
                delay(1000);
                int lastsensorValue = sensorValue;
                 
                Serial.println(sensorValue);
                int sendValue =  map(sensorValue, 1023, 250, 0, 100); 
                  gw.send( msg.set( sendValue ) );  
                  gw.sleep(3000);
                
                
                 // take a number of analog samples and add them up
                    while (sample_count < NUM_SAMPLES) {
                        sum += analogRead(A1);
                        sample_count++;
                        delay(10);
                    }
                    // calculate the voltage
                    // use 5.0 for a 5.0V ADC reference voltage
                    // 5.015V is the calibrated reference voltage
                    voltage = ((float)sum / (float)NUM_SAMPLES * 4.92) / 1024.0;
                    int Vmax = 9.10;
                int Vmin =4.92;
                int spanning = voltage * 11.714;
                   int batteryPcnt = ((spanning-Vmin)/(Vmax-Vmin))*100;
                   
                      if ( batteryPcnt > 100 )
                  batteryPcnt = 100;
                    // send voltage for display on Serial Monitor
                    // voltage multiplied by 11 when using voltage divider that
                    // divides by 11. 11.132 is the calibrated voltage divide
                    // value
                    Serial.print(voltage * 11.714);
                    Serial.println (" V");
                    
                    sample_count = 0;
                    sum = 0;
                  Serial.print("Battery percent: ");
                  Serial.print(batteryPcnt);
                  Serial.println(" %");
                      
                
                  }
                
                
                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bram81
                  wrote on last edited by
                  #8

                  Oh and I mean shown as 0% in the serial monitor since I haven't put in the gw.send command in yet..

                  1 Reply Last reply
                  0
                  • rvendrameR Offline
                    rvendrameR Offline
                    rvendrame
                    Hero Member
                    wrote on last edited by
                    #9

                    Did you try to define batteryPcnt as 'float' instead 'int'?

                    Home Assistant / Vera Plus UI7
                    ESP8266 GW + mySensors 2.3.2
                    Alexa / Google Home

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Bram81
                      wrote on last edited by
                      #10

                      define batteryPcn as float didn't make a difference, but after that I did:

                      float spanning = voltage * 11.714;
                      float batteryPcnt = ((spanning-Vmin)/(Vmax-Vmin))*100;

                      It seems to be working now! Thanks a million again..

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Bram81
                        wrote on last edited by
                        #11

                        Do i have to use a different command to send the battterypcnt to the gateway?

                        gw.send(batteryPcnt);

                        gives this error:

                        no matching function for call to 'MySensor::send(float&)'

                        1 Reply Last reply
                        0
                        • rvendrameR Offline
                          rvendrameR Offline
                          rvendrame
                          Hero Member
                          wrote on last edited by
                          #12

                          it means gw.send() only accepts a integer --- So you need to set batteryPcnt back to int.

                          Home Assistant / Vera Plus UI7
                          ESP8266 GW + mySensors 2.3.2
                          Alexa / Google Home

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            Bram81
                            wrote on last edited by
                            #13

                            When i set batteryPcnt back to int it shows: no matching function for call to 'MySensor::send(int&)'

                            1 Reply Last reply
                            0
                            • rvendrameR Offline
                              rvendrameR Offline
                              rvendrame
                              Hero Member
                              wrote on last edited by
                              #14

                              I use this:

                              // Report battery level to controller 
                              gw.sendBatteryLevel( bat_level );
                              

                              Home Assistant / Vera Plus UI7
                              ESP8266 GW + mySensors 2.3.2
                              Alexa / Google Home

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                Bram81
                                wrote on last edited by
                                #15

                                Working like a charm now! Thank you very much for your help!!

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  Bram81
                                  wrote on last edited by
                                  #16

                                  Hi there,

                                  I'm having another sketch problem.. In order to try and save battery power on my moisture sensors I'm trying to use a IO pin as power by setting it high for some time, let the sensor take a measurement, send the results to the gateway, setting the IO pin low again, let the node go to sleep for some time .

                                  This works only one time, the node presents itself to the gateway, sends data but after that it's not seen again after the expected sleep time.
                                  My guess is that somehow after puting the IO pin low it doesn't come back up in a high state. The strange thing is that when I measure the current on the pins of my moisture sensor with a multimeter the current goes from 0 to aprox 4.27V in the given time, so actually that indicates the pin is going from high to low as needed, but somehow the radio doesn't come back up... This is my code:

                                  #include <SPI.h>
                                  #include <MySensor.h>
                                  
                                  #define CHILD_ID_LIGHT 0
                                  #define LIGHT_SENSOR_ANALOG_PIN 1
                                  #define MIN 300  // Liquid
                                  #define MAX 700 // Air
                                  
                                  int sensorPin = A0; // select the input pin for the potentiometer
                                  int sensorValue = 0; // variable to store the value coming from the sensor
                                  int power =4;
                                  MySensor gw;
                                  MyMessage msg(CHILD_ID_LIGHT, V_HUM);
                                  int lastLightLevel;
                                  
                                  
                                  
                                  void setup(){
                                  pinMode(power, OUTPUT);
                                  digitalWrite(power, HIGH);
                                  delay(3000);
                                  Serial.begin(9600);
                                  
                                  gw.begin();
                                  
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Mobile Sensor 1", "Kruiden");
                                  
                                  // Register all sensors to gateway (they will be created as child devices)
                                  gw.present(CHILD_ID_LIGHT, S_HUM);
                                  
                                  delay (2000);
                                  }
                                  void loop(){
                                  // read the value from the sensor:
                                  
                                  
                                  digitalWrite(power, HIGH);
                                  delay (5000);
                                  sensorValue = analogRead(sensorPin);
                                  delay(1000);
                                  int lastsensorValue = sensorValue;
                                   
                                  Serial.println(sensorValue);
                                  int sendValue =  map(sensorValue, 0, 842, 0, 100); 
                                    gw.send( msg.set( sendValue ) );
                                   delay(10000); 
                                  digitalWrite(power, LOW);
                                  delay (2000);
                                  gw.sleep(60000);
                                  }
                                   
                                  

                                  Does anyone have any clue..?

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


                                  9

                                  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