IoT_watering project - supervisor

Dependencies:   mbed-rtos mbed ssWi

Revision:
3:e49dd7bf7f1c
Parent:
2:69d2d4c76f02
--- a/main.cpp	Mon Jan 23 20:36:42 2017 +0000
+++ b/main.cpp	Thu Feb 16 22:08:27 2017 +0000
@@ -10,10 +10,10 @@
 #define     printf(fmt,...)     
 #endif
 
-int last_watering[MAX_NUM_NODES];
-
+// global configuration
 global_confg_t global_config;
 
+// ssWi sockets
 ssWiSocket* socket_command;
 ssWiSocket* socket_moisture[MAX_NUM_NODES];
 ssWiSocket* socket_temperature[MAX_NUM_NODES];
@@ -23,16 +23,23 @@
 
 LocalFileSystem local("local");
 
+// read counter value from file
 bool read_counter();
+// read board configuration from file
 bool read_configuration();
 
+// return true if a message has been received, false otherwise
 bool read_from_port(ssWiSocket *socket, int *value);
+// blocking read - timeout after 10 seconds
 bool read_timeout(ssWiSocket *socket, int *value);
 
 void do_sampling();
 void do_watering();
 
 int main() {
+    DigitalOut power_device(POWER_EN_PIN);
+    power_device = POWER_OFF;
+
     // supervisor configuration
     printf("SUPERVISOR - config\r\n");
 
@@ -41,7 +48,7 @@
         error("Impossible to read configuration");
 
     // network configuration
-    XBeeModule xbee(p9, p10, PAN_ID, CHANNEL_ID);
+    XBeeModule xbee(XBEE_PIN_TX, XBEE_PIN_RX, 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++) {
@@ -57,16 +64,11 @@
                         global_config.nodes[i].address * 5 + PORT_WATER_OFFSET);
     }
 
-    // initialize shared variables
-    for (int i = 0; i < MAX_NUM_NODES; i++)
-        last_watering[i] = 1;
-
     // start
     printf("SUPERVISOR - start\r\n");
 
     while(1) {
         int minute_counters = 0;
-        read_counter();
         printf("SUPERVISOR - waiting\r\n");
         do {
             // wait 1 minute
@@ -81,6 +83,12 @@
         led_busy = 1;
         FILE* fp_busy = fopen(FILE_BSY, "w");
 
+        // power watering units
+        power_device = POWER_ON;
+        wait(INTERVAL_POWER_START);
+        
+        read_counter();
+
         // sample and water
         printf("SUPERVISOR - sampling\r\n");
         do_sampling();
@@ -98,7 +106,11 @@
 
         wait(INTERVAL_SYNC);
         socket_command->write(COMM_SHUTDOWN);
+        wait(INTERVAL_SYNC * 2);
 
+        // power off devices
+        power_device = POWER_OFF;
+        
         // mark as not busy
         led_busy = 0;
         fclose(fp_busy);
@@ -169,8 +181,8 @@
                                      global_config.nodes[i].address,
                                      code,
                                      global_config.nodes[i].watering_seconds);
-        fclose(fp);
     }
+    fclose(fp);
 }
 
 bool read_from_port(ssWiSocket *socket, int *value) {
@@ -199,7 +211,6 @@
     int state = 0;
     int n_unit = 0;
     
-    // read configuration file
     FILE *fp = fopen(FILE_CFG, "r");
     if(fp == NULL)
         return false;
@@ -234,7 +245,6 @@
 }
 
 bool read_counter () {
-    // read counter
     FILE *fp = fopen(FILE_CNT, "r");
     if(fp == NULL) {
         fp = fopen(FILE_CNT, "w");