diff --git a/IHM_Local/main.c b/IHM_Local/main.c index 1820844..8506ce4 100644 --- a/IHM_Local/main.c +++ b/IHM_Local/main.c @@ -8,36 +8,66 @@ #include "utils.h" // await #include "menu.h" // menu #include "help.h" -//#include -#include -//#include +#include #include "mqtt.h" #include "queue.h" - -//USE WIRINGPI PIN NUMBERS -#define LCD_RS 13 //Register select pin -#define LCD_E 18 //Enable Pin -#define LCD_D4 21 //Data pin D4 -#define LCD_D5 24 //Data pin D5 -#define LCD_D6 26 //Data pin D6 -#define LCD_D7 27 //Data pin D7 +#include // Botões #define BUTTON_1 19 #define BUTTON_2 23 #define BUTTON_3 25 +void *myThreadFun(void *vargp) { + + ContextData *data = vargp; + + char cmd[20] = "\0\0\0\0"; + + printf("Não morreu ainda: \n"); + + while(1) { + /** + * Lê o sensor analogico + */ + if (data->analogic->type == Analogic) { // Se existe um sensor analogico + struct queue_head *leituras = &(data->analogic->values); + cmd[0] = GET_ANALOG_INPUT_VALUE; + cmd[1] = GET_ANALOG_INPUT_VALUE; + //printf("Não analog cmd: \n"); + publish(data->client, "comand", (char*) cmd); + await(20); + char history[255]; + + //printf("Não anl his: \n"); + publish(data->client, "analogic/history", queue_to_string(leituras, &history)); + await(20); + } + + /** + */ + for (int i = 0; i < *data->digitalQtd; i++) { + struct queue_head *leituras = &(data->digitals[i].values); + cmd[0] = GET_DIGITAL_INPUT_VALUE; + cmd[1] = (char) data->digitals[i].id; + //printf("Não dig cmd: \n"); + publish(data->client, "comand", (char*) cmd); // Solicita leitura do sensor + char history[255]; + char topic[255]; + sprintf(topic, "%i/history", cmd[1]); + //printf("Não dig his: \n"); + await(20); + publish(data->client, topic, queue_to_string(leituras, &history)); + await(20); // Aguarda comando ser processado + //serialReadBytes(ler); // lê resposta + } + } +} + MQTTClient client; -/*int lcd; -void escreverEmDuasLinhas(char linha1[], char linha2[]) { - lcdHome(lcd); - lcdPuts(lcd, linha1); - lcdPosition(lcd,0,1); - lcdPuts(lcd, linha2); -}*/ -//int isPressed(int btt); +int isPressed(int btt); int get_number_of_digital_ios(); char *getSubstring(char *dst, const char *src, size_t start, size_t end); @@ -55,31 +85,35 @@ int add_digital_sensor(char *sensor_info, Sensor *digital); */ int main(int argc, char *argv[]) { // Configuração inicial - init_display(); // configura o display - init_display(); - init_display(); - clear_display(); // Limpa o conteudo do diaplay - write_string("Iniciando"); - uart_configure(); // configura a uart - //wiringPiSetup(); - //lcd = lcdInit (2, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7,0,0,0,0); - //pinMode(BUTTON_1,INPUT); - //pinMode(BUTTON_2,INPUT); - //pinMode(BUTTON_3,INPUT); - - //escreverEmDuasLinhas(" MI - SD ", "Protocolo MQTT"); - /*while (1) { +// init_display(); // configura o display + // init_display(); + // init_display(); + // clear_display(); // Limpa o conteudo do diaplay + // write_string("Iniciando"); +// uart_configure(); // configura a uart + wiringPiSetup(); + init_lcd(); + + pinMode(BUTTON_1,INPUT); + pinMode(BUTTON_2,INPUT); + pinMode(BUTTON_3,INPUT); + + /* + while (1) { //printf("no loop\n\n"); isPressed(BUTTON_1); isPressed(BUTTON_2); + isPressed(BUTTON_3); //await(1000); }*/ + MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; conn_opts.username = USERNAME; conn_opts.password = PASSWORD; + Sensor analogico; Sensor digital[31]; int digitalQtd = 0; // Quantidade de sensores digitais selecionados @@ -294,53 +328,188 @@ int main(int argc, char *argv[]) { char cmd[2] = "OO"; + pthread_t thread_id; + pthread_create(&thread_id, NULL, myThreadFun, &data);//pthread_join(thread_id, NULL); + + struct timeval start; + long long current_time; + long long elapsed_milliseconds; + int exibitionTime = 2000; + long long initial_time; + while(1) { /** * Lê o sensor analogico */ + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; // tempo inicial em ms if (analogico.type == Analogic) { // Se existe um sensor analogico struct queue_head *leituras = &(analogico.values); int val = peek_value(leituras); print_sensor_to_console(analogico.name, val); - send_command(GET_ANALOG_INPUT_VALUE, GET_ANALOG_INPUT_VALUE); + //send_command(GET_ANALOG_INPUT_VALUE, GET_ANALOG_INPUT_VALUE); cmd[0] = GET_ANALOG_INPUT_VALUE; cmd[1] = GET_ANALOG_INPUT_VALUE; // solicita valor para a ESP - publish(data.client, "comand", (char*) cmd); + //publish(data.client, "comand", (char*) cmd); // publica historico de valores char history[255]; - publish(data.client, "analogic/history", queue_to_string(leituras, &history)); - await(1000); + //publish(data.client, "analogic/history", queue_to_string(leituras, &history)); + //await(2000); //serialReadBytes(ler); // lê resposta //analogico.value = command_to_int(ler[1], ler[2]); + elapsed_milliseconds = 0; + while(elapsed_milliseconds <= exibitionTime) { + gettimeofday(&start, NULL); + current_time = start.tv_sec*1000LL + start.tv_usec/1000; // tempo atual em ms + elapsed_milliseconds = current_time - initial_time; // tempo decorrido + if (isPressed(BUTTON_1)) { + printf("Historico do analogico\n\n"); + data.update_blocked = 1; + int cdn = 1; + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; + int histIndex = 0; + while (cdn) + { + gettimeofday(&start, NULL); + current_time = start.tv_sec*1000LL + start.tv_usec/1000; // tempo atual em ms + elapsed_milliseconds = current_time - initial_time; + + if (isPressed(BUTTON_2)) { + cdn = 0; + } + + if (elapsed_milliseconds >= 2000) { + //print + escreverEmDuasLinhas("hist.. analogico", " "); + elapsed_milliseconds = 0; + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; + //analogico.values. + + struct node * current_item = analogico.values.first_item; + + // iterando + int i = 0; + while (current_item != NULL) + { + if (histIndex >= analogico.values.number_of_items) { + histIndex = 0; + i = -1; + current_item = analogico.values.first_item; + } + if (histIndex == i) { + char hjk[16]; + sprintf(hjk, "%12i", current_item->value); + printf("valor do analog %i - em: %i - %i\n", current_item->value, histIndex, i); + escreverEmDuasLinhas("hist.. analogico", hjk); + current_item = NULL; + histIndex++; + i = -1; + } else { + current_item = current_item->next; + } + i++; + + } + } + } + + } + //if (elapsed_milliseconds > 2000) + } + data.update_blocked = 0; + initial_time = current_time; + escreverEmDuasLinhas(" ", " "); + printf("Saiu\n"); } /** */ + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; for (int i = 0; i < digitalQtd; i++) { struct queue_head *leituras = &(digital[i].values); int val = peek_value(leituras); - print_sensor_to_console(digital[i].name, val); - send_command(GET_DIGITAL_INPUT_VALUE, (char) digital[i].id); + print_sensor_to_console(digital[i].name, val); + //send_command(GET_DIGITAL_INPUT_VALUE, (char) digital[i].id); cmd[0] = GET_DIGITAL_INPUT_VALUE; cmd[1] = (char) digital[i].id; - publish(data.client, "comand", (char*) cmd); // Solicita leitura do sensor + //publish(data.client, "comand", (char*) cmd); // Solicita leitura do sensor char history[255]; char topic[255]; sprintf(topic, "%i/history", cmd[1]); - printf("Topico: %s\n", topic); - publish(data.client, topic, queue_to_string(leituras, &history)); - await(1000); // Aguarda comando ser processado + //printf("Topico: %s\n", topic); + //publish(data.client, topic, queue_to_string(leituras, &history)); + elapsed_milliseconds = 0; + while(elapsed_milliseconds <= exibitionTime) { + gettimeofday(&start, NULL); + current_time = start.tv_sec*1000LL + start.tv_usec/1000; // tempo atual em ms + elapsed_milliseconds = current_time - initial_time; // tempo decorrido + if (isPressed(BUTTON_1)) { + printf("Historico do digigal: %i\n\n", i); + data.update_blocked = 1; + int cdn2 = 1; + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; + int histIndex = 0; + while (cdn2) + { + gettimeofday(&start, NULL); + current_time = start.tv_sec*1000LL + start.tv_usec/1000; // tempo atual em ms + elapsed_milliseconds = current_time - initial_time; + + if (isPressed(BUTTON_2)) { + cdn2 = 0; + } + + if (elapsed_milliseconds >= 2000) { + //print + escreverEmDuasLinhas("hist... digital ", " "); + elapsed_milliseconds = 0; + gettimeofday(&start, NULL); + initial_time = start.tv_sec*1000LL + start.tv_usec/1000; + //analogico.values. + + struct node * current_item = digital[i].values.first_item; + + // iterando + int i = 0; + while (current_item != NULL) + { + if (histIndex >= analogico.values.number_of_items) { + histIndex = 0; + i = -1; + current_item = analogico.values.first_item; + } + if (histIndex == i) { + char hjk[16]; + sprintf(hjk, "%12i", current_item->value); + printf("valor do analog %i - em: %i - %i\n", current_item->value, histIndex, i); + escreverEmDuasLinhas("hist... digital ", hjk); + current_item = NULL; + histIndex++; + i = -1; + } else { + current_item = current_item->next; + } + i++; + + } + } + } + + } + //if (elapsed_milliseconds > 2000) + } + data.update_blocked = 0; + initial_time = current_time; + escreverEmDuasLinhas(" ", " "); + printf("Saiu digital\n"); // Aguarda comando ser processado //serialReadBytes(ler); // lê resposta - if (0) { // Se houve erro na leitura - printf("NodeMCU com problema. Endereço do sensor é inválido!\n"); - clear_display(); - write_string("NodeMCU com erro"); - } else { // Se lido com sucesso - //digital[i].value = ler[1] - '0'; // salva o valor lido - //print_sensor_to_console(digital[i].name, digital[i].value); // Exibe informações lidas - } + } if (analogico.type != Analogic && digitalQtd < 1) { // Se não houver nenhum sensor @@ -543,7 +712,7 @@ int add_digital_sensor(char *sensor_info, Sensor *digital) { } -/* + int isPressed(int gpio_number){ if(digitalRead(gpio_number) == 0) { await(100); @@ -554,4 +723,18 @@ int isPressed(int gpio_number){ } return 0; } -*/ \ No newline at end of file + + +/*int isPressed(int gpio_number){ + while(digitalRead(gpio_number) == 0) { + // + } + if() { + await(100); + if(digitalRead(gpio_number) == 0){ + printf("Pressed %d! \n", gpio_number); + return 1; + } + } + return 0; +}*/ \ No newline at end of file diff --git a/IHM_Local/mqtt.c b/IHM_Local/mqtt.c index 00c11fe..e6955b8 100644 --- a/IHM_Local/mqtt.c +++ b/IHM_Local/mqtt.c @@ -47,26 +47,38 @@ int on_message(void *context, char *topicName, int topicLen, MQTTClient_message // Atualiza o valor de um sensor digital if (response_type == ANALOG_LEVEL) { + int update = 1; + if (data->update_blocked == 1) { + update = 0; + } //printf("Atualizando valor do analogico\n"); //data->analogic->valuees = command_to_int(payload[1], payload[2]); - if (data->analogic->values.number_of_items >= data->analogic->values.max_lenght) { - pop(&data->analogic->values); + if (update) { + if (data->analogic->values.number_of_items >= data->analogic->values.max_lenght) { + pop(&data->analogic->values); + } + push(&data->analogic->values, command_to_int(payload[1], payload[2])); } - push(&data->analogic->values, command_to_int(payload[1], payload[2])); } // Atualiza valor de um dos sensores digitais if (response_type == DIGITAL_LEVEL) { - int digital_sensor_address = payload[2]; - for (int i = 0; i < *data->digitalQtd; i++) { - if (digital_sensor_address == data->digitals[i].id) { - //data->digitals[i].value = payload[1] - '0'; - if (data->digitals[i].values.number_of_items >= data->digitals[i].values.max_lenght) { - pop(&data->digitals[i].values); + int update = 1; + if (data->update_blocked == 1) { + update = 0; + } + if (update) { + int digital_sensor_address = payload[2]; + for (int i = 0; i < *data->digitalQtd; i++) { + if (digital_sensor_address == data->digitals[i].id) { + //data->digitals[i].value = payload[1] - '0'; + if (data->digitals[i].values.number_of_items >= data->digitals[i].values.max_lenght) { + pop(&data->digitals[i].values); + } + push(&data->digitals[i].values, payload[1] - '0'); + //printf("Sensor %i: %s\n", i + 1, data->digitals[i].name); + //printf("new val: %i \n", data->digitals[i].value); } - push(&data->digitals[i].values, payload[1] - '0'); - //printf("Sensor %i: %s\n", i + 1, data->digitals[i].name); - //printf("new val: %i \n", data->digitals[i].value); } } } diff --git a/IHM_Local/mqtt.h b/IHM_Local/mqtt.h index 29f4c8f..1509a73 100644 --- a/IHM_Local/mqtt.h +++ b/IHM_Local/mqtt.h @@ -3,7 +3,7 @@ #ifndef MQTT_H #define MQTT_H -#define MQTT_ADDRESS "192.168.1.2" +#define MQTT_ADDRESS "10.0.0.101" #define CLIENTID "98631145_c_client_paho_tcp" #define USERNAME "aluno" @@ -19,6 +19,7 @@ struct mqtt_context { Sensor *digitals; int *digitalQtd; MQTTClient_connectOptions *MQTT_CONFIG; + int update_blocked; }; typedef struct mqtt_context ContextData; diff --git a/IHM_Local/sensor.c b/IHM_Local/sensor.c index f6d793e..727cd0f 100644 --- a/IHM_Local/sensor.c +++ b/IHM_Local/sensor.c @@ -1,11 +1,26 @@ #include "display.h" // clear_display, write_string +#include #include // sprintf, printf void print_sensor_to_console(char *sensor_name, int sensor_value) { char str[16]; sprintf(str, "%9s: %4i", sensor_name, sensor_value); - clear_display(); + //clear_display(); //str[17] = '\0'; - write_string((char*) str); + //write_string((char*) str); //printf("%9s: %4i\n", sensor_name, sensor_value); + escreverEmDuasLinhas(str, " "); } + + +void escreverEmDuasLinhas(char linha1[], char linha2[]) { + lcdHome(lcd); + lcdPuts(lcd, linha1); + lcdPosition(lcd,0,1); + lcdPuts(lcd, linha2); +} + +void init_lcd() { + lcd = lcdInit (2, 16, 4, LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7,0,0,0,0); +} + diff --git a/IHM_Local/sensor.h b/IHM_Local/sensor.h index 2d5bbc0..5f0ad37 100644 --- a/IHM_Local/sensor.h +++ b/IHM_Local/sensor.h @@ -2,6 +2,16 @@ #ifndef SENSOR_H #define SENSOR_H +//USE WIRINGPI PIN NUMBERS - 103 +#define LCD_RS 13 //Register select pin +#define LCD_E 18 //Enable Pin +#define LCD_D4 21 //Data pin D4 +#define LCD_D5 24 //Data pin D5 +#define LCD_D6 26 //Data pin D6 +#define LCD_D7 27 //Data pin D7 + +int lcd; + enum sensor_type { Undefined, Analogic, Digital }; /** @@ -20,5 +30,7 @@ struct sensor_data { typedef struct sensor_data Sensor; void print_sensor_to_console(char *sensor_name, int sensor_value); +void escreverEmDuasLinhas(char linha1[], char linha2[]); +void init_lcd(); #endif