1
- // Default sensor sketch for MySensor Micro module
1
+ // Default sensor sketch for Sensebender Micro module
2
2
// Act as a temperature / humidity sensor by default.
3
3
//
4
4
// If A0 is held low while powering on, it will enter testmode, which verifies all on-board peripherals
5
5
//
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)
7
7
8
8
9
9
#include < MySensor.h>
@@ -52,6 +52,7 @@ MyMessage msgBattery(CHILD_ID_BATT, V_VOLTAGE);
52
52
53
53
// Global settings
54
54
int measureCount = 0 ;
55
+ boolean isMetric = true ;
55
56
56
57
// Storage of old measurements
57
58
float lastTemperature = -100 ;
@@ -70,7 +71,7 @@ void setup() {
70
71
pinMode (TEST_PIN,INPUT);
71
72
digitalWrite (TEST_PIN, HIGH); // Enable pullup
72
73
if (!digitalRead (TEST_PIN)) testMode ();
73
-
74
+
74
75
digitalWrite (TEST_PIN,LOW);
75
76
digitalWrite (LED_PIN, HIGH);
76
77
@@ -84,27 +85,32 @@ void setup() {
84
85
85
86
humiditySensor.begin ();
86
87
87
- gw.sendSketchInfo (" MysensorMicro " , " 1.0" );
88
+ gw.sendSketchInfo (" Sensebender Micro " , " 1.0" );
88
89
89
90
gw.present (CHILD_ID_TEMP,S_TEMP);
90
91
gw.present (CHILD_ID_HUM,S_HUM);
91
92
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
+
94
96
}
95
97
96
98
97
99
// Main loop function
98
100
void loop () {
99
101
measureCount ++;
100
102
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.
101
107
102
108
if (measureCount > FORCE_TRANSMIT_INTERVAL
103
109
) { // force a transmission
104
110
forceTransmit = true ;
105
111
measureCount = 0 ;
106
112
}
107
-
113
+
108
114
gw.process ();
109
115
sendBattLevel (forceTransmit);
110
116
sendTempHumidityMeasurements (forceTransmit);
@@ -127,16 +133,17 @@ void sendTempHumidityMeasurements(bool force)
127
133
128
134
si7021_env data = humiditySensor.getHumidityAndTemperature ();
129
135
130
- float temperature = data.celsiusHundredths / 100 ;
136
+ float temperature = (isMetric ? data.celsiusHundredths : data. fahrenheitHundredths ) / 100.0 ;
131
137
132
138
int humidity = data.humidityPercent ;
133
139
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
+
135
144
gw.send (msgTemp.set (temperature,1 ));
136
- lastTemperature = temperature;
137
- }
138
- if (lastHumidity != humidity) {
139
145
gw.send (msgHum.set (humidity));
146
+ lastTemperature = temperature;
140
147
lastHumidity = humidity;
141
148
}
142
149
}
@@ -154,10 +161,10 @@ void sendBattLevel(bool force)
154
161
if (vcc != lastBattery) {
155
162
lastBattery = vcc;
156
163
// Calculate percentage
157
- gw. send (msgBattery. set (vcc));
164
+
158
165
vcc = vcc - 1900 ; // subtract 1.9V from vcc, as this is the lowest voltage we will operate at
159
166
160
- long percent = vcc / 14 ;
167
+ long percent = vcc / 14.0 ;
161
168
gw.sendBatteryLevel (percent);
162
169
}
163
170
}
@@ -278,7 +285,7 @@ void testMode()
278
285
while (1 ) // Blink OK pattern!
279
286
{
280
287
digitalWrite (LED_PIN, HIGH);
281
- delay (800 );
288
+ delay (200 );
282
289
digitalWrite (LED_PIN, LOW);
283
290
delay (200 );
284
291
}
@@ -288,10 +295,7 @@ void testMode()
288
295
Serial.println (F (" ----> Selftest failed!" ));
289
296
while (1 ) // Blink FAILED pattern! Rappidly blinking..
290
297
{
291
- digitalWrite (LED_PIN, HIGH);
292
- delay (100 );
293
- digitalWrite (LED_PIN, LOW);
294
- delay (100 );
295
298
}
296
299
}
297
300
}
301
+
0 commit comments