It is a simple IoT solution for plant life monitoring and maintenance, based on STM32NUCLEO boards and expansion modules.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed

GreenYourLife, A Plant Life Monitoring and Maintenance System

Elaborated By Engineers, Designed For The World

Introduction

A healthy ecosystem is one where all the actors in it work in synergy: each of them playing a part that serves the other. Plants play an important part in regulating our quality of life. In the modern world, they would often times be placed in buildings and houses, as decorations or as medicinal and therapeutic purposes. Whatever the reason, GreenYourLife is here to help you manage them.

GreenYourLife is an Internet of Things (IoT) solution that will allow you to perform the following:

  • Follow your plant's well-being on your connected device (smartphone, etc.)
  • Maintain your plant's well-being automatically

We've developed an indoor plant monitoring system with the STM32NUCLEO development board. The system will collect the plant's environmental data (air temperature, air humidity and soil moisture) and transfer them to your connected device. The data collected will then be stored on a cloud server, where you can follow the progress of your plant life.

How it works

Plant maintanence

Our concept of maintanence is the automatic catering of the plant's needs. In this version, we focus on water supply. Future versions may indeed include more than that.

On-board sensors on the STM32NUCLEO will obtain measurements of 3 main parameters on the plant surrounding:

  1. Air temperature
  2. Air humidity
  3. Soil moisture

Based on these values, the software then acts upon the water pump: activating it when in need of water and deactivating it if not.

Plant monitoring

Plant monitoring, for us, is the follow-up of the plant's health by monitoring environment quality and plant nutritions. In this version, we only monitor the three parameters listed above. Future version may include more advanced and smarter monitoring technique.

The STM32NUCLEO device acts as a Bluetooth server and awaits connection from any client (smartphone, etc.). Upon connection, the server updates the plant environment parameters at a fixed interval. The client application will then transfer these received characteristics to the cloud.

Details

Hardware

The target platform is the STM32NUCLEO L476RG development board with two additional modules:

  • the IDB05A1 Bluetooth LE expansion board, which uses a BlueNRG coprocessor
  • and the IKS01A1 MEMS sensor expansion board

There are several additional materials that constitute the system:

  • a Funduino Soil Moisture Sensor, hooked up to 5V at pin PB_1
  • a relay, hooked up to 3.3V at pin PC_8 and controlled by a PWM pulse
  • a DC water pump

Software

We have used the ARM mbed platform for general code bring-up. We've used the following libraries:

  • mbed library by ARM
  • BLE_API library by the Bluetooth Low Energy team
  • X_NUCLEO_IDB0XA1 library by the ST Microelectronics team
  • X_NUCLEO_IKS01A1 library by the ST Microelectronics team

The acquired data on the client device is then sent to a cloud server that we hosted on ThingSpeak, so that people can remotely access the data. It is available at the GreenYourLife channel .

Reference

Revision:
2:326a19b95766
Parent:
1:b30300f95d4a
Child:
3:c460d60ffda6
--- a/main.cpp	Mon Nov 28 15:19:38 2016 +0000
+++ b/main.cpp	Tue Nov 29 18:16:38 2016 +0000
@@ -23,7 +23,7 @@
 
 
 /** Defines **/
-#define GB_SOIL_MOISTURE_MAX 100                                                // Soil moisture threshold value
+#define GB_SOIL_MOISTURE_MAX 70                                                 // Soil moisture threshold value
 
 
 
@@ -48,11 +48,21 @@
 float getHumidityValue();
 float getTemperatureValue();
 void errorLoop(void);
+void activateFastSensorPoll();
+void deactivateFastSensorPoll();
+void pumpActivateCallback(void);
+void pumpDeactivateCallback(void);
 
 Ticker sanityTicker;
 Ticker sensorPollTicker;
+Ticker fastSensorPollTicker;
+Timeout pumpWaitTimeout;
 bool sensorPolling;
-
+bool fastSensorPolling;
+bool pumpActivate;
+bool waitOnce;
+bool bleActive;
+bool pumpActive;
 
 
 /** Callbacks **/
@@ -61,12 +71,17 @@
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)    // Callback for everytime the connection gets disconnected
 {
     ble.gap().startAdvertising();                                               // Restart advertising
+    if(!pumpActive)
+        deactivateFastSensorPoll();
+    bleActive = false;
 //    printf("\r\n> BLE  : Disconnected. Advertising restarted.");
 }
 
 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)          // Callback for everytime the connection is established
 {
 //    ble.gap().stopAdvertising();                                                // Stop advertising
+    activateFastSensorPoll();
+    bleActive = true;
 //    printf("\r\n> BLE  : Connected to %x. Accept no subsequent connections.", params->peerAddr);
 }
 
@@ -146,6 +161,34 @@
     sensorPolling = true;
 }
 
+void fastSensorPollCallback(void)
+{
+    fastSensorPolling = true;
+}
+
+void pumpActivateCallback(void)
+{
+    pumpActivate = true;
+}
+
+void pumpDeactivateCallback(void)
+{
+    pumpActivate = false;
+}
+
+void activateFastSensorPoll(void)
+{
+    fastSensorPolling = true;
+    fastSensorPollTicker.attach(&fastSensorPollCallback, 1);
+}
+
+void deactivateFastSensorPoll(void)
+{
+    fastSensorPolling = false;
+    fastSensorPollTicker.detach();
+}
+
+
 void errorLoop(void)
 {
     sanityTicker.detach();
@@ -170,9 +213,14 @@
     printf("\r\n/**\r\n * Green Building Sensor Device: Debug Info\r\n */");
     
     sensorPolling = false;
+    fastSensorPolling = false;
+    pumpActivate = false;
+    waitOnce = true;
+    bleActive = false;
+    pumpActive = false;
     
     sanityTicker.attach(sanityCallback, 1.1);                                   // LED sanity checker
-    sensorPollTicker.attach(sensorPollCallback, 5);                             // Sensor poll ticker
+    sensorPollTicker.attach(sensorPollCallback, 4.9);                           // Sensor poll ticker
     
     printf("\r\n> MAIN : Tickers initialized.");
  
@@ -188,9 +236,10 @@
     // Infinite loop
     while (true) {
         
-        if(sensorPolling)
+        if(sensorPolling || fastSensorPolling)
         {
             sensorPolling = false;                                              // Deassert polling bit
+            fastSensorPolling = false;
             
             peVal.soilMoisture   = (uint8_t) getMoistureValue();                // Update all measurements
             peVal.airHumidity    = (uint8_t) getHumidityValue();
@@ -199,26 +248,48 @@
             if(ble.getGapState().connected)                                     // Update characteristic if connected
                 gbServicePtr->updatePlantEnvironment(peVal);
             
-            printf("\r\n> MAIN : Current soil moisture    = %d", peVal.soilMoisture);
-            printf("\r\n> MAIN : Current air humidity     = %d", peVal.airHumidity);
-            printf("\r\n> MAIN : Current air temperature  = %d", peVal.airTemperature);
-            printf("\r\n");
+//            printf("\r\n> MAIN : Current soil moisture    = %d", peVal.soilMoisture);
+//            printf("\r\n> MAIN : Current air humidity     = %d", peVal.airHumidity);
+//            printf("\r\n> MAIN : Current air temperature  = %d", peVal.airTemperature);
+            printf("%d\t%d\t%d\r\n", peVal.airTemperature, peVal.airHumidity, peVal.soilMoisture);
             
-            if (peVal.soilMoisture < 0.6*GB_SOIL_MOISTURE_MAX)                  // If moisture is below 60% of max
+            // If moisture is below 50% of max when user is present
+            //    or if less than 30% of max
+            if( ( ((peVal.soilMoisture < 0.5*GB_SOIL_MOISTURE_MAX) &&  ble.getGapState().connected)   ||
+                  ((peVal.soilMoisture < 0.3*GB_SOIL_MOISTURE_MAX) && !ble.getGapState().connected) ) &&
+                waitOnce
+            )
+            {
+                pumpWaitTimeout.attach(&pumpActivateCallback, 3);               // Waiting time is hard coded but may be calculated, I think
+                activateFastSensorPoll();
+                waitOnce = false;
+                pumpActive = true;
+            }
+            else if((peVal.soilMoisture >= 0.6*GB_SOIL_MOISTURE_MAX) && pumpActivate)
             {
-                printf("\r\n> MAIN : Activating water pump.");
-                pumpPWM.write(1);                                               // Activate pump
-                pumpCount = 0;
-                while((getMoistureValue()<0.75*GB_SOIL_MOISTURE_MAX)&&(pumpCount<40)) // Activate for 20s or until 75% max moisture
-                {
-                    wait(0.5);
-                    pumpCount++;
-                }
+                pumpWaitTimeout.detach();
+                pumpDeactivateCallback();
+                if(!bleActive)
+                    deactivateFastSensorPoll();
+                waitOnce = true;
+                pumpActive = false;
+            }
+            
+            if(pumpActivate)
+            {
+//                printf("\r\n> MAIN : Activating water pump.");
+                pumpPWM.write(0.75);
+                for(pumpCount=0; pumpCount<3;++pumpCount)
+                    wait(0.9);
                 pumpPWM.write(0);
+                
+                pumpActivate = false;
+                pumpWaitTimeout.attach(&pumpActivateCallback, 1);
             }
+            
         }
         else
-            ble.waitForEvent();                                                 // Low power wait for event   
+            ble.waitForEvent();                                                 //Low power wait for event   
         
     }
 }