it connects :3

This commit is contained in:
Laila van Reenen 2024-06-20 21:38:38 +02:00
parent a9bffe1b4c
commit fd6e6fa48e
Signed by: LailaTheElf
GPG Key ID: 1F4E6EE3E6DDF769
6 changed files with 311 additions and 258 deletions

16
rx_esp32/src/config.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <stdint.h>
#define WIFI_SSID "UPC46273"
// #define WIFI_AUTH WIFI_AUTH_WPA_WPA2_PSK
#define WIFI_AUTH WIFI_AUTH_WPA_PSK
#define WIFI_PASS "SPHZHKRY"
#define UDP_PORT 1234
static uint8_t BoatId = 1;
static const char *TAG = "rcrf-rx_esp32";
#endif

View File

@ -10,257 +10,36 @@
#include "../lib/cli/CLI/CLI.h"
#include "../lib/cli/CMDList/CMDList.h"
#include "config.h"
#include "utils.h"
#include "commands.h"
#define WIFI_SSID "UPC46273"
#define WIFI_PASS "SPHZHKRY"
#define UDP_PORT 1234
static const char *TAG = "rcrf-rx_esp32";
#define MAX_RETRY_ATTEMPTS 20
// static int s_ap_creds_num = 0;
static int s_retry_num = 0;
uint8_t BoatId = 1;
#include "wifi.h"
bool running = true;
CMDList_t* cmdList;
bool rxBuffer_overflow = false;
static EventGroupHandle_t s_wifi_event_group;
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
/*
* CLI char out function. used to print back to a CLI, but the lib
* is only used for reciving command and not having a full cli
*/
int charOut(const char* line)
{
// don't print anything
printf(line);
return 0;
}
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT)
{
switch (event_id)
{
case WIFI_EVENT_WIFI_READY:
ESP_LOGI(TAG, "WiFi ready");
break;
case WIFI_EVENT_SCAN_DONE:
ESP_LOGI(TAG, "Finished scanning AP");
break;
case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "Station start");
esp_wifi_connect();
break;
case WIFI_EVENT_STA_STOP:
ESP_LOGI(TAG, "Station stop");
break;
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Station connected to AP");
break;
case WIFI_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Station disconnected from AP");
if (s_retry_num < MAX_RETRY_ATTEMPTS)
{
s_retry_num++;
ESP_LOGI(TAG, "retry num %d", s_retry_num);
for (unsigned long i=0; i < 100000; i++)
{
printf(".");
}
esp_wifi_connect();
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
break;
break;
case WIFI_EVENT_STA_AUTHMODE_CHANGE:
ESP_LOGI(TAG, "the auth mode of AP connected by device's station changed");
break;
case WIFI_EVENT_STA_WPS_ER_SUCCESS:
ESP_LOGI(TAG, "Station wps succeeds in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_FAILED:
ESP_LOGI(TAG, "Station wps fails in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
ESP_LOGI(TAG, "Station wps timeout in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_PIN:
ESP_LOGI(TAG, "Station wps pin code in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP:
ESP_LOGI(TAG, "Station wps overlap in enrollee mode");
break;
case WIFI_EVENT_AP_START:
ESP_LOGI(TAG, "Soft-AP start");
break;
case WIFI_EVENT_AP_STOP:
ESP_LOGI(TAG, "Soft-AP stop");
break;
case WIFI_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "a station connected to Soft-AP");
break;
case WIFI_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "a station disconnected from Soft-AP");
break;
case WIFI_EVENT_AP_PROBEREQRECVED:
ESP_LOGI(TAG, "Receive probe request packet in soft-AP interface");
break;
case WIFI_EVENT_FTM_REPORT:
ESP_LOGI(TAG, "Receive report of FTM procedure");
break;
case WIFI_EVENT_STA_BSS_RSSI_LOW:
ESP_LOGI(TAG, "AP's RSSI crossed configured threshold");
break;
case WIFI_EVENT_ACTION_TX_STATUS:
ESP_LOGI(TAG, "Status indication of Action Tx operation");
break;
case WIFI_EVENT_ROC_DONE:
ESP_LOGI(TAG, "Remain-on-Channel operation complete");
break;
case WIFI_EVENT_STA_BEACON_TIMEOUT:
ESP_LOGI(TAG, "Station beacon timeout");
break;
case WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START:
ESP_LOGI(TAG, "Connectionless module wake interval start");
break;
case WIFI_EVENT_AP_WPS_RG_SUCCESS:
ESP_LOGI(TAG, "Soft-AP wps succeeds in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_FAILED:
ESP_LOGI(TAG, "Soft-AP wps fails in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_TIMEOUT:
ESP_LOGI(TAG, "Soft-AP wps timeout in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_PIN:
ESP_LOGI(TAG, "Soft-AP wps pin code in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_PBC_OVERLAP:
ESP_LOGI(TAG, "Soft-AP wps overlap in registrar mode");
break;
case WIFI_EVENT_ITWT_SETUP:
ESP_LOGI(TAG, "iTWT setup");
break;
case WIFI_EVENT_ITWT_TEARDOWN:
ESP_LOGI(TAG, "iTWT teardown");
break;
case WIFI_EVENT_ITWT_PROBE:
ESP_LOGI(TAG, "iTWT probe");
break;
case WIFI_EVENT_ITWT_SUSPEND:
ESP_LOGI(TAG, "iTWT suspend");
break;
case WIFI_EVENT_NAN_STARTED:
ESP_LOGI(TAG, "NAN Discovery has started");
break;
case WIFI_EVENT_NAN_STOPPED:
ESP_LOGI(TAG, "NAN Discovery has stopped");
break;
case WIFI_EVENT_NAN_SVC_MATCH:
ESP_LOGI(TAG, "NAN Service Discovery match found");
break;
case WIFI_EVENT_NAN_REPLIED:
ESP_LOGI(TAG, "Replied to a NAN peer with Service Discovery match");
break;
case WIFI_EVENT_NAN_RECEIVE:
ESP_LOGI(TAG, "Received a Follow-up message");
break;
case WIFI_EVENT_NDP_INDICATION:
ESP_LOGI(TAG, "Received NDP Request from a NAN Peer");
break;
case WIFI_EVENT_NDP_CONFIRM:
ESP_LOGI(TAG, "NDP Confirm Indication");
break;
case WIFI_EVENT_NDP_TERMINATED:
ESP_LOGI(TAG, "NAN Datapath terminated indication");
break;
case WIFI_EVENT_HOME_CHANNEL_CHANGE:
ESP_LOGI(TAG, "WiFi home channel changedoesn't occur when scanning");
break;
case WIFI_EVENT_MAX:
ESP_LOGI(TAG, "Invalid WiFi event ID");
break;
}
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifiInit()
{
// init network interface
ESP_ERROR_CHECK(esp_netif_init());
// create event group
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
// init wifi as default station
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// register events
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
// set wifi connection config
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
*/
.threshold.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.sae_pwe_h2e = WPA3_SAE_PWE_HUNT_AND_PECK,
.sae_h2e_identifier = "",
},
};
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_LOGI(TAG, "wifi_init_sta finished.");
}
void app_main() {
// disable watchdog
ESP_ERROR_CHECK(esp_task_wdt_deinit());
// wait so I have time to open the serial monitor
for (unsigned long i=0; i < 100000; i++)
for (unsigned long i=0; i < 10000; i++)
{
printf(".");
}
printf("\n");
/* Initialize NVS — it is used to store PHY calibration data */
esp_err_t ret = nvs_flash_init();
@ -275,6 +54,23 @@ void app_main() {
wifiInit();
// while (true)
// {
// wifi_scan();
// // wait so I have time to open the serial monitor
// for (unsigned long i=1; i < 1000000; i++)
// {
// if (i % 100 == 0)
// {
// printf(",");
// }
// }
// printf("\n");
// }
wifi_connect();
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
@ -287,45 +83,51 @@ void app_main() {
* happened. */
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS);
printf("INFO: connected to ap SSID:%s password:%s\n", WIFI_SSID, WIFI_PASS);
}
else if (bits & WIFI_FAIL_BIT)
{
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS);
printf("ERROR: Failed to connect to SSID:%s, password:%s\n", WIFI_SSID, WIFI_PASS);
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
printf("FAITAL: UNEXPECTED EVENT\n");
return;
}
// while(true)
{
ESP_LOGI(TAG, ":3");
printf(":3");
}
// running = true;
running = true;
// cmdList = getCMDList();
cmdList = getCMDList();
// // init cli
// CLI_init((CLI_charOutFn)&charOut, cmdList);
// init cli
CLI_init((CLI_charOutFn)&charOut, cmdList);
// while (running)
// {
// int packetSize = UDP.parsePacket();
// while (packetSize) {
// char c;
// int len = UDP.read(&c, 1);
// if (len == 1)
// {
// CLI_charIn(c);
// packetSize -= c;
// }
// }
// }
while (running)
{
int packetSize = UDP.parsePacket();
while (packetSize) {
char c;
int len = UDP.read(&c, 1);
if (len == 1)
{
CLI_charIn(c);
packetSize--;
}
else
{
printf("UDP read error");
break;
}
}
}
// CLI_deinit();
// CMDList_deinit(cmdList);
CLI_deinit();
CMDList_deinit(cmdList);
return;
}

225
rx_esp32/src/wifi.c Normal file
View File

@ -0,0 +1,225 @@
#include "wifi.h"
#include <stdbool.h>
#include <stdint.h>
#include "nvs_flash.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "config.h"
#define MAX_RETRY_ATTEMPTS 20
// static int s_ap_creds_num = 0;
static int s_retry_num = 0;
EventGroupHandle_t s_wifi_event_group;
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT)
{
switch (event_id)
{
case WIFI_EVENT_WIFI_READY:
ESP_LOGI(TAG, "WiFi ready");
break;
case WIFI_EVENT_SCAN_DONE:
ESP_LOGI(TAG, "Finished scanning AP");
break;
case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "Station start");
esp_wifi_connect();
break;
case WIFI_EVENT_STA_STOP:
ESP_LOGI(TAG, "Station stop");
break;
case WIFI_EVENT_STA_CONNECTED:
ESP_LOGI(TAG, "Station connected to AP");
break;
case WIFI_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "Station disconnected from AP");
if (s_retry_num < MAX_RETRY_ATTEMPTS)
{
s_retry_num++;
ESP_LOGI(TAG, "retry num %d", s_retry_num);
for (unsigned long i=0; i < 10000000; i++)
{
// printf(".");
}
esp_wifi_connect();
}
else
{
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
break;
break;
case WIFI_EVENT_STA_AUTHMODE_CHANGE:
ESP_LOGI(TAG, "the auth mode of AP connected by device's station changed");
break;
case WIFI_EVENT_STA_WPS_ER_SUCCESS:
ESP_LOGI(TAG, "Station wps succeeds in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_FAILED:
ESP_LOGI(TAG, "Station wps fails in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
ESP_LOGI(TAG, "Station wps timeout in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_PIN:
ESP_LOGI(TAG, "Station wps pin code in enrollee mode");
break;
case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP:
ESP_LOGI(TAG, "Station wps overlap in enrollee mode");
break;
case WIFI_EVENT_AP_START:
ESP_LOGI(TAG, "Soft-AP start");
break;
case WIFI_EVENT_AP_STOP:
ESP_LOGI(TAG, "Soft-AP stop");
break;
case WIFI_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "a station connected to Soft-AP");
break;
case WIFI_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "a station disconnected from Soft-AP");
break;
case WIFI_EVENT_AP_PROBEREQRECVED:
ESP_LOGI(TAG, "Receive probe request packet in soft-AP interface");
break;
case WIFI_EVENT_FTM_REPORT:
ESP_LOGI(TAG, "Receive report of FTM procedure");
break;
case WIFI_EVENT_STA_BSS_RSSI_LOW:
ESP_LOGI(TAG, "AP's RSSI crossed configured threshold");
break;
case WIFI_EVENT_ACTION_TX_STATUS:
ESP_LOGI(TAG, "Status indication of Action Tx operation");
break;
case WIFI_EVENT_ROC_DONE:
ESP_LOGI(TAG, "Remain-on-Channel operation complete");
break;
case WIFI_EVENT_STA_BEACON_TIMEOUT:
ESP_LOGI(TAG, "Station beacon timeout");
break;
case WIFI_EVENT_CONNECTIONLESS_MODULE_WAKE_INTERVAL_START:
ESP_LOGI(TAG, "Connectionless module wake interval start");
break;
case WIFI_EVENT_AP_WPS_RG_SUCCESS:
ESP_LOGI(TAG, "Soft-AP wps succeeds in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_FAILED:
ESP_LOGI(TAG, "Soft-AP wps fails in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_TIMEOUT:
ESP_LOGI(TAG, "Soft-AP wps timeout in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_PIN:
ESP_LOGI(TAG, "Soft-AP wps pin code in registrar mode");
break;
case WIFI_EVENT_AP_WPS_RG_PBC_OVERLAP:
ESP_LOGI(TAG, "Soft-AP wps overlap in registrar mode");
break;
case WIFI_EVENT_ITWT_SETUP:
ESP_LOGI(TAG, "iTWT setup");
break;
case WIFI_EVENT_ITWT_TEARDOWN:
ESP_LOGI(TAG, "iTWT teardown");
break;
case WIFI_EVENT_ITWT_PROBE:
ESP_LOGI(TAG, "iTWT probe");
break;
case WIFI_EVENT_ITWT_SUSPEND:
ESP_LOGI(TAG, "iTWT suspend");
break;
case WIFI_EVENT_NAN_STARTED:
ESP_LOGI(TAG, "NAN Discovery has started");
break;
case WIFI_EVENT_NAN_STOPPED:
ESP_LOGI(TAG, "NAN Discovery has stopped");
break;
case WIFI_EVENT_NAN_SVC_MATCH:
ESP_LOGI(TAG, "NAN Service Discovery match found");
break;
case WIFI_EVENT_NAN_REPLIED:
ESP_LOGI(TAG, "Replied to a NAN peer with Service Discovery match");
break;
case WIFI_EVENT_NAN_RECEIVE:
ESP_LOGI(TAG, "Received a Follow-up message");
break;
case WIFI_EVENT_NDP_INDICATION:
ESP_LOGI(TAG, "Received NDP Request from a NAN Peer");
break;
case WIFI_EVENT_NDP_CONFIRM:
ESP_LOGI(TAG, "NDP Confirm Indication");
break;
case WIFI_EVENT_NDP_TERMINATED:
ESP_LOGI(TAG, "NAN Datapath terminated indication");
break;
case WIFI_EVENT_HOME_CHANNEL_CHANGE:
ESP_LOGI(TAG, "WiFi home channel changedoesn't occur when scanning");
break;
case WIFI_EVENT_MAX:
ESP_LOGI(TAG, "Invalid WiFi event ID");
break;
}
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
void wifiInit()
{
// init network interface
ESP_ERROR_CHECK(esp_netif_init());
// create event group
s_wifi_event_group = xEventGroupCreate();
assert(s_wifi_event_group);
ESP_ERROR_CHECK(esp_event_loop_create_default());
// init wifi as default station
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// register events
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
ESP_LOGI(TAG, "wifi_init_sta finished.");
}
void wifi_connect()
{
// set wifi connection config
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
*/
.threshold.authmode = WIFI_AUTH,
.sae_pwe_h2e = WPA3_SAE_PWE_HUNT_AND_PECK,
.sae_h2e_identifier = "",
},
};
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());
}

17
rx_esp32/src/wifi.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef WIFI_H
#define WIFI_H
#include "esp_event.h"
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
extern EventGroupHandle_t s_wifi_event_group;
void wifiInit();
void wifi_connect();
void wifi_scan();
#endif

View File

@ -1,4 +1,4 @@
#include "wifi_scan.h"
#include "wifi.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
@ -8,7 +8,6 @@
#include "esp_event.h"
#include "nvs_flash.h"
#include "regex.h"
#include "esp_task_wdt.h"
#define SCAN_LIST_SIZE 30
@ -144,7 +143,7 @@ static void array_2_channel_bitmap(const uint8_t channel_list[], const uint8_t c
#endif /*USE_CHANNEL_BTIMAP*/
/* Initialize Wi-Fi as sta and set scan method */
static void wifi_scan(void)
void wifi_scan()
{
uint16_t number = SCAN_LIST_SIZE;
uint16_t ap_count = 0;

View File

@ -1,6 +0,0 @@
#ifndef WIFI_SCAN_H
#define WIFI_SCAN_H
static void wifi_scan(void);
#endif