2c12d938ab7705814563d61476a476be926b6f2e
[Arduino] / wlan_si / wlan_si.ino
1 #include <Wire.h>
2 #include <Adafruit_MPL115A2.h>
3
4 Adafruit_MPL115A2 mpl115a2;
5
6 int led = 5; // LED on d5
7 int pir = 6; // PIR on d6
8
9 // hardware based on https://dev.wlan-si.net/wiki/Telemetry/sensgw
10 // original software https://github.com/SloMusti/sensgw
11
12 float runningAverage(float M) {
13   #define LM_SIZE 100
14   static float LM[LM_SIZE];      // LastMeasurements
15   static byte index = 0;
16   static float sum = 0;
17   static byte count = 0;
18
19   // keep sum updated to improve speed.
20   sum -= LM[index];
21   LM[index] = M;
22   sum += LM[index];
23   index++;
24   index = index % LM_SIZE;
25   if (count < LM_SIZE) count++;
26
27   return sum / count;
28 }
29
30 void setup(void) 
31 {
32   Serial.begin(9600);
33   Serial.println("Hello!");
34   
35   Serial.println("Getting barometric pressure ...");
36   mpl115a2.begin();
37   
38   pinMode(led, OUTPUT);
39   pinMode(pir, INPUT);
40 }
41
42 int count = 0;
43
44 void loop(void) 
45 {
46   digitalWrite(led, HIGH);
47
48   float pressureKPA = 0, temperatureC = 0;    
49
50   mpl115a2.getPT(&pressureKPA,&temperatureC);
51
52   if ( count++ % 10 == 0 ) {
53   Serial.print("Pressure="); Serial.print(runningAverage(pressureKPA), 4); Serial.print(" kPa ");
54   Serial.print("Temp="); Serial.print(temperatureC, 1); Serial.print(" C");
55
56   Serial.print(" PIR="); Serial.print( digitalRead(pir) );
57
58   Serial.print(" A0="); Serial.print( analogRead(0) );
59
60   Serial.println();
61
62   } else {
63         runningAverage(pressureKPA);
64   }
65
66   digitalWrite(led, LOW);
67   
68   delay(100);
69 }