-
라즈베리파이 소스코드프로젝트/스마트 에어컨 2019. 4. 4. 01:30
add80.c
#include <stdio.h> #include <stdlib.h> int add80(int x) { x = x + 80; return x; }
DCPI.c
#include "DCPI.h" double DCPI(int t, int rh) { double result; result = (9/5)*t - 0.55*(1 - rh)*((9/5)*t - 26) + 32; return result; }
DCPI.h
#define MOTION_IN 0 //gpio17 #define FAN 22 #define RED 7 //27 #define GREEN 9 //28 #define BLUE 8 //29 #define DBHOST "127.0.0.1" // 라즈베리 파이 IP 주소 #define DBUSER "root" #define DBPASS "root" #define DBNAME "team7" extern double DCPI(int t, int rh); extern int ret_humid; extern int ret_temp; extern int read_dht22_dat(); extern int motion(); extern int LED_RED(); extern int LED_BLUE(); extern int LED_GREEN();
fan.c
#include <signal.h> //Signal 사용 헤더파일 #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> //exit() 사용 헤더파일 #include "DCPI.h" #include <wiringPi.h> int humandetect; extern int add80(int x); void fanon(int put) { pinMode (FAN, OUTPUT) ; softPwmCreate(FAN, 0, 100); printf("put = %d\n", put); int a = add80(put); printf("add80 result : %d\n", a); if(humandetect==1) { if(a >= 0 && a < 40) { softPwmWrite(FAN,0); // On LED_GREEN(); } else if(a >= 40 && a < 50) { softPwmWrite(FAN,30); LED_BLUE(); } else if(a >= 50) { softPwmWrite(FAN,100); LED_RED(); } } else { digitalWrite (FAN, 0); } }
LED.c
#include <signal.h> //Signal 사용 헤더파일 #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> //exit() 사용 헤더파일 #include "DCPI.h" #include <wiringPi.h> int LED_RED (void) { pinMode(RED, OUTPUT); pinMode(BLUE, OUTPUT); pinMode(GREEN, OUTPUT); digitalWrite(RED, 1); digitalWrite(BLUE, 0); digitalWrite(GREEN, 0); return 0 ; } int LED_BLUE (void) { pinMode(RED, OUTPUT); pinMode(BLUE, OUTPUT); pinMode(GREEN, OUTPUT); digitalWrite(RED, 0); digitalWrite(BLUE, 1); digitalWrite(GREEN, 0); return 0 ; } int LED_GREEN (void) { pinMode(RED, OUTPUT); pinMode(BLUE, OUTPUT); pinMode(GREEN, OUTPUT); digitalWrite(RED, 0); digitalWrite(BLUE, 0); digitalWrite(GREEN, 1); return 0 ; }
main.c
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <wiringPi.h> #include <errno.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> #include <mysql/mysql.h> #include "DCPI.h" MYSQL *connector; MYSQL_RES *result; MYSQL_ROW row; double dcpi; char query[1024]; int humandetect = 0; void sig_handler(int signo) { printf("process stop\n"); digitalWrite (FAN, 0) ; // Off digitalWrite(RED, 0); digitalWrite(BLUE, 0); digitalWrite(GREEN, 0); exit(0); } static PI_THREAD(thThread) { motion(); while(1) { (void) read_dht22_dat(); connector = mysql_init(NULL); if (!mysql_real_connect(connector, DBHOST, DBUSER, DBPASS, DBNAME, 3306, NULL, 0)) { fprintf(stderr, "%s\n", mysql_error(connector)); return 0; } printf("MySQL(rpidb) opened.\n"); //Write iotfarmDB sprintf(query,"insert into thl values (now(),%d,%d)",ret_temp,ret_humid); if(mysql_query(connector, query)) { fprintf(stderr, "%s\n", mysql_error(connector)); printf("Write DB error\n"); } dcpi = DCPI(ret_temp, ret_humid); printf("DCPI : %lf\n", dcpi); fanon((int)dcpi); delay(3000); } } int main() { signal(SIGINT, (void *)sig_handler); if (wiringPiSetup() == -1) exit(EXIT_FAILURE) ; if (setuid(getuid()) < 0) { perror("Dropping privileges failed\n"); exit(EXIT_FAILURE); } piThreadCreate(thThread); if(humandetect == 1) { printf("human detect\n"); } printf("--\n"); while(1); return 0; }
Makefile
team7.exe : temperature.o fan.o LED.o motion.o DCPI.o add80.o main.o gcc -o team7.exe temperature.o fan.o LED.o DCPI.o motion.o add80.o main.o -lwiringPi -lmysqlclient temperature.o : temperature.c gcc -c -o temperature.o temperature.c -lwiringPi fan.o : fan.c gcc -c -o fan.o fan.c -lwiringPi LED.o : LED.c gcc -c -o LED.o LED.c -lwiringPi motion.o : motion.c gcc -c -o motion.o motion.c -lwiringPi DCPI.o : DCPI.c gcc -c -o DCPI.o DCPI.c -lwiringPi add80.o: add80.s gcc -c -o add80.o add80.s main.o : main.c gcc -c -o main.o main.c -lwiringPi clean : rm *.o team7.exe
motion.c
#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <wiringPi.h> #include "DCPI.h" // Use GPIO Pin 17, which is Pin 0 for wiringPi library #define MOTION_IN 0 //gpio17 extern int motion(); extern int humandetect; extern void fanon(); extern void myInterrupt(); double dcpi = 0; void myInterrupt(void) { if (humandetect == 1) humandetect = 0; else if (humandetect == 0) humandetect = 1; } int motion(void) { if ( wiringPiISR (MOTION_IN, INT_EDGE_BOTH, &myInterrupt) < 0 ) { fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)); return 1; } }
shell.sh
#!/bin/bash echo "Count of all records" mysql -uroot -proot team7<<EOFMYSQL select * from thl; EOFMYSQL
temperature.c
/* * dht22.c: * Simple test program to test the wiringPi functions * Based on the existing dht11.c * Amended by technion@lolware.net */ #include <wiringPi.h> #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/types.h> #include <unistd.h> #include <pthread.h> //#include "locking.h" #define MAXTIMINGS 85 int ret_humid; int ret_temp; //extern int read_dht22_dat(); //static int DHTPIN = 7; static int DHTPIN = 11; static uint8_t sizecvt(const int read) { /* digitalRead() and friends from wiringpi are defined as returning a value < 256. However, they are returned as int() types. This is a safety function */ if (read > 255 || read < 0) { printf("Invalid data from wiringPi library\n"); exit(EXIT_FAILURE); } return (uint8_t)read; } int read_dht22_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; int dht22_dat[5] = {0,0,0,0,0}; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; // pull pin down for 18 milliseconds pinMode(DHTPIN, OUTPUT); digitalWrite(DHTPIN, HIGH); delay(10); digitalWrite(DHTPIN, LOW); delay(18); // then pull it up for 40 microseconds digitalWrite(DHTPIN, HIGH); delayMicroseconds(40); // prepare to read the pin pinMode(DHTPIN, INPUT); // detect change and read data for ( i=0; i< MAXTIMINGS; i++) { counter = 0; while (sizecvt(digitalRead(DHTPIN)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = sizecvt(digitalRead(DHTPIN)); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht22_dat[j/8] <<= 1; if (counter > 25) dht22_dat[j/8] |= 1; j++; } } // check we read 40 bits (8bit x 5 ) + verify checksum in the last byte // print it out if data is good if ((j >= 40) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) { float t, h; h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1]; h /= 10; t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3]; t /= 10.0; if ((dht22_dat[2] & 0x80) != 0) t *= -1; ret_humid = (int)h; ret_temp = (int)t; //printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t ); printf("Temperature = %d , Humidity = %d\n", ret_temp, ret_humid); return ret_temp; } else { printf("Data not good, skip\n"); return 0; } } //Humidity = %d , ret_humid