156 lines
4.6 KiB
C
156 lines
4.6 KiB
C
/* MQTT (over TCP) Example
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include "esp_wifi.h"
|
|
#include "esp_system.h"
|
|
#include "nvs_flash.h"
|
|
#include "esp_event.h"
|
|
#include "esp_netif.h"
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/queue.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
#include "lwip/dns.h"
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "esp_log.h"
|
|
#include "mqtt_client.h"
|
|
#include "driver/gpio.h"
|
|
#include "bsp.h"
|
|
|
|
static const char *TAG = "MQTT_EXAMPLE";
|
|
|
|
#define BLINK_GPIO 2
|
|
static uint8_t s_led_state = 0;
|
|
|
|
|
|
|
|
/*
|
|
* @brief Event handler registered to receive MQTT events
|
|
*
|
|
* This function is called by the MQTT client event loop.
|
|
*
|
|
* @param handler_args user data registered to the event.
|
|
* @param base Event base for the handler(always MQTT Base in this example).
|
|
* @param event_id The id for the received event.
|
|
* @param event_data The data for the event, esp_mqtt_event_handle_t.
|
|
*/
|
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
|
{
|
|
switch (QUISUISJE){
|
|
case BATEAU:
|
|
mqtt_event_handler_bateau(handler_args,base,event_id,event_data);
|
|
break;
|
|
case RADIO_BATEAU:
|
|
mqtt_event_handler_radio_bateau(handler_args,base,event_id,event_data);
|
|
break;
|
|
case VOITURE:
|
|
mqtt_event_handler_voiture(handler_args,base,event_id,event_data);
|
|
break;
|
|
case RADIO_VOITURE:
|
|
mqtt_event_handler_radio_voiture(handler_args,base,event_id,event_data);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void mqtt_app_start(void)
|
|
{
|
|
esp_mqtt_client_config_t mqtt_cfg = {
|
|
.broker.address.uri = CONFIG_BROKER_URL,
|
|
};
|
|
|
|
|
|
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
|
|
/* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
|
|
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
|
esp_mqtt_client_start(client);
|
|
//*
|
|
int msg_id;
|
|
//uint16_t count=0;
|
|
char data[10];
|
|
while(1)
|
|
{
|
|
/*switch (QUISUISJE){
|
|
case BATEAU:
|
|
loop_bateau();
|
|
break;
|
|
case RADIO_BATEAU:
|
|
loop_radio_bateau();
|
|
break;
|
|
case VOITURE:
|
|
loop_voiture();
|
|
break;
|
|
case RADIO_VOITURE:
|
|
loop_radio_voiture();
|
|
break;
|
|
case STARTER_VOITURE:
|
|
loop_starter_voiture();
|
|
break;
|
|
default:
|
|
break;
|
|
}*/
|
|
s_led_state=!s_led_state;
|
|
gpio_set_level(BLINK_GPIO, s_led_state);
|
|
sprintf(data,"%i",s_led_state);
|
|
//if(MQTT_CONNECTED) {
|
|
msg_id = esp_mqtt_client_publish(client, "/topic/qos0",data, 0, 0, 0);
|
|
//}
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
}
|
|
//*/
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "[APP] Startup..");
|
|
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
|
|
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
|
|
|
esp_log_level_set("*", ESP_LOG_INFO);
|
|
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
|
|
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
|
|
esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE);
|
|
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
|
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);
|
|
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
// ESP_ERROR_CHECK(esp_netif_init());
|
|
// ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
* examples/protocols/README.md for more information about this function.
|
|
*/
|
|
ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
|
|
//gpio_reset_pin(BLINK_GPIO);
|
|
gpio_reset_pin(15);
|
|
/* Set the GPIO as a push/pull output */
|
|
//pio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
|
|
gpio_set_direction(15, GPIO_MODE_OUTPUT);
|
|
boardInit();
|
|
setupBoard();
|
|
loop_board();
|
|
//ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
|
//esp_wifi_set_ps(WIFI_PS_NONE) ;
|
|
//mqtt_app_start();
|
|
}
|