Skip to content

Commit e3c1c19

Browse files
authored
Merge pull request #5 from RonnyA/SSL
Committer: Ronny Hansen <ronnyhan@gmail.com>
2 parents 9a180aa + 075c25c commit e3c1c19

File tree

1 file changed

+66
-19
lines changed

1 file changed

+66
-19
lines changed

main/app_main.c

100644100755
+66-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
#include "esp_wifi.h"
99
#include "esp_system.h"
10+
#include "nvs_flash.h"
11+
#include "esp_event_loop.h"
12+
1013

1114
#include "freertos/FreeRTOS.h"
1215
#include "freertos/task.h"
@@ -75,8 +78,12 @@ void data_cb(void *self, void *params)
7578
}
7679

7780
mqtt_settings settings = {
78-
.host = "192.168.0.103",
79-
.port = 1883,
81+
.host = "test.mosquitto.org",
82+
#if defined(CONFIG_MQTT_SECURITY_ON)
83+
.port = 8883, // encrypted
84+
#else
85+
.port = 1883, // unencrypted
86+
#endif
8087
.client_id = "mqtt_client_id",
8188
.username = "user",
8289
.password = "pass",
@@ -94,9 +101,61 @@ mqtt_settings settings = {
94101
.data_cb = data_cb
95102
};
96103

104+
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
105+
{
106+
107+
switch(event->event_id) {
108+
case SYSTEM_EVENT_STA_START:
109+
ESP_ERROR_CHECK(esp_wifi_connect());
110+
break;
111+
112+
case SYSTEM_EVENT_STA_GOT_IP:
113+
114+
mqtt_start(&settings);
115+
// Notice that, all callback will called in mqtt_task
116+
// All function publish, subscribe
117+
break;
118+
case SYSTEM_EVENT_STA_DISCONNECTED:
119+
/* This is a workaround as ESP32 WiFi libs don't currently
120+
auto-reassociate. */
121+
122+
mqtt_stop();
123+
ESP_ERROR_CHECK(esp_wifi_connect());
124+
break;
125+
default:
126+
break;
127+
}
128+
return ESP_OK;
129+
130+
131+
}
132+
133+
void wifi_conn_init(void)
134+
{
135+
INFO("[APP] Start, connect to Wifi network: %s ..\n", WIFI_SSID);
136+
137+
tcpip_adapter_init();
138+
139+
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
140+
141+
wifi_init_config_t icfg = WIFI_INIT_CONFIG_DEFAULT();
142+
ESP_ERROR_CHECK( esp_wifi_init(&icfg) );
143+
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
144+
145+
wifi_config_t wifi_config = {
146+
.sta = {
147+
.ssid = WIFI_SSID,
148+
.password = WIFI_PASS
149+
},
150+
};
151+
152+
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA));
153+
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
154+
ESP_ERROR_CHECK( esp_wifi_start());
155+
}
156+
97157
void app_main()
98158
{
99-
wifi_config_t cfg;
100159
INFO("[APP] Startup..\n");
101160
INFO("[APP] Free memory: %d bytes\n", system_get_free_heap_size());
102161
INFO("[APP] SDK version: %s, Build time: %s\n", system_get_sdk_version(), BUID_TIME);
@@ -107,19 +166,7 @@ void app_main()
107166
WRITE_PERI_REG(CPU_PER_CONF_REG, 0x01);
108167
INFO("[APP] Setup CPU run as 160MHz - Done\n");
109168
#endif
110-
INFO("[APP] Start, connect to Wifi network: %s ..\n", WIFI_SSID);
111-
112-
strcpy(cfg.sta.ssid, WIFI_SSID);
113-
strcpy(cfg.sta.password, WIFI_PASS);
114-
115-
esp_wifi_set_mode(WIFI_MODE_STA);
116-
esp_wifi_set_config(WIFI_IF_STA, &cfg);
117-
esp_wifi_start();
118-
esp_wifi_connect();
119-
120-
INFO("[APP] Initial MQTT task\r\n");
121-
122-
// Notice that, all callback will called in mqtt_task
123-
// All function publish, subscribe
124-
mqtt_start(&settings);
125-
}
169+
170+
nvs_flash_init();
171+
wifi_conn_init();
172+
}

0 commit comments

Comments
 (0)