Created
July 26, 2018 20:33
-
-
Save Sedwin97/308ab1f562fbd9984a67023ade0c194e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <unistd.h> | |
#include <stddef.h> | |
#include <string.h> | |
#include "esp_wifi.h" | |
#include "esp_system.h" | |
#include "esp_pm.h" | |
#include "nvs_flash.h" | |
#include "esp_event_loop.h" | |
#include "freertos/FreeRTOS.h" | |
#include "freertos/task.h" | |
#include "freertos/semphr.h" | |
#include "freertos/queue.h" | |
#include "freertos/event_groups.h" | |
#include "lwip/sockets.h" | |
#include "lwip/dns.h" | |
#include "lwip/netdb.h" | |
#include "esp_log.h" | |
#include "mqtt_client.h" | |
/*set the ssid and password via "make menuconfig"*/ | |
#define DEFAULT_SSID CONFIG_WIFI_SSID | |
#define DEFAULT_PASS CONFIG_WIFI_PASSWORD | |
#define DEFAULT_LISTEN_INTERVAL CONFIG_WIFI_LISTEN_INTERVAL | |
#if CONFIG_POWER_SAVE_MIN_MODEM | |
#define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM | |
#elif CONFIG_POWER_SAVE_MAX_MODEM | |
#define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM | |
#elif CONFIG_POWER_SAVE_NONE | |
#define DEFAULT_PS_MODE WIFI_PS_NONE | |
#else | |
#define DEFAULT_PS_MODE WIFI_PS_NONE | |
#endif /*CONFIG_POWER_SAVE_MODEM*/ | |
static const char *TAG = "MQTT_SAMPLE"; | |
static const char *TAG_PM = "power_save"; | |
static EventGroupHandle_t wifi_event_group; | |
const static int CONNECTED_BIT = BIT0; | |
char *topic = "esp/test"; | |
esp_mqtt_client_handle_t client; | |
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) | |
{ | |
esp_mqtt_client_handle_t client = event->client; | |
int msg_id = 0; | |
// your_context_t *context = event->context; | |
switch (event->event_id) { | |
case MQTT_EVENT_CONNECTED: | |
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); | |
esp_mqtt_client_publish(client, topic, "hello world", 0, 0, 0); | |
//msg_id = esp_mqtt_client_subscribe(client, topic, 0); | |
//ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); | |
break; | |
case MQTT_EVENT_DISCONNECTED: | |
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); | |
break; | |
case MQTT_EVENT_SUBSCRIBED: | |
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); | |
msg_id = esp_mqtt_client_publish(client, topic, "hello world", 0, 0, 0); | |
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); | |
break; | |
case MQTT_EVENT_UNSUBSCRIBED: | |
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); | |
break; | |
case MQTT_EVENT_PUBLISHED: | |
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); | |
//esp_wifi_stop(); | |
break; | |
case MQTT_EVENT_DATA: | |
ESP_LOGI(TAG, "MQTT_EVENT_DATA"); | |
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); | |
printf("DATA=%.*s\r\n", event->data_len, event->data); | |
break; | |
case MQTT_EVENT_ERROR: | |
ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); | |
break; | |
} | |
return ESP_OK; | |
} | |
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) | |
{ | |
switch(event->event_id) { | |
case SYSTEM_EVENT_STA_START: | |
ESP_LOGI(TAG_PM, "SYSTEM_EVENT_STA_START"); | |
ESP_ERROR_CHECK(esp_wifi_connect()); | |
break; | |
case SYSTEM_EVENT_STA_GOT_IP: | |
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); | |
ESP_LOGI(TAG_PM, "SYSTEM_EVENT_STA_GOT_IP"); | |
ESP_LOGI(TAG_PM, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); | |
break; | |
case SYSTEM_EVENT_STA_DISCONNECTED: | |
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); | |
ESP_LOGI(TAG_PM, "SYSTEM_EVENT_STA_DISCONNECTED"); | |
ESP_ERROR_CHECK(esp_wifi_connect()); | |
break; | |
default: | |
break; | |
} | |
return ESP_OK; | |
} | |
static void wifi_init(void) | |
{ | |
tcpip_adapter_init(); | |
wifi_event_group = xEventGroupCreate(); | |
ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); | |
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | |
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); | |
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); | |
wifi_config_t wifi_config = { | |
.sta = { | |
.ssid = DEFAULT_SSID, | |
.password = DEFAULT_PASS, | |
.listen_interval = DEFAULT_LISTEN_INTERVAL, | |
}, | |
}; | |
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); | |
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); | |
ESP_LOGI(TAG, "start the WIFI SSID:[%s] password:[%s]", CONFIG_WIFI_SSID, "******"); | |
ESP_ERROR_CHECK(esp_wifi_start()); | |
ESP_LOGI(TAG, "Waiting for wifi"); | |
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); | |
ESP_LOGI(TAG, "esp_wifi_set_ps()."); | |
esp_wifi_set_ps(DEFAULT_PS_MODE); | |
} | |
static void mqtt_app_start(void) | |
{ | |
const esp_mqtt_client_config_t mqtt_cfg = { | |
.uri = "", | |
.event_handle = mqtt_event_handler, | |
.host = "192.168.2.121", | |
.username = "pi_broker", | |
.password = "raspberry", | |
.port = 1883 | |
// .user_context = (void *)your_context | |
}; | |
//esp_mqtt_client_handle_t | |
client = esp_mqtt_client_init(&mqtt_cfg); | |
//esp_mqtt_client_start(client); | |
} | |
void app_main() | |
{ | |
ESP_LOGI(TAG, "[APP] Startup.."); | |
ESP_LOGI(TAG, "[APP] Free memory: %d 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("TRANSPORT_TCP", ESP_LOG_VERBOSE); | |
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); | |
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); | |
esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); | |
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 ); | |
#if CONFIG_PM_ENABLE | |
// Configure dynamic frequency scaling: maximum frequency is set in sdkconfig, | |
// minimum frequency is XTAL. | |
rtc_cpu_freq_t max_freq; | |
rtc_clk_cpu_freq_from_mhz(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ, &max_freq); | |
esp_pm_config_esp32_t pm_config = { | |
.max_cpu_freq = max_freq, | |
.min_cpu_freq = RTC_CPU_FREQ_XTAL, | |
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE | |
.light_sleep_enable = true | |
#endif | |
}; | |
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) ); | |
#endif // CONFIG_PM_ENABLE | |
wifi_init(); | |
mqtt_app_start(); | |
while(1) { | |
esp_mqtt_client_start(client); | |
ESP_LOGI("Checking: ", "just started"); | |
sleep(1); | |
esp_mqtt_client_publish(client, topic, "hello world", 0, 0, 0); | |
ESP_LOGI("Checking: ", "just published"); | |
esp_mqtt_client_stop(client); | |
ESP_LOGI("Checking: ", "just stopped"); | |
sleep(2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment