IoT_watering project - supervisor

Dependencies:   mbed-rtos mbed ssWi

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Mon Jan 23 20:36:42 2017 +0000
Parent:
1:dcfe7e79a45c
Child:
3:e49dd7bf7f1c
Commit message:
review

Changed in this revision

config.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
--- a/config.h	Thu Jan 19 23:31:18 2017 +0000
+++ b/config.h	Mon Jan 23 20:36:42 2017 +0000
@@ -4,7 +4,7 @@
 // TIME
 #define TIMEOUT_READ                10.0
 #define INTERVAL_SAMPLING           40.0
-#define INTERVAL_SYNC               6.0
+#define INTERVAL_SYNC               5.0
 #define INTERVAL_1_SECOND           1.0
 #define INTERVAL_60_SECONDS         60
 
@@ -39,6 +39,14 @@
 #define FILE_WTR                    "/local/WTR.txt"
 #define FILE_BSY                    "/local/BSY.txt"
 
+// NETWORK
+#define PAN_ID                      102
+#define CHANNEL_ID                  14
+#define XBEE_TX_PER_SECOND          3
+#define XBEE_RX_PER_SECOND          10
+#define XBEE_PIN_TX                 p9
+#define XBEE_PIN_RX                 p10
+
 struct watering_unit_node_t {
     int address;
     int watering_wait;
--- a/main.cpp	Thu Jan 19 23:31:18 2017 +0000
+++ b/main.cpp	Mon Jan 23 20:36:42 2017 +0000
@@ -6,6 +6,10 @@
 #include "xbee.hpp"
 #include "ssWiSocket.hpp"
 
+#ifndef DEBUG
+#define     printf(fmt,...)     
+#endif
+
 int last_watering[MAX_NUM_NODES];
 
 global_confg_t global_config;
@@ -23,7 +27,6 @@
 bool read_configuration();
 
 bool read_from_port(ssWiSocket *socket, int *value);
-
 bool read_timeout(ssWiSocket *socket, int *value);
 
 void do_sampling();
@@ -31,16 +34,15 @@
 
 int main() {
     // supervisor configuration
-    printf("6\r\n");
-#ifdef DEBUG
     printf("SUPERVISOR - config\r\n");
-#endif
+
+    // read configuration
     if (!read_configuration())
         error("Impossible to read configuration");
 
     // network configuration
-    XBeeModule xbee(p9, p10, 102, 14);
-    xbee.init(5, 10);
+    XBeeModule xbee(p9, p10, PAN_ID, CHANNEL_ID);
+    xbee.init(XBEE_TX_PER_SECOND, XBEE_RX_PER_SECOND);
     socket_command = ssWiSocket::createSocket(PORT_COMMANDS);
     for (int i = 0; i < global_config.num_units; i++) {
         socket_moisture[i] = ssWiSocket::createSocket(
@@ -60,25 +62,19 @@
         last_watering[i] = 1;
 
     // start
-#ifdef DEBUG
     printf("SUPERVISOR - start\r\n");
-#endif
 
     while(1) {
         int minute_counters = 0;
         read_counter();
-#ifdef DEBUG
-        printf("SUPERVISOR - wait...\r\n");
-#endif
+        printf("SUPERVISOR - waiting\r\n");
         do {
             // wait 1 minute
             Thread::wait(INTERVAL_60_SECONDS * 1000);
             minute_counters++;
         } while (minute_counters < global_config.wait_minutes);
 
-#ifdef DEBUG
         printf("SUPERVISOR - active\r\n");
-#endif
 
         // mark as busy
         DigitalOut led_busy(LED4);
@@ -86,13 +82,9 @@
         FILE* fp_busy = fopen(FILE_BSY, "w");
 
         // sample and water
-#ifdef DEBUG
         printf("SUPERVISOR - sampling\r\n");
-#endif
         do_sampling();
-#ifdef DEBUG
         printf("SUPERVISOR - watering\r\n");
-#endif
         do_watering();
         
         // increment counter
@@ -102,18 +94,11 @@
         fclose(fp);
 
         // send shutdown
-#ifdef DEBUG
         printf("SUPERVISOR - shutdown\r\n");
-#endif
 
-        Timer t;
-        t.start();
-        while(t.read() < INTERVAL_SYNC);
+        wait(INTERVAL_SYNC);
         socket_command->write(COMM_SHUTDOWN);
 
-        t.stop(); t.reset(); t.start();
-        while(t.read() < INTERVAL_SYNC);
-
         // mark as not busy
         led_busy = 0;
         fclose(fp_busy);
@@ -122,17 +107,12 @@
 
 void do_sampling () {
     FILE* fp = fopen(FILE_SNS, "a");
-    Timer t;
 
     socket_command->write(COMM_START_SAMPLING);
-    t.start();
-    while(t.read() < INTERVAL_SAMPLING);
+    wait(INTERVAL_SAMPLING);
     
     socket_command->write(COMM_STOP_SAMPLING);
-        t.stop();
-        t.reset();
-        t.start();
-    while(t.read() < INTERVAL_SYNC);
+    wait(INTERVAL_SYNC);
 
     for (int i = 0; i < global_config.num_units; i++) {
         int temp = 0, humi = 0, mois = 0, sampling_feedback = COMM_NO_VALUE;
@@ -162,28 +142,18 @@
 }
 
 void do_watering () {
+    FILE* fp = fopen(FILE_WTR, "a");
     for (int i = 0; i < global_config.num_units; i++) {
         if (global_config.count % global_config.nodes[i].watering_wait)
             continue;
-        FILE* fp = fopen(FILE_WTR, "a");
-
+        // write watering time in seconds
         socket_water[i]->write(global_config.nodes[i].watering_seconds);
-        
-        Timer t;
-        t.start();
-        while (t.read() < (global_config.nodes[i].watering_seconds +
-                                                                INTERVAL_SYNC));
-
-        t.stop(); t.reset(); t.start();
-        while (t.read() < INTERVAL_1_SECOND);
-        
+        wait(INTERVAL_SYNC);
+        // send watering command
         socket_command->write(COMM_START_WATERING_OFFSET + 
                                                 global_config.nodes[i].address);
-
-        t.stop(); t.reset(); t.start();
-        while (t.read() < (global_config.nodes[i].watering_seconds +
-                                                                INTERVAL_SYNC));
-        t.stop();
+        wait(global_config.nodes[i].watering_seconds + INTERVAL_SYNC + 
+                                                                 INTERVAL_SYNC);
         
         int watering_response = 0;
         int code = 4;
@@ -259,7 +229,7 @@
         }
     }
     fclose(fp);
-       
+
     return true;
 }