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