Commit da66105c authored by ayodyabanuka's avatar ayodyabanuka

Updated Arduino Code

parent 844069e5
......@@ -7,8 +7,8 @@
// Set these to run example.
#define FIREBASE_HOST "ifarm-4ec55-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "0XoTKCHRo5iB63DShpRfq5Q545GQfDlTcBxQrEkP"
#define WIFI_SSID "Dialog 4G CB3"
#define WIFI_PASSWORD "BMy7M977"
#define WIFI_SSID "abfiber"
#define WIFI_PASSWORD "Ayobanuka@1998"
#define LED1 D1
#define LED2 D2
#define TRIGGER_PIN D7
......@@ -21,13 +21,13 @@ NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Declare the Firebase Data object in the global scope
FirebaseData firebaseData;
FirebaseData firebaseDataMoist, firebaseDataLevel, firebaseDataServo, firebaseDataError;
// Declare global variable to store value
int val = 0;
Servo servo;
const int AirValue = 1024.0; //you need to replace this value with Value_1
const int WaterValue = 950.0; //you need to replace this value with Value_2
const int AirValue = 1024.0;
const int WaterValue = 950.0;
float foodMoistureValue = 0.0;
float foodmoisturepercent = 0.0;
float foodpercent = 0.0;
......@@ -42,11 +42,11 @@ void setup() {
pinMode(LED1, OUTPUT);
digitalWrite(LED1, HIGH);
pinMode(LED2, OUTPUT);
digitalWrite(LED2, LOW);
// connect to wifi.
servo.attach(2); //D4
servo.write(90);
digitalWrite(LED2, LOW);
servo.attach(2); //D4
servo.write(0);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
......@@ -60,55 +60,70 @@ digitalWrite(LED2, LOW);
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.setInt(firebaseData, "/Food/"+productnumber+"/SERVO", 0);
Firebase.setInt(firebaseDataServo, "/Food/" + productnumber + "/SERVO", 0);
Firebase.setInt(firebaseDataError, "/Food/" + productnumber + "/ERROR", 2);
Serial.println("Sensor Data\n");
}
int s = 0;
int e = 1;
void loop() {
float foodlimit;
float foodlimit;
float dis = sonar.ping_cm();
if(dis == 9 ){
float dis = sonar.ping_cm();
if (dis == 9 ) {
foodlimit = 0;
}else if(dis <= 1){
} else if (dis <= 1) {
foodlimit = 100;
}
else if(dis > 1 || dis < 9){
foodlimit = round(((8 - dis )/8)*100);
else if (dis > 1 || dis < 9) {
foodlimit = round(((9 - dis ) / 9) * 100);
}
Firebase.setFloat(firebaseDataLevel, "/Food/" + productnumber + "/dis", foodlimit);
Serial.print(" Soil Moisture: "); Serial.print(foodpercent); Serial.print("%");
Firebase.getInt(firebaseDataError, "/Food/" + productnumber + "/ERROR");
e = firebaseDataError.intData();
Serial.print(" ERROR! "); Serial.println(e);
if (e == 1) {
digitalWrite(LED1, HIGH);
}
else if (e == 2) {
digitalWrite(LED1, LOW);
}
Firebase.setFloat(firebaseData, "/Food/"+productnumber+"/dis",foodlimit);
foodMoistureValue = analogRead(A0);
foodmoisturepercent = map(foodMoistureValue, AirValue, WaterValue, 0, 100);
if(foodmoisturepercent >= 100.0){
if (foodmoisturepercent >= 100.0) {
foodpercent = 100.0;
}else if(foodmoisturepercent <= 0.0){
} else if (foodmoisturepercent <= 0.0) {
foodpercent = 0.0;
}else if (foodmoisturepercent >0.0 && foodmoisturepercent < 100.0){
} else if (foodmoisturepercent > 0.0 && foodmoisturepercent < 100.0) {
foodpercent = foodmoisturepercent;
}
Serial.print(" Soil Moisture: "); Serial.print(foodpercent); Serial.print("%");
Firebase.setFloat(firebaseData, "/Food/"+productnumber+"/Moist/foodMoist", foodpercent);
if (Firebase.getInt(firebaseData, "/Food/"+productnumber+"/SERVO")) {
s = firebaseData.intData();
Serial.print(" Servo: "); Serial.println(s);
if (foodpercent == 0) {
Firebase.setInt(firebaseDataMoist, "/Food/" + productnumber + "/ERROR", 2);
} else {
Firebase.setInt(firebaseDataMoist, "/Food/" + productnumber + "/ERROR", 1);
}
Firebase.setFloat(firebaseDataMoist, "/Food/" + productnumber + "/Moist/foodMoist", foodpercent);
Firebase.getInt(firebaseDataServo, "/Food/" + productnumber + "/SERVO");
s = firebaseDataServo.intData();
Serial.print(" Servo: "); Serial.println(s);
if (s == 0) {
servo.write(0); // tell servo to go to position in variable 'pos'
delay(2000); return;
}
else {
servo.write(180);
delay(2000);
return;
}
delay(1000);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment