Components and supplies
plastic tube
Axial Fan, 12 VDC
Relay Module (Generic)
hinge
Jumper wires (generic)
Heater
SparkFun Snappable Protoboard
Arduino UNO
Greenhouse plastic
Soil moisture Sensor Module V1.2
DataLogger Module Data Recorder Shield
Bmp180
Wood
water pump
Tools and machines
Drill / Driver, Cordless
Welder
Project description
Code
Source Code
arduino
1 2#include <SFE_BMP180.h> 3#include <Wire.h> 4SFE_BMP180 pressure; 5#define ALTITUDE 88.0 // 6 7//relays 8#define pinRelayHeater 2 9#define pinRelayIrrigation 3 10#define pinRelayFan 4 11 12//analog humidity sensor 13#define pinHumidity A0 14 15// Values 16#define heaterThreshold 9 17#define fanThreshold 30 18#define humidityThreshold 30 19#define irrigationTime 2500 20byte HeaterSd = 0; 21byte fanSd = 0; 22byte pumpSd = 0; 23 24//sd 25#include <SPI.h> 26#include <SD.h> 27const int chipSelect = 10; 28//Millis 29unsigned long MillisSD = 0; 30unsigned long MillisIrrigation = 0; 31 32//RTC 33#include "RTClib.h" 34RTC_DS1307 rtc; 35char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 36 37 38 39//*************************void setup************************* 40 41void setup() 42{ 43 Serial.begin(57600); 44 Serial.println("REBOOT"); 45 pinMode(pinRelayHeater, OUTPUT); 46 pinMode(pinRelayIrrigation, OUTPUT); 47 pinMode(pinRelayFan, OUTPUT); 48 pinMode (pinHumidity, INPUT); 49 50 //temperature sensor 51 if (pressure.begin()) 52 Serial.println("Temperature sensor is working"); 53 else 54 { 55 // Temperature sensor failed, check connections 56 Serial.println("Temperature sensor failed\ 57"); 58 while (1); // Pause 59 } 60 61 //RTC 62 while (!Serial); // wait for serial port to connect. Needed for native USB 63 64 if (! rtc.begin()) { 65 Serial.println("RTC could not be found"); 66 Serial.flush(); 67 abort(); 68 } 69 70 if (! rtc.isrunning()) { 71 Serial.println("RTC off"); 72 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 73 } 74 75 //sd 76 if (!SD.begin(chipSelect)) { 77 Serial.println("Error SD"); 78 while (1); //Pause 79 } 80} 81 82//**************************void loop************************** 83 84void loop() 85{ 86 //**************Temperature************** 87 //*****Ambient sensor (temperature, pressure)***** 88 char status; 89 double T, P, p0; 90 91 status = pressure.startTemperature(); 92 if (status != 0) 93 { 94 delay(status); 95 96 status = pressure.getTemperature(T); 97 if (status != 0) 98 { 99 status = pressure.startPressure(3); 100 if (status != 0) 101 { 102 delay(status); 103 status = pressure.getPressure(P, T); 104 if (status != 0) 105 { 106 p0 = pressure.sealevel(P, ALTITUDE); 107 } 108 else Serial.println("error retrieving pressure measurement\ 109"); 110 } 111 else Serial.println("error starting pressure measurement\ 112"); 113 } 114 else Serial.println("error retrieving temperature measurement\ 115"); 116 } 117 else Serial.println("error starting temperature measurement\ 118"); 119 120 121 //*****Temperature control***** 122 //Heater 123 if (T <= heaterThreshold) { 124 digitalWrite(pinRelayHeater, HIGH); 125 HeaterSd = 1; 126 } 127 else if (T >= (heaterThreshold + 1)) { 128 digitalWrite(pinRelayHeater, LOW); 129 } 130 else { 131 HeaterSd = 1; 132 } 133 134 //Fan 135 if (T >= (fanThreshold - 1)) { 136 digitalWrite(pinRelayFan, HIGH); 137 fanSd = 1; 138 } 139 else if (T <= fanThreshold) { 140 digitalWrite(pinRelayFan, LOW); 141 } 142 else { 143 fanSd = 1; 144 } 145 146 147 //**************Humitat************** 148 int AnalogHumidity; 149 int HumidityPercentage; 150 151 AnalogHumidity = analogRead(pinHumidity); 152 HumidityPercentage = map(AnalogHumidity, 775, 326, 0, 100); // manually calibrated 153 154 Serial.print("Analog: "); 155 Serial.print(AnalogHumidity); 156 Serial.print(" Percentage: "); 157 Serial.println(HumidityPercentage); 158 159 //*****Irrigation system***** 160 161 if (((millis() - MillisIrrigation) >= 120000)) { 162 MillisIrrigation = millis(); 163 if (HumidityPercentage <= humidityThreshold) { 164 digitalWrite(pinRelayIrrigation, HIGH); 165 delay (irrigationTime); 166 digitalWrite(pinRelayIrrigation, LOW); 167 pumpSd = 1; 168 } 169 } 170 171 //**************RTC i SD************** 172 DateTime now = rtc.now(); 173 174 //sd 175 File dataFile = SD.open("datalog.txt", FILE_WRITE); 176 177 //Monitor serie 178 Serial.print(now.day(), DEC); 179 Serial.print('/'); 180 Serial.print(now.month(), DEC); 181 Serial.print('/'); 182 Serial.print(now.year(), DEC); 183 Serial.print(" ("); 184 Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); 185 Serial.print(") "); 186 Serial.print(now.hour(), DEC); 187 Serial.print(':'); 188 Serial.print(now.Minutee(), DEC); 189 Serial.print(':'); 190 Serial.println(now.second(), DEC); 191 Serial.print("temperature"); 192 Serial.println(T); 193 194 195 int Minute = now.Minutee(); 196 int Hour = now.hour(); 197 int Second = now.second(); 198 int Day = now.day(); 199 int Month = now.month(); 200 int Year = now.year(); 201 202 if (dataFile) { 203 if (((millis() - MillisSD) >= 60000)) { 204 // Day/Month/Year Hour:Minute:Second, temperature(C), Pressure(mb), Relative Pressure(mb), Humidity(%), heater, fan, water pump 205 206 MillisSD = millis(); 207 dataFile.print(Day); 208 dataFile.print("/"); 209 dataFile.print(Month); 210 dataFile.print("/"); 211 dataFile.print(Year); 212 dataFile.print(" "); 213 dataFile.print(Hour); 214 dataFile.print(":"); 215 dataFile.print(Minute); 216 dataFile.print(":"); 217 dataFile.print(Second); 218 dataFile.print(", "); 219 dataFile.print(T); 220 dataFile.print(", "); 221 dataFile.print(P); 222 dataFile.print(", "); 223 dataFile.print(p0); 224 dataFile.print(", "); 225 dataFile.print(HumidityPercentage); 226 227 dataFile.print(", "); 228 dataFile.print(HeaterSd); 229 dataFile.print(", "); 230 dataFile.print(fanSd); 231 dataFile.print(", "); 232 dataFile.println(pumpSd); 233 234 fanSd = 0; 235 HeaterSd = 0; 236 pumpSd = 0; 237 } 238 dataFile.close(); 239 } 240} 241
Comments
Only logged in users can leave comments