Updated with input from Bruce Lacey

This commit is contained in:
Thomas Mørch
2015-04-15 21:34:02 +02:00
parent c0dee158df
commit 5e5f979122
@@ -1,9 +1,9 @@
// Default sensor sketch for MySensor Micro module // Default sensor sketch for Sensebender Micro module
// Act as a temperature / humidity sensor by default. // Act as a temperature / humidity sensor by default.
// //
// If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals // If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals
// //
// Battery voltage is repported as child sensorId 199, as well as battery percentage // Battery voltage is repported as child sensorId 199, as well as battery percentage (Internal message)
#include <MySensor.h> #include <MySensor.h>
@@ -52,6 +52,7 @@ MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
// Global settings // Global settings
int measureCount = 0; int measureCount = 0;
boolean isMetric = true;
// Storage of old measurements // Storage of old measurements
float lastTemperature = -100; float lastTemperature = -100;
@@ -84,13 +85,14 @@ void setup() {
humiditySensor.begin(); humiditySensor.begin();
gw.sendSketchInfo("MysensorMicro", "1.0"); gw.sendSketchInfo("Sensebender Micro", "1.0");
gw.present(CHILD_ID_TEMP,S_TEMP); gw.present(CHILD_ID_TEMP,S_TEMP);
gw.present(CHILD_ID_HUM,S_HUM); gw.present(CHILD_ID_HUM,S_HUM);
gw.present(CHILD_ID_BATT, S_POWER); isMetric = gw.getConfig().isMetric;
switchClock(1<<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power. Serial.print("isMetric: "); Serial.println(isMetric);
} }
@@ -99,6 +101,10 @@ void loop() {
measureCount ++; measureCount ++;
bool forceTransmit = false; bool forceTransmit = false;
// When we wake up the 5th time after power on, switch to 1Mhz clock
// This allows us to print debug messages on startup (as serial port is dependend on oscilator settings).
if (measureCount == 5) switchClock(1<<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
if (measureCount > FORCE_TRANSMIT_INTERVAL if (measureCount > FORCE_TRANSMIT_INTERVAL
) { // force a transmission ) { // force a transmission
forceTransmit = true; forceTransmit = true;
@@ -127,16 +133,17 @@ void sendTempHumidityMeasurements(bool force)
si7021_env data = humiditySensor.getHumidityAndTemperature(); si7021_env data = humiditySensor.getHumidityAndTemperature();
float temperature = data.celsiusHundredths/100; float temperature = (isMetric ? data.celsiusHundredths : data.fahrenheitHundredths) / 100.0;
int humidity = data.humidityPercent; int humidity = data.humidityPercent;
if (lastTemperature != temperature) { if ((lastTemperature != temperature) | (lastHumidity != humidity)) {
Serial.print("T: ");Serial.println(temperature);
Serial.print("H: ");Serial.println(humidity);
gw.send(msgTemp.set(temperature,1)); gw.send(msgTemp.set(temperature,1));
lastTemperature = temperature;
}
if (lastHumidity != humidity) {
gw.send(msgHum.set(humidity)); gw.send(msgHum.set(humidity));
lastTemperature = temperature;
lastHumidity = humidity; lastHumidity = humidity;
} }
} }
@@ -154,10 +161,10 @@ void sendBattLevel(bool force)
if (vcc != lastBattery) { if (vcc != lastBattery) {
lastBattery = vcc; lastBattery = vcc;
// Calculate percentage // Calculate percentage
gw.send(msgBattery.set(vcc));
vcc = vcc - 1900; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at vcc = vcc - 1900; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at
long percent = vcc / 14; long percent = vcc / 14.0;
gw.sendBatteryLevel(percent); gw.sendBatteryLevel(percent);
} }
} }
@@ -278,7 +285,7 @@ void testMode()
while (1) // Blink OK pattern! while (1) // Blink OK pattern!
{ {
digitalWrite(LED_PIN, HIGH); digitalWrite(LED_PIN, HIGH);
delay(800); delay(200);
digitalWrite(LED_PIN, LOW); digitalWrite(LED_PIN, LOW);
delay(200); delay(200);
} }
@@ -288,10 +295,7 @@ void testMode()
Serial.println(F("----> Selftest failed!")); Serial.println(F("----> Selftest failed!"));
while (1) // Blink FAILED pattern! Rappidly blinking.. while (1) // Blink FAILED pattern! Rappidly blinking..
{ {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
} }
} }
} }