This video shows the plants at 3 different times of a 5 day period. Based on the first three days, a change was made to the code and hardware because the soil dried out and the plants started to wilt. The very next day the plants had bounced back. After a full week, not shown in video, the plants have actually grown and are thriving in the environment. There is still room for improvement, but the concept has been proven. There is still a long way to go to reach my initial goal and I will continue to work on the project and update it throughout the summer.
// Program: element14 Design for a Cause 2021 garden controller
// Programmer: Tony Briceño - Mitchell Community College
// Date: April 25th, 2019
// This project code is in the public domain.
#include <math.h>
#include <ArduinoBLE.h>
int SoilRead = A0;
int TempRead = A1;
int H2ORead = 4;
int PumpSwitch = 2;
static unsigned long soil = 0;
static unsigned long tempDegree = 0;
float temp = 0;
bool h2o = 0;
bool pumpStatus = false;
String message;
BLEService stringService("1f14be9c-bdbc-11eb-8529-0242ac130003"); // BLE String Service
BLECharacteristic soilValue("1f14bb72-bdbc-11eb-8529-0242ac130003", BLERead, 1023);
BLEFloatCharacteristic tempValue("1f14c202-bdbc-11eb-8529-0242ac130003", BLERead);
BLECharacteristic tempSettingDegree("1f14c3d8-bdbc-11eb-8529-0242ac130003", BLERead, 0);
BLECharacteristic h2oValue("1f14bfb4-bdbc-11eb-8529-0242ac130003", BLERead, "Low");
BLEByteCharacteristic pumpState("1f14c0f4-bdbc-11eb-8529-0242ac130003", BLERead | BLEWrite | BLENotify);
void setup()
{
pinMode(2, OUTPUT);
pinMode(4, INPUT);
pinMode(15, INPUT);
pinMode(20, OUTPUT);
// Include serial printing for debugging
Serial.begin(9600);
Serial.println("Garden Controller v_1.0");
Serial.println("***********************");
// initialize the BLE hardware
if (!BLE.begin()) {
Serial.println("BLE initialation failed");
Serial.println("***********************");
}
if (BLE.begin()) {
BLE.setLocalName("Garden_controller");
BLE.setAdvertisedService(stringService);
Serial.println("BLE initialation complete");
Serial.println(BLE.address());
Serial.println("***********************");
}
// add the characteristic to the service
stringService.addCharacteristic(soilValue);
stringService.addCharacteristic(h2oValue);
stringService.addCharacteristic(pumpState);
stringService.addCharacteristic(tempValue);
stringService.addCharacteristic(tempSettingDegree);
// add service
BLE.addService(stringService);
// Start advertising
BLE.advertise();
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
}
void loop() {
// poll for BLE events
BLE.poll();
while (BLE.connected())
{
// Make the first analog read
soil = analogRead(SoilRead);
delay(100); //Include a slight delay to make sure ADC gets a full read
h2o = digitalRead(H2ORead);
delay(100);
temp = analogRead(TempRead);
delay(100);
if(soil >600)
{
// Pump control
digitalWrite(PumpSwitch, HIGH); //turn off pump
pumpState.writeValue(0);
pumpStatus = false;
}
else
{
if(h2o == HIGH){
digitalWrite(PumpSwitch,LOW ); //turn on pump
pumpState.writeValue(1);
pumpStatus = true;
h2oValue.writeValue("OK");
}
else if(h2o == LOW){
digitalWrite(PumpSwitch, HIGH); //turn off pump
pumpState.writeValue(0);
pumpStatus = false;
h2oValue.writeValue("Low");
}
}
if (tempSettingDegree.written())
{
if (tempSettingDegree.value())
{
tempDegree = 1;
}
else{
tempDegree = 0;
}
}
temp = tempcalc(temp, tempDegree);
soilValue.writeValue(soil);
tempValue.writeValue(temp);
if (pumpState.written())
{
if (pumpState.value() == 1){
Serial.println("ON");
}
else {
Serial.println("OFF");
}
Serial.println("Received");
}
//Value check for debug
Serial.println("^_^_^_^_^_^_^_^");
Serial.print("soil - ");
Serial.println(soil);
Serial.print("h2o - ");
Serial.println(h2o);
Serial.print("pump - ");
if(pumpStatus == true)
{
Serial.println("On");
}
else
{
Serial.println("Off");
}
Serial.print("temp - ");
Serial.println(temp);
delay(1000); //Add a delay so the serial monitor is not too fast
}
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
float tempcalc(float temp, long tempDegree){
//Calculations to covert temperature from a voltage reading to a degrees F/C reading
float calctemp = 0;
calctemp = (3.279/1023); //volts per ADC digit
calctemp = (calctemp * temp); //voltage of analog read
calctemp = (calctemp - 0.6); //voltage offset for sensor range
calctemp = (calctemp/0.01); //convert voltage to degrees C, 10mV per degree C
if (tempDegree == 0)
{
return calctemp;
}
else
{
calctemp = ((calctemp*1.8)+32);
return calctemp;
}
}