Temperature sensor WITH battery monitor



  • Hi,

    I'm new to all of this arduino stuff, but I've managed to get a Serial GW and 2 tempsensors going.

    Now I would like to add the battery monitor to the temp sensor sketch, to monitor the 3,7V LiPo batteries.

    What I've done is, merged the battery sketch setup with the temp sketch setup, and also done the same with the Void sections.

    No errors on upload - weee!

    Problem is now, that I only get the Volt reading from the Node and no temperature reading anymore. 😞

    So where did I go wrong?



  • Here's my sketch for the tempnode:

    // Example sketch showing how to send in OneWire temperature readings
    #include <MySensor.h>
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>

    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 120000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true;
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);

    int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
    int oldBatteryPcnt = 0;

    void setup()
    {
    // Startup OneWire
    sensors.begin();

    // Startup and initialize MySensors library. Set callback for incoming messages.
    gw.begin();

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Temperature Sensor", "1.0");

    // Fetch the number of attached temperature sensors
    numSensors = sensors.getDeviceCount();

    // Present all sensors to controller
    for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    gw.present(i, S_TEMP);
    }

    // use the 1.1 V internal reference
    analogReference(INTERNAL);
    }

    void loop()
    {
    // Process incoming messages (like config from server)
    gw.process();

    // Fetch temperatures from Dallas sensors
    sensors.requestTemperatures();

    // Read temperatures and send them to controller
    for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {

    // Fetch and round temperature to one decimal
    float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
    
    // Only send data if temperature has changed and no error
    if (lastTemperature[i] != temperature && temperature != -127.00) {
    
      // Send in the new temperature
      gw.send(msg.setSensor(i).set(temperature,1));
      lastTemperature[i]=temperature;
    }
    

    }

    // get the battery Voltage
    int sensorValue = analogRead(BATTERY_SENSE_PIN);
    Serial.println(sensorValue);
    // 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.004106;
    int batteryPcnt = sensorValue / 10;
    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(SLEEP_TIME);
    }



  • DOH!!! Stupid me!

    Forgot the temp only transmits on temp change...

    it's working now 🙂



  • Perhaps related to the topic. This program was used for monitoring battery level, but can not read the battery level. 😞

    mqtt="<[mysensor:MyMQTT/21/255/V_BATTERY_LEVEL:state:default]"

    So I tried to read, the program opanhab, but not getting value. Any ideas what went wrong.



  • Hi
    I know this is an old post but I try to convert this Sketch to use it with Version 2.1.and I am not sure If this can be work because I am tottaly new in Arduino and Mysenseors

    // Enable debug prints to serial monitor
    //#define MY_DEBUG 
    
    // Enable and select radio type attached
     #define MY_RADIO_NRF24
     
    // Example sketch showing how to send in OneWire temperature readings
     #include <MySensors.h>
     #include <SPI.h>
     #include <DallasTemperature.h>
     #include <OneWire.h>
    
     #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
     #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
     #define MAX_ATTACHED_DS18B20 16
     unsigned long SLEEP_TIME = 300000; // Sleep time between reads (in milliseconds)
     OneWire oneWire(ONE_WIRE_BUS);
     DallasTemperature sensors(&oneWire);
     //MySensors gw;
     float lastTemperature[MAX_ATTACHED_DS18B20];
     int numSensors=0;
     boolean receivedConfig = false;
     boolean metric = true;
     // Initialize temperature message
     MyMessage msg(0,V_TEMP);
    
    int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
     int oldBatteryPcnt = 0;
    
    void setup()
     {
     // Startup OneWire
     sensors.begin();
    
    // Send the sketch version information to the gateway and Controller
     sendSketchInfo("Temperature Sensor", "1.0");
    
    // Fetch the number of attached temperature sensors
     numSensors = sensors.getDeviceCount();
    
    // Present all sensors to controller
     for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
     present(i, S_TEMP);
     }
    
    // use the 1.1 V internal reference
     analogReference(INTERNAL);
     }
    
    void loop()
     {
     
    // Fetch temperatures from Dallas sensors
     sensors.requestTemperatures();
    
    // Read temperatures and send them to controller
     for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    // Fetch and round temperature to one decimal
    float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
    
    // Only send data if temperature has changed and no error
    if (lastTemperature[i] != temperature && temperature != -127.00) {
    
      // Send in the new temperature
      send(msg.setSensor(i).set(temperature,1));
      lastTemperature[i]=temperature;
    }
    }
    
    // get the battery Voltage
     int sensorValue = analogRead(BATTERY_SENSE_PIN);
     Serial.println(sensorValue);
     // 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.004106;
     int batteryPcnt = sensorValue / 10;
     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
     sendBatteryLevel(batteryPcnt);
     oldBatteryPcnt = batteryPcnt;
     }
    
    sleep(SLEEP_TIME);
     }
    

    Would be great If someone confirm Ifthis is correct so far...

    Many thanks

    Markus


  • Hero Member

    @Markus. I would say: give it a go. It looks fine at first instance. It's rather hard to say it's correct without running it through a compiler. For readability's sake I would advice you to look at the coding guidelines for reference.



  • Its working so far.:-) But the battery voltage is only available as serial print. How must i change the file to get also the voltage send to the gateway? Simply by adding the send command before the sleep?

    Thx

    M.


  • Hero Member

    @Markus. You need to send the voltage explicit with a V_VOLTAGE (S_MULTIMETER) message.



  • @AWI Hope you can give me an example because it seems that the requiered programming Level currently are over my Knowledge 😉

    THX
    Markus



  • Anyone here an idea how i can get the voltage send tomthe gateway? Works great so far except the send of the voltage ....:-(


Log in to reply
 

Suggested Topics

66
Online

11.4k
Users

11.1k
Topics

112.6k
Posts