make upgrade - stop systemd, upload, start it again
[GroveSensor] / GroveSensor.ino
1 #include <Arduino.h>
2 #include <U8x8lib.h>
3
4 #include "RunningAverage.h"
5
6 RunningAverage temp_avg(10);
7 RunningAverage hum_avg(10);
8
9 #include "DHT.h"
10
11 #include <Adafruit_BMP280.h>
12
13 U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
14
15 // U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display
16  
17
18 #define DHTPIN 3     // what pin we're connected to
19 #define DHTTYPE DHT11   // DHT 11 
20
21 DHT dht(DHTPIN, DHTTYPE);
22
23
24 Adafruit_BMP280 bmp; // I2C
25
26
27 #include "LIS3DHTR.h"
28 #include <Wire.h>
29 LIS3DHTR<TwoWire> LIS; //IIC
30 #define WIRE Wire
31
32
33 #define BUTTON_PIN 6 
34
35 #define SOUND_PIN A2
36 #define LIGHT_PIN A6
37  
38 void setup(void) {
39   //u8x8.setBusClock(100000);  // If you breakout other modules, please enable this line
40   u8x8.begin();
41   u8x8.setPowerSave(0);
42   u8x8.setFlipMode(1);
43
44   Serial.begin(115200);
45
46   dht.begin();
47
48   if (!bmp.begin()) {
49     Serial.println(F("Could not find a valid BMP280 sensor!"));
50   }
51
52   /* Default settings from datasheet. */
53   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
54                   Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
55                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
56                   Adafruit_BMP280::FILTER_X16,      /* Filtering. */
57                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
58
59   pinMode(BUTTON_PIN, OUTPUT);
60   pinMode(SOUND_PIN, INPUT);
61   pinMode(LIGHT_PIN, INPUT);
62
63
64   LIS.begin(WIRE, 0x19);
65   LIS.openTemp();
66   delay(100);
67   LIS.setFullScaleRange(LIS3DHTR_RANGE_2G);
68   LIS.setOutputDataRate(LIS3DHTR_DATARATE_5KHZ);
69   LIS.setHighSolution(true); //High solution enable
70
71 }
72
73 unsigned long t;
74  
75 void loop(void) {
76
77   t = millis();
78
79   int oled_active = ! digitalRead(BUTTON_PIN);
80   u8x8.setPowerSave( oled_active );
81
82   temp_avg.addValue( dht.readTemperature() );
83   hum_avg.addValue( dht.readHumidity() );
84   float temp = temp_avg.getAverage();
85   float humi = hum_avg.getAverage();
86
87   u8x8.setFont(u8x8_font_chroma48medium8_r);
88   u8x8.setCursor(0, 0);
89   //u8x8.print("Temp:");
90   u8x8.print(temp);
91   u8x8.print("C ");
92   Serial.print("dht_temp=");
93   Serial.print(temp);
94
95   temp = bmp.readTemperature();
96   u8x8.print( temp );
97   Serial.print(",bmp_temp=");
98   Serial.print(temp);
99
100   u8x8.print(" ");
101
102   u8x8.setCursor(0,1 * 9);
103   u8x8.print("Humidity:");
104   u8x8.print(humi);
105   u8x8.print("%");
106
107   Serial.print(",dht_humidity=");
108   Serial.print(humi);
109
110   float pressure;
111   pressure = bmp.readPressure();
112
113   u8x8.setCursor(0,2 * 9);
114   u8x8.print( pressure );
115   Serial.print(",bmp_pressure=");
116   Serial.print(pressure);
117
118   unsigned long sumSquare;
119   for(int i=0; i++; i<1000) {
120     int sound = analogRead(SOUND_PIN);
121     sumSquare += sound * sound;
122   }
123   int rms_sound = sqrt(sumSquare / 1000);
124   Serial.print(",rms_sound=");
125   Serial.print(rms_sound);
126
127   int sound = analogRead(SOUND_PIN);
128   u8x8.setCursor(0,3 * 9);
129   u8x8.print( sound );
130   Serial.print(",sound=");
131   Serial.print(sound);
132
133   int light = analogRead(LIGHT_PIN);
134   u8x8.setCursor(0,3 * 9);
135   u8x8.print( light );
136   Serial.print(",light=");
137   Serial.print(light);
138
139   u8x8.refreshDisplay();
140
141   float x = 0, y = 0, z = 0;
142   for(int i=0; i<=300; i++) {
143         x += LIS.getAccelerationX();
144         y += LIS.getAccelerationY();
145         z += LIS.getAccelerationZ();
146   }
147
148   Serial.print(",x="); Serial.print(x);
149   Serial.print(",y="); Serial.print(y);
150   Serial.print(",z="); Serial.print(z);
151
152   Serial.println();
153
154   t = t + 1000 - millis();
155   delay(t);
156
157 }
158