make upgrade - stop systemd, upload, start it again
[GroveSensor] / GroveSensor.ino
index fe54a7d..6437b02 100644 (file)
@@ -1,6 +1,11 @@
 #include <Arduino.h>
 #include <U8x8lib.h>
 
+#include "RunningAverage.h"
+
+RunningAverage temp_avg(10);
+RunningAverage hum_avg(10);
+
 #include "DHT.h"
 
 #include <Adafruit_BMP280.h>
@@ -19,6 +24,12 @@ DHT dht(DHTPIN, DHTTYPE);
 Adafruit_BMP280 bmp; // I2C
 
 
+#include "LIS3DHTR.h"
+#include <Wire.h>
+LIS3DHTR<TwoWire> LIS; //IIC
+#define WIRE Wire
+
+
 #define BUTTON_PIN 6 
 
 #define SOUND_PIN A2
@@ -49,18 +60,29 @@ void setup(void) {
   pinMode(SOUND_PIN, INPUT);
   pinMode(LIGHT_PIN, INPUT);
 
+
+  LIS.begin(WIRE, 0x19);
+  LIS.openTemp();
+  delay(100);
+  LIS.setFullScaleRange(LIS3DHTR_RANGE_2G);
+  LIS.setOutputDataRate(LIS3DHTR_DATARATE_5KHZ);
+  LIS.setHighSolution(true); //High solution enable
+
 }
 
-unsigned long button_timeout;
+unsigned long t;
  
 void loop(void) {
 
+  t = millis();
+
   int oled_active = ! digitalRead(BUTTON_PIN);
   u8x8.setPowerSave( oled_active );
 
-  float temp, humi;
-  temp = dht.readTemperature();
-  humi = dht.readHumidity();
+  temp_avg.addValue( dht.readTemperature() );
+  hum_avg.addValue( dht.readHumidity() );
+  float temp = temp_avg.getAverage();
+  float humi = hum_avg.getAverage();
 
   u8x8.setFont(u8x8_font_chroma48medium8_r);
   u8x8.setCursor(0, 0);
@@ -81,6 +103,7 @@ void loop(void) {
   u8x8.print("Humidity:");
   u8x8.print(humi);
   u8x8.print("%");
+
   Serial.print(",dht_humidity=");
   Serial.print(humi);
 
@@ -92,6 +115,15 @@ void loop(void) {
   Serial.print(",bmp_pressure=");
   Serial.print(pressure);
 
+  unsigned long sumSquare;
+  for(int i=0; i++; i<1000) {
+    int sound = analogRead(SOUND_PIN);
+    sumSquare += sound * sound;
+  }
+  int rms_sound = sqrt(sumSquare / 1000);
+  Serial.print(",rms_sound=");
+  Serial.print(rms_sound);
+
   int sound = analogRead(SOUND_PIN);
   u8x8.setCursor(0,3 * 9);
   u8x8.print( sound );
@@ -103,10 +135,24 @@ void loop(void) {
   u8x8.print( light );
   Serial.print(",light=");
   Serial.print(light);
-  Serial.println();
 
   u8x8.refreshDisplay();
-  delay(1000);
+
+  float x = 0, y = 0, z = 0;
+  for(int i=0; i<=300; i++) {
+       x += LIS.getAccelerationX();
+       y += LIS.getAccelerationY();
+       z += LIS.getAccelerationZ();
+  }
+
+  Serial.print(",x="); Serial.print(x);
+  Serial.print(",y="); Serial.print(y);
+  Serial.print(",z="); Serial.print(z);
+
+  Serial.println();
+
+  t = t + 1000 - millis();
+  delay(t);
 
 }