Skip to content

Commit 310ea54

Browse files
committed
improve WiFi handling, display IP when no data
1 parent 8006b7c commit 310ea54

File tree

5 files changed

+93
-51
lines changed

5 files changed

+93
-51
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,3 @@ gst-launch-1.0 filesrc location=components/rtpjpeg/BigBuckBunny_320x180.mp4 ! de
6060
- Both LVGL and the JPEG decoder use this same buffer, rendering one stripe at a time, which is then sent to the display.
6161
- When frames are arriving, LVGL is deactivated by not calling `lv_timer_handler()`.
6262
- We are not using the esp_jpeg component (or ROM decoder) because its API does not allow to receive decoded data block by block.
63-
64-
## TODOs
65-
66-
- [ ] Show IP address WiFi status
67-
- [ ] Honor RTP timestamps

main/jpeg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ esp_err_t init_jpeg_decoder(lcd_t *lcd, uint8_t *px_buf, ptrdiff_t px_buf_sz, jp
111111

112112
out->jdec = malloc(sizeof(*(out->jdec)));
113113
if (out->jdec == NULL) {
114-
ESP_LOGW(TAG, "failed alloc of js sz=%u", sizeof(*(out->jdec)));
114+
ESP_LOGW(TAG, "Failed alloc of js sz=%u", sizeof(*(out->jdec)));
115115
free(out->px_buf);
116116
return ESP_ERR_NO_MEM;
117117
}
118118

119119
out->work = malloc(TJPGD_WORK_SZ);
120120
if (out->work == NULL) {
121-
ESP_LOGW(TAG, "failed alloc of work arena sz=%d", TJPGD_WORK_SZ);
121+
ESP_LOGW(TAG, "Failed alloc of work arena sz=%d", TJPGD_WORK_SZ);
122122
free(out->px_buf);
123123
free(out->jdec);
124124
return ESP_ERR_NO_MEM;

main/main.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ void app_main(void) {
7373

7474
print_free_heap_stack();
7575
ESP_LOGI(TAG, "Initialize WIFI");
76-
init_wifi();
76+
init_wifi_sta();
77+
esp_netif_ip_info_t ip_info = {0};
78+
ESP_ERROR_CHECK(get_ip_info(&ip_info));
79+
smpte_image_set_text("Initializing...");
80+
lv_timer_handler();
7781

7882
print_free_heap_stack();
7983
ESP_LOGI(TAG, "Initialize mDNS");
@@ -101,7 +105,10 @@ void app_main(void) {
101105

102106
// Main loop.
103107
print_free_heap_stack();
104-
smpte_image_set_text("No stream available ");
108+
char no_stream_text[70] = {0};
109+
snprintf(no_stream_text, sizeof(no_stream_text),
110+
"No stream available, send RTP/JPEG data to " IPSTR " ", IP2STR(&ip_info.ip));
111+
smpte_image_set_text(no_stream_text);
105112
int64_t last_frame_recv_us = 0;
106113
bool reset_screen = false;
107114
while (1) {

main/wifi.c

Lines changed: 76 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "wifi.h"
22

3-
#include <esp_err.h>
4-
#include <esp_log.h>
3+
#include <assert.h>
4+
#include <stdint.h>
55
#include <string.h>
66
#pragma GCC diagnostic push
77
#pragma GCC diagnostic ignored "-Wsign-compare"
@@ -20,41 +20,82 @@
2020
#define WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WPA2_PSK
2121
#define WIFI_H2E_IDENTIFIER "PASSWORD IDENTIFIER"
2222

23-
/* FreeRTOS event group to signal when we are connected*/
23+
// FreeRTOS event group to signal when we are connected.
2424
static EventGroupHandle_t s_wifi_event_group;
2525

26-
/* The event group allows multiple bits for each event, but we only care about two events:
27-
* - we are connected to the AP with an IP
28-
* - we failed to connect after the maximum amount of retries */
26+
static bool have_ip_info = false;
27+
static esp_netif_ip_info_t ip_info = {0};
28+
29+
// The event group allows multiple bits for each event, but we only care about two events:
30+
// - we are connected to the AP with an IP.
31+
// - we failed to connect after the maximum amount of retries.
2932
#define WIFI_CONNECTED_BIT BIT0
3033
#define WIFI_FAIL_BIT BIT1
3134

3235
static const char* TAG = "sta";
3336

3437
static int s_retry_num = 0;
3538

39+
esp_err_t get_ip_info(esp_netif_ip_info_t* ip_info_out) {
40+
assert(ip_info_out != NULL);
41+
if (have_ip_info) {
42+
memcpy(ip_info_out, &ip_info, sizeof(*ip_info_out));
43+
return ESP_OK;
44+
}
45+
return ESP_ERR_INVALID_STATE;
46+
}
47+
3648
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id,
3749
void* event_data) {
38-
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
39-
esp_wifi_connect();
40-
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
41-
if (s_retry_num < CONFIG_SMALLTV_WIFI_MAXIMUM_RETRY) {
42-
esp_wifi_connect();
43-
s_retry_num++;
44-
ESP_LOGI(TAG, "retry to connect to the AP");
45-
} else {
46-
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
50+
if (event_base == WIFI_EVENT) {
51+
switch (event_id) {
52+
case WIFI_EVENT_STA_START:
53+
ESP_LOGI(TAG, "esp_wifi_connect() initial");
54+
esp_wifi_connect();
55+
break;
56+
case WIFI_EVENT_STA_DISCONNECTED:
57+
if (s_retry_num < CONFIG_SMALLTV_WIFI_MAXIMUM_RETRY) {
58+
s_retry_num++;
59+
ESP_LOGI(TAG, "esp_wifi_connect() retry=%d", s_retry_num);
60+
esp_wifi_connect();
61+
} else {
62+
ESP_LOGI(TAG, "esp_wifi_connect() giving up");
63+
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
64+
}
65+
break;
66+
default:
67+
ESP_LOGV(TAG, "Received unhandled WIFI_EVENT event_id=%ld", event_id);
68+
break;
69+
}
70+
return;
71+
}
72+
73+
if (event_base == IP_EVENT) {
74+
switch (event_id) {
75+
case IP_EVENT_STA_GOT_IP: {
76+
ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
77+
ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip));
78+
memcpy(&ip_info, &event->ip_info, sizeof(ip_info));
79+
have_ip_info = true;
80+
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
81+
} break;
82+
case IP_EVENT_STA_LOST_IP: {
83+
ESP_LOGE(TAG, "Lost IP, resetting");
84+
fflush(stdout);
85+
fflush(stderr);
86+
esp_restart();
87+
} break;
88+
default:
89+
ESP_LOGV(TAG, "Received unhandled IP_EVENT event_id=%ld", event_id);
90+
break;
4791
}
48-
ESP_LOGI(TAG, "connect to the AP fail");
49-
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
50-
ip_event_got_ip_t* event = (ip_event_got_ip_t*)event_data;
51-
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
52-
s_retry_num = 0;
53-
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
92+
return;
5493
}
94+
95+
ESP_LOGI(TAG, "Received unhandled event_base=%p event_id=%ld", event_base, event_id);
5596
}
5697

57-
void init_wifi_sta(void) {
98+
void init_wifi_sta() {
5899
s_wifi_event_group = xEventGroupCreate();
59100

60101
ESP_ERROR_CHECK(esp_netif_init());
@@ -65,8 +106,8 @@ void init_wifi_sta(void) {
65106
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
66107
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
67108

68-
esp_event_handler_instance_t instance_any_id;
69-
esp_event_handler_instance_t instance_got_ip;
109+
esp_event_handler_instance_t instance_any_id = {0};
110+
esp_event_handler_instance_t instance_got_ip = {0};
70111
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
71112
&event_handler, NULL, &instance_any_id));
72113
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
@@ -77,7 +118,10 @@ void init_wifi_sta(void) {
77118
{
78119
.ssid = CONFIG_SMALLTV_WIFI_SSID,
79120
.password = CONFIG_SMALLTV_WIFI_PASSWORD,
121+
.scan_method = WIFI_ALL_CHANNEL_SCAN,
122+
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
80123
.threshold.authmode = WIFI_SCAN_AUTH_MODE_THRESHOLD,
124+
.failure_retry_cnt = 2,
81125
.sae_pwe_h2e = WIFI_SAE_MODE,
82126
.sae_h2e_identifier = WIFI_H2E_IDENTIFIER,
83127
},
@@ -86,28 +130,19 @@ void init_wifi_sta(void) {
86130
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
87131
ESP_ERROR_CHECK(esp_wifi_start());
88132

89-
ESP_LOGI(TAG, "wifi_init_sta finished.");
90-
91-
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed
92-
* for the maximum number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see
93-
* above) */
133+
// Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed
134+
// for the maximum number of re-tries (WIFI_FAIL_BIT).
135+
// The bits are set by event_handler().
94136
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
95137
pdFALSE, pdFALSE, portMAX_DELAY);
96138

97-
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which
98-
* event actually happened. */
139+
// xEventGroupWaitBits() returns the bits before the call returned, hence we can test which
140+
// event actually happened.
99141
if (bits & WIFI_CONNECTED_BIT) {
100-
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", CONFIG_SMALLTV_WIFI_SSID,
101-
CONFIG_SMALLTV_WIFI_PASSWORD);
142+
ESP_LOGI(TAG, "Connected to AP SSID: %s", CONFIG_SMALLTV_WIFI_SSID);
102143
} else if (bits & WIFI_FAIL_BIT) {
103-
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", CONFIG_SMALLTV_WIFI_SSID,
104-
CONFIG_SMALLTV_WIFI_PASSWORD);
144+
ESP_LOGW(TAG, "Failed to connect to SSID:%s ", CONFIG_SMALLTV_WIFI_SSID);
105145
} else {
106-
ESP_LOGE(TAG, "UNEXPECTED EVENT");
146+
ESP_LOGE(TAG, "Unexpected event");
107147
}
108148
}
109-
110-
void init_wifi(void) {
111-
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
112-
init_wifi_sta();
113-
}

main/wifi.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
#pragma once
22

3-
void init_wifi();
3+
#include <esp_err.h>
4+
#include <esp_netif_types.h>
5+
#include <esp_wifi_types.h>
6+
7+
void init_wifi_sta();
8+
esp_err_t get_ip_info(esp_netif_ip_info_t *ip_info_out);

0 commit comments

Comments
 (0)