Help adding battery level to switch sketch



  • I have a sketch witch is used to report when a button is switched:

    #include <MySensor.h>
    #include <SPI.h>
    #include <MyTransportRFM69.h>
    
    #define SKETCH_NAME "BinSwSleepBat"
    #define SKETCH_VERSION "1-2"
    #define CHILD_ID 0
    
    MyTransportRFM69 transport; 
    MySensor gw(transport);
    
    MySensor mys;
    
    MyMessage msg(CHILD_ID, V_TRIPPED); // Change to V_LIGHT if you use S_LIGHT in presentation below
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    int sentValue=2;
    
    void setup()  
    {    
      mys.begin();  
      
      pinMode(BUTTON_PIN, INPUT);   // Setup the buttons
      digitalWrite(BUTTON_PIN, HIGH);   // Activate internal pull-ups
      
      mys.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      mys.present(CHILD_ID, S_DOOR);
    }
    
    void loop()  // Loop will iterate on changes on the BUTTON_PINs
    {
    
      mys.sleep(50);  // Short delay to allow buttons to properly settle 
      
      uint8_t val = digitalRead(BUTTON_PIN);
      if (val != sentValue) {  // If value has changed from last transmission, send the updated value
         mys.send(msg.set(val==HIGH ? 1 : 0));
         sentValue = val;
      }
       
      mys.sleep(BUTTON_PIN-2, CHANGE, 0); // Sleep until something happens with the sensor
    }
    

    I want to add the battery level sketch so that it sleep until button is switched as before but also reports battery-level every 120min

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * This is an example that demonstrates how to report the battery level for a sensor
     * Instructions for measuring battery capacity on A0 are available here:
     * http://www.mysensors.org/build/battery
     * 
     */
    
    
    #include <SPI.h>
    #include <MySensor.h>
    
    int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
    
    MySensor gw;
    unsigned long SLEEP_TIME = 900000;  // sleep time between reads (seconds * 1000 milliseconds)
    int oldBatteryPcnt = 0;
    
    void setup()  
    {
       // use the 1.1 V internal reference
    #if defined(__AVR_ATmega2560__)
       analogReference(INTERNAL1V1);
    #else
       analogReference(INTERNAL);
    #endif
       gw.begin();
    
       // Send the sketch version information to the gateway and Controller
       gw.sendSketchInfo("Battery Meter", "1.0");
    }
    
    void loop()
    {
       // get the battery Voltage
       int sensorValue = analogRead(BATTERY_SENSE_PIN);
       #ifdef DEBUG
       Serial.println(sensorValue);
       #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
       float batteryV  = sensorValue * 0.003363075;
       int batteryPcnt = sensorValue / 10;
    
       #ifdef DEBUG
       Serial.print("Battery Voltage: ");
       Serial.print(batteryV);
       Serial.println(" V");
    
       Serial.print("Battery percent: ");
       Serial.print(batteryPcnt);
       Serial.println(" %");
       #endif
    
       if (oldBatteryPcnt != batteryPcnt) {
         // Power up radio after sleep
         gw.sendBatteryLevel(batteryPcnt);
         oldBatteryPcnt = batteryPcnt;
       }
       gw.sleep(SLEEP_TIME);
    }
    

  • Admin

    @Cliff-Karlsson said:

    mys.sleep(BUTTON_PIN-2, CHANGE, 0);

    Change to

    wakeUpReason = mys.sleep(BUTTON_PIN-2, CHANGE, SLEEP_TIME);

    See documentation on wakeUpReason if you only want to send battery value at wakeup.


Log in to reply
 

Suggested Topics

  • 4
  • 8
  • 2
  • 3
  • 15
  • 17

54
Online

11.4k
Users

11.1k
Topics

112.6k
Posts