IoT_watering project - supervisor

Dependencies:   mbed-rtos mbed ssWi

Files at this revision

API Documentation at this revision

Comitter:
mariob
Date:
Thu Feb 16 22:08:27 2017 +0000
Parent:
2:69d2d4c76f02
Commit message:
release 1.0

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
ssWi.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 69d2d4c76f02 -r e49dd7bf7f1c config.h
--- a/config.h	Mon Jan 23 20:36:42 2017 +0000
+++ b/config.h	Thu Feb 16 22:08:27 2017 +0000
@@ -7,6 +7,7 @@
 #define INTERVAL_SYNC               5.0
 #define INTERVAL_1_SECOND           1.0
 #define INTERVAL_60_SECONDS         60
+#define INTERVAL_POWER_START        20.0
 
 // COMM VALUES
 #define COMM_NO_VALUE               0x0000
@@ -47,6 +48,10 @@
 #define XBEE_PIN_TX                 p9
 #define XBEE_PIN_RX                 p10
 
+#define POWER_EN_PIN                p27
+#define POWER_ON                    0
+#define POWER_OFF                   1
+
 struct watering_unit_node_t {
     int address;
     int watering_wait;
diff -r 69d2d4c76f02 -r e49dd7bf7f1c main.cpp
--- 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");
diff -r 69d2d4c76f02 -r e49dd7bf7f1c ssWi.lib
--- a/ssWi.lib	Mon Jan 23 20:36:42 2017 +0000
+++ b/ssWi.lib	Thu Feb 16 22:08:27 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mariob/code/ssWi/#133e42dc82b0
+http://mbed.org/users/mariob/code/ssWi/#e8058fd532d6