1- // Default sensor sketch for MySensor Micro module
1+ // Default sensor sketch for Sensebender Micro module
22// Act as a temperature / humidity sensor by default.
33//
44// If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals
55//
6- // Battery voltage is repported as child sensorId 199, as well as battery percentage
6+ // Battery voltage is repported as child sensorId 199, as well as battery percentage (Internal message)
77
88
99#include < MySensor.h>
@@ -52,6 +52,7 @@ MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
5252
5353// Global settings
5454int measureCount = 0 ;
55+ boolean isMetric = true ;
5556
5657// Storage of old measurements
5758float lastTemperature = -100 ;
@@ -70,7 +71,7 @@ void setup() {
7071 pinMode (TEST_PIN,INPUT);
7172 digitalWrite (TEST_PIN, HIGH); // Enable pullup
7273 if (!digitalRead (TEST_PIN)) testMode ();
73-
74+
7475 digitalWrite (TEST_PIN,LOW);
7576 digitalWrite (LED_PIN, HIGH);
7677
@@ -84,27 +85,32 @@ void setup() {
8485
8586 humiditySensor.begin ();
8687
87- gw.sendSketchInfo (" MysensorMicro " , " 1.0" );
88+ gw.sendSketchInfo (" Sensebender Micro " , " 1.0" );
8889
8990 gw.present (CHILD_ID_TEMP,S_TEMP);
9091 gw.present (CHILD_ID_HUM,S_HUM);
9192
92- gw.present (CHILD_ID_BATT, S_POWER);
93- switchClock (1 <<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
93+ isMetric = gw.getConfig ().isMetric ;
94+ Serial.print (" isMetric: " ); Serial.println (isMetric);
95+
9496}
9597
9698
9799// Main loop function
98100void loop () {
99101 measureCount ++;
100102 bool forceTransmit = false ;
103+
104+ // When we wake up the 5th time after power on, switch to 1Mhz clock
105+ // This allows us to print debug messages on startup (as serial port is dependend on oscilator settings).
106+ if (measureCount == 5 ) switchClock (1 <<CLKPS2); // Switch to 1Mhz for the reminder of the sketch, save power.
101107
102108 if (measureCount > FORCE_TRANSMIT_INTERVAL
103109 ) { // force a transmission
104110 forceTransmit = true ;
105111 measureCount = 0 ;
106112 }
107-
113+
108114 gw.process ();
109115 sendBattLevel (forceTransmit);
110116 sendTempHumidityMeasurements (forceTransmit);
@@ -127,16 +133,17 @@ void sendTempHumidityMeasurements(bool force)
127133
128134 si7021_env data = humiditySensor.getHumidityAndTemperature ();
129135
130- float temperature = data.celsiusHundredths / 100 ;
136+ float temperature = (isMetric ? data.celsiusHundredths : data. fahrenheitHundredths ) / 100.0 ;
131137
132138 int humidity = data.humidityPercent ;
133139
134- if (lastTemperature != temperature) {
140+ if ((lastTemperature != temperature) | (lastHumidity != humidity)) {
141+ Serial.print (" T: " );Serial.println (temperature);
142+ Serial.print (" H: " );Serial.println (humidity);
143+
135144 gw.send (msgTemp.set (temperature,1 ));
136- lastTemperature = temperature;
137- }
138- if (lastHumidity != humidity) {
139145 gw.send (msgHum.set (humidity));
146+ lastTemperature = temperature;
140147 lastHumidity = humidity;
141148 }
142149}
@@ -154,10 +161,10 @@ void sendBattLevel(bool force)
154161 if (vcc != lastBattery) {
155162 lastBattery = vcc;
156163 // Calculate percentage
157- gw. send (msgBattery. set (vcc));
164+
158165 vcc = vcc - 1900 ; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at
159166
160- long percent = vcc / 14 ;
167+ long percent = vcc / 14.0 ;
161168 gw.sendBatteryLevel (percent);
162169 }
163170}
@@ -278,7 +285,7 @@ void testMode()
278285 while (1 ) // Blink OK pattern!
279286 {
280287 digitalWrite (LED_PIN, HIGH);
281- delay (800 );
288+ delay (200 );
282289 digitalWrite (LED_PIN, LOW);
283290 delay (200 );
284291 }
@@ -288,10 +295,7 @@ void testMode()
288295 Serial.println (F (" ----> Selftest failed!" ));
289296 while (1 ) // Blink FAILED pattern! Rappidly blinking..
290297 {
291- digitalWrite (LED_PIN, HIGH);
292- delay (100 );
293- digitalWrite (LED_PIN, LOW);
294- delay (100 );
295298 }
296299 }
297300}
301+
0 commit comments