Automatic Tropical Terrarium
This project is my journey of developing an automated terrarium for tropical plants.
Components and supplies
Adafruit Data Logger Shield
RGB LCD Shield Kit, 16x2 Character Display
Terrarium Moisting System
Axial Fan, 12 V
Arduino UNO
Relay (generic)
DHT22 temperature-humidity sensor
Project description
Code
terra_log_1.ino
arduino
This is the beginning of coding for the terrarium. For now, the code is only supposed to read the temperature in the terrarium, compare it to the threshold and turn on a fan when the temp is above the threshold. However, for now the fan is not connected to a power supply. The only purpose is to log data about temperature and humidity to the SD card so I can learn about the climate in the terrarium first. Critical termperatures or humdities are not reached.
1#include <DHT.h> // include library 2#include <SPI.h> 3#include <SD.h> 4#include "RTClib.h" 5 6const int chipSelect = 10; 7const int relayPin1 = 8; 8const int DHTPIN = 7; 9 10#define DHTTYPE DHT22 11DHT dht(DHTPIN, DHTTYPE);// Initialize DHT sensor 12RTC_DS1307 rtc; 13 14File myFile; 15char dateBuffer[12]; 16/********************************/ 17void setup() { 18 // Open serial communications and wait for port to open: 19 Serial.begin(9600); 20 while (!Serial) { 21 ; // wait for serial port to connect. Needed for native USB port only 22 } 23 if (! rtc.begin()) { 24 Serial.println("No RTC"); //Could not fint RTC 25 Serial.flush(); 26 abort(); 27 } 28 if (! rtc.isrunning()) { 29 Serial.println("RTC off, set the time"); 30 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 31 } 32 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 33 34 35 Serial.print("Initializing SD card..."); 36 if (!SD.begin(chipSelect)) { 37 Serial.println("initialization failed!"); 38 while (1); 39 } 40 Serial.println("initialization done."); 41 if (SD.exists("terralog.txt")) { 42 Serial.println("terralog.txt exists."); 43 } else { 44 Serial.println("terralog.txt doesn't exist."); 45 Serial.println("Creating terraaaaa.txt"); 46 myFile = SD.open("terralog.txt", FILE_WRITE); 47 myFile.close(); 48 if (SD.exists("terralog.txt")) { 49 Serial.println("terralog.txt was successfully created."); 50 } else { 51 Serial.println("File creation failed."); 52 } 53 } 54 55 pinMode(relayPin1, OUTPUT); 56 dht.begin(); 57 58 Serial.println("date,time,temperature,humidity,fanState"); 59} 60 61void loop() 62{ 63 64 float h = dht.readHumidity(); 65 float t = dht.readTemperature(); 66 // Check if any reads failed and exit early (to try again). 67 if (isnan(h) || isnan(t)) { 68 Serial.println("Failed to read from DHT sensor!"); 69 return; 70 } 71 72 DateTime time = rtc.now(); 73 74 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 75 Serial.print(dateBuffer); 76 Serial.print(","); 77 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 78 Serial.print(dateBuffer); 79 Serial.print(","); 80 Serial.print(t); 81 Serial.print(","); 82 Serial.print(h); 83 Serial.print(","); 84 85 File myFile = SD.open("terralog.txt", FILE_WRITE); 86 87 if (myFile) { 88 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 89 myFile.print(dateBuffer); 90 myFile.print(","); 91 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 92 myFile.print(dateBuffer); 93 myFile.print(","); 94 myFile.print(t); 95 myFile.print(","); 96 myFile.print(h); 97 myFile.print(","); 98 myFile.close(); 99 } 100 else { 101 Serial.println("error opening terralog.txt"); 102 } 103 if (t >= 27) { 104 digitalWrite(relayPin1, HIGH); 105 Serial.println("on"); 106 File myFile = SD.open("terralog.txt", FILE_WRITE); 107 myFile.println("on"); 108 myFile.close(); 109 } 110 else { 111 digitalWrite(relayPin1, LOW); 112 Serial.println("off"); 113 File myFile = SD.open("terralog.txt", FILE_WRITE); 114 myFile.println("off"); 115 myFile.close(); 116 } 117 118 delay(30000); 119 120} 121
terra_log_fan_and_moisting_2
abap
This is a further developed code than the previous one. I remade everything in a way I think is more optimized and more functional.
1#include <DHT.h> 2#include <SPI.h> 3#include <SD.h> 4#include <Wire.h> 5#include <LiquidCrystal_I2C.h> 6#include "RTClib.h" 7 8//const int lightRelay = 12; 9const int rainRelay = 11; 10const int fanRelay = 8; 11const int chipSelect = 10; 12const int DHTPIN_1 = 5; 13const int DHTPIN_2 = 6; 14 15int fanState = 0; 16int rainState = 0; 17 18int maxTemp = 29; 19int normTemp = 25; 20 21int minHum = 96; 22 23int i = 0; 24 25LiquidCrystal_I2C lcd(0x27, 16, 2); 26 27#define DHTTYPE DHT22 28DHT dht_1(DHTPIN_1, DHTTYPE); 29DHT dht_2(DHTPIN_2, DHTTYPE); 30RTC_DS1307 rtc; 31 32File myFile; 33char dateBuffer[12]; 34char sensBuffer[10]; 35 36void setup() { 37 Serial.begin(9600); 38 while (!Serial) { 39 ; 40 } 41 42 lcd.init(); 43 lcd.backlight(); 44 lcd.setCursor(0, 0); 45 46 if (! rtc.begin()) { 47 error("No RTC"); 48 Serial.flush(); 49 abort(); 50 } 51 52 if (! rtc.isrunning()) { 53 error("RTC was off. Setting new time."); 54 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 55 } 56 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 57 Serial.print(F("Initializing SD card...")); 58 if (!SD.begin(chipSelect)) { 59 error("SD initialization failed"); 60 while (1); 61 } 62 63 Serial.println(F("initialization done.")); 64 if (SD.exists(F("terralog.txt"))) { 65 Serial.println(F("terralog.txt exists.")); 66 } else { 67 Serial.println(F("terralog.txt doesn't exist.")); 68 Serial.println(F("Creating terraaaaa.txt")); 69 myFile = SD.open("terralog.txt", FILE_WRITE); 70 myFile.println("date,time,temp_1,temp_2,hum_1,hum_2,fanState, rainState,i"); 71 myFile.close(); 72 if (SD.exists("terralog.txt")) { 73 Serial.println(F("terralog.txt was successfully created.")); 74 } else { 75 Serial.println(F("File creation failed.")); 76 } 77 } 78 79 pinMode(fanRelay, OUTPUT); 80 dht_1.begin(); 81 dht_2.begin(); 82 Serial.println(F("date,time,temp_1,temp_2,hum_1,hum_2,fanState,rainState,i")); 83} 84 85void loop() { 86 float h_1 = dht_1.readHumidity(); 87 float t_1 = dht_1.readTemperature(); 88 if (isnan(h_1) || isnan(t_1)) { 89 error("DHT 1"); 90 return; 91 } 92 float h_2 = dht_2.readHumidity(); 93 float t_2 = dht_2.readTemperature(); 94 if (isnan(h_2) || isnan(t_2)) { 95 error("DHT 2"); 96 return; 97 } 98 99 DateTime time = rtc.now(); 100 lcd.setCursor(0, 0); 101 lcd.print(t_1); 102 lcd.print(", "); 103 lcd.print(h_1); 104 lcd.setCursor(0, 1); 105 lcd.print(t_2); 106 lcd.print(", "); 107 lcd.print(h_2); 108 109 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 110 Serial.print(dateBuffer); 111 Serial.print(","); 112 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 113 Serial.print(dateBuffer); 114 Serial.print(F(",")); 115 Serial.print(t_1); 116 Serial.print(F(",")); 117 Serial.print(t_2); 118 Serial.print(F(",")); 119 Serial.print(h_1); 120 Serial.print(F(",")); 121 Serial.print(h_2); 122 Serial.print(F(",")); 123 124 File myFile = SD.open("terralog.txt", FILE_WRITE); 125 if (!myFile) { 126 error("logfile"); 127 } 128 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 129 myFile.print(dateBuffer); 130 myFile.print(","); 131 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 132 myFile.print(dateBuffer); 133 myFile.print(","); 134 myFile.print(t_1); 135 myFile.print(","); 136 myFile.print(t_2); 137 myFile.print(","); 138 myFile.print(h_1); 139 myFile.print(","); 140 myFile.print(h_2); 141 myFile.print(","); 142 143 // 144 // Temperature Control 145 // 146 147 if (t_1 >= maxTemp) { 148 fanState = 1; 149 digitalWrite(fanRelay, HIGH); 150 Serial.print(fanState); 151 myFile.print(fanState); 152 } 153 else if (t_1 < maxTemp && t_1 >= normTemp) { 154 Serial.print(fanState); 155 myFile.print(fanState); 156 } 157 else if (t_1 < normTemp) { 158 fanState = 0; 159 digitalWrite(fanRelay, LOW); 160 Serial.print(fanState); 161 myFile.print(fanState); 162 } 163 164 // 165 // Rain Control 166 // 167 168 if (h_1 < 80 && fanState == 0) { 169 if (i <= 4) { 170 rainState = 1; 171 digitalWrite(rainRelay, HIGH); 172 i++; 173 } 174 else if (i > 4 && i <= 300) { 175 rainState = 2; 176 digitalWrite(rainRelay, LOW); 177 i++; 178 } 179 } 180 else { 181 rainState = 0; 182 i = 0; 183 } 184 Serial.print(","); 185 Serial.print(rainState); 186 Serial.print(","); 187 Serial.print(i); 188 189 myFile.println(); 190 myFile.close(); 191 Serial.println(); 192 delay(1000); 193} 194 195void error(char *str) { 196 Serial.print(F("Fehler: ")); 197 Serial.println(str); 198 lcd.clear(); 199 lcd.print("Error: "); 200 lcd.print(str); 201 delay(5000); 202 lcd.clear(); 203 //while(1); //halt command 204}
terra_log_fan_and_moisting_2
abap
This is a further developed code than the previous one. I remade everything in a way I think is more optimized and more functional.
1#include <DHT.h> 2#include <SPI.h> 3#include <SD.h> 4#include <Wire.h> 5#include <LiquidCrystal_I2C.h> 6#include "RTClib.h" 7 8//const int lightRelay = 12; 9const int rainRelay = 11; 10const int fanRelay = 8; 11const int chipSelect = 10; 12const int DHTPIN_1 = 5; 13const int DHTPIN_2 = 6; 14 15int fanState = 0; 16int rainState = 0; 17 18int maxTemp = 29; 19int normTemp = 25; 20 21int minHum = 96; 22 23int i = 0; 24 25LiquidCrystal_I2C lcd(0x27, 16, 2); 26 27#define DHTTYPE DHT22 28DHT dht_1(DHTPIN_1, DHTTYPE); 29DHT dht_2(DHTPIN_2, DHTTYPE); 30RTC_DS1307 rtc; 31 32File myFile; 33char dateBuffer[12]; 34char sensBuffer[10]; 35 36void setup() { 37 Serial.begin(9600); 38 while (!Serial) { 39 ; 40 } 41 42 lcd.init(); 43 lcd.backlight(); 44 lcd.setCursor(0, 0); 45 46 if (! rtc.begin()) { 47 error("No RTC"); 48 Serial.flush(); 49 abort(); 50 } 51 52 if (! rtc.isrunning()) { 53 error("RTC was off. Setting new time."); 54 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 55 } 56 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 57 Serial.print(F("Initializing SD card...")); 58 if (!SD.begin(chipSelect)) { 59 error("SD initialization failed"); 60 while (1); 61 } 62 63 Serial.println(F("initialization done.")); 64 if (SD.exists(F("terralog.txt"))) { 65 Serial.println(F("terralog.txt exists.")); 66 } else { 67 Serial.println(F("terralog.txt doesn't exist.")); 68 Serial.println(F("Creating terraaaaa.txt")); 69 myFile = SD.open("terralog.txt", FILE_WRITE); 70 myFile.println("date,time,temp_1,temp_2,hum_1,hum_2,fanState, rainState,i"); 71 myFile.close(); 72 if (SD.exists("terralog.txt")) { 73 Serial.println(F("terralog.txt was successfully created.")); 74 } else { 75 Serial.println(F("File creation failed.")); 76 } 77 } 78 79 pinMode(fanRelay, OUTPUT); 80 dht_1.begin(); 81 dht_2.begin(); 82 Serial.println(F("date,time,temp_1,temp_2,hum_1,hum_2,fanState,rainState,i")); 83} 84 85void loop() { 86 float h_1 = dht_1.readHumidity(); 87 float t_1 = dht_1.readTemperature(); 88 if (isnan(h_1) || isnan(t_1)) { 89 error("DHT 1"); 90 return; 91 } 92 float h_2 = dht_2.readHumidity(); 93 float t_2 = dht_2.readTemperature(); 94 if (isnan(h_2) || isnan(t_2)) { 95 error("DHT 2"); 96 return; 97 } 98 99 DateTime time = rtc.now(); 100 lcd.setCursor(0, 0); 101 lcd.print(t_1); 102 lcd.print(", "); 103 lcd.print(h_1); 104 lcd.setCursor(0, 1); 105 lcd.print(t_2); 106 lcd.print(", "); 107 lcd.print(h_2); 108 109 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 110 Serial.print(dateBuffer); 111 Serial.print(","); 112 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 113 Serial.print(dateBuffer); 114 Serial.print(F(",")); 115 Serial.print(t_1); 116 Serial.print(F(",")); 117 Serial.print(t_2); 118 Serial.print(F(",")); 119 Serial.print(h_1); 120 Serial.print(F(",")); 121 Serial.print(h_2); 122 Serial.print(F(",")); 123 124 File myFile = SD.open("terralog.txt", FILE_WRITE); 125 if (!myFile) { 126 error("logfile"); 127 } 128 sprintf(dateBuffer, "%02u-%02u-%04u", time.day(), time.month(), time.year()); 129 myFile.print(dateBuffer); 130 myFile.print(","); 131 sprintf(dateBuffer, "%02u:%02u:%02u", time.hour(), time.minute(), time.second()); 132 myFile.print(dateBuffer); 133 myFile.print(","); 134 myFile.print(t_1); 135 myFile.print(","); 136 myFile.print(t_2); 137 myFile.print(","); 138 myFile.print(h_1); 139 myFile.print(","); 140 myFile.print(h_2); 141 myFile.print(","); 142 143 // 144 // Temperature Control 145 // 146 147 if (t_1 >= maxTemp) { 148 fanState = 1; 149 digitalWrite(fanRelay, HIGH); 150 Serial.print(fanState); 151 myFile.print(fanState); 152 } 153 else if (t_1 < maxTemp && t_1 >= normTemp) { 154 Serial.print(fanState); 155 myFile.print(fanState); 156 } 157 else if (t_1 < normTemp) { 158 fanState = 0; 159 digitalWrite(fanRelay, LOW); 160 Serial.print(fanState); 161 myFile.print(fanState); 162 } 163 164 // 165 // Rain Control 166 // 167 168 if (h_1 < 80 && fanState == 0) { 169 if (i <= 4) { 170 rainState = 1; 171 digitalWrite(rainRelay, HIGH); 172 i++; 173 } 174 else if (i > 4 && i <= 300) { 175 rainState = 2; 176 digitalWrite(rainRelay, LOW); 177 i++; 178 } 179 } 180 else { 181 rainState = 0; 182 i = 0; 183 } 184 Serial.print(","); 185 Serial.print(rainState); 186 Serial.print(","); 187 Serial.print(i); 188 189 myFile.println(); 190 myFile.close(); 191 Serial.println(); 192 delay(1000); 193} 194 195void error(char *str) { 196 Serial.print(F("Fehler: ")); 197 Serial.println(str); 198 lcd.clear(); 199 lcd.print("Error: "); 200 lcd.print(str); 201 delay(5000); 202 lcd.clear(); 203 //while(1); //halt command 204}
Comments
Only logged in users can leave comments