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:
- Air temperature
- Air humidity
- 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
main.cpp@5:b98f4f7cee5a, 2016-12-05 (annotated)
- Committer:
- kaiserhaz
- Date:
- Mon Dec 05 15:06:52 2016 +0000
- Revision:
- 5:b98f4f7cee5a
- Parent:
- 4:e5550110184d
Added licence statement to GreenBuildingService.h.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaiserhaz | 0:7dce5e74ad91 | 1 | /************************ STM32NUCLEO IOT Contest ****************************** |
kaiserhaz | 0:7dce5e74ad91 | 2 | * |
kaiserhaz | 0:7dce5e74ad91 | 3 | * Green Building IoT Solution for |
kaiserhaz | 0:7dce5e74ad91 | 4 | * Plant Life Monitoring And Maintenance |
kaiserhaz | 0:7dce5e74ad91 | 5 | * |
kaiserhaz | 0:7dce5e74ad91 | 6 | * Authored by |
kaiserhaz | 0:7dce5e74ad91 | 7 | * Dien Hoa Truong |
kaiserhaz | 0:7dce5e74ad91 | 8 | * Muhammad Haziq Bin Kamarul Azman |
kaiserhaz | 0:7dce5e74ad91 | 9 | * |
kaiserhaz | 0:7dce5e74ad91 | 10 | * for the |
kaiserhaz | 0:7dce5e74ad91 | 11 | * eSAME 2016 STM32NUCLEO IoT Contest in Sophia-Antipolis |
kaiserhaz | 0:7dce5e74ad91 | 12 | * |
kaiserhaz | 0:7dce5e74ad91 | 13 | * main.cpp | Program main |
kaiserhaz | 0:7dce5e74ad91 | 14 | * |
kaiserhaz | 4:e5550110184d | 15 | * See LICENCE.txt for information on copyrights |
kaiserhaz | 4:e5550110184d | 16 | * |
kaiserhaz | 0:7dce5e74ad91 | 17 | ******************************************************************************/ |
kaiserhaz | 0:7dce5e74ad91 | 18 | |
kaiserhaz | 0:7dce5e74ad91 | 19 | /** Includes **/ |
kaiserhaz | 0:7dce5e74ad91 | 20 | #include "mbed.h" // ARM mbed library |
kaiserhaz | 0:7dce5e74ad91 | 21 | #include "x_nucleo_iks01a1.h" // STM32NUCLEO board library |
kaiserhaz | 0:7dce5e74ad91 | 22 | #include "ble/BLE.h" // Bluetooth LE library |
kaiserhaz | 0:7dce5e74ad91 | 23 | #include "GreenBuildingService.h" // Green Building service library |
kaiserhaz | 0:7dce5e74ad91 | 24 | |
kaiserhaz | 0:7dce5e74ad91 | 25 | |
kaiserhaz | 0:7dce5e74ad91 | 26 | |
kaiserhaz | 0:7dce5e74ad91 | 27 | /** Defines **/ |
kaiserhaz | 2:326a19b95766 | 28 | #define GB_SOIL_MOISTURE_MAX 70 // Soil moisture threshold value |
kaiserhaz | 0:7dce5e74ad91 | 29 | |
kaiserhaz | 0:7dce5e74ad91 | 30 | |
kaiserhaz | 0:7dce5e74ad91 | 31 | |
kaiserhaz | 0:7dce5e74ad91 | 32 | /** Device declarations **/ |
kaiserhaz | 0:7dce5e74ad91 | 33 | |
kaiserhaz | 0:7dce5e74ad91 | 34 | // Board-specific |
kaiserhaz | 1:b30300f95d4a | 35 | PwmOut pumpPWM(PC_8); // PWM motor control out pin |
kaiserhaz | 0:7dce5e74ad91 | 36 | DigitalOut led1(LED1, 1); // Debug pin instance |
kaiserhaz | 1:b30300f95d4a | 37 | AnalogIn moisture_sensor(PB_1); // Moisture sensor |
kaiserhaz | 0:7dce5e74ad91 | 38 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15); // Expansion board instance |
kaiserhaz | 0:7dce5e74ad91 | 39 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; // Expansion board humidity sensor instance |
kaiserhaz | 0:7dce5e74ad91 | 40 | static TempSensor *temp_sensor = mems_expansion_board->ht_sensor; // Expansion board temperature sensor instance |
kaiserhaz | 0:7dce5e74ad91 | 41 | |
kaiserhaz | 0:7dce5e74ad91 | 42 | // BLE-specific |
kaiserhaz | 0:7dce5e74ad91 | 43 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); // BLE device instance |
kaiserhaz | 0:7dce5e74ad91 | 44 | const static char DEVICE_NAME[] = "GB-Sensor"; // Device name |
kaiserhaz | 0:7dce5e74ad91 | 45 | static const uint16_t uuid16_list[] = {GreenBuildingService::UUID_GREEN_BUILDING_SERVICE}; |
kaiserhaz | 0:7dce5e74ad91 | 46 | GreenBuildingService *gbServicePtr; // Service pointer |
kaiserhaz | 0:7dce5e74ad91 | 47 | |
kaiserhaz | 0:7dce5e74ad91 | 48 | // Program-specific |
kaiserhaz | 0:7dce5e74ad91 | 49 | float getMoistureValue(); |
kaiserhaz | 0:7dce5e74ad91 | 50 | float getHumidityValue(); |
kaiserhaz | 0:7dce5e74ad91 | 51 | float getTemperatureValue(); |
kaiserhaz | 0:7dce5e74ad91 | 52 | void errorLoop(void); |
kaiserhaz | 2:326a19b95766 | 53 | void activateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 54 | void deactivateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 55 | void pumpActivateCallback(void); |
kaiserhaz | 2:326a19b95766 | 56 | void pumpDeactivateCallback(void); |
kaiserhaz | 0:7dce5e74ad91 | 57 | |
kaiserhaz | 0:7dce5e74ad91 | 58 | Ticker sanityTicker; |
kaiserhaz | 0:7dce5e74ad91 | 59 | Ticker sensorPollTicker; |
kaiserhaz | 2:326a19b95766 | 60 | Ticker fastSensorPollTicker; |
kaiserhaz | 2:326a19b95766 | 61 | Timeout pumpWaitTimeout; |
kaiserhaz | 3:c460d60ffda6 | 62 | uint8_t usersConnected; |
kaiserhaz | 0:7dce5e74ad91 | 63 | bool sensorPolling; |
kaiserhaz | 2:326a19b95766 | 64 | bool fastSensorPolling; |
kaiserhaz | 2:326a19b95766 | 65 | bool pumpActivate; |
kaiserhaz | 2:326a19b95766 | 66 | bool waitOnce; |
kaiserhaz | 2:326a19b95766 | 67 | bool bleActive; |
kaiserhaz | 2:326a19b95766 | 68 | bool pumpActive; |
kaiserhaz | 0:7dce5e74ad91 | 69 | |
kaiserhaz | 0:7dce5e74ad91 | 70 | |
kaiserhaz | 0:7dce5e74ad91 | 71 | /** Callbacks **/ |
kaiserhaz | 0:7dce5e74ad91 | 72 | |
kaiserhaz | 0:7dce5e74ad91 | 73 | // BLE-specific callback |
kaiserhaz | 0:7dce5e74ad91 | 74 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) // Callback for everytime the connection gets disconnected |
kaiserhaz | 0:7dce5e74ad91 | 75 | { |
kaiserhaz | 0:7dce5e74ad91 | 76 | ble.gap().startAdvertising(); // Restart advertising |
kaiserhaz | 3:c460d60ffda6 | 77 | if((!pumpActive)||(!usersConnected)) |
kaiserhaz | 2:326a19b95766 | 78 | deactivateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 79 | bleActive = false; |
kaiserhaz | 3:c460d60ffda6 | 80 | --usersConnected; |
kaiserhaz | 0:7dce5e74ad91 | 81 | // printf("\r\n> BLE : Disconnected. Advertising restarted."); |
kaiserhaz | 0:7dce5e74ad91 | 82 | } |
kaiserhaz | 0:7dce5e74ad91 | 83 | |
kaiserhaz | 0:7dce5e74ad91 | 84 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) // Callback for everytime the connection is established |
kaiserhaz | 0:7dce5e74ad91 | 85 | { |
kaiserhaz | 3:c460d60ffda6 | 86 | ble.gap().stopAdvertising(); // Stop advertising |
kaiserhaz | 2:326a19b95766 | 87 | activateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 88 | bleActive = true; |
kaiserhaz | 3:c460d60ffda6 | 89 | ++usersConnected; |
kaiserhaz | 0:7dce5e74ad91 | 90 | // printf("\r\n> BLE : Connected to %x. Accept no subsequent connections.", params->peerAddr); |
kaiserhaz | 0:7dce5e74ad91 | 91 | } |
kaiserhaz | 0:7dce5e74ad91 | 92 | |
kaiserhaz | 0:7dce5e74ad91 | 93 | void onBleInitError(BLE &ble, ble_error_t error) |
kaiserhaz | 0:7dce5e74ad91 | 94 | { |
kaiserhaz | 0:7dce5e74ad91 | 95 | // printf("\r\n> BLE : Init error encountered. Error returned: %d", error); |
kaiserhaz | 0:7dce5e74ad91 | 96 | errorLoop(); |
kaiserhaz | 0:7dce5e74ad91 | 97 | } |
kaiserhaz | 0:7dce5e74ad91 | 98 | |
kaiserhaz | 0:7dce5e74ad91 | 99 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
kaiserhaz | 0:7dce5e74ad91 | 100 | { |
kaiserhaz | 0:7dce5e74ad91 | 101 | BLE& ble = params->ble; |
kaiserhaz | 0:7dce5e74ad91 | 102 | ble_error_t error = params->error; |
kaiserhaz | 0:7dce5e74ad91 | 103 | |
kaiserhaz | 0:7dce5e74ad91 | 104 | if (error != BLE_ERROR_NONE) { // Check to see init errors |
kaiserhaz | 0:7dce5e74ad91 | 105 | onBleInitError(ble, error); |
kaiserhaz | 0:7dce5e74ad91 | 106 | errorLoop(); |
kaiserhaz | 0:7dce5e74ad91 | 107 | } |
kaiserhaz | 0:7dce5e74ad91 | 108 | |
kaiserhaz | 0:7dce5e74ad91 | 109 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { // If this is not default instance (double instanciation?) |
kaiserhaz | 0:7dce5e74ad91 | 110 | // printf("\r\n> BLE : BLE controller instance is invalid."); |
kaiserhaz | 0:7dce5e74ad91 | 111 | errorLoop(); |
kaiserhaz | 0:7dce5e74ad91 | 112 | } |
kaiserhaz | 0:7dce5e74ad91 | 113 | |
kaiserhaz | 0:7dce5e74ad91 | 114 | ble.gap().onDisconnection(disconnectionCallback); // Register disconnection callback |
kaiserhaz | 0:7dce5e74ad91 | 115 | ble.gap().onConnection(connectionCallback); // Register connection callback |
kaiserhaz | 0:7dce5e74ad91 | 116 | |
kaiserhaz | 0:7dce5e74ad91 | 117 | gbServicePtr = new GreenBuildingService(ble); // Init service with initial value |
kaiserhaz | 0:7dce5e74ad91 | 118 | |
kaiserhaz | 0:7dce5e74ad91 | 119 | /* Setup advertising. */ |
kaiserhaz | 0:7dce5e74ad91 | 120 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
kaiserhaz | 0:7dce5e74ad91 | 121 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
kaiserhaz | 0:7dce5e74ad91 | 122 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,(uint8_t *)uuid16_list, sizeof(uuid16_list)); |
kaiserhaz | 0:7dce5e74ad91 | 123 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
kaiserhaz | 0:7dce5e74ad91 | 124 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
kaiserhaz | 0:7dce5e74ad91 | 125 | ble.gap().startAdvertising(); |
kaiserhaz | 0:7dce5e74ad91 | 126 | |
kaiserhaz | 0:7dce5e74ad91 | 127 | // printf("\r\n> BLE : BLE Init done."); |
kaiserhaz | 0:7dce5e74ad91 | 128 | } |
kaiserhaz | 0:7dce5e74ad91 | 129 | |
kaiserhaz | 0:7dce5e74ad91 | 130 | // Helper functions for retrieving data from sensors |
kaiserhaz | 0:7dce5e74ad91 | 131 | float getMoistureValue() |
kaiserhaz | 0:7dce5e74ad91 | 132 | { |
kaiserhaz | 0:7dce5e74ad91 | 133 | float moisture = 0; |
kaiserhaz | 0:7dce5e74ad91 | 134 | for (int i = 1;i<=10;i++) { |
kaiserhaz | 0:7dce5e74ad91 | 135 | moisture += moisture_sensor.read(); // Get ten samples |
kaiserhaz | 0:7dce5e74ad91 | 136 | } |
kaiserhaz | 0:7dce5e74ad91 | 137 | moisture = moisture / 10; |
kaiserhaz | 0:7dce5e74ad91 | 138 | moisture = moisture * 3300; // Change the value to be in the 0 to 3300 range |
kaiserhaz | 0:7dce5e74ad91 | 139 | moisture = moisture / 33; // Convert to percentage |
kaiserhaz | 0:7dce5e74ad91 | 140 | return moisture; |
kaiserhaz | 0:7dce5e74ad91 | 141 | } |
kaiserhaz | 0:7dce5e74ad91 | 142 | |
kaiserhaz | 0:7dce5e74ad91 | 143 | float getHumidityValue() |
kaiserhaz | 0:7dce5e74ad91 | 144 | { |
kaiserhaz | 0:7dce5e74ad91 | 145 | float humidity = 0; |
kaiserhaz | 0:7dce5e74ad91 | 146 | humidity_sensor->GetHumidity(&humidity); |
kaiserhaz | 0:7dce5e74ad91 | 147 | return humidity; |
kaiserhaz | 0:7dce5e74ad91 | 148 | } |
kaiserhaz | 0:7dce5e74ad91 | 149 | |
kaiserhaz | 0:7dce5e74ad91 | 150 | float getTemperatureValue() |
kaiserhaz | 0:7dce5e74ad91 | 151 | { |
kaiserhaz | 0:7dce5e74ad91 | 152 | float temperature = 0; |
kaiserhaz | 0:7dce5e74ad91 | 153 | temp_sensor->GetTemperature(&temperature); |
kaiserhaz | 0:7dce5e74ad91 | 154 | return temperature; |
kaiserhaz | 0:7dce5e74ad91 | 155 | } |
kaiserhaz | 0:7dce5e74ad91 | 156 | |
kaiserhaz | 0:7dce5e74ad91 | 157 | |
kaiserhaz | 0:7dce5e74ad91 | 158 | // Miscellaneous callbacks & functions |
kaiserhaz | 0:7dce5e74ad91 | 159 | void sanityCallback(void) |
kaiserhaz | 0:7dce5e74ad91 | 160 | { |
kaiserhaz | 0:7dce5e74ad91 | 161 | led1 = !led1; // Blink LED1 to indicate system sanity |
kaiserhaz | 0:7dce5e74ad91 | 162 | } |
kaiserhaz | 0:7dce5e74ad91 | 163 | |
kaiserhaz | 0:7dce5e74ad91 | 164 | void sensorPollCallback(void) |
kaiserhaz | 0:7dce5e74ad91 | 165 | { |
kaiserhaz | 0:7dce5e74ad91 | 166 | sensorPolling = true; |
kaiserhaz | 0:7dce5e74ad91 | 167 | } |
kaiserhaz | 0:7dce5e74ad91 | 168 | |
kaiserhaz | 2:326a19b95766 | 169 | void fastSensorPollCallback(void) |
kaiserhaz | 2:326a19b95766 | 170 | { |
kaiserhaz | 2:326a19b95766 | 171 | fastSensorPolling = true; |
kaiserhaz | 2:326a19b95766 | 172 | } |
kaiserhaz | 2:326a19b95766 | 173 | |
kaiserhaz | 2:326a19b95766 | 174 | void pumpActivateCallback(void) |
kaiserhaz | 2:326a19b95766 | 175 | { |
kaiserhaz | 2:326a19b95766 | 176 | pumpActivate = true; |
kaiserhaz | 2:326a19b95766 | 177 | } |
kaiserhaz | 2:326a19b95766 | 178 | |
kaiserhaz | 2:326a19b95766 | 179 | void pumpDeactivateCallback(void) |
kaiserhaz | 2:326a19b95766 | 180 | { |
kaiserhaz | 2:326a19b95766 | 181 | pumpActivate = false; |
kaiserhaz | 2:326a19b95766 | 182 | } |
kaiserhaz | 2:326a19b95766 | 183 | |
kaiserhaz | 2:326a19b95766 | 184 | void activateFastSensorPoll(void) |
kaiserhaz | 2:326a19b95766 | 185 | { |
kaiserhaz | 2:326a19b95766 | 186 | fastSensorPolling = true; |
kaiserhaz | 3:c460d60ffda6 | 187 | fastSensorPollTicker.attach(&fastSensorPollCallback, 0.9); |
kaiserhaz | 2:326a19b95766 | 188 | } |
kaiserhaz | 2:326a19b95766 | 189 | |
kaiserhaz | 2:326a19b95766 | 190 | void deactivateFastSensorPoll(void) |
kaiserhaz | 2:326a19b95766 | 191 | { |
kaiserhaz | 2:326a19b95766 | 192 | fastSensorPolling = false; |
kaiserhaz | 2:326a19b95766 | 193 | fastSensorPollTicker.detach(); |
kaiserhaz | 2:326a19b95766 | 194 | } |
kaiserhaz | 2:326a19b95766 | 195 | |
kaiserhaz | 2:326a19b95766 | 196 | |
kaiserhaz | 0:7dce5e74ad91 | 197 | void errorLoop(void) |
kaiserhaz | 0:7dce5e74ad91 | 198 | { |
kaiserhaz | 0:7dce5e74ad91 | 199 | sanityTicker.detach(); |
kaiserhaz | 0:7dce5e74ad91 | 200 | sensorPollTicker.detach(); |
kaiserhaz | 0:7dce5e74ad91 | 201 | ble.shutdown(); |
kaiserhaz | 1:b30300f95d4a | 202 | // printf("\r\n> ERROR : Error encountered. Infinite looping."); |
kaiserhaz | 0:7dce5e74ad91 | 203 | while(true) |
kaiserhaz | 0:7dce5e74ad91 | 204 | { |
kaiserhaz | 0:7dce5e74ad91 | 205 | led1 != led1; |
kaiserhaz | 0:7dce5e74ad91 | 206 | } |
kaiserhaz | 0:7dce5e74ad91 | 207 | } |
kaiserhaz | 0:7dce5e74ad91 | 208 | |
kaiserhaz | 0:7dce5e74ad91 | 209 | |
kaiserhaz | 0:7dce5e74ad91 | 210 | |
kaiserhaz | 0:7dce5e74ad91 | 211 | /** Pre-main inits **/ |
kaiserhaz | 0:7dce5e74ad91 | 212 | |
kaiserhaz | 0:7dce5e74ad91 | 213 | |
kaiserhaz | 0:7dce5e74ad91 | 214 | |
kaiserhaz | 0:7dce5e74ad91 | 215 | /** Main loop **/ |
kaiserhaz | 0:7dce5e74ad91 | 216 | int main(void) |
kaiserhaz | 0:7dce5e74ad91 | 217 | { |
kaiserhaz | 3:c460d60ffda6 | 218 | pumpPWM.write(1); |
kaiserhaz | 3:c460d60ffda6 | 219 | pumpPWM.period(1.0f); |
kaiserhaz | 3:c460d60ffda6 | 220 | |
kaiserhaz | 0:7dce5e74ad91 | 221 | printf("\r\n/**\r\n * Green Building Sensor Device: Debug Info\r\n */"); |
kaiserhaz | 0:7dce5e74ad91 | 222 | |
kaiserhaz | 0:7dce5e74ad91 | 223 | sensorPolling = false; |
kaiserhaz | 2:326a19b95766 | 224 | fastSensorPolling = false; |
kaiserhaz | 2:326a19b95766 | 225 | pumpActivate = false; |
kaiserhaz | 2:326a19b95766 | 226 | waitOnce = true; |
kaiserhaz | 2:326a19b95766 | 227 | bleActive = false; |
kaiserhaz | 2:326a19b95766 | 228 | pumpActive = false; |
kaiserhaz | 0:7dce5e74ad91 | 229 | |
kaiserhaz | 0:7dce5e74ad91 | 230 | sanityTicker.attach(sanityCallback, 1.1); // LED sanity checker |
kaiserhaz | 2:326a19b95766 | 231 | sensorPollTicker.attach(sensorPollCallback, 4.9); // Sensor poll ticker |
kaiserhaz | 0:7dce5e74ad91 | 232 | |
kaiserhaz | 0:7dce5e74ad91 | 233 | printf("\r\n> MAIN : Tickers initialized."); |
kaiserhaz | 0:7dce5e74ad91 | 234 | |
kaiserhaz | 0:7dce5e74ad91 | 235 | volatile GreenBuildingService::PlantEnvironmentType_t peVal; // Plant environment var |
kaiserhaz | 3:c460d60ffda6 | 236 | uint8_t pumpWaitTime = 3; // Pump waiting time |
kaiserhaz | 0:7dce5e74ad91 | 237 | |
kaiserhaz | 0:7dce5e74ad91 | 238 | ble.init(bleInitComplete); // Pass BLE init complete function upon init |
kaiserhaz | 0:7dce5e74ad91 | 239 | |
kaiserhaz | 1:b30300f95d4a | 240 | // while(ble.hasInitialized() == false); |
kaiserhaz | 0:7dce5e74ad91 | 241 | |
kaiserhaz | 0:7dce5e74ad91 | 242 | printf("\r\n> MAIN : BLE Init procedure done."); |
kaiserhaz | 0:7dce5e74ad91 | 243 | |
kaiserhaz | 0:7dce5e74ad91 | 244 | // Infinite loop |
kaiserhaz | 0:7dce5e74ad91 | 245 | while (true) { |
kaiserhaz | 0:7dce5e74ad91 | 246 | |
kaiserhaz | 2:326a19b95766 | 247 | if(sensorPolling || fastSensorPolling) |
kaiserhaz | 0:7dce5e74ad91 | 248 | { |
kaiserhaz | 0:7dce5e74ad91 | 249 | sensorPolling = false; // Deassert polling bit |
kaiserhaz | 2:326a19b95766 | 250 | fastSensorPolling = false; |
kaiserhaz | 0:7dce5e74ad91 | 251 | |
kaiserhaz | 0:7dce5e74ad91 | 252 | peVal.soilMoisture = (uint8_t) getMoistureValue(); // Update all measurements |
kaiserhaz | 0:7dce5e74ad91 | 253 | peVal.airHumidity = (uint8_t) getHumidityValue(); |
kaiserhaz | 0:7dce5e74ad91 | 254 | peVal.airTemperature = (int8_t) getTemperatureValue(); |
kaiserhaz | 0:7dce5e74ad91 | 255 | |
kaiserhaz | 0:7dce5e74ad91 | 256 | if(ble.getGapState().connected) // Update characteristic if connected |
kaiserhaz | 0:7dce5e74ad91 | 257 | gbServicePtr->updatePlantEnvironment(peVal); |
kaiserhaz | 0:7dce5e74ad91 | 258 | |
kaiserhaz | 2:326a19b95766 | 259 | // printf("\r\n> MAIN : Current soil moisture = %d", peVal.soilMoisture); |
kaiserhaz | 2:326a19b95766 | 260 | // printf("\r\n> MAIN : Current air humidity = %d", peVal.airHumidity); |
kaiserhaz | 2:326a19b95766 | 261 | // printf("\r\n> MAIN : Current air temperature = %d", peVal.airTemperature); |
kaiserhaz | 2:326a19b95766 | 262 | printf("%d\t%d\t%d\r\n", peVal.airTemperature, peVal.airHumidity, peVal.soilMoisture); |
kaiserhaz | 0:7dce5e74ad91 | 263 | |
kaiserhaz | 2:326a19b95766 | 264 | // If moisture is below 50% of max when user is present |
kaiserhaz | 2:326a19b95766 | 265 | // or if less than 30% of max |
kaiserhaz | 2:326a19b95766 | 266 | if( ( ((peVal.soilMoisture < 0.5*GB_SOIL_MOISTURE_MAX) && ble.getGapState().connected) || |
kaiserhaz | 2:326a19b95766 | 267 | ((peVal.soilMoisture < 0.3*GB_SOIL_MOISTURE_MAX) && !ble.getGapState().connected) ) && |
kaiserhaz | 2:326a19b95766 | 268 | waitOnce |
kaiserhaz | 2:326a19b95766 | 269 | ) |
kaiserhaz | 2:326a19b95766 | 270 | { |
kaiserhaz | 3:c460d60ffda6 | 271 | pumpWaitTimeout.attach(&pumpActivateCallback, pumpWaitTime); // Waiting time is hard coded but may be calculated, I think |
kaiserhaz | 2:326a19b95766 | 272 | activateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 273 | waitOnce = false; |
kaiserhaz | 2:326a19b95766 | 274 | pumpActive = true; |
kaiserhaz | 2:326a19b95766 | 275 | } |
kaiserhaz | 3:c460d60ffda6 | 276 | else if((peVal.soilMoisture >= 0.6*GB_SOIL_MOISTURE_MAX) && pumpActivate) // Stop condition: when soil moisture is at 60% of max |
kaiserhaz | 0:7dce5e74ad91 | 277 | { |
kaiserhaz | 3:c460d60ffda6 | 278 | pumpPWM.write(1); |
kaiserhaz | 2:326a19b95766 | 279 | pumpWaitTimeout.detach(); |
kaiserhaz | 2:326a19b95766 | 280 | pumpDeactivateCallback(); |
kaiserhaz | 2:326a19b95766 | 281 | if(!bleActive) |
kaiserhaz | 2:326a19b95766 | 282 | deactivateFastSensorPoll(); |
kaiserhaz | 2:326a19b95766 | 283 | waitOnce = true; |
kaiserhaz | 2:326a19b95766 | 284 | pumpActive = false; |
kaiserhaz | 2:326a19b95766 | 285 | } |
kaiserhaz | 2:326a19b95766 | 286 | |
kaiserhaz | 2:326a19b95766 | 287 | if(pumpActivate) |
kaiserhaz | 2:326a19b95766 | 288 | { |
kaiserhaz | 2:326a19b95766 | 289 | // printf("\r\n> MAIN : Activating water pump."); |
kaiserhaz | 3:c460d60ffda6 | 290 | pumpPWM.write(0.7); |
kaiserhaz | 2:326a19b95766 | 291 | pumpActivate = false; |
kaiserhaz | 2:326a19b95766 | 292 | pumpWaitTimeout.attach(&pumpActivateCallback, 1); |
kaiserhaz | 0:7dce5e74ad91 | 293 | } |
kaiserhaz | 2:326a19b95766 | 294 | |
kaiserhaz | 0:7dce5e74ad91 | 295 | } |
kaiserhaz | 0:7dce5e74ad91 | 296 | else |
kaiserhaz | 2:326a19b95766 | 297 | ble.waitForEvent(); //Low power wait for event |
kaiserhaz | 0:7dce5e74ad91 | 298 | |
kaiserhaz | 0:7dce5e74ad91 | 299 | } |
kaiserhaz | 0:7dce5e74ad91 | 300 | } |