-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaspiAutoHumidifier.c
More file actions
411 lines (315 loc) · 7.93 KB
/
RaspiAutoHumidifier.c
File metadata and controls
411 lines (315 loc) · 7.93 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <wiringPiI2C.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define SW 5 /* GPIO24 */
#define LED 1 /* GPIO18 */
#define MAXTIMINGS 83
#define DHTPIN 7 /* dht11, GPIO4 */
/*4x4 keypad 변수*/
unsigned char row_pins[4] = {25, 24, 23, 22};
unsigned char col_pins[4] = {29, 28, 27, 26};
unsigned char key;
int i = 0;
unsigned char input_key_buffer = 0;
unsigned char lcd_state = 0;
unsigned char humidifier_state = 0;
unsigned char user_hum[2];
float user_hum_integer = 50;
char key_code[16] ={'1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'D', '0', 'E', 'F'};
/*dht11 변수*/
int dht11_dat[5] = {0, } ;
int hum;
int tem;
char buffer[20];
/* lcd 관련*/
// Define some device parameters
#define I2C_ADDR 0x27 // I2C device address
// Define some device constants
#define LCD_CHR 1 // Mode - Sending data
#define LCD_CMD 0 // Mode - Sending command
#define LINE1 0x80 // 1st line
#define LINE2 0xC0 // 2nd line
#define LCD_BACKLIGHT 0x08 // On
// LCD_BACKLIGHT = 0x00 # Off
#define ENABLE 0b00000100 // Enable bit
void lcd_init(void);
void lcd_byte(int bits, int mode);
void lcd_toggle_enable(int bits);
// added by Lewis
void typeInt(int i);
void typeFloat(float myFloat);
void lcdLoc(int line); //move cursor
void ClrLcd(void); // clr LCD return home
void typeln(const char *s);
void typeChar(char val);
int fd; // seen by all subroutines
int notes[] = {
391, 391, 440, 440, 391, 391, 329, 329.63, 329.63, \
391, 391, 329.63, 329.63, 293.66, 293.66, 293.66, 0, \
391, 391, 440, 391, 391, 391, 329.63, 329.63, \
391, 329.63, 293.66, 329.63, 261.63, 261.63, 261.63, 0
};
/*int musicPlay()
{
int i;
softToneCreate(SPKR);
for (i = 0; i < TOTAL; ++i) {
softToneWrite(SPKR, notes[i]);
delay(280);
}
return 0;
}*/
/* 4x4 keypad*/
unsigned char keyScan()
{
int i, j;
unsigned char idx, scan;
for(i=0; i<4; i++) {
pinMode(row_pins[i], OUTPUT);
digitalWrite(row_pins[i], HIGH);
pinMode(col_pins[i], INPUT);
pullUpDnControl(col_pins[i], PUD_UP);
}
for(i=0, idx=0, scan=0xff; i<4; i++) {
digitalWrite(row_pins[i], LOW);
delay(10);
for(j=0; j<4; j++, idx++) {
if(digitalRead(col_pins[j]) == LOW) {
scan = idx;
}
}
digitalWrite(row_pins[i], HIGH);
};
return scan;
}
int keypad(void)
{
key = keyScan();
if(key != 0xff)
{
printf("Scan key code is %d(%c)\n", key, key_code[key]);
input_key_buffer = key_code[key];
}
}
/*4x4 keypad end*/
/*dht11*/
void read_dht11_dat()
{
uint8_t laststate = HIGH ;
uint8_t counter = 0 ;
uint8_t j = 0, i ;
uint8_t flag = HIGH ;
uint8_t state = 0 ;
float f ;
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0 ;
pinMode(DHTPIN, OUTPUT) ;
digitalWrite(DHTPIN, LOW) ;
delay(18) ;
digitalWrite(DHTPIN, HIGH) ;
delayMicroseconds(30) ;
pinMode(DHTPIN, INPUT) ;
for (i = 0; i < MAXTIMINGS; i++) {
counter = 0 ;
while ( digitalRead(DHTPIN) == laststate) {
counter++ ;
delayMicroseconds(1) ;
if (counter == 200) break ;
}
laststate = digitalRead(DHTPIN) ;
if (counter == 200) break ; // if while breaked by timer, break for
if ((i >= 4) && (i % 2 == 0)) {
dht11_dat[j / 8] <<= 1 ;
if (counter > 20) dht11_dat[j / 8] |= 1 ;
j++ ;
}
}
if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xff))) {
printf("humidity = %d.%d %% Temperature = %d.%d *C \n", dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3]) ;
hum = dht11_dat[0];
tem = dht11_dat[2];
}
else
{
;
}
}
/* dht11 end*/
void input_key_process()
{
if(input_key_buffer == 'A')
{
lcd_state = 1;
}
if(input_key_buffer == 'B')
{
lcd_state = 2;
humidifier_state = 2; //가습기 자동모드
printf("Humidifier Auto\n");
}
if(input_key_buffer == 'C')
{
lcd_state = 3;
humidifier_state = 1; // 가습기 켜짐
printf("Humidifier On\n");
}
if(input_key_buffer == 'F')
{
lcd_state = 4;
humidifier_state = 0; // 가습기 꺼짐
printf("Humidifier Off\n");
}
}
void menu()
{
if(lcd_state == 0)
{
ClrLcd();
lcdLoc(LINE1); typeln("Welcome! This is");
lcdLoc(LINE2); typeln("Auto Humidifier");
}
if(lcd_state == 1)
{
ClrLcd();
lcdLoc(LINE1); typeln("Tem : "); typeInt(tem);
lcdLoc(LINE2); typeln("Hum : "); typeInt(hum);
}
if(lcd_state == 2)
{
ClrLcd();
lcdLoc(LINE1); typeln("Auto Mode");
lcdLoc(LINE2); typeln("Set Hum L:"); typeFloat(user_hum_integer);
if(input_key_buffer == '1')
{ user_hum_integer++; input_key_buffer = 'B';}
if(input_key_buffer == '7')
{ user_hum_integer--; input_key_buffer = 'B';}
if(input_key_buffer == '2')
{ user_hum_integer += 5; input_key_buffer = 'B';}
if(input_key_buffer == '8')
{ user_hum_integer-= 5; input_key_buffer = 'B';}
if(input_key_buffer == '3')
{ user_hum_integer += 10; input_key_buffer = 'B';}
if(input_key_buffer == '9')
{ user_hum_integer-= 10; input_key_buffer = 'B';}
if(user_hum_integer > 100) {user_hum_integer = 100;}
if(user_hum_integer < 0) {user_hum_integer = 0;}
}
if(lcd_state == 3)
{
ClrLcd();
lcdLoc(LINE1); typeln("Humidifier On");
}
if(lcd_state == 4)
{
ClrLcd();
lcdLoc(LINE1); typeln("Humidifier Off");
}
}
void humidifier_work()
{
if(humidifier_state == 0)
{
digitalWrite(1, LOW);
}
if(humidifier_state == 1)
{
digitalWrite(1, HIGH);
}
if(humidifier_state == 2)
{
if(hum >= user_hum_integer)
{
digitalWrite(1, LOW);
}
if(hum < user_hum_integer)
{
digitalWrite(1, HIGH);
}
}
}
int main(int argc, char** argv)
{
wiringPiSetup();
if (wiringPiSetup () == -1) exit (1);
fd = wiringPiI2CSetup(I2C_ADDR);
pinMode(1, OUTPUT);
//printf("fd = %d ", fd);
lcd_init(); // setup LCD
while(1)
{
keypad();
input_key_process();
menu();
humidifier_work();
read_dht11_dat();
delay(1000);
}
return 0 ;
}
// float to string
void typeFloat(float myFloat) {
char buffer[20];
sprintf(buffer, "%4.2f", myFloat);
typeln(buffer);
}
// int to string
void typeInt(int i) {
char array1[20];
sprintf(array1, "%d", i);
typeln(array1);
}
// clr lcd go home loc 0x80
void ClrLcd(void) {
lcd_byte(0x01, LCD_CMD);
lcd_byte(0x02, LCD_CMD);
}
// go to location on LCD
void lcdLoc(int line) {
lcd_byte(line, LCD_CMD);
}
// out char to LCD at current position
void typeChar(char val) {
lcd_byte(val, LCD_CHR);
}
// this allows use of any size string
void typeln(const char *s) {
while ( *s ) lcd_byte(*(s++), LCD_CHR);
}
void lcd_byte(int bits, int mode) {
//Send byte to data pins
// bits = the data
// mode = 1 for data, 0 for command
int bits_high;
int bits_low;
// uses the two half byte writes to LCD
bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT ;
bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT ;
// High bits
wiringPiI2CReadReg8(fd, bits_high);
lcd_toggle_enable(bits_high);
// Low bits
wiringPiI2CReadReg8(fd, bits_low);
lcd_toggle_enable(bits_low);
}
void lcd_toggle_enable(int bits) {
// Toggle enable pin on LCD display
delayMicroseconds(500);
wiringPiI2CReadReg8(fd, (bits | ENABLE));
delayMicroseconds(500);
wiringPiI2CReadReg8(fd, (bits & ~ENABLE));
delayMicroseconds(500);
}
void lcd_init() {
// Initialise display
lcd_byte(0x33, LCD_CMD); // Initialise
lcd_byte(0x32, LCD_CMD); // Initialise
lcd_byte(0x06, LCD_CMD); // Cursor move direction
lcd_byte(0x0C, LCD_CMD); // 0x0F On, Blink Off
lcd_byte(0x28, LCD_CMD); // Data length, number of lines, font size
lcd_byte(0x01, LCD_CMD); // Clear display
delayMicroseconds(500);
}