SEDO subject project

Dependencies:   ds3231 mbed-rtos mbed DHT

Files at this revision

API Documentation at this revision

Comitter:
ValenSalLop
Date:
Mon May 15 18:30:20 2017 +0000
Parent:
11:a9f41d6489c9
Commit message:
final;

Changed in this revision

light_sensor.cpp Show annotated file Show diff for this revision Revisions of this file
light_sensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
motor_cnt.cpp Show annotated file Show diff for this revision Revisions of this file
motor_cnt.h Show annotated file Show diff for this revision Revisions of this file
diff -r a9f41d6489c9 -r 1d544cdab2cf light_sensor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light_sensor.cpp	Mon May 15 18:30:20 2017 +0000
@@ -0,0 +1,48 @@
+/*
+ * Light sensor operations
+ */
+#include "mbed.h"
+#include "rtos.h"
+#include "main.h"
+#include "light_sensor.h"
+
+AnalogIn LDR(A5);
+
+uint32_t light_init(void const *args)
+{
+    return 1;
+}
+void light_thread(void const *args)
+{
+//DEBUG
+    mutexPCComm.lock();
+    pc.printf("LDR: thread init\n");
+    mutexPCComm.unlock();
+    float lightCurr;
+    int8_t error = 0;
+    int8_t attempts=0;
+    while(true) {
+        Thread::signal_wait(0x1);
+        mutexPCComm.lock();
+        pc.printf("LDR: loop\n");
+        mutexPCComm.unlock();
+        error = -1;
+        attempts=0;
+        while(error!=0 && attempts<MAX_READ_ATTEMPTS_LIGHT && sensors_running == true) {
+            lightCurr=LDR.read();
+            error = 0;
+            if(error==0) {
+                mutexData.lock();
+                data.light = lightCurr;
+                data.lightError = 0;
+                mutexData.unlock();
+            } else {
+                mutexData.lock();
+                data.lightError = 0;
+                mutexData.unlock();
+                attempts++;
+                Thread::wait(3000);
+            }
+        } // while read attempts
+    } // main thread while
+} // thread function
diff -r a9f41d6489c9 -r 1d544cdab2cf light_sensor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/light_sensor.h	Mon May 15 18:30:20 2017 +0000
@@ -0,0 +1,10 @@
+#ifndef LIGHT_SENSOR_H
+#define LIGHT_SENSOR_H
+
+#define MAX_READ_ATTEMPTS_LIGHT 3
+
+
+extern uint32_t light_init(void const  *args);
+extern void light_thread(void const *args);
+
+#endif
\ No newline at end of file
diff -r a9f41d6489c9 -r 1d544cdab2cf main.cpp
--- a/main.cpp	Mon May 08 18:30:12 2017 +0000
+++ b/main.cpp	Mon May 15 18:30:20 2017 +0000
@@ -5,6 +5,7 @@
 #include "real_time_clock.h"
 #include "temp_hum_sensor.h"
 #include "motor_cnt.h"
+#include "light_sensor.h"
 
 #define TASKS_NUMBER    (sizeof(taskList)/sizeof(taskList[0]))
 
@@ -19,7 +20,7 @@
 
 static struct task_definition_struct taskList[] = {
     { temp_hum_thread,    &temp_hum_init, NULL, NULL, osPriorityNormal, 100, NULL, 0 },
-    //{ nodeComm_thread,    &initNodeComm, NULL, NULL, osPriorityNormal, 100, NULL, 0 }
+    { light_thread,    &light_init, NULL, NULL, osPriorityNormal, 100, NULL, 0 }
 };
 
 
@@ -55,13 +56,14 @@
         // Stop all remain tasks
         sensors_running = false;
         elapsed_time = rtc.get_epoch()-epoch_time;
-        
-    mutexPCComm.lock();
-    pc.printf("MAIN: elapsed %ld\n",(long)elapsed_time);
-    mutexPCComm.unlock();
-        if(10-elapsed_time>0){
+
+        mutexPCComm.lock();
+        pc.printf("MAIN: elapsed %ld\n",(long)elapsed_time);
+        mutexPCComm.unlock();
+        if(10-(long)elapsed_time>0) {
             Thread::wait((10-elapsed_time)*1000);
-        }
+        } else
+            Thread::wait(1000);
     }
 }
 
@@ -69,6 +71,7 @@
 {
     Timer t;
     int error = -1;
+    time_t ts  = rtc.get_epoch();
     t.reset();
     t.start();
     while(error != 0 && t.read()<MAX_TIME_TO_WAIT_NODE_COMM) {
@@ -81,8 +84,13 @@
             pc.printf("Temp\t%f\n",data.temperature);
             pc.printf("Hum\t%f\n",data.humidity);
             pc.printf("DewP\t%f\n",data.dewPoint);
+            pc.printf("Light\t%f\n",data.light);
+            pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError);
+            pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.lightError);
             mutexPCComm.unlock();
-            xbee.printf("{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}}\n",data.temperature,data.humidity,data.dewPoint);
+            xbee.printf("{%ld:{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}},{LIGHT:{light:%f}})\n",(long)ts, data.temperature,data.humidity,data.dewPoint,data.light);
+            set_motor_direction(1);
+            set_motor_speed(data.light);
         }
         mutexData.unlock();
         if(error!=0) {
@@ -101,13 +109,24 @@
             pc.printf("Temp:\t%f\n",data.temperature);
             pc.printf("Hum:\t%f\n",data.humidity);
             pc.printf("DewP:\t%f\n",data.dewPoint);
-            pc.printf("DHTError:\t%d\n",data.DHTError);
-            xbee.printf("{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}}\n",data.temperature,data.humidity,data.dewPoint);
+            pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError);
+            xbee.printf("{%ld:{DHT:{temp:%4.2f,hum:%4.2f,dew:%4.2f}}\n",(long)ts, data.temperature,data.humidity,data.dewPoint);
             mutexPCComm.unlock();
-        } else {
+        }
+        if( data.lightError == 0) {
             mutexPCComm.lock();
-            pc.printf("DHTError:\t%d\n",data.DHTError);
-            xbee.printf("{DHT:{error:\t%d}}\n",data.DHTError);
+            pc.printf("Light\t%f\n",data.light);
+            pc.printf("%ld\tlightError:\t%d\n",(long)ts, data.lightError);
+            xbee.printf("{%ld:{LIGHT:{light:%f}})\n",(long)ts, data.light);
+            mutexPCComm.unlock();
+            set_motor_direction(1);
+            set_motor_speed(data.light);
+        }
+        if( data.lightError != 0 || data.lightError != 0) {
+            mutexPCComm.lock();
+            pc.printf("%ld\tDHTError:\t%d\n",(long)ts, data.DHTError);
+            pc.printf("%ld\tlightError:\t%d\n",(long)ts, data.lightError);
+            xbee.printf("{%ld:{DHT:{error:\t%d},{LIGHT:{error:%f}}}\n",(long)ts, data.DHTError, data.light);
             mutexPCComm.unlock();
         }
         mutexData.unlock();
@@ -115,6 +134,7 @@
     // Reset all data status
     mutexData.lock();
     data.DHTError = -128;
+    data.lightError = -128;
     mutexData.unlock();
     return error;
 }
diff -r a9f41d6489c9 -r 1d544cdab2cf main.h
--- a/main.h	Mon May 08 18:30:12 2017 +0000
+++ b/main.h	Mon May 15 18:30:20 2017 +0000
@@ -10,6 +10,8 @@
     float humidity;
     float dewPoint;
     int8_t DHTError;
+    float light;
+    int8_t lightError;
 };
 
 struct task_definition_struct {
diff -r a9f41d6489c9 -r 1d544cdab2cf motor_cnt.cpp
--- a/motor_cnt.cpp	Mon May 08 18:30:12 2017 +0000
+++ b/motor_cnt.cpp	Mon May 15 18:30:20 2017 +0000
@@ -1,12 +1,12 @@
 #include "mbed.h"
 #include "motor_cnt.h"
 
-static PwmOut m_speed(D5);
-static DigitalOut m_direction(D4);
-static uint8_t speed_current = 0;
-static uint8_t direction_current = 0;
+PwmOut m_speed(D5);
+DigitalOut m_direction(D4);
+uint8_t speed_current = 0;
+uint8_t direction_current = 0;
 
-int set_motor_speed(uint8_t speed)
+int set_motor_speed(float speed)
 {
     m_speed = speed;
     speed_current = speed;
diff -r a9f41d6489c9 -r 1d544cdab2cf motor_cnt.h
--- a/motor_cnt.h	Mon May 08 18:30:12 2017 +0000
+++ b/motor_cnt.h	Mon May 15 18:30:20 2017 +0000
@@ -4,7 +4,7 @@
 extern PwmOut m_speed;
 extern DigitalOut m_direction;
 
-extern int set_motor_speed(uint8_t speed);
+extern int set_motor_speed(float speed);
 extern int set_motor_direction(uint8_t direction);
 extern int get_motor_speed(void);
 extern int get_motor_direction(void);