BcSensor Battery

  • -is connected directly to the battery
  • 2x connectors for Dallas temperature sensor
  • 1 - photocell (light sensor)

(this version is reporting the battery level)

		Partlist
Exported from x1-v2.2.brd at 22.02.2015 20:58:51
Assembly variant: 

Part      Value          Package      Library         Position (mm)         Orientation

C1        0.1            C1206        adafruit        (49.53 60.96)         MR180
DS18B20                  1X03         adafruit        (62.23 81.915)        R270
DS18B20-2                1X03         adafruit        (62.23 71.755)        R270
NRF                      2X04         adafruit        (63.1825 51.435)      R180
PHOTOCELL                1X02         pinhead         (50.8 80.645)         R180
PWR                      1X02         pinhead         (62.23 62.5475)       R90
R1        4k7            1206         digital-toy     (60.0075 66.3575)     MR0
R2        1M             1206         digital-toy     (48.895 69.85)        MR90
R3        470k           1206         digital-toy     (49.53 64.77)         MR180
R4        10k            1206         digital-toy     (53.34 76.2)          MR270
R5        0              1206         digital-toy     (44.45 67.31)         MR270
R6        0              1206         digital-toy     (53.0225 68.58)       MR270
U$1       ARDUINO_MINI   ARDUINO_MINI SparkFun-Boards (48.26 67.31)         R0
--------------------------------------------------------------------------------------------------------------------------
#include <MySensor.h>
#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Vcc.h>

const float VccMin   = 3.0;           // Minimum expected Vcc level, in Volts.
const float VccMax   = 4.2;           // Maximum expected Vcc level, in Volts.
const float VccCorrection = 1/1;  // Measured Vcc by multimeter divided by reported Vcc
Vcc vcc(VccCorrection);

int oldBatteryPcnt = 0;

#define LIGHT_SENSOR_ANALOG_PIN A2
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 16
unsigned long SLEEP_TIME = 1000; // Sleep time between reads (in milliseconds)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MySensor gw;
float lastTemperature[MAX_ATTACHED_DS18B20];
//float last_voltage;
int numSensors = 0;
boolean receivedConfig = false;
boolean metric = true;
// Initialize temperature message
MyMessage msg(0, V_TEMP);
MyMessage msgBattery(0, V_VAR1);
#ifdef LIGHT_SENSOR_ANALOG_PIN
  int lastLightLevel;
   MyMessage msgL(0, V_LIGHT_LEVEL);
 #endif
void setup()
{
  analogReference(INTERNAL);
  // 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);
  }
    #ifdef LIGHT_SENSOR_ANALOG_PIN // light sensor
    pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
    gw.present(0, S_LIGHT_LEVEL);
  #endif
}


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;
    }
  }
    #ifdef LIGHT_SENSOR_ANALOG_PIN // senzor lumina
      int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
      if (lightLevel != lastLightLevel) {
        gw.send(msgL.set(lightLevel));
        lastLightLevel = lightLevel;
      }
    #endif // end senzor lumina
    
//  float v = vcc.Read_Volts();
//  Serial.print("VCC = ");
//  Serial.print(v);
//  Serial.println(" Volts"); 
  int batteryPcnt = vcc.Read_Perc(VccMin, VccMax); 

  if (oldBatteryPcnt != batteryPcnt) {
    // Power up radio after sleep
//    gw.sendBatteryLevel(batteryPcnt);
  gw.send(msgBattery.set(batteryPcnt, 1));
    oldBatteryPcnt = batteryPcnt;
  }
  gw.sleep(SLEEP_TIME);
}