diff --git a/Central_Control_Board/Central_Main.c b/Central_Control_Board/Central_Main.c new file mode 100644 index 0000000000000000000000000000000000000000..cebc37202494fa2559ef618e4bda8ff31c575b02 --- /dev/null +++ b/Central_Control_Board/Central_Main.c @@ -0,0 +1,200 @@ + +#include "JaeYukBookkom.h" +#include <arpa/inet.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <sys/select.h> +#include <sys/time.h> +#include <pthread.h> +#include "LCD.c" +#include "GPIO.c" +#include "Networking.c" +#include "PWM.c" + +short int Window_Open = 0; +short int problem_sensing = 0; +short int user = 0; +void *Communicating(void *args); +int ms_handle_sense(void); +int ms_handle_oper(void); +int toggle_window(void); +void *getFromAPI(void *args); +void *AutoRefrash(void *args); +struct timeval timeout = {0, 1000}; +pthread_t *mainThread, *APIthread, *TimerThread; +int main(int argc, char *argv[]){ + if (argc != 2){ + printf("Usage : %s <port>\n", argv[0]); + exit(1); + } + + ServerInit(&serv_sock, atoi(argv[1])); + GPIOInit(); + LCD_init(); + mainThread = malloc(sizeof(pthread_t)); + APIthread = malloc(sizeof(pthread_t)); + TimerThread = malloc(sizeof(pthread_t)); + if (pthread_create(mainThread, NULL, Communicating, NULL) < 0) + perror("main thread create error:"); + if (pthread_create(APIthread, NULL, getFromAPI, NULL) < 0) + perror("API thread create error:"); + if (pthread_create(TimerThread, NULL, AutoRefrash, NULL) < 0) + perror("API thread create error:"); + + pthread_join(*mainThread, NULL); + pthread_join(*APIthread, NULL); + pthread_join(*TimerThread, NULL); + free(mainThread); + free(APIthread); + free(TimerThread); + return (0); +} +void *Communicating(void *args){ + int state = 1; + int prev_state = 1; + while (1){ + state = GPIORead(BUTTON_IN); + if (prev_state == 0 && state == 1) // button polling + toggle_window(); + prev_state = state; + + clients = clients_socket; + if (select(max_fd + 1, &clients, NULL, NULL, &timeout)){ // polling with listening clients message + if (FD_ISSET(clnt_sock[0], &clients)) // message from sensing board + ms_handle_sense(); + if (FD_ISSET(clnt_sock[1], &clients)) // message from sensing board + ms_handle_oper(); + } + usleep(500 * 100); + } + + //dispose socket + for (int i = 0; i < MAX_CONNECT_BOARD; i++){ + close(clnt_sock[i]); + } + close(serv_sock); +} +int ms_handle_sense(void){ // message handler from sensing board + char input_str[3] = {0}; + int str_len = read(clnt_sock[0], input_str, sizeof(input_str)); + if (str_len == -1){ + error_handling("read() error"); + return -1; + } + problem_sensing = atoi(input_str); + printf("problem: %d\n"); + LCD_messaging_error(problem_sensing); + if (problem_sensing && !user && problem_sensing != 8) + if (Window_Open) + toggle_window(); +} +int ms_handle_oper(void){ // message handler from operation board + char input_str[3] = {0}; + int str_len = read(clnt_sock[1], input_str, sizeof(input_str)); + if (str_len == -1){ + error_handling("read() error"); + return -1; + } + int danger = atoi(input_str); + if (danger){ + problem_sensing = 8; + LCD_messaging_error(problem_sensing); + boozerThread = malloc(sizeof(pthread_t)); + if (pthread_create(boozerThread, NULL, bozzering, NULL) < 0) + perror("buzzor thread create error:"); + } +} +int toggle_window(void){ + printf("Window toggle\n"); + if (problem_sensing == 8){ + pthread_cancel(*boozerThread); // boozer off + problem_sensing = 0; + LCD_messaging_error(problem_sensing); + return -1; + } + else if (problem_sensing && !Window_Open){ + boozer_Warning(); + user = 1; + } + Window_Open = !Window_Open; + char output_str[3] = {0}; + snprintf(output_str, 3, "%d", Window_Open); + write(clnt_sock[1], output_str, sizeof(output_str)); // open /close call to operation board + + printf("msg = %s\n", output_str); + if (Window_Open) + GPIOWrite(BUTTON_LED, HIGH); + else{ + GPIOWrite(BUTTON_LED, LOW); + user = 0; + } + +} +void *getFromAPI(void *args){ + fd_set API_socket, API; + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 1000; + FD_ZERO(&API_socket); + FD_SET(clnt_sock[2], &API_socket); + int max_fd2 = clnt_sock[2]; + int str_len; + char str[50]; + float temp, hyd; + int weatherCode, dust, rainPer; + while (1){ + if (select(max_fd2 + 1, &clients, NULL, NULL, &timeout)){ // polling with listening clients message + if (FD_ISSET(clnt_sock[2], &clients)){ // message from sensing board + str_len = read(clnt_sock[2], str, sizeof(str)); + if (str_len == -1){ + error_handling("read() error"); + continue; + } + printf("Receive message from Client : %s\n", str); + + char *ptr = strtok(str, " "); + printf("temp: %s\n", ptr); + temp = atof(ptr); + ptr = strtok(NULL, " "); + printf("hye: %s\n", ptr); + hyd = atof(ptr); + ptr = strtok(NULL, " "); + printf("wheatherCode: %s\n", ptr); + weatherCode = atoi(ptr); + ptr = strtok(NULL, " "); + printf("dust: %s\n", ptr); + dust = atoi(ptr); + ptr = strtok(NULL, " "); + printf("rainPer: %s\n", ptr); + rainPer = atoi(ptr); + LCD_weather_prediction(temp, hyd, weatherCode, dust, rainPer); + printf("API data received\n"); + } + } + usleep(5000000); + } +} +void *AutoRefrash(void *args){ + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + int state = 1, prev_state = 1; + printf("refresh sensing start"); + while (1){ + state = GPIORead(TIMER_IN); + if (prev_state == 0 && state == 1){ // button polling + printf("Auto reFrash with 15s\n"); + if (!Window_Open && !problem_sensing)// when window closed >> op -> cl + toggle_window(); + usleep(15000000); // waiting 15min(alternative: sec) with window open + if (problem_sensing != 8 && Window_Open)// if not door interrupt and not user close excuted + toggle_window(); + } + prev_state = state; + usleep(50000); + } +} \ No newline at end of file diff --git a/Central_Control_Board/GPIO.c b/Central_Control_Board/GPIO.c new file mode 100644 index 0000000000000000000000000000000000000000..1a02a82498fd6093de8e41875f9b52d3de3093a0 --- /dev/null +++ b/Central_Control_Board/GPIO.c @@ -0,0 +1,134 @@ +#include "JaeYukBookkom.h" +#include <stdio.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/types.h> + +void error_handling(char *message); +static int GPIOExport(int pin); +static int GPIOUnexport(int pin); +static int GPIODirection(int pin, int dir); +static int GPIORead(int pin); +static int GPIOWrite(int pin, int value); + +int GPIOInit(void){ + if (-1 == GPIOExport(BUTTON_IN) || -1 == GPIOExport(BUTTON_OUT)|| -1 == GPIOExport(BUTTON_LED)||-1 == GPIOExport(TIMER_IN) || -1 == GPIOExport(TIMER_OUT)) + return (1); + if (-1 == GPIODirection(BUTTON_IN, IN) || -1 == GPIODirection(BUTTON_OUT, OUT) || -1 == GPIODirection(BUTTON_LED, OUT)||-1 == GPIODirection(TIMER_IN, IN) || -1 == GPIODirection(TIMER_OUT, OUT)) + return (2); + if (-1 == GPIOWrite(BUTTON_OUT, 1)||-1 == GPIOWrite(TIMER_OUT, 1)) + return (3); +} +void GPIODispose(void){ + GPIOUnexport(BUTTON_IN); + GPIOUnexport(BUTTON_OUT); + GPIOUnexport(BUTTON_LED); + GPIOUnexport(TIMER_IN); + GPIOUnexport(TIMER_OUT); +} + +static int GPIOExport(int pin){ + char buffer[BUFFER_MAX]; + ssize_t bytes_written; + int fd; + + fd = open("/sys/class/gpio/export", O_WRONLY); + if (-1 == fd) + { + fprintf(stderr, "Failed to open export for writing!\n"); + return (-1); + } + + bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin); + write(fd, buffer, bytes_written); + close(fd); + return (0); +} +static int GPIOUnexport(int pin){ + char buffer[BUFFER_MAX]; + ssize_t bytes_written; + int fd; + + fd = open("/sys/class/gpio/unexport", O_WRONLY); + if (-1 == fd) + { + fprintf(stderr, "Failed to open unexport for writing!\n"); + return (-1); + } + bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin); + write(fd, buffer, bytes_written); + close(fd); + return (0); +} +static int GPIODirection(int pin, int dir){ + static const char s_directions_str[] = "in\0out"; + + char path[DIRECTION_MAX] = "/sys/class/gpio/gpio%d/direction"; + int fd; + + snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin); + + fd = open(path, O_WRONLY); + if (-1 == fd) + { + fprintf(stderr, "Failed to open gpio direction for writing!\n"); + return (-1); + } + + if (-1 == + write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) + { + fprintf(stderr, "Failed to set direction!\n"); + return (-1); + } + + close(fd); + return (0); +} +static int GPIORead(int pin){ + char path[VALUE_MAX]; + char value_str[3]; + int fd; + + snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin); + fd = open(path, O_RDONLY); + if (-1 == fd) + { + fprintf(stderr, "Failed to open gpio value for reading!\n"); + return (-1); + } + + if (-1 == read(fd, value_str, 3)) + { + fprintf(stderr, "Failed to read value!\n"); + return (-1); + } + + close(fd); + + return (atoi(value_str)); +} +static int GPIOWrite(int pin, int value){ + static const char s_values_str[] = "01"; + char path[VALUE_MAX]; + int fd; + + snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin); + fd = open(path, O_WRONLY); + if (-1 == fd) + { + fprintf(stderr, "Failed to open gpio value for writing!\n"); + close(fd); + return (-1); + } + + if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) + { + fprintf(stderr, "Failed to write value!\n"); + close(fd); + return (-1); + } + close(fd); + return (0); +} diff --git a/Central_Control_Board/JaeYukBookkom.h b/Central_Control_Board/JaeYukBookkom.h new file mode 100644 index 0000000000000000000000000000000000000000..94c1a7712fa8a0b4175560fe299b26c7e6b57d5a --- /dev/null +++ b/Central_Control_Board/JaeYukBookkom.h @@ -0,0 +1,29 @@ +#ifndef __JaeYukBookkom__ +#define __JaeYukBookkom__ +#define MAX_CONNECT_BOARD 3 +#define IN 0 +#define OUT 1 +#define LOW 0 +#define HIGH 1 +#define BUFFER_MAX 3 +#define DIRECTION_MAX 256 +#define VALUE_MAX 256 +// GPIO PIN Settings + +#define BUTTON_LED 26 +#define BUTTON_OUT 19 +#define BUTTON_IN 13 +#define TIMER_OUT 27 +#define TIMER_IN 22 + +#define SPEAKER 0 // PWM 0 + +enum WeatherCast +{ + Sunny = 0, + Cloud, + Windy, + Rain, + Snow +}; +#endif diff --git a/Central_Control_Board/Kim_JangYeon_202126904.zip b/Central_Control_Board/Kim_JangYeon_202126904.zip new file mode 100644 index 0000000000000000000000000000000000000000..b6b8e4763246684b4baa3279dd24cf7b79b4a929 Binary files /dev/null and b/Central_Control_Board/Kim_JangYeon_202126904.zip differ diff --git a/Central_Control_Board/LCD.c b/Central_Control_Board/LCD.c new file mode 100644 index 0000000000000000000000000000000000000000..b9f429536978ecb5ccd848c44edf70a963b9fdf6 --- /dev/null +++ b/Central_Control_Board/LCD.c @@ -0,0 +1,189 @@ +#include "JaeYukBookkom.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <linux/i2c-dev.h> +#include "LCD.h" + +LCDDevice lcd1, lcd2, lcd_error; + +int LCD_init(void){ + LCDDevice **initlist = malloc(LCD_COUNT * sizeof(LCDDevice *)); + initlist[0] = &lcd1, initlist[1] = &lcd2, initlist[2] = &lcd_error; + + if (LCD_open(&lcd1, 0x23, BUS_NUMBER) < 0 || LCD_open(&lcd2, 0x26, BUS_NUMBER) < 0 || LCD_open(&lcd_error, 0x27, BUS_NUMBER) < 0) + { + return -1; + } + for (int i = 0; i < LCD_COUNT; i++) + { + LCD_write(initlist[i], 0x03, 0); + LCD_write(initlist[i], 0x03, 0); + LCD_write(initlist[i], 0x03, 0); + LCD_write(initlist[i], 0x02, 0); + LCD_write(initlist[i], LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE, 0); + LCD_write(initlist[i], LCD_DISPLAYCONTROL | LCD_DISPLAYON, 0); + LCD_write(initlist[i], LCD_CLEARDISPLAY, 0); + LCD_write(initlist[i], LCD_ENTRYMODESET | LCD_ENTRYLEFT, 0); + LCD_clear(initlist[i]); + LCD_display_string(initlist[i], "S A T ", 1); + LCD_display_string(initlist[i], " T R ", 2); + } + + usleep(5000000); + for (int i = 0; i < LCD_COUNT; i++) + { + LCD_clear(initlist[i]); + } + free(initlist); +} +void LCD_weather_prediction(float temp, float hyd, int weatherCode, int dust, int rainPer){ + LCD_clear(&lcd1); + char str[16]; + snprintf(str, 16, "%.1f C, %.1f %", temp, hyd); + LCD_display_string(&lcd1, str, 1); +/* + Sunny = 0, + Cloud, + Windy, + Rain, + Snow +*/ + if (weatherCode == Sunny) + snprintf(str, 16, "Sunny"); + else if (weatherCode == Cloud) + snprintf(str, 16, "Cloud"); + else if (weatherCode == Windy) + snprintf(str, 16, "Windy"); + else if (weatherCode == Rain) + snprintf(str, 16, "Rain"); + else if (weatherCode == Snow) + snprintf(str, 16, "Snow"); + LCD_display_string(&lcd1, str, 2); + LCD_clear(&lcd2); + if(dust > 100) + snprintf(str, 16, "dust: BAD(%d)",dust); + else + snprintf(str, 16, "dust: GOOD(%d)",dust); + LCD_display_string(&lcd2, str, 1); + + if(rainPer>60) + snprintf(str,16,"!!!!!rainPer: %d",rainPer); + else + snprintf(str,16,"rainPer : %d",rainPer); + LCD_display_string(&lcd2, str, 2); +} +void LCD_messaging_error(int errorcode){ + if (errorcode == 0) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Nothing Problem", 1); + LCD_display_string(&lcd_error, "Outdoor is GOOD", 2); + } + else if (errorcode == 1) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Raining", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 2) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Gas", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 3) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Raining, Dust", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 4) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Noisy", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 5) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Raining, Noisy", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 6) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Dust, Noisy", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 7) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Rain,Dust,Noisy", 1); + LCD_display_string(&lcd_error, "Outdoor is BAD", 2); + } + else if (errorcode == 8) + { + LCD_clear(&lcd_error); + LCD_display_string(&lcd_error, "Danger", 1); + LCD_display_string(&lcd_error, "Check Window", 2); + } +} +int LCD_open(LCDDevice *dev, int addr, int bus){ + char filename[20]; + snprintf(filename, 19, "/dev/i2c-%d", bus); + dev->file = open(filename, O_RDWR); + if (dev->file < 0) + { + perror("Failed to open the i2c bus"); + return -1; + } + dev->addr = addr; + dev->bus = bus; + if (ioctl(dev->file, I2C_SLAVE, dev->addr) < 0) + { + perror("Failed to acquire bus access and/or talk to slave"); + return -1; + } + return 0; +} +void LCDdispose(void){ + close(lcd1.file); + close(lcd2.file); + close(lcd_error.file); +} +void i2c_write_cmd(LCDDevice *dev, uint8_t cmd){ + if (write(dev->file, &cmd, 1) != 1) + perror("Failed to write command to the i2c bus"); + usleep(100); +} +void LCD_strobe_signal(LCDDevice *dev, uint8_t data){ // strobe signal: notify send data + i2c_write_cmd(dev, data | En | LCD_BACKLIGHT); + usleep(500); + i2c_write_cmd(dev, ((data & ~En) | LCD_BACKLIGHT)); + usleep(100); +} +void LCD_write(LCDDevice *dev, uint8_t cmd, uint8_t mode){ + i2c_write_cmd(dev, mode | (cmd & 0xF0) | LCD_BACKLIGHT); + LCD_strobe_signal(dev, mode | (cmd & 0xF0)); + i2c_write_cmd(dev, mode | ((cmd << 4) & 0xF0) | LCD_BACKLIGHT); + LCD_strobe_signal(dev, mode | ((cmd << 4) & 0xF0)); +} +void LCD_display_string(LCDDevice *dev, const char *string, int line){ + if(line == 1) LCD_write(dev, 0x80, 0); + else if(line == 2) LCD_write(dev, 0xC0, 0); + + while (*string){ + LCD_write(dev, *string++, Rs); + } +} +void LCD_clear(LCDDevice *dev) //clear display +{ + LCD_write(dev, LCD_CLEARDISPLAY, 0); + LCD_write(dev, LCD_RETURNHOME, 0); +} + diff --git a/Central_Control_Board/LCD.h b/Central_Control_Board/LCD.h new file mode 100644 index 0000000000000000000000000000000000000000..dd5c27aebaad1786150dd668f57f91b0616ad94e --- /dev/null +++ b/Central_Control_Board/LCD.h @@ -0,0 +1,65 @@ +// LCD Instruction +#define LCD_CLEARDISPLAY 0x01 +#define LCD_RETURNHOME 0x02 +#define LCD_ENTRYMODESET 0x04 +#define LCD_DISPLAYCONTROL 0x08 +#define LCD_CURSORSHIFT 0x10 +#define LCD_FUNCTIONSET 0x20 +#define LCD_SETCGRAMADDR 0x40 +#define LCD_SETDDRAMADDR 0x80 + +// Display mode +#define LCD_ENTRYRIGHT 0x00 +#define LCD_ENTRYLEFT 0x02 +#define LCD_ENTRYSHIFTINCREMENT 0x01 +#define LCD_ENTRYSHIFTDECREMENT 0x00 + +// Display on/off Control +#define LCD_DISPLAYON 0x04 +#define LCD_DISPLAYOFF 0x00 +#define LCD_CURSORON 0x02 +#define LCD_CURSOROFF 0x00 +#define LCD_BLINKON 0x01 +#define LCD_BLINKOFF 0x00 + +// Display Cursor Movement +#define LCD_DISPLAYMOVE 0x08 +#define LCD_CURSORMOVE 0x00 +#define LCD_MOVERIGHT 0x04 +#define LCD_MOVELEFT 0x00 + +// Mode Setting +#define LCD_4BITMODE 0x00 +#define LCD_2LINE 0x08 +#define LCD_5x8DOTS 0x00 + +// Using BackLight +#define LCD_BACKLIGHT 0x08 +#define LCD_NOBACKLIGHT 0x00 + +// GPIO Bit Settings +#define En 0b00000100 // Enable +#define Rw 0b00000010 // ReadWrite +#define Rs 0b00000001 // Register Select + +//Number of LCD Setting +#define LCD_COUNT 3 +// I2C Bus Number Setting +#define BUS_NUMBER 1 + +typedef struct { + int addr; + int bus; + int file; +} LCDDevice; + +int LCD_init(void); +void LCD_weather_prediction(float temp, float hyd, int weatherCode, int dust, int rainPer); +void LCD_messaging_error(int errorcode); +int LCD_open(LCDDevice *dev, int addr, int bus); +void LCDdispose(void); +void i2c_write_cmd(LCDDevice *dev, uint8_t cmd); +void LCD_strobe_signal(LCDDevice *dev, uint8_t data); +void LCD_write(LCDDevice *dev, uint8_t cmd, uint8_t mode); +void LCD_display_string(LCDDevice *dev, const char *string, int line); +void LCD_clear(LCDDevice *dev); \ No newline at end of file diff --git a/Central_Control_Board/Networking.c b/Central_Control_Board/Networking.c new file mode 100644 index 0000000000000000000000000000000000000000..f796b4da90f679f5f6513dccaacc0d0d6f4a762f --- /dev/null +++ b/Central_Control_Board/Networking.c @@ -0,0 +1,66 @@ +#include "JaeYukBookkom.h" +#include <arpa/inet.h> +#include<stdlib.h> +#include<stdio.h> +#include<string.h> +void ServerInit(int *serv_sock, int port); +int max(int *list, int length); +void error_handling(char *message); + +int clnt_sock[MAX_CONNECT_BOARD]; +int serv_sock = -1, max_fd; +fd_set clients_socket, clients; + +void ServerInit(int *serv_sock, int port){ + for (int i = 0; i < MAX_CONNECT_BOARD; i++){ + clnt_sock[i] = -1; + } + struct sockaddr_in serv_addr; + struct sockaddr_in clnt_addr[MAX_CONNECT_BOARD]; + socklen_t clnt_addr_size; + *serv_sock = socket(PF_INET, SOCK_STREAM, 0); + if (*serv_sock == -1) + error_handling("socket() error"); + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); + serv_addr.sin_port = htons(port); + + if (bind(*serv_sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) + error_handling("bind() error"); + + if (listen(*serv_sock, 5) == -1) + error_handling("listen() error"); + for (int i = 0; i < MAX_CONNECT_BOARD; i++){ + if (clnt_sock[i] < 0){ + clnt_addr_size = sizeof(clnt_addr[i]); + clnt_sock[i] = + accept(*serv_sock, (struct sockaddr *)&clnt_addr[i], &clnt_addr_size); + if (clnt_sock[i] == -1) + error_handling("accept() error"); + } + printf("Connection established with client %d\n",i); + } + printf("Connection End\n"); + + FD_ZERO(&clients_socket); + for (int i = 0; i < MAX_CONNECT_BOARD; i++){ + FD_SET(clnt_sock[i], &clients_socket); + } + + max_fd = max(clnt_sock, MAX_CONNECT_BOARD); +} +int max(int *list, int length){ + int temp = list[0]; + for (int i = 1; i < length; i++){ + if (list[i - 1] < list[i]) + temp = list[i]; + } + return temp; +} +void error_handling(char *message){ + fputs(message, stderr); + fputc('\n', stderr); + exit(1); +} diff --git a/Central_Control_Board/PWM.c b/Central_Control_Board/PWM.c new file mode 100644 index 0000000000000000000000000000000000000000..ccb59d2cc9f189d564bb6c77065ad125abe99e11 --- /dev/null +++ b/Central_Control_Board/PWM.c @@ -0,0 +1,155 @@ +#include "JaeYukBookkom.h" +#include <pthread.h> +#include <fcntl.h> +#include <stdio.h> + +static int PWMExport(int pwmnum); +static int PWMUnExport(int pwmnum); +static int PWMEnable(int pwmnum); +static int PWMWritePeriod(int pwmnum, int value); +static int PWMWriteDutyCycle(int pwmnum, int value); +int PWMInit(int pwm, int period, int dutycycle); +void *bozzering(void *arg); +void boozer_Warning(void); +void SpeakerUnExport(void *args); + +int boozer_on = 0; +pthread_t *boozerThread; +int PWMInit(int pwm, int period, int dutycycle){ + PWMExport(pwm); + PWMWritePeriod(pwm, period); + PWMWriteDutyCycle(pwm, dutycycle); +} +void *bozzering(void *arg){ + PWMInit(SPEAKER,10000000,0); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pthread_cleanup_push(SpeakerUnExport, NULL); + while (1){ + for (int i = 0; i < 1000; i++){ + PWMWriteDutyCycle(SPEAKER, i * 10000); + usleep(1000); + } + for (int i = 1000; i > 0; i--){ + PWMWriteDutyCycle(SPEAKER, i * 10000); + usleep(1000); + } + } + pthread_cleanup_pop(0); +} +void boozer_Warning(void){ + PWMInit(SPEAKER,10000000,0); + for (int i = 0; i < 1000; i++){ + PWMWriteDutyCycle(SPEAKER, i * 10000); + usleep(1000); + } + for (int i = 1000; i > 0; i--){ + PWMWriteDutyCycle(SPEAKER, i * 10000); + usleep(1000); + } + PWMUnExport(SPEAKER); +} +static int PWMExport(int pwmnum){ + char buffer[BUFFER_MAX]; + int fd, byte; + + // TODO: Enter the export path. + fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY); + if (-1 == fd){ + fprintf(stderr, "Failed to open export for export!\n"); + return (-1); + } + + byte = snprintf(buffer, BUFFER_MAX, "%d", pwmnum); + write(fd, buffer, byte); + close(fd); + + usleep(1000000); + return (0); +} +static int PWMUnExport(int pwmnum){ + char buffer[BUFFER_MAX]; + int fd, byte; + // TODO: Enter the export path. + fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY); + if (-1 == fd){ + fprintf(stderr, "Failed to open export for export!\n"); + return (-1); + } + byte = snprintf(buffer, BUFFER_MAX, "%d", pwmnum); + write(fd, buffer, byte); + close(fd); + + usleep(1000); + return (0); +} +void SpeakerUnExport(void *args){ + PWMUnExport(SPEAKER); + free(boozerThread); + sleep(1); +} +static int PWMEnable(int pwmnum){ + static const char s_enable_str[] = "1"; + + char path[DIRECTION_MAX]; + int fd; + + // TODO: Enter the enable path. + snprintf(path, DIRECTION_MAX, "/sys/class/pwm/pwmchip0/pwm0/enable", pwmnum); + fd = open(path, O_WRONLY); + if (-1 == fd){ + fprintf(stderr, "Failed to open in enable!\n"); + return -1; + } + + write(fd, s_enable_str, strlen(s_enable_str)); + close(fd); + + return (0); +} +static int PWMWritePeriod(int pwmnum, int value){ + char s_value_str[VALUE_MAX]; + char path[VALUE_MAX]; + int fd, byte; + + // TODO: Enter the period path. + snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/period", pwmnum); + fd = open(path, O_WRONLY); + if (-1 == fd){ + fprintf(stderr, "Failed to open in period!\n"); + return (-1); + } + byte = snprintf(s_value_str, VALUE_MAX, "%d", value); + + if (-1 == write(fd, s_value_str, byte)){ + fprintf(stderr, "Failed to write value in period!\n"); + close(fd); + return -1; + } + close(fd); + + return (0); +} +static int PWMWriteDutyCycle(int pwmnum, int value) +{ + char s_value_str[VALUE_MAX]; + char path[VALUE_MAX]; + int fd, byte; + + // TODO: Enter the duty_cycle path. + snprintf(path, VALUE_MAX, "/sys/class/pwm/pwmchip0/pwm0/duty_cycle", pwmnum); + fd = open(path, O_WRONLY); + if (-1 == fd){ + fprintf(stderr, "Failed to open in duty cycle!\n"); + return (-1); + } + byte = snprintf(s_value_str, VALUE_MAX, "%d", value); + + if (-1 == write(fd, s_value_str, byte)){ + fprintf(stderr, "Failed to write value in duty cycle!\n"); + close(fd); + return -1; + } + close(fd); + + return (0); +} diff --git a/Central_Control_Board/WheatherAPI/.gitkeep b/Central_Control_Board/WheatherAPI/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/Central_Control_Board/WheatherAPI/Wheather.c b/Central_Control_Board/WheatherAPI/Wheather.c new file mode 100644 index 0000000000000000000000000000000000000000..9b58bd78b7871d8df924b72c6e7b2fb1bc05cdcd --- /dev/null +++ b/Central_Control_Board/WheatherAPI/Wheather.c @@ -0,0 +1,75 @@ +#include <arpa/inet.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <sys/select.h> +#include <sys/time.h> + +#define BUFFER_MAX 3 +#define DIRECTION_MAX 256 +#define VALUE_MAX 256 + +#define IN 0 +#define OUT 1 + +#define LOW 0 +#define HIGH 1 +void error_handling(char *message){ + fputs(message, stderr); + fputc('\n', stderr); + exit(1); +} + +int main(int argc, char *argv[]){ + int sock; + struct sockaddr_in serv_addr; + int str_len; + if (argc != 2){ + printf("Usage : %s <port>\n", argv[0]); + exit(1); + } + + sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock == -1) + error_handling("socket() error"); + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + serv_addr.sin_port = htons(atoi(argv[1])); + + if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) + error_handling("connect() error"); + + printf("Connection established\n"); + + char str[50]; + while (1) + { + float temperature, hyderation; + int dust, rainPer; + int weatherCode; + printf("input temperature, hyderation: "); + scanf("%f %f",&temperature,&hyderation); + printf("input Whether prediction(1.Sunny,2.Cloud,3.Windy,4.Rain,5.Snow)\n:"); + scanf("%d",&weatherCode); + printf("input dust, rainPer:"); + scanf("%d %d",&dust,&rainPer); + + snprintf(str, 50,"%.2f %.2f %d %d %d", temperature, hyderation, weatherCode, dust, rainPer); + int bytes_written = write(sock, str, sizeof(str)); + if (bytes_written == -1) + error_handling("write() error"); + usleep(500 * 100); + printf("Write success: %s\n",str); + } + + close(sock); + + return (0); +} \ No newline at end of file diff --git a/Central_Control_Board/WheatherAPI/readme.txt b/Central_Control_Board/WheatherAPI/readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..df09f7f292ccbf61b1d630de12c92438c2260c2f --- /dev/null +++ b/Central_Control_Board/WheatherAPI/readme.txt @@ -0,0 +1,2 @@ +this Wheather.c is assuming that Wheather API. +this program communicate with central.c by socket communication assuming that webSocket. \ No newline at end of file diff --git a/Central_Control_Board/WheatherAPI/whe b/Central_Control_Board/WheatherAPI/whe new file mode 100644 index 0000000000000000000000000000000000000000..60ab16999a1a151484dff9e5b4203604bd3860df Binary files /dev/null and b/Central_Control_Board/WheatherAPI/whe differ diff --git a/Central_Control_Board/central b/Central_Control_Board/central new file mode 100644 index 0000000000000000000000000000000000000000..9503b2fb6097945084f9920f08d96971d5e73413 Binary files /dev/null and b/Central_Control_Board/central differ diff --git a/Central_Control_Board/readme.txt b/Central_Control_Board/readme.txt new file mode 100644 index 0000000000000000000000000000000000000000..563dc27a6ddd2da2eeaf39e0aaa1e8c1a859f9ba --- /dev/null +++ b/Central_Control_Board/readme.txt @@ -0,0 +1,4 @@ +main code: Central_Main, Excute file: central. +you should connect by socket communication in order of (0: Sensing Board, 1: Operation Board, 2: WeatherAPI) +please excute 1)this board - central ==> 2) Sensing Board - main ==> 3) operation board - actuabor + ==> 4)this board - WeatherAPI - whe \ No newline at end of file