added some basic wifi connection retrys and debugging

This commit is contained in:
Skot Croshere 2023-06-21 19:00:09 -04:00 committed by johnny9
parent 9f22c21f17
commit 260ba51edb
6 changed files with 93 additions and 35 deletions

View File

@ -12,6 +12,8 @@
#include "lwip/err.h"
#include "lwip/sys.h"
#include "connect.h"
#define MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
@ -49,20 +51,12 @@
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* 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
static const char *TAG = "wifi station";
static int s_retry_num = 0;
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
@ -82,8 +76,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
}
}
void wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
{
EventBits_t wifi_init_sta(const char * wifi_ssid, const char * wifi_pass) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
@ -96,16 +89,8 @@ void wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
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_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));
wifi_config_t wifi_config = {
.sta = {
@ -140,11 +125,6 @@ void wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "Connected to SSID: %s", wifi_ssid);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID: %s", wifi_ssid);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
return bits;
}

View File

@ -4,7 +4,15 @@
#include <lwip/netdb.h>
#include <arpa/inet.h>
#include "freertos/event_groups.h"
#define WIFI_SSID CONFIG_ESP_WIFI_SSID
#define WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
void wifi_init_sta(const char * ssid, const char * pass);
/* 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
EventBits_t wifi_init_sta(const char * ssid, const char * pass);

View File

@ -26,7 +26,6 @@ static GlobalState GLOBAL_STATE = {
}
};
static const char *TAG = "miner";
void app_main(void)
@ -35,19 +34,39 @@ void app_main(void)
//wait between 0 and 5 seconds for multiple units
vTaskDelay(rand() % 5001 / portTICK_RATE_MS);
ESP_ERROR_CHECK(nvs_flash_init());
//ESP_ERROR_CHECK(esp_netif_init());
//ESP_ERROR_CHECK(esp_event_loop_create_default());
xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, (void*)&GLOBAL_STATE, 3, NULL);
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
//pull the wifi credentials out of NVS
char * wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, WIFI_SSID);
char * wifi_pass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, WIFI_PASS);
wifi_init_sta(wifi_ssid, wifi_pass);
//copy the wifi ssid to the global state
strncpy(GLOBAL_STATE.SYSTEM_MODULE.ssid, wifi_ssid, 20);
//init and connect to wifi
EventBits_t result_bits = wifi_init_sta(wifi_ssid, wifi_pass);
if (result_bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "Connected to SSID: %s", wifi_ssid);
strncpy(GLOBAL_STATE.SYSTEM_MODULE.wifi_status, "Connected!", 20);
} else if (result_bits & WIFI_FAIL_BIT) {
ESP_LOGE(TAG, "Failed to connect to SSID: %s", wifi_ssid);
strncpy(GLOBAL_STATE.SYSTEM_MODULE.wifi_status, "Failed to connect", 20);
esp_restart(); //this is pretty much fatal, so just restart
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
strncpy(GLOBAL_STATE.SYSTEM_MODULE.wifi_status, "unexpected error", 20);
esp_restart(); //this is pretty much fatal, so just restart
}
free(wifi_ssid);
free(wifi_pass);
queue_init(&GLOBAL_STATE.stratum_queue);
queue_init(&GLOBAL_STATE.ASIC_jobs_queue);
@ -55,6 +74,9 @@ void app_main(void)
BM1397_init(GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value);
//set the startup_done flag
GLOBAL_STATE.SYSTEM_MODULE.startup_done = true;
xTaskCreate(stratum_task, "stratum admin", 8192, (void*)&GLOBAL_STATE, 5, NULL);
xTaskCreate(create_jobs_task, "stratum miner", 8192, (void*)&GLOBAL_STATE, 10, NULL);
xTaskCreate(POWER_MANAGEMENT_task, "power mangement", 8192, (void*)&GLOBAL_STATE, 10, NULL);

View File

@ -39,9 +39,17 @@ static void _init_system(SystemModule* module) {
module->start_time = esp_timer_get_time();
module->lastClockSync = 0;
module->FOUND_BLOCK = false;
module->startup_done = false;
//set the best diff string to 0
_suffix_string(0, module->best_diff_string, DIFF_STRING_SIZE, 0);
//set the ssid string to blank
memset(module->ssid, 0, 20);
//set the wifi_status to blank
memset(module->wifi_status, 0, 20);
//test the LEDs
// ESP_LOGI(TAG, "Init LEDs!");
// ledc_init();
@ -172,6 +180,32 @@ static void _update_esp32_info(SystemModule* module) {
}
static void _init_connection(SystemModule* module) {
if (OLED_status()) {
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Connecting to:");
OLED_writeString(0, 1, module->oled_buf);
}
}
static void _update_connection(SystemModule* module) {
if (OLED_status()) {
OLED_clearLine(2);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "%s", module->ssid);
OLED_writeString(0, 2, module->oled_buf);
OLED_clearLine(3);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "%s", module->wifi_status);
OLED_writeString(0, 3, module->oled_buf);
}
}
static void _update_system_performance(SystemModule* module){
@ -196,6 +230,8 @@ static void _update_system_performance(SystemModule* module){
}
}
static double _calculate_network_difficulty(uint32_t nBits) {
uint32_t mantissa = nBits & 0x007fffff; // Extract the mantissa from nBits
uint8_t exponent = (nBits >> 24) & 0xff; // Extract the exponent from nBits
@ -286,6 +322,16 @@ void SYSTEM_task(void *pvParameters) {
_init_system(module);
_clear_display();
_init_connection(module);
//show the connection screen
while (!module->startup_done) {
_update_connection(module);
vTaskDelay(100 / portTICK_RATE_MS);
}
while(1){
_clear_display();
module->screen_page = 0;
@ -387,4 +433,3 @@ void SYSTEM_notify_found_nonce(SystemModule* module, double pool_diff, double fo

View File

@ -20,6 +20,9 @@ typedef struct {
uint32_t best_nonce_diff;
char best_diff_string[DIFF_STRING_SIZE];
bool FOUND_BLOCK;
bool startup_done;
char ssid[20];
char wifi_status[20];
uint32_t lastClockSync;
} SystemModule;

View File

@ -10,7 +10,7 @@
#include "math.h"
#include "serial.h"
#define POLL_RATE 1000/60
#define POLL_RATE 5000
#define MAX_TEMP 90.0
#define THROTTLE_TEMP 80.0
#define THROTTLE_TEMP_RANGE (MAX_TEMP - THROTTLE_TEMP)