Software for Sensor box, read analogue values and send using nRF

Dependencies:   MMA8451Q mbed nRF24L01P

Fork of Acclerometer_node by Sensor CDT

Revision:
6:7198985d3442
Parent:
5:7c53f5f2af8d
Child:
7:46da94ccfa67
--- a/main.cpp	Wed Jul 01 21:16:03 2015 +0000
+++ b/main.cpp	Tue Jul 21 10:15:20 2015 +0000
@@ -1,95 +1,110 @@
 #include "mbed.h"
-#include "nRF24L01P.h"
-#include "MMA8451Q.h"
-#include <vector>
-#include <math.h>
+#include "math.h"
 
-#define MMA8451_I2C_ADDRESS (0x1d<<1)
-#define TRANSFER_SIZE   24
+const int ARRAY_LENGTH    = 500;
+float temperature_celsius, temperature_voltage, light_voltage, raw_pir_reading, noise_voltage, noise_maximum, noise_average, noise_total, noise_variance, noise_sd, presence_percentage, num_consecutive_runs;
+char publishString[40];
+float noise_array[ARRAY_LENGTH];
+int pir_array[ARRAY_LENGTH];
 
-Serial pc(USBTX, USBRX); // tx, rx
-
-PinName const SDA = PTE25;
-PinName const SCL = PTE24;
+//bool DEBUG_MODE = false;
 
 AnalogIn light_ain(A0);
 AnalogIn temp_ain(A1);
-AnalogIn pir_ain(A3);
-
-nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0);    // mosi, miso, sck, csn, ce, irq
-
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
+AnalogIn pir_ain(A2);
+AnalogIn noise_ain(A3);
 
-uint16_t light;
-uint16_t temp;
-uint16_t pir;
+void publish_measurements() {
+    temperature_voltage = temp_ain.read_u16();
+    // Convert DAC reading to millivolts
+    temperature_voltage = (temperature_voltage * 3.3 * 1000) / 4095;
+    // Convert millivolts to Celsius using datasheet equation
+    temperature_celsius = (2103 - temperature_voltage) / 10.9;
+    //if(DEBUG_MODE) {
+        //Serial.println("Temperature (Celsius): " + String(temperature_celsius));
+    //}
 
-std::vector<uint16_t> light_data;
-std::vector<uint16_t> temp_data;
-std::vector<uint16_t> pir_data;
+    light_voltage = light_ain.read_u16();
 
-void getdata()
-{
-    light = light_ain.read_u16();
-    temp = temp_ain.read_u16();
-    pir = pir_ain.read_u16();
+    sprintf(publishString,"%.1f, %.1f, %.1f, %.1f, %.1f", temperature_celsius, light_voltage, noise_maximum, noise_average, noise_variance);
+    //Spark.publish("measurements", publishString);
+    sprintf(publishString, "%.1f, %.1f", presence_percentage, num_consecutive_runs);
+    //Spark.publish("measurements", publishString);
+}
+
+
+void measure_pir_and_noise() {
+    noise_voltage = 0;
 
-    light_data.push_back (light);
-    temp_data.push_back (temp);
-    pir_data.push_back (pir);
-
-    light_data.pop_back();
-    temp_data.pop_back();
-    pir_data.pop_back();
-    
-        
-    printf("%f\n", light_data);
-   
+    for (int i = 0; i < ARRAY_LENGTH; i++) {
+        raw_pir_reading = pir_ain.read_u16();
+        noise_voltage = noise_ain.read_u16();
+        noise_array[i] = noise_voltage;
+        if (raw_pir_reading > 3000) {
+            pir_array[i] = 1;
+        } else {
+            pir_array[i] = 0;
+        }
+        //if (DEBUG_MODE) {
+            //Serial.println("PIR: " + String(raw_pir_reading));
+            //Serial.println("Noise: " + String(noise_voltage));
+        //}
+        wait(0.1);
+    }
 }
 
 
-int main()
-{
-
-    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
-
-    light_data.assign (20,0);
-    temp_data.assign (20,0);
-    pir_data.assign (20,0);
-
-
-    char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE];
-    int txDataCnt = 0;
-    int rxDataCnt = 0;
-
-    my_nrf24l01p.powerUp();
+void noise_analysis() {
+    float residuals[ARRAY_LENGTH];
+    noise_maximum = 0;
+    noise_average = 0;
+    noise_variance = 0;
+    noise_total = 0;
 
-    // Display the (default) setup of the nRF24L01+ chip
-    //pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
-    //pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
-    //pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
-    //pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
-    //pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
-
-    //pc.printf( "Type keys to test transfers:\r\n  (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
-
-    my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
+    for (int i = 0; i < ARRAY_LENGTH; i++) {
+        if (noise_array[i] > noise_maximum) noise_maximum = noise_array[i];
+        noise_total += noise_array[i];
+    }
+    noise_average = noise_total / (ARRAY_LENGTH * 1.0);
 
-    my_nrf24l01p.setReceiveMode();
-    my_nrf24l01p.enable();
-
-    printf("MMA8451 ID: %d\n", acc.getWhoAmI());
-
-    while (1) {
-        getdata();
-
-        // txDataCnt =  sprintf(txData, "  %1.3f   %1.3f   %1.3f\n", x,y,z);
-        //my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
-        myled1 = !myled1;
-        wait(0.5);
+    for (int i = 0; i < ARRAY_LENGTH; i++) {
+        residuals[i] = noise_array[i] - noise_average;
+        noise_variance += residuals[i] * residuals[i];
     }
-
+    noise_variance = noise_variance / (ARRAY_LENGTH * 1.0);
 }
 
 
+void pir_analysis() {
+    int counts_over_1s = 0;
+    int longest_consecutive_run = 0;
+    int curr_num_consecutive = 0;
+
+    counts_over_1s = pir_array[0] == 1 ? 1 : 0;
+    for (int i = 1; i < ARRAY_LENGTH; i++) {
+        if (pir_array[i] == 1) {
+            ++counts_over_1s;
+        }
+        if (pir_array[i] == 1 && pir_array[i-1] == 1) {
+            ++curr_num_consecutive;
+        } else {
+            if (curr_num_consecutive > longest_consecutive_run) {
+                longest_consecutive_run = curr_num_consecutive;
+                curr_num_consecutive = 0;
+            }
+        }
+    }
+
+    presence_percentage = 100 * counts_over_1s * 1.0 / ARRAY_LENGTH;
+    num_consecutive_runs = longest_consecutive_run;
+}
+
+
+int main() {
+    while (1) {
+    measure_pir_and_noise();
+    noise_analysis();
+    pir_analysis();
+    publish_measurements();
+}
+}
\ No newline at end of file