JNP3 SmartAlarm / Mbed 2 deprecated budzik

Dependencies:   TextLCD nRF24L01P mbed

Revision:
11:c94b9db597e9
Parent:
10:3417715786be
Child:
12:752ad129f385
--- a/main.cpp	Sun Jan 22 20:03:32 2017 +0000
+++ b/main.cpp	Mon Jan 23 13:07:47 2017 +0000
@@ -1,5 +1,4 @@
 #include "mbed.h"
-//#include "rtos.h"
 #include "TextLCD.h"
 #include "custom-chars.h"
 #include "melodies.h"
@@ -21,10 +20,9 @@
 double light_sum;
 int light_samples;
 time_t next_light = 0;
-//Mutex light_mutex;
 
 void lightSensor(){
-    light_sum += light_sensor;
+    light_sum += light_sensor.read();
     light_samples++;
     next_light = time(NULL) + LIGHT_INTERVAL;
 }
@@ -75,12 +73,15 @@
 std::vector<std::string> last_wireless_data;
 time_t last_wireless_time;
 
+//TODO: usredniac dane, w tym momencie wysyla tylko ostatnia wartosc!
+double last_min = 0, last_max = 0, last_avg = 0, last_temp = 0;
+
 void processWirelessData(char* data_string){
     std::string s = data_string;
     std::vector<std::string> data;
     size_t pos = 0;
     std::string token;
-    std::string delimiter = "&";
+    std::string delimiter = ";";
     
     while ((pos = s.find(delimiter)) != std::string::npos) {
         token = s.substr(0, pos);
@@ -91,22 +92,30 @@
     last_wireless_data = data;
     last_wireless_time = time(NULL);
     
+    pc.printf("data %d\r\n",data.size());
     if (data.size() >= 3){
-        double min = atof(data[0].c_str());
-        double max = atof(data[1].c_str());
-        double avg = atof(data[2].c_str());
+        last_min = atof(data[0].c_str());
+        last_max = atof(data[1].c_str());
+        last_avg = atof(data[2].c_str());
         
-        if (max > 0.3 && avg > 0.1)
+        if (last_max > 0.6 && last_avg > 0.2){
+            pc.printf("moved\r\n");
             movement_detected = true;
-        else
+        }
+        else{
+            pc.printf("not moved\r\n");
             movement_detected = false;
+        }
+    }
+    if (data.size() >= 4){
+        last_temp = atof(data[3].c_str());
     }
 }
 
 void radio_recv(){
     char rxData[TRANSFER_SIZE+1];
     int rx_bytes=0;
-
+    
     if(radio.readable(NRF24L01P_PIPE_P1)){
         rx_bytes = radio.read(NRF24L01P_PIPE_P1, rxData, TRANSFER_SIZE);
         rxData[TRANSFER_SIZE] = '\0';
@@ -263,7 +272,7 @@
         t.reset();
         t.start();
         while (!wifiDataReady && t <= timeout){
-            wait(0.01f);
+            wait(0.1f);
         }
         
         pc.printf("http GET trying #%d\r\n", retry);
@@ -280,14 +289,14 @@
 
 time_t getNextSync(){
     //TODO: zmiennej czasu nie powinno sie chyba tak modyfikowac
-    return time (NULL) + 23;
+    return time (NULL) + 45;
 }
 
 int syncFunction(){
     disp.sync_in_progress = 1;
     last_sync = time(NULL);
     std::string time_string = httpGETWithRetry("GET " 
-                "/time/current/ "
+                "/time/current "
                 "HTTP/1.1\r\n"
                 "Host: 10.1.8.202:8080\r\n\r\n", 2.0, 5);
     if (time_string == ""){
@@ -298,7 +307,7 @@
     }
     
     time_string = httpGETWithRetry("GET " 
-                "/alarm/get/nearest/id/5881e409e93a2218d42fe888 "
+                "/alarm/get/nearest/id/58362c0bd0d41f4f91240f29 "
                 "HTTP/1.1\r\n"
                 "Host: 10.1.8.202:8080\r\n\r\n", 2.0, 5);
     if (time_string == ""){
@@ -306,12 +315,12 @@
     else {
         next_alarm = atoi(time_string.c_str());
     }
-    
+    /*
     time_t now;
     time(&now);
     char time_str[50];
     strftime(time_str, 50, "%FT%TZ", gmtime(&now));
-    
+    */
     //Get light sensor data
     double light_sensor_avg;
     light_sensor_avg = light_samples == 0 ? 0 : light_sum / light_samples ;
@@ -319,21 +328,33 @@
     light_samples = 0;
     
     wifi.printf("POST " 
-                "/alarm/get/nearest/id/5881e409e93a2218d42fe888 "
+                "/statistics/value/%f/device_id/58362c0bd0d41f4f91240f29/type/light "
+                "HTTP/1.1\r\n"
+                "Host: 10.1.8.202:8080\r\n\r\n"
+                "\r\n\r\n", light_sensor_avg);
+                
+    if (last_temp != 0.0){
+        wifi.printf("POST " 
+                "/statistics/value/%f/device_id/58362c0bd0d41f4f91240f29/type/temperature "
                 "HTTP/1.1\r\n"
                 "Host: 10.1.8.202:8080\r\n\r\n"
-                "{"
-                "\"date\":\"%s\""
-                "\"value\":%f"
-                "\"device_id\":\"5881e409e93a2218d42fe888\""
-                "\"type\":\"light\""
-                "}\r\n\r\n", time_str, light_sensor_avg);
+                "\r\n\r\n", last_temp);
+        last_temp = 0;
+    }
+    if (last_max != 0.0){
+        wifi.printf("POST " 
+                "/statistics/value/%f/device_id/58362c0bd0d41f4f91240f29/type/movement_max "
+                "HTTP/1.1\r\n"
+                "Host: 10.1.8.202:8080\r\n\r\n"
+                "\r\n\r\n", last_max);
+        last_max = 0.0;
+    }
     next_sync = getNextSync();
     return 0;
 }
 
 //Alarm
-PwmOut sound(D13);
+PwmOut sound(D12);
 bool alarm_playing = false;
 bool alarm_stopped = false;
 
@@ -356,6 +377,10 @@
     alarm_playing = false;
     disp.alarm_on = 0;
     sound = 0.0f;
+    wifi.printf("DELETE " 
+                "/alarm/delete/nearest/58362c0bd0d41f4f91240f29 "
+                "HTTP/1.1\r\n"
+                "Host: 10.1.8.202:8080\r\n\r\n"); 
 }
 
 //TODO: won't work if melody contains -1 per at the beginning, has to have at least one note
@@ -363,8 +388,8 @@
     if (!alarm_playing)
         return;
     
-    const float bpm = 100.0f; //tempo
-    const float base_period = 0.5f;
+    const float bpm = 120.0f; //tempo
+    const float base_period = 0.25f;
     const float base_time = bpm / 60.0f;
 
     if (per[next_note] <= 0)
@@ -379,7 +404,7 @@
     }
     
     next_note++;
-    next_note_timeout.attach(&play_next_note, base_time/per[next_note]);    
+    next_note_timeout.attach(&play_next_note, base_time/per[next_note-1]);    
 }
 
 int main() {
@@ -393,11 +418,11 @@
     pc.printf("Waiting for time\r\n");
     
     std::string time_string = httpGETWithRetry("GET " 
-                "/time/current/ "
+                "/time/current "
                 "HTTP/1.1\r\n"
                 "Host: 10.4.8.136:8080\r\n\r\n", 5.0, 5);
     
-    pc.printf("Get time returned >>%s<< %d\r\n", time_string.c_str());
+    //pc.printf("Get time returned >>%s<< %d\r\n", time_string.c_str());
     
     if (time_string == ""){
         lcd.locate(0,1); //Put in top row
@@ -412,7 +437,7 @@
     initRadio(6, NRF24L01P_TX_PWR_ZERO_DB, NRF24L01P_DATARATE_250_KBPS);
     
     while (1){
-        pc.printf("ok\r\n");
+        //pc.printf("ok %f\r\n",light_sensor.read());
         
         //Handling button presses
         if (userButtonPressed) {
@@ -426,7 +451,6 @@
             userButtonTimer.stop();
             if (userButtonTimer.read_ms() > userButtonLongPress){
                 pc.printf("User button long pressed");
-                disp.backlightOff(); //that was just debug code anyway
                 if (alarm_playing){
                     stop_alarm();
                 }
@@ -462,8 +486,11 @@
         
         //Check if alarm should be played
         if (next_alarm != 0 && !alarm_playing){
+            //pc.printf("alarm0\r\n");
             if (time(NULL) >= next_alarm-(15*60) ){ //Hardcoded to 15 minutes before set time
+            pc.printf("alarm1\r\n");
                 if (movement_detected || time(NULL) >= next_alarm){
+                    //pc.printf("alarm2\r\n");
                     start_alarm();
                 }
             }