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.
Diff: main.cpp
- Revision:
- 3:c460d60ffda6
- Parent:
- 2:326a19b95766
- Child:
- 4:e5550110184d
- Child:
- 5:9d68ed883e95
--- a/main.cpp Tue Nov 29 18:16:38 2016 +0000 +++ b/main.cpp Thu Dec 01 08:28:55 2016 +0000 @@ -57,6 +57,7 @@ Ticker sensorPollTicker; Ticker fastSensorPollTicker; Timeout pumpWaitTimeout; +uint8_t usersConnected; bool sensorPolling; bool fastSensorPolling; bool pumpActivate; @@ -71,17 +72,19 @@ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) // Callback for everytime the connection gets disconnected { ble.gap().startAdvertising(); // Restart advertising - if(!pumpActive) + if((!pumpActive)||(!usersConnected)) deactivateFastSensorPoll(); bleActive = false; + --usersConnected; // 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 + ble.gap().stopAdvertising(); // Stop advertising activateFastSensorPoll(); bleActive = true; + ++usersConnected; // printf("\r\n> BLE : Connected to %x. Accept no subsequent connections.", params->peerAddr); } @@ -179,7 +182,7 @@ void activateFastSensorPoll(void) { fastSensorPolling = true; - fastSensorPollTicker.attach(&fastSensorPollCallback, 1); + fastSensorPollTicker.attach(&fastSensorPollCallback, 0.9); } void deactivateFastSensorPoll(void) @@ -210,6 +213,9 @@ /** Main loop **/ int main(void) { + pumpPWM.write(1); + pumpPWM.period(1.0f); + printf("\r\n/**\r\n * Green Building Sensor Device: Debug Info\r\n */"); sensorPolling = false; @@ -225,7 +231,7 @@ printf("\r\n> MAIN : Tickers initialized."); volatile GreenBuildingService::PlantEnvironmentType_t peVal; // Plant environment var - uint8_t pumpCount = 0; // Pump timer + uint8_t pumpWaitTime = 3; // Pump waiting time ble.init(bleInitComplete); // Pass BLE init complete function upon init @@ -260,13 +266,14 @@ waitOnce ) { - pumpWaitTimeout.attach(&pumpActivateCallback, 3); // Waiting time is hard coded but may be calculated, I think + pumpWaitTimeout.attach(&pumpActivateCallback, pumpWaitTime); // 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) + else if((peVal.soilMoisture >= 0.6*GB_SOIL_MOISTURE_MAX) && pumpActivate) // Stop condition: when soil moisture is at 60% of max { + pumpPWM.write(1); pumpWaitTimeout.detach(); pumpDeactivateCallback(); if(!bleActive) @@ -278,11 +285,7 @@ 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); - + pumpPWM.write(0.7); pumpActivate = false; pumpWaitTimeout.attach(&pumpActivateCallback, 1); }