Created
July 25, 2022 17:24
-
-
Save mussacharles60/cac2f87d3eee5ddc8bf273d299aa8c3b to your computer and use it in GitHub Desktop.
Smart hydroponic system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <elapsedMillis.h> | |
#include <SoftwareSerial.h> | |
#include <LiquidCrystal_I2C.h> | |
#include <Wire.h> | |
#include <afstandssensor.h> | |
SoftwareSerial Bluetooth(11, 10); | |
elapsedMillis send_timer; | |
elapsedMillis view_timer; | |
elapsedMillis pump_timer; | |
elapsedMillis light_timer; | |
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); | |
AfstandsSensor ultrasonicSensor(A1, A0); | |
int light_state = 0; | |
int pump_state = 0; | |
float humidity = 0; | |
float temperature = 0; | |
float water_level = 0; | |
float ph_reading = 0; | |
float growth = 0; | |
int display_switcher = 0; | |
#define ph_pin A3 | |
#define pump_pin A2 | |
#define light_pin 5 | |
#define Offset 0.00 //deviation compensate | |
#define samplingInterval 20 | |
#define printInterval 800 | |
#define ArrayLenth 40 //times of collection | |
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback | |
int pHArrayIndex = 0; | |
void setup() { | |
Serial.begin(9600); | |
Bluetooth.begin(9600); | |
Wire.begin(); | |
while (!Serial) {;;} | |
while (!Bluetooth) {;;} | |
// pinMode(ph_pin, INPUT); | |
pinMode(pump_pin, OUTPUT); | |
pinMode(light_pin, OUTPUT); | |
digitalWrite(pump_pin, 1); | |
digitalWrite(light_pin, 1); | |
lcd.begin(16, 2); | |
lcd.print("HYDROPONIC "); | |
lcd.setCursor(0, 1); | |
lcd.print(" SYSTEM"); | |
delay(3000); | |
lcd.clear(); | |
} | |
void loop() { | |
if (view_timer > 3000) { | |
view_timer = 0; | |
display_switcher++; | |
if (display_switcher > 7) { | |
display_switcher = 1; | |
} | |
} | |
if (send_timer > 3000) { | |
send_timer = 0; | |
sendData(); | |
} | |
if (pump_timer > (60000 * 2) && digitalRead(pump_pin) == 1) { | |
pump_timer = 0; | |
digitalWrite(pump_pin, 0); | |
} | |
else if (pump_timer > (60000 * 1) && digitalRead(pump_pin) == 0) { | |
pump_timer = 0; | |
digitalWrite(pump_pin, 1); | |
} | |
if (light_timer > (60000 * 5) ) { | |
light_timer = 0; | |
digitalWrite(light_pin, !digitalRead(light_pin)); | |
} | |
display(); | |
_light(); | |
_water(); | |
_temperature(); | |
_pump(); | |
_humidity(); | |
_ph(); | |
_growth(); | |
// if (Serial.available() > 0) { | |
// Bluetooth.write(Serial.read()); | |
// } | |
// if (Bluetooth.available() > 0) { | |
// Serial.write(Bluetooth.read()); | |
// } | |
// digitalWrite(pump_pin, 1); | |
// digitalWrite(light_pin, 1); | |
// delay(1000); | |
// digitalWrite(pump_pin, 0); | |
// digitalWrite(light_pin, 0); | |
// delay(1000); | |
} | |
void display() { | |
if (display_switcher == 1) { | |
lcd.setCursor(0, 0); | |
lcd.print("Light status: "); | |
lcd.setCursor(0, 1); | |
String state = light_state == 1 ? "on " : "off"; | |
lcd.print(" "); | |
lcd.print(state); | |
lcd.print(" "); | |
} | |
else if (display_switcher == 2) { | |
lcd.setCursor(0, 0); | |
lcd.print("Water level: "); | |
lcd.setCursor(0, 1); | |
String level = " " + String(water_level) + " cm "; | |
if (level.length() > 16) { | |
level = level.substring(0, 16); | |
} | |
lcd.print(level); | |
} | |
else if (display_switcher == 3) { | |
lcd.setCursor(0, 0); | |
lcd.print("Temperature: "); | |
lcd.setCursor(0, 1); | |
String temp = " " + String(temperature) + " *C "; | |
if (temp.length() > 16) { | |
temp = temp.substring(0, 16); | |
} | |
lcd.print(temp); | |
} | |
else if (display_switcher == 4) { | |
lcd.setCursor(0, 0); | |
lcd.print("Pump status: "); | |
lcd.setCursor(0, 1); | |
String state = light_state == 1 ? "on " : "off"; | |
lcd.print(" "); | |
lcd.print(state); | |
lcd.print(" "); | |
} | |
else if (display_switcher == 5) { | |
lcd.setCursor(0, 0); | |
lcd.print("Humidity: "); | |
lcd.setCursor(0, 1); | |
String humi = " " + String(humidity) + " % "; | |
if (humi.length() > 16) { | |
humi = humi.substring(0, 16); | |
} | |
lcd.print(humi); | |
} | |
else if (display_switcher == 6) { | |
lcd.setCursor(0, 0); | |
lcd.print("PH reading: "); | |
lcd.setCursor(0, 1); | |
String ph = " " + String(ph_reading) + " "; | |
if (ph.length() > 16) { | |
ph = ph.substring(0, 16); | |
} | |
lcd.print(ph); | |
} | |
else if (display_switcher == 7) { | |
lcd.setCursor(0, 0); | |
lcd.print("Growth rate: "); | |
lcd.setCursor(0, 1); | |
String rate = " " + String(growth) + " cm "; | |
if (rate.length() > 16) { | |
rate = rate.substring(0, 16); | |
} | |
lcd.print(rate); | |
} | |
} | |
void _light() { | |
light_state = digitalRead(light_pin); // 0 or 1 | |
} | |
void _water() { | |
water_level = 10; // cm | |
} | |
void _temperature() { | |
temperature = 24.56; // degrees Celsius | |
} | |
void _pump() { | |
pump_state = digitalRead(pump_pin); // 0 or 1 | |
} | |
void _humidity() { | |
humidity = 30.23; // % | |
} | |
void _ph() { | |
ph_reading = analogRead(ph_pin); | |
// // float Po = analogRead(ph_pin) * 5.0 / 1024; | |
// // float phValue = 7 - (2.5 - Po) * m; | |
// static unsigned long samplingTime = millis(); | |
// static unsigned long printTime = millis(); | |
// static float pHValue, voltage; | |
// if (millis() - samplingTime > samplingInterval) { | |
// pHArray[pHArrayIndex++] = analogRead(ph_pin); | |
// if (pHArrayIndex == ArrayLenth) pHArrayIndex = 0; | |
// voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024; | |
// pHValue = 3.5 * voltage + Offset; | |
// samplingTime = millis(); | |
// } | |
// if (millis() - printTime > printInterval) { //Every 800 milliseconds, print a numerical, convert the state of the LED indicator | |
// // Serial.print("Voltage:"); | |
// // Serial.print(voltage, 2); | |
// // Serial.print(" pH value: "); | |
// // Serial.println(pHValue, 2); | |
// ph_reading = pHValue; | |
// // if (ph_reading < 1 || ph_reading > 14) ph_reading = 7.00; | |
// // digitalWrite(LED, digitalRead(LED)^1); | |
// printTime = millis(); | |
// } | |
} | |
void _growth() { | |
growth = ultrasonicSensor.afstandCM(); // cm | |
if (growth < 0) growth = 0; | |
} | |
void sendData() { | |
// Serial.print("{"); | |
// Serial.print("\"data\":"); | |
// Serial.print("{"); | |
// Serial.print("\"light\":"); | |
// Serial.print(light_state); | |
// Serial.print(","); | |
// Serial.print("\"water\":"); | |
// Serial.print(water_level); | |
// Serial.print(","); | |
// Serial.print("\"temperature\":"); | |
// Serial.print(temperature); | |
// Serial.print(","); | |
// Serial.print("\"pump\":"); | |
// Serial.print(pump_state); | |
// Serial.print(","); | |
// Serial.print("\"humidity\":"); | |
// Serial.print(humidity); | |
// Serial.print(","); | |
// Serial.print("\"ph\":"); | |
// Serial.print(ph_reading); | |
// Serial.print(","); | |
// Serial.print("\"growth\":"); | |
// Serial.print(growth); | |
// Serial.print("}}"); | |
// Serial.println(); | |
Bluetooth.print("{"); | |
Bluetooth.print("\"data\":"); | |
Bluetooth.print("{"); | |
Bluetooth.print("\"light\":"); | |
Bluetooth.print(light_state); | |
Bluetooth.print(","); | |
Bluetooth.print("\"water\":"); | |
Bluetooth.print(water_level); | |
Bluetooth.print(","); | |
Bluetooth.print("\"temperature\":"); | |
Bluetooth.print(temperature); | |
Bluetooth.print(","); | |
Bluetooth.print("\"pump\":"); | |
Bluetooth.print(pump_state); | |
Bluetooth.print(","); | |
Bluetooth.print("\"humidity\":"); | |
Bluetooth.print(humidity); | |
Bluetooth.print(","); | |
Bluetooth.print("\"ph\":"); | |
Bluetooth.print(ph_reading); | |
Bluetooth.print(","); | |
Bluetooth.print("\"growth\":"); | |
Bluetooth.print(growth); | |
Bluetooth.print("}}"); | |
Bluetooth.println(); | |
// Bluetooth.flush(); | |
} | |
double avergearray(int* arr, int number) { | |
int i; | |
int max,min; | |
double avg; | |
long amount=0; | |
if (number <= 0) { | |
Serial.println("Error number for the array to avraging!/n"); | |
return 0; | |
} | |
if (number < 5) { //less than 5, calculated directly statistics | |
for(i=0; i < number; i++) { | |
amount += arr[i]; | |
} | |
avg = amount / number; | |
return avg; | |
} | |
else { | |
if (arr[0] < arr[1]) { | |
min = arr[0]; | |
max = arr[1]; | |
} | |
else { | |
min = arr[1]; | |
max = arr[0]; | |
} | |
for (i=2; i < number; i++) { | |
if (arr[i] < min) { | |
amount += min; //arr<min | |
min = arr[i]; | |
} else { | |
if (arr[i] > max) { | |
amount += max; //arr>max | |
max = arr[i]; | |
} else { | |
amount += arr[i]; //min<=arr<=max | |
} | |
} | |
} | |
avg = (double) amount / (number - 2); | |
} | |
return avg; | |
} | |
/********************************************* | |
* | |
* Please leve this section for future use | |
* | |
* Created By Mussa Charles | |
* | |
* @ 2021 | |
* | |
* https://www.facebook.com/mussacharles60 | |
* | |
* https://www.github.com/mussacharles60 | |
* | |
* https://www.instagram.com/mussacharles60 | |
* | |
* https://www.twitter.com/mussacharles60 | |
* | |
* https://www.youtube.com/mussacharles60 | |
* | |
* mussacharles60@gmail.com | |
* | |
* +255 716 581 823 | |
* | |
* --- let it happen :) --- | |
* | |
* Please leve this section for future use | |
* | |
********************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment