#include <stdio.h>
#include <stdbool.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_err.h>
#include "freertos/semphr.h"
#include "ultrasonic.h"
#include "esp_system.h"
#include <string.h> //strlen
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "mqtt_client.h" //provides important functions to connect with MQTT
//#include "protocol_examples_common.h" //important for running different protocols in code
#include "esp_event.h" //managing events of mqtt
//PROTÓTIPO DE FUNÇÕES=====================================================================
void app_main(void);
void setup();
void task_ultrasonic_test(void *pvParameters);
void task_control_leds(void *pvParameters);
void task_send_messages(void *distancia);
void task_i_am_alive(void *pvParameters);
void wifi_connect(void);
static void mqtt_event_handler(esp_mqtt_event_handle_t event);
static void mqtt_initialize(void);
//DEFINIÇÕES E CONSTATNTES=================================================================
#define delay(value) vTaskDelay(value/portTICK_PERIOD_MS)
#define ECHO_GPIO 12
#define TRIGGER_GPIO 13
#define MAX_DISTANCE_CM 500 // Maximum of 5 meters
#define LED_VERD GPIO_NUM_23
#define LED_AMAR GPIO_NUM_19
#define LED_VERM GPIO_NUM_4
enum estados_led {VERM, AMAR, VERD, INIC};
#define SSID "Wokwi-GUEST"
#define PASSPHARSE ""
//#define TCPServerIP "159.203.79.141"
//#define PORT 50000
#define ALIVE "alive"
static const char *id = "201811250039";//id dispositivo para cabeçalho
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static const char *TAG="LOG_MQTT";
//MÁSCARA PARA SAÍDAS====================================================================
#define GPIO_OUTPUT_PIN_SEL ((1ULL<<LED_VERD) | (1ULL<<LED_AMAR) | (1ULL<<LED_VERM))
//VARIÁVEIS==============================================================================
static TaskHandle_t xtask_handle_leds = NULL;
static TaskHandle_t xtask_handle_ultrasonic = NULL;
static TaskHandle_t xtask_handle_send_msg = NULL;
static TaskHandle_t xtask_handle_alive = NULL;
esp_mqtt_client_handle_t client;
enum estados_led estado;
float distance;
char distance_string[16] = "";
SemaphoreHandle_t mutex;
//FUNÇÕES E TASKS=======================================================================
void wifi_connect() {
wifi_config_t cfg = {
.sta = {
.ssid = SSID,
.password = PASSPHARSE,
},
};
ESP_ERROR_CHECK(esp_wifi_disconnect());
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg));
ESP_ERROR_CHECK(esp_wifi_connect());
}
static esp_err_t event_handler(void *ctx, system_event_t *event) {
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void) {
esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
}
void task_control_leds(void *pvParameters) {
while(true){
if(estado == VERM) {
gpio_set_level(LED_AMAR, 0);
gpio_set_level(LED_VERD, 0);
gpio_set_level(LED_VERM, 1);
}
else if(estado == VERD) {
gpio_set_level(LED_VERM, 0);
gpio_set_level(LED_AMAR, 0);
gpio_set_level(LED_VERD, 1);
}
else if(estado == AMAR) {
gpio_set_level(LED_VERM, 0);
gpio_set_level(LED_VERD, 0);
gpio_set_level(LED_AMAR, 1);
}
else {
gpio_set_level(LED_VERM, 1);
gpio_set_level(LED_VERD, 1);
gpio_set_level(LED_AMAR, 1);
}
}
}
void task_ultrasonic_test(void *pvParameters) {
ultrasonic_sensor_t sensor = {
.trigger_pin = TRIGGER_GPIO,
.echo_pin = ECHO_GPIO
};
ultrasonic_init(&sensor);
while (true) {
esp_err_t res = ultrasonic_measure(&sensor, MAX_DISTANCE_CM, &distance);
sprintf(distance_string, "%.2f ", distance);
if (res == ESP_OK) {
xSemaphoreTake(mutex, portMAX_DELAY);// pega o Mutex
vTaskSuspend(xtask_handle_leds); // Suspende a task dos leds
//Atualiza o estado dos leds
if(distance <= 1){
estado = VERM;
}
else if (distance <= 2.5){
estado = AMAR;
}
else {
estado = VERD;
}
printf("Nível do Reservatório: %s\n", distance_string);
vTaskResume(xtask_handle_leds); //reativa a task dos leds
vTaskResume(xtask_handle_send_msg); //reativa a task de send_msg
xSemaphoreGive(mutex); //libera o Mutex
} // Print error
else {
printf("Error %d: ", res);
switch (res) {
case ESP_ERR_ULTRASONIC_PING:
printf("Cannot ping (device is in invalid state)\n");
break;
case ESP_ERR_ULTRASONIC_PING_TIMEOUT:
printf("Ping timeout (no device found)\n");
break;
case ESP_ERR_ULTRASONIC_ECHO_TIMEOUT:
printf("Echo timeout (i.e. distance too big)\n");
break;
default:
printf("%s\n", esp_err_to_name(res));
}
}
delay(35000);
}
}
static void mqtt_event_handler(esp_mqtt_event_handle_t event){ //here esp_mqtt_event_handle_t is a struct which receieves struct event from mqtt app start funtion
esp_mqtt_client_handle_t client = event->client; //making obj client of struct esp_mqtt_client_handle_t and assigning it the receieved event client
if(event->event_id == MQTT_EVENT_CONNECTED) {
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
printf("conectado... mqtt\n");
}
else if(event->event_id == MQTT_EVENT_DISCONNECTED)
{
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); //if disconnected
}
else if(event->event_id == MQTT_EVENT_SUBSCRIBED)
{
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED");
}
else if(event->event_id == MQTT_EVENT_UNSUBSCRIBED) //when subscribed
{
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED");
}
else if(event->event_id == MQTT_EVENT_DATA)//when unsubscribed
{
ESP_LOGI(TAG, "MQTT_EVENT_DATA");
printf("Mensagem recebida -> %s\n", event->data);
}
else if(event->event_id == MQTT_EVENT_ERROR)//when any error
{
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
}
}
static void mqtt_initialize(void) {/*Depending on your website or cloud there could be more parameters in mqtt_cfg.*/
esp_log_level_set(TAG, ESP_LOG_VERBOSE);
const esp_mqtt_client_config_t mqtt_cfg = {
.host= "test.mosquitto.org",//"broker.hivemq.com", //Uniform Resource Identifier includes path,protocol
.port = 1883,
.transport = MQTT_TRANSPORT_OVER_TCP,
.event_handle=mqtt_event_handler //described above event handler
};
client=esp_mqtt_client_init(&mqtt_cfg); //sending struct as a parameter in init client function
esp_mqtt_client_start(client); //starting the process
}
void task_i_am_alive(void *pvParameters) {
while(true) {
delay(10000);
xSemaphoreTake(mutex, portMAX_DELAY);
int msg_pub_id = esp_mqtt_client_publish(client, "topic/exemplo", ALIVE, 0, 0, 0);
printf("Enviando: %s\n", ALIVE);
xSemaphoreGive(mutex);
}
}
void task_send_messages(void *distancia) {
while(1) {
int msg_pub_id = esp_mqtt_client_publish(client, "topic/exemplo", distancia, 0, 0, 0);
printf("Enviando Distância -> %s\n", distancia);
vTaskDelay(100 / portTICK_RATE_MS);
vTaskSuspend(NULL);
}
}
void setup() {
//CONFIGURANDO SAÍDAS===================================================
gpio_config_t output_config = {};//variável descritora
output_config.intr_type = GPIO_INTR_DISABLE;//desabilita interrupção externa
output_config.mode = GPIO_MODE_OUTPUT;//configura GPIO como saídas
output_config.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;//carrega GPIOs configuradas
output_config.pull_up_en = 0;//desativa pull_up
output_config.pull_down_en = 0;//desativa pull_down
gpio_config(&output_config);//configura GPIO conforme descritor
estado = INIC;
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_event_group = xEventGroupCreate();
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
initialise_wifi();
xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,false,true,portMAX_DELAY);
mqtt_initialize();
}
void app_main() {
setup();
mutex = xSemaphoreCreateMutex();
int msg_sub_id = esp_mqtt_client_subscribe(client, "topic/exemplo2", 0);
xTaskCreate(&task_send_messages,"send messages",2048, (void *) distance_string, 5, &xtask_handle_send_msg);
xTaskCreate(&task_control_leds, "LEDs", 2048, NULL, 3, &xtask_handle_leds);
xTaskCreate(&task_ultrasonic_test, "ultrasonic_test", configMINIMAL_STACK_SIZE * 3, NULL, 5, &xtask_handle_ultrasonic);
xTaskCreate(&task_i_am_alive, "device_alive", 4096, NULL, 5, &xtask_handle_alive);
}