본문 바로가기

메이커

스마트 곤충키트 아두이노 나노 전체 소스 코드


스마트 곤충키트 소프트웨어 전체 소스코드


전체 개발 자료는 한 마리 한 마리 천천히 올려두기로 하고, 생각 난 김에 곤충키트 전체 소스코드를 올려둔다. 안드로이드 앱과 연동하는데 공개 코드는 아두이노 나노에서 동작하는 전체 코드이다. 전원이 공급되면 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(78); //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 =
{
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x180x00x380x60x00x00x00x00x00x00x10xc3,
  0x800x00x00x00x180x00x380x60x00x400x00x00x00x00x1,
  0xc30x800x00x00x00x00x00x380x00x00xc00x00x00x00x0,
  0x10xc30x800x00x00x00x00x00x380x00x00xc00x00x00x0,
  0x00x10xc30x8e0xc0x6e0x600xf80x70xb80x3e0x30xfc0x380x18,
  0x00x00x10xc30x8e0xc0x7e0xf00xf80xf0xf80x3e0x30xfc0x38,
  0x380x00x00x10xff0x8e0xc0x770x300x180xe0x380x60x00xc0,
  0x180x300x00x00x10xff0x8e0xc0x670x300x180x1c0x380x60x0,
  0xc00x1c0x300x00x00x10xff0x8e0xc0x630x300x180x1c0x380x6,
  0x00xc00xc0x700x00x00x10xc30x8e0xc0x630x300x180x1c0x18,
  0x60x00xc00xc0x600x00x00x10xc30x8e0xc0x630x300x180x1c,
  0x180x60x00xc00xe0x600x00x00x10xc30x860x1c0x630x300x18,
  0x1c0x180x60x00xc00x60xc00x00x00x10xc30x870x1c0x630x30,
  0x180xe0x380x60x00xe00x70xc00x00x00x10xc30x870xec0x63,
  0x310xff0x8f0xd80x7f0xe00xfe0x30xc00x00x00x10xc30x830xcc,
  0x630x310xff0x870x980x7f0xe00x7e0x30x800x00x00x00x00x0,
  0x00x00x00x00x00x100x00x00x00x30x800x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x70x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x1f0x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x1e0x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x3c0x380x00x00x800x00x00x0,
  0x00x00x00x00x00x00x00x00x3c0x780x00x10x800x00x0,
  0x00x00x00x00x00x00x00x00x00x3c0x780x00x10x800x0,
  0x00x00x00x00x00x00x00x00x00x00x3e0x780x3e0x70xf8,
  0xf0x810xcf0x00x00x00x00x00x00x00x00x360xd80x7f0x7,
  0xf80x1f0xc00xdf0x00x00x00x00x00x00x00x00x360xd80xe1,
  0x810x800x380x600xf00x00x00x00x00x00x00x00x00x330xd8,
  0xc10xc10x800x300x700xe00x00x00x00x00x00x00x00x00x33,
  0x990xff0xc10x800x7f0xf00xe00x00x00x00x00x00x00x00x0,
  0x330x990xff0xc10x800x7f0xf00xc00x00x00x00x00x00x00x0,
  0x00x310x990xc00x10x800x700x00xc00x00x00x00x00x00x0,
  0x00x00x300x180xc00x10x800x300x00xc00x00x00x00x00x0,
  0x00x00x00x300x180xe10xc10xc00x380x700xc00x00x00x00x0,
  0x00x00x00x00x300x180x7f0x810xfc0x1f0xe00xc00x00x00x0,
  0x00x00x00x00x00x300x180x3e0x00xfc0xf0x800xc00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x00x00x00x00x00x00x00x00x00x00x00x0,
  0x00x00x00x0
};
 
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(255255255);
  }
 
  // picture loop
  u8g.firstPage();
 
  do
  {
    u8g.drawBitmapP(001664, 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(40"Hum %");
  u8g.setPrintPos(800);
  u8g.print(*humPerc);
#ifdef METRIC
  // if metric system, display Celsius
  u8g.drawStr(420"Temp C");
#else
  //display Farenheit
  u8g.drawStr(420"Temp F");
#endif
 
  u8g.setPrintPos(8020);
  u8g.print(*temp);
  u8g.drawStr(440"Heat Ind");
  u8g.setPrintPos(8040);
  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(1030"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;
}