Components and supplies
IO Expander Bundle
IO Expander
Arduino Nano R3
Apps and platforms
Arduino IDE
Project description
Code
Basement/Crawlspace Ventilation System
c_cpp
Use the Arduino Nano to create a Smart Ventilation System
1/* IO Expander 2 * 3 * Basement/Crawlspace Ventilation System v1.1 4 5 * 6 */ 7 8#include <math.h> 9#include <SoftwareSerial.h> 10#include <avr/wdt.h> 11#include 12 "IOExpander.h" 13 14#define FAHRENHEIT 15#define ONEWIRE_TO_I2C_ROM "i4s71" 16#define 17 INIT_OLED "st13;si;sc;sd" 18#define HUMIDITY_SENSOR_INSIDE "s6t1" 19#define 20 HUMIDITY_SENSOR_OUTSIDE "s8t1" 21#define FAN_ON "r1o" 22#define 23 FAN_OFF "r1f" 24#define ABSOLUTE_DELTA_FAN_ON 1 // 25 Fan on if absolute humidity delta of inside >= outside 26#define ABSOLUTE_DELTA_FAN_OFF 27 0.5 // Fan off if absolute humidity delta of inside <= outside 28#define 29 OUTSIDE_RELATIVE_FAN_ON 88 // Fan on if outside relative humidity is <= 30 % 31#define OUTSIDE_RELATIVE_FAN_OFF 90 // Fan off if outside relative 32 humidity is >= % 33#define MINIMUM_TEMPERATURE 15 // Cycle vent on/off 34 if outside temperature <= 15C/59F 35#define FAN_ON_TIME (20*60*1000L) 36 // 20 min 37#define FAN_OFF_TIME (20*60*1000L) // 20 min 38 39//#define 40 SERIAL_DEBUG 41#define SERIAL_TIMEOUT 5000 // 5 sec delay between 42 DHT22 reads 43 44#ifdef SERIAL_DEBUG 45SoftwareSerial swSerial(8,7); 46#endif 47 48struct 49 HS { 50 float temp; 51 float relative; 52 float absolute; 53 bool error; 54}; 55 56int 57 led = 13; 58bool init_oled = true; 59long ontime, offtime; 60 61#ifdef FAHRENHEIT 62#define 63 C2F(temp) CelsiusToFahrenheit(temp) 64float CelsiusToFahrenheit(float celsius) 65{ 66 67 return ((celsius*9)/5)+32; 68} 69#else 70#define C2F(temp) (temp) 71#endif 72 73void 74 SerialPrint(const char* str, float decimal, char error) 75{ 76 Serial.print(str); 77 78 if (error) Serial.print(F("NA")); 79 else Serial.print(decimal, 1); 80} 81 82float 83 DewPoint(float temp, float humidity) 84{ 85 float t = (17.625 * temp) / (243.04 86 + temp); 87 float l = log(humidity/100); 88 float b = l + t; 89 // Use the 90 August-Roche-Magnus approximation 91 return (243.04*b)/(17.625-b); 92} 93 94#define 95 MOLAR_MASS_OF_WATER 18.01534 96#define UNIVERSAL_GAS_CONSTANT 8.21447215 97 98float 99 AbsoluteHumidity(float temp, float relative) 100{ 101 //taken from https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/ 102 103 //precision is about 0.1°C in range -30 to 35°C 104 //August-Roche-Magnus 6.1094 105 exp(17.625 x T)/(T + 243.04) 106 //Buck (1981) 6.1121 exp(17.502 x T)/(T + 107 240.97) 108 //reference https://www.eas.ualberta.ca/jdwilson/EAS372_13/Vomel_CIRES_satvpformulae.html 109 // Use Buck (1981) 110 return (6.1121 * pow(2.718281828, (17.67 * temp) / (temp 111 + 243.5)) * relative * MOLAR_MASS_OF_WATER) / ((273.15 + temp) * UNIVERSAL_GAS_CONSTANT); 112} 113 114void 115 ReadHumiditySensor(HS* hs) 116{ 117 SerialCmd("sr"); 118 if (SerialReadFloat(&hs->temp) 119 && 120 SerialReadFloat(&hs->relative)) { 121 //hs->dewpoint = DewPoint(hs->temp, 122 hs->relative); 123 hs->absolute = AbsoluteHumidity(hs->temp, hs->relative); 124 125 hs->error = false; 126 } 127 else hs->error = true; 128 SerialReadUntilDone(); 129} 130 131void 132 setup() { 133 Serial.begin(115200); 134#ifdef SERIAL_DEBUG 135 swSerial.begin(115200); 136 137 //swSerialEcho = &swSerial; 138#endif 139 pinMode(led, OUTPUT); 140 wdt_enable(WDTO_8S); 141 142 offtime = millis() - FAN_OFF_TIME; 143} 144 145void loop() { 146 HS inside, outside; 147 148 static bool fan = false; 149 static bool cycle = false; 150 static long last_time 151 = -(60L * 1000L); 152 153 Serial.println(); 154 if (SerialReadUntilDone()) { 155 156 //if (init_oled) { 157 // if (SerialCmdNoError(ONEWIRE_TO_I2C_ROM)) { 158 159 // SerialCmdDone(INIT_OLED); 160 // init_oled = false; 161 // } 162 163 //} 164 165 // Read the humidity sensors only once a minute or they will 166 self heat if read too quickly 167 if (millis() - last_time > 60L * 1000L) 168 169 { 170 if (SerialCmdDone(HUMIDITY_SENSOR_INSIDE)) 171 ReadHumiditySensor(&inside); 172 173 174 if (SerialCmdDone(HUMIDITY_SENSOR_OUTSIDE)) 175 ReadHumiditySensor(&outside); 176 177 178 if (inside.error || outside.error) fan = false; 179 else { 180 181 if (fan) { 182 if (outside.relative >= OUTSIDE_RELATIVE_FAN_OFF 183 || inside.absolute - outside.absolute <= ABSOLUTE_DELTA_FAN_OFF) 184 cycle 185 = fan = false; 186 else { 187 if (cycle && outside.temp <= MINIMUM_TEMPERATURE 188 && 189 millis() - ontime > FAN_ON_TIME) fan = false; 190 } 191 192 if (!fan) offtime = millis(); 193 } 194 else { 195 if 196 (outside.relative <= OUTSIDE_RELATIVE_FAN_ON && inside.absolute - outside.absolute 197 >= ABSOLUTE_DELTA_FAN_ON) 198 cycle = fan = true; 199 if (cycle 200 && outside.temp <= MINIMUM_TEMPERATURE) 201 fan = (millis() - offtime 202 > FAN_OFF_TIME) ? true : false; 203 if (fan) ontime = millis(); 204 205 } 206 } 207 208 if (fan) SerialCmdDone(FAN_ON); 209 else 210 SerialCmdDone(FAN_OFF); 211 212 if (SerialCmdNoError(ONEWIRE_TO_I2C_ROM)) 213 { 214 if (init_oled) { 215 SerialCmdDone(INIT_OLED); 216 init_oled 217 = false; 218 } 219 SerialCmdDone("st13;sc;sf0;sa1;sd70,0,\\"INSIDE\\";sd127,0,\\"OUTSIDE\\";sf1;sa0;sd0,12,248,\\"" 220 221 #ifdef FAHRENHEIT 222 "F" 223 #else 224 "C" 225 226 #endif 227 "\\";sd0,30,\\"%\\";sf0;sd0,50,\\"g/m\\";sd20,46,\\"3\\";"); 228 229 SerialPrint("sf1;sa1;sd70,12,\\"", C2F(inside.temp), inside.error); 230 231 SerialPrint("\\";sd70,30,\\"", inside.relative, inside.error); 232 SerialPrint("\\";sd70,48,\\"", 233 inside.absolute, inside.error); 234 SerialPrint("\\";sd127,12,\\"", C2F(outside.temp), 235 outside.error); 236 SerialPrint("\\";sd127,30,\\"", outside.relative, 237 outside.error); 238 SerialPrint("\\";sd127,48,\\"", outside.absolute, 239 outside.error); 240 Serial.print("\\";"); 241 Serial.print("sf0;sa0;sd0,0,\\""); 242 243 if (fan) Serial.print("FAN"); 244 else Serial.print("v1.1"); 245 246 Serial.println("\\";sd"); 247 SerialReadUntilDone(); 248 } 249 250 else init_oled = true; 251 252 last_time = millis(); 253 } 254 255 256 delay(1000); 257 } 258 else { 259 digitalWrite(led, HIGH); 260 delay(500); 261 262 digitalWrite(led, LOW); 263 delay(500); 264 init_oled = true; 265 } 266 267 wdt_reset(); 268}
Downloadable files
Basement/Crawlspace Ventilation System
Use the Arduino Nano to create a Smart Ventilation System.
Basement/Crawlspace Ventilation System
Basement/Crawlspace Ventilation System
Use the Arduino Nano to create a Smart Ventilation System.
Basement/Crawlspace Ventilation System
Comments
Only logged in users can leave comments
chuygen
0 Followers
•0 Projects
Table of contents
Intro
22
0