Skip to content

Commit

Permalink
remove user_config, add setup wifi via menuconfig and
Browse files Browse the repository at this point in the history
update espmqtt
remove degub.h, replace with esp_log
remove config run at 160MHz
  • Loading branch information
tuanpmt committed Aug 9, 2017
1 parent 1146f22 commit da0e976
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 202 deletions.
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,7 @@

`git clone --recursive https://github.com/tuanpmt/esp32-mqtt`

Run command `make menuconfig` to change SERIAL PORT configuration and the options at `Component config -> MQTT `:

- `CONFIG_MQTT_PROTOCOL_311`: default is yes
- `CONFIG_MQTT_SECURITY_ON`: default is yes
- `CONFIG_MQTT_PRIORITY`: default 5
- `CONFIG_MQTT_LOG_ERROR_ON`: defaut is yes
- `CONFIG_MQTT_LOG_WARN_ON`: defaut is yes
- `CONFIG_MQTT_LOG_INFO_ON`: defaut is yes
- `CONFIG_MQTT_RECONNECT_TIMEOUT`: defaut is 60 seconds
- `CONFIG_MQTT_QUEUE_BUFFER_SIZE_WORD`: default is 1024 (4096 bytes)
- `CONFIG_MQTT_BUFFER_SIZE_BYTE`: default is 1024 bytes
- `CONFIG_MQTT_MAX_HOST_LEN`: default is 64 bytes
- `CONFIG_MQTT_MAX_CLIENT_LEN`: default is 32 bytes
- `CONFIG_MQTT_MAX_USERNAME_LEN`: default is 32 bytes
- `CONFIG_MQTT_MAX_PASSWORD_LEN`: default is 32 bytes
- `CONFIG_MQTT_MAX_LWT_TOPIC`: default is 32 bytes
- `CONFIG_MQTT_MAX_LWT_MSG`: default is 32 bytes

Copy `main/include/user_config.sample.h` to `main/include/user_config.local.h` and provide `WIFI_SSID`, `WIFI_PASS`
Run command `make menuconfig` to change SERIAL PORT configuration and the options at `MQTT Application Sample ` and provide `WIFI_SSID`, `WIFI_PASS`

`make && make flash`

Expand Down Expand Up @@ -82,7 +64,7 @@ typedef struct mqtt_event_data_t
## Todo list
- [x] Create MQTT task server all protocol defined - Support subscribing, publishing, authentication, will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client).
- [ ] Support mbedtls for SSL connection
- [x] Support openssl for SSL connection
- [ ] Write document
## Progress:
Expand Down
2 changes: 1 addition & 1 deletion components/espmqtt
15 changes: 15 additions & 0 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
menu "MQTT Application sample"

config WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.

config WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.

endmenu
117 changes: 51 additions & 66 deletions main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include "soc/rtc_cntl_reg.h"

#include "esp_wifi.h"
#include "esp_system.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 "user_config.h"
#include "debug.h"

#include "esp_log.h"
#include "mqtt.h"

const char *MQTT_TAG = "MQTT_SAMPLE";

static EventGroupHandle_t wifi_event_group;
const static int CONNECTED_BIT = BIT0;

void connected_cb(void *self, void *params)
{
Expand All @@ -42,7 +41,7 @@ void reconnect_cb(void *self, void *params)
}
void subscribe_cb(void *self, void *params)
{
INFO("[APP] Subscribe ok, test publish msg\n");
ESP_LOGI(MQTT_TAG, "[APP] Subscribe ok, test publish msg");
mqtt_client *client = (mqtt_client *)self;
mqtt_publish(client, "/test", "abcde", 5, 0, 0);
}
Expand All @@ -56,34 +55,34 @@ void data_cb(void *self, void *params)
mqtt_client *client = (mqtt_client *)self;
mqtt_event_data_t *event_data = (mqtt_event_data_t *)params;

if (event_data->data_offset == 0) {
if(event_data->data_offset == 0) {

char *topic = malloc(event_data->topic_length + 1);
memcpy(topic, event_data->topic, event_data->topic_length);
topic[event_data->topic_length] = 0;
INFO("[APP] Publish topic: %s\n", topic);
ESP_LOGI(MQTT_TAG, "[APP] Publish topic: %s", topic);
free(topic);
}

// char *data = malloc(event_data->data_length + 1);
// memcpy(data, event_data->data, event_data->data_length);
// data[event_data->data_length] = 0;
INFO("[APP] Publish data[%d/%d bytes]\n",
event_data->data_length + event_data->data_offset,
event_data->data_total_length);
// data);
ESP_LOGI(MQTT_TAG, "[APP] Publish data[%d/%d bytes]",
event_data->data_length + event_data->data_offset,
event_data->data_total_length);
// data);

// free(data);

}

mqtt_settings settings = {
.host = "test.mosquitto.org",
#if defined(CONFIG_MQTT_SECURITY_ON)
#if defined(CONFIG_MQTT_SECURITY_ON)
.port = 8883, // encrypted
#else
.port = 1883, // unencrypted
#endif
#endif
.client_id = "mqtt_client_id",
.username = "user",
.password = "pass",
Expand All @@ -101,72 +100,58 @@ mqtt_settings settings = {
.data_cb = data_cb
};



static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{

switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_ERROR_CHECK(esp_wifi_connect());
break;

case SYSTEM_EVENT_STA_GOT_IP:

mqtt_start(&settings);
// Notice that, all callback will called in mqtt_task
// All function publish, subscribe
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */

mqtt_stop();
ESP_ERROR_CHECK(esp_wifi_connect());
break;
default:
break;
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
mqtt_start(&settings);
//init app here
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
mqtt_stop();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;


}

void wifi_conn_init(void)
static void wifi_conn_init(void)
{
INFO("[APP] Start, connect to Wifi network: %s ..\n", WIFI_SSID);

tcpip_adapter_init();

ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );

wifi_init_config_t icfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&icfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );

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 = WIFI_SSID,
.password = WIFI_PASS
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASSWORD,
},
};

ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK( esp_wifi_start());
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(MQTT_TAG, "start the WIFI SSID:[%s] password:[%s]", CONFIG_WIFI_SSID, "******");
ESP_ERROR_CHECK(esp_wifi_start());
}

void app_main()
{
INFO("[APP] Startup..\n");
INFO("[APP] Free memory: %d bytes\n", system_get_free_heap_size());
INFO("[APP] SDK version: %s, Build time: %s\n", system_get_sdk_version(), BUID_TIME);

#ifdef CPU_FREQ_160MHZ
INFO("[APP] Setup CPU run as 160MHz\n");
SET_PERI_REG_BITS(RTC_CLK_CONF, RTC_CNTL_SOC_CLK_SEL, 0x1, RTC_CNTL_SOC_CLK_SEL_S);
WRITE_PERI_REG(CPU_PER_CONF_REG, 0x01);
INFO("[APP] Setup CPU run as 160MHz - Done\n");
#endif

ESP_LOGI(MQTT_TAG, "[APP] Startup..");
ESP_LOGI(MQTT_TAG, "[APP] Free memory: %d bytes", system_get_free_heap_size());
ESP_LOGI(MQTT_TAG, "[APP] SDK version: %s, Build time: %s", system_get_sdk_version(), BUID_TIME);

nvs_flash_init();
wifi_conn_init();
}
}
5 changes: 0 additions & 5 deletions main/include/debug.h

This file was deleted.

35 changes: 0 additions & 35 deletions main/include/user_config.h

This file was deleted.

34 changes: 0 additions & 34 deletions main/include/user_config.sample.h

This file was deleted.

Loading

0 comments on commit da0e976

Please sign in to comment.