반응형
스마트 곤충키트 소프트웨어 전체 소스코드
전체 개발 자료는 한 마리 한 마리 천천히 올려두기로 하고, 생각 난 김에 곤충키트 전체 소스코드를 올려둔다. 안드로이드 앱과 연동하는데 공개 코드는 아두이노 나노에서 동작하는 전체 코드이다. 전원이 공급되면 OLED 에 현재 온도와 습도를 표시하고, 블루투스로 데이터를 날려준다. 이것을 반복한다.
프로그래밍의 대부분은 재능이 아니라 훈련에 의존한다.-견하
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | // Written by EmbeddedUser // Refer to blog http://www.jaimeferns.in/2016/10/how-to-build-humidity-and-temperature.html // Youtube Video https://www.youtube.com/watch?v=g-rcEd_2I-w // Humidity meter with DHT11, Arduino and I2c OLED display // Now support displaying sensor error and configuring deg C or deg F #include <DHT.h> #include "U8glib.h" #include <SoftwareSerial.h> SoftwareSerial BTSerial(7, 8); //Connect BT's TX, RX #define SENDING_INTERVAL 4000 unsigned long prevReadTime = 0; // Display defaults to Degree F. TO use metric display, // comment out next line to display temperature in Degree C, #define METRIC #define DHTPIN 2 // what digital pin we're connected to // Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 // to 3.3V instead of 5V! // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 4 (on the right) of the sensor to GROUND // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor // Initialize DHT sensor. // Note that older versions of this library took an optional third parameter to // tweak the timings for faster processors. This parameter is no longer needed // as the current DHT reading algorithm adjusts itself to work on faster procs. DHT dht(DHTPIN, DHTTYPE); U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC const uint8_t logo[] PROGMEM = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 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0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; bool first = true; float hum = 0.0; float temp = 0.0; float hIndex = 0.0; bool dht_test(float* humPerc, float* tempF, float* heatIndex); void setup(void) { Serial.begin(9600); dht.begin(); first = true; //BLE BTSerial.begin(9600); // assign default color value if (u8g.getMode() == U8G_MODE_R3G3B2) { u8g.setColorIndex(255); // white } else if (u8g.getMode() == U8G_MODE_GRAY2BIT) { u8g.setColorIndex(3); // max intensity } else if (u8g.getMode() == U8G_MODE_BW) { u8g.setColorIndex(1); // pixel on } else if (u8g.getMode() == U8G_MODE_HICOLOR) { u8g.setHiColorByRGB(255, 255, 255); } // picture loop u8g.firstPage(); do { u8g.drawBitmapP(0, 0, 16, 64, logo); } while (u8g.nextPage()); dht_test(&hum, &temp, &hIndex); } void HumMeter(float* humPerc, float* temp, float* heatIndex) { u8g.setFont(u8g_font_fub11); u8g.setFontRefHeightExtendedText(); u8g.setDefaultForegroundColor(); u8g.setFontPosTop(); u8g.drawStr(4, 0, "Hum %"); u8g.setPrintPos(80, 0); u8g.print(*humPerc); #ifdef METRIC // if metric system, display Celsius u8g.drawStr(4, 20, "Temp C"); #else //display Farenheit u8g.drawStr(4, 20, "Temp F"); #endif u8g.setPrintPos(80, 20); u8g.print(*temp); u8g.drawStr(4, 40, "Heat Ind"); u8g.setPrintPos(80, 40); u8g.print(*heatIndex); } void loop(void) { bool result = dht_test(&hum, &temp, &hIndex); if (first) { // skip displaying readings first time, as its stale data. first = false; } else { if(result == false) { u8g.firstPage(); do { // sensor error u8g.setFont(u8g_font_fub11); u8g.setFontRefHeightExtendedText(); u8g.setDefaultForegroundColor(); u8g.setFontPosTop(); u8g.drawStr(10, 30, "Sensor Error"); } while (u8g.nextPage()); } else { u8g.firstPage(); do { HumMeter(&hum, &temp, &hIndex); } while (u8g.nextPage()); } } } bool dht_test(float* humPerc, float* temp, float* heatIndex) { // Wait a few seconds between measurements. delay(2000); *humPerc = 0; *temp = 0; *heatIndex = 0; // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); //here oled print??? //if(millis() - prevReadTime > SENDING_INTERVAL) { //BTSerial.print("thingspeak:key=xxx&field1="); BTSerial.print(t); //BTSerial.print("&field2="); BTSerial.print(" "); BTSerial.print(h); BTSerial.print("[*]"); //여기까지 // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return false; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); *humPerc = h; #ifdef METRIC // metric system, load degree celsius *temp = t; *heatIndex = hic; #else *temp = f; *heatIndex = hif; #endif return true; } |
반응형
'메이커' 카테고리의 다른 글
미세먼지 공기 청정기 만들 때 재료비 산정 예 (0) | 2019.12.26 |
---|---|
메이커 프로젝트 활동 교육과정 (0) | 2019.11.25 |
반응하는 인형 외관 디자인 - 피카소의 ’도라마르의 초상’ 작품 (0) | 2019.03.14 |
fritzing 에 이미지 추가하기 - 부품 이미지, 사용자 이미지 다 됩니다. (0) | 2019.01.28 |
미세먼지 공기청정기 구성을 한눈에 보기 (0) | 2018.11.27 |