Skip to content
Snippets Groups Projects
Commit 515d4caa authored by Jens Nolte's avatar Jens Nolte
Browse files

Allow simultaneous network connections (wlan and ethernet)

parent 23333f81
No related branches found
No related tags found
1 merge request!8Updating bme280-qthing
CXXFLAGS += -std=c++17 CXXFLAGS += -std=c++17
COMPONENT_SRCDIRS := . device network peripherals COMPONENT_SRCDIRS := . device network peripherals
COMPONENT_ADD_INCLUDEDIRS := . COMPONENT_ADD_INCLUDEDIRS := . network
\ No newline at end of file \ No newline at end of file
...@@ -85,10 +85,7 @@ static void eth_gpio_config_rmii(void) ...@@ -85,10 +85,7 @@ static void eth_gpio_config_rmii(void)
void enable_ethernet_lan8720() void enable_ethernet_lan8720()
{ {
esp_err_t ret = ESP_OK; initialize_network();
tcpip_adapter_init();
start_network_event_loop();
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
config.phy_addr = PHY1; config.phy_addr = PHY1;
...@@ -97,8 +94,8 @@ void enable_ethernet_lan8720() ...@@ -97,8 +94,8 @@ void enable_ethernet_lan8720()
config.clock_mode = ETH_CLOCK_GPIO0_IN; config.clock_mode = ETH_CLOCK_GPIO0_IN;
config.phy_power_enable = phy_device_power_enable_via_gpio; config.phy_power_enable = phy_device_power_enable_via_gpio;
esp_err_t ret = ESP_OK;
ret = esp_eth_init(&config); ret = esp_eth_init(&config);
if (ret == ESP_OK) { if (ret == ESP_OK) {
esp_eth_enable(); esp_eth_enable();
} }
......
#include "qthing.h" #include "qthing.h"
#include "network.h"
#include "io.h" #include "io.h"
#include "environment.h" #include "environment.h"
#include "device/device_config.h" #include "device/device_config.h"
#include "event.h" #include "event.h"
#include "mqtt.h" #include "mqtt.h"
#include "network/udp.h"
#include "string.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
...@@ -15,11 +17,13 @@ ...@@ -15,11 +17,13 @@
#include "esp_event_loop.h" #include "esp_event_loop.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#define TAG "qthing-network" #define TAG "network"
bool network_initialized = false;
bool clients_started = false; bool clients_started = false;
void start_network_clients_task(void* arg) { void start_network_clients_task(void* arg) {
start_udp_server();
mqtt_client_start(); mqtt_client_start();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
...@@ -41,6 +45,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) ...@@ -41,6 +45,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, DEVICE_NAME)); ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, DEVICE_NAME));
ESP_ERROR_CHECK(esp_wifi_connect()); ESP_ERROR_CHECK(esp_wifi_connect());
break; break;
// case SYSTEM_EVENT_STA_CONNECTED:
// tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
// break;
case SYSTEM_EVENT_STA_GOT_IP: case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
...@@ -54,6 +61,10 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) ...@@ -54,6 +61,10 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
update_wlan_connection_status(disconnected); update_wlan_connection_status(disconnected);
ESP_ERROR_CHECK(esp_wifi_connect()); ESP_ERROR_CHECK(esp_wifi_connect());
break; break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STA_GOT_IP6");
ESP_LOGI(TAG, "IPv6: %s", ip6addr_ntoa(&event->event_info.got_ip6.ip6_info.ip));
break;
case SYSTEM_EVENT_ETH_START: case SYSTEM_EVENT_ETH_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_START"); ESP_LOGI(TAG, "SYSTEM_EVENT_ETH_START");
ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, DEVICE_NAME)); ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, DEVICE_NAME));
...@@ -63,55 +74,22 @@ static esp_err_t event_handler(void *ctx, system_event_t *event) ...@@ -63,55 +74,22 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); ESP_LOGI(TAG, "got ip:%s\n", ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
start_network_clients(); start_network_clients();
log_oled("Ethernet connected"); log_oled("Ethernet connected");
break;
default: default:
break; break;
} }
return ESP_OK; return ESP_OK;
} }
void start_network_event_loop() { void initialize_network() {
ESP_LOGI(TAG, "starting network event loop"); if (network_initialized) {
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); return;
ESP_LOGI(TAG, "started network event loop");
}
void enable_wlan() {
log_oled("WLAN initializing");
update_wlan_connection_status(connecting);
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
} }
ESP_ERROR_CHECK( ret ); network_initialized = true;
ESP_LOGI(TAG, "initializing tcpip adapter");
tcpip_adapter_init(); tcpip_adapter_init();
// Don't run a DHCP client ESP_LOGI(TAG, "starting network event loop");
#ifdef STATIC_IP ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
tcpip_adapter_ip_info_t ipInfo;
inet_pton(AF_INET, DEVICE_IP, &ipInfo.ip);
inet_pton(AF_INET, DEVICE_GW, &ipInfo.gw);
inet_pton(AF_INET, DEVICE_NETMASK, &ipInfo.netmask);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
#endif
start_network_event_loop();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config;
memset(&wifi_config, sizeof(wifi_config_t), 1);
wifi_config.sta = wifi_sta_config_t {
WLAN_SSID,
WLAN_PASSPHRASE,
WIFI_ALL_CHANNEL_SCAN,
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
} }
#pragma once #pragma once
void start_network_clients(); void start_network_clients();
void start_network_event_loop(); void initialize_network();
\ No newline at end of file \ No newline at end of file
#include "qthing.h"
#include "network.h"
#include "io.h"
#include "environment.h"
#include "device/device_config.h"
#include "event.h"
#include "mqtt.h"
#include "network/udp.h"
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include <lwip/sockets.h>
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
void enable_wlan() {
log_oled("WLAN initializing");
update_wlan_connection_status(connecting);
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
initialize_network();
// Don't run a DHCP client
#ifdef STATIC_IP
tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
tcpip_adapter_ip_info_t ipInfo;
inet_pton(AF_INET, DEVICE_IP, &ipInfo.ip);
inet_pton(AF_INET, DEVICE_GW, &ipInfo.gw);
inet_pton(AF_INET, DEVICE_NETMASK, &ipInfo.netmask);
tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
#endif
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config;
memset(&wifi_config, sizeof(wifi_config_t), 1);
wifi_config.sta = wifi_sta_config_t {
WLAN_SSID,
WLAN_PASSPHRASE,
WIFI_ALL_CHANNEL_SCAN,
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment