Skip to content

Commit 81a2a14

Browse files
committed
feat: Create webpage to set configuration
1 parent f258e2d commit 81a2a14

File tree

11 files changed

+553
-38
lines changed

11 files changed

+553
-38
lines changed

include/b2mconfig.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef HEADER_B2MCONFIG
2+
#define HEADER_B2MCONFIG
3+
4+
typedef struct b2mconfig_def
5+
{
6+
bool not_initialized;
7+
8+
char *wifi_ssid;
9+
char *wifi_password;
10+
11+
char *broker_ip_address;
12+
char *broker_port;
13+
char *broker_username;
14+
char *broker_password;
15+
} b2mconfig_t;
16+
17+
#endif

include/ble2mqtt_config.h_example

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#ifndef HEADER_BLE2MQTT_CONFIG
22
#define HEADER_BLE2MQTT_CONFIG
33

4-
/* WIFI settings */
5-
/* wifi ssid */
6-
#define WIFI_SSID ""
7-
/* wifi password */
8-
#define WIFI_PASSWORD ""
4+
/* WIFI AccessPoint */
5+
#define WIFI_AP_SSID "ble2mqtt"
6+
#define WIFI_AP_PASSWORD "12345678"
97

108
/* MQTT settings */
11-
/* Broker uri. Examples: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mqtt.html#uri */
12-
#define MQTT_BROKER_HOST "192.168.1.14"
13-
#define MQTT_BROKER_PORT 1883
14-
#define MQTT_USER "mqttuser"
15-
#define MQTT_PASSWORD "mqttpassword"
16-
179
/* Subscribed topic */
1810
#define MQTT_TOPIC_SUBSCRIBE "/ble2mqtt/dev/#"
1911
#define MQTT_TOPIC_PUBLISH "/ble2mqtt/app/"

include/tb2mconfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef HEADER_TCONFIG
2+
#define HEADER_TCONFIG
3+
4+
#include "b2mconfig.h"
5+
6+
b2mconfig_t* b2mconfig_create(void);
7+
void b2mconfig_load(b2mconfig_t *b2mconfig);
8+
9+
void vTaskB2MConfig(void *pvParameters);
10+
#endif

sdkconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
610610
#
611611
# HTTP Server
612612
#
613-
CONFIG_HTTPD_MAX_REQ_HDR_LEN=512
613+
CONFIG_HTTPD_MAX_REQ_HDR_LEN=2048
614614
CONFIG_HTTPD_MAX_URI_LEN=512
615615
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
616616
CONFIG_HTTPD_PURGE_BUF_LEN=32

sdkconfig.esp32dev_debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS=y
677677
#
678678
# HTTP Server
679679
#
680-
CONFIG_HTTPD_MAX_REQ_HDR_LEN=512
680+
CONFIG_HTTPD_MAX_REQ_HDR_LEN=2048
681681
CONFIG_HTTPD_MAX_URI_LEN=512
682682
CONFIG_HTTPD_ERR_RESP_NO_DELAY=y
683683
CONFIG_HTTPD_PURGE_BUF_LEN=32

src/ble2mqtt/ble2mqtt.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
#include "freertos/semphr.h"
88
#include "esp_log.h"
99
#include "esp_gattc_api.h"
10-
10+
#include "ble2mqtt_config.h"
1111
#include "ble2mqtt.h"
12+
#include "b2mconfig.h"
1213

1314
static const char *TAG = "ble2mqtt";
1415

15-
ble2mqtt_t *ble2mqtt_create(void)
16+
ble2mqtt_t *ble2mqtt_create()
1617
{
1718
ble2mqtt_t *ble2mqtt = malloc(sizeof(ble2mqtt_t));
1819

@@ -27,10 +28,11 @@ ble2mqtt_t *ble2mqtt_create(void)
2728
return ble2mqtt;
2829
}
2930

30-
bool ble2mqtt_init(ble2mqtt_t *ble2mqtt)
31+
bool ble2mqtt_init(ble2mqtt_t *ble2mqtt, b2mconfig_t *b2mconfig)
3132
{
3233
ESP_LOGI(TAG, "Init ble2mqtt_t");
3334

35+
ble2mqtt->b2mconfig = b2mconfig;
3436
ble2mqtt->s_event_group = xEventGroupCreate();
3537
ble2mqtt->xMutexDevices = xSemaphoreCreateMutex();
3638
if (!ble2mqtt->xMutexDevices)

src/ble2mqtt/ble2mqtt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "freertos/event_groups.h"
88
#include "esp_gattc_api.h"
99
#include "freertos/queue.h"
10+
#include "b2mconfig.h"
1011

1112
#define BLE2MQTT_WIFI_CONNECTED_BIT BIT0 /* Connected to WiFi */
1213
#define BLE2MQTT_MQTT_CONNECTED_BIT BIT1 /* Connected to MQTT Broker */
@@ -66,17 +67,19 @@ typedef struct ble2mqtt_def
6667
uint8_t devices_len; /* Number of items in 'devices' array */
6768

6869
QueueHandle_t xQueue; /* Messages from btle devices */
70+
71+
b2mconfig_t *b2mconfig; /* Configuration (wifi, mqtt)*/
6972
} ble2mqtt_t;
7073

7174
/**
7275
* Create instance of ble2mqtt_t struct
7376
*/
74-
ble2mqtt_t *ble2mqtt_create(void);
77+
ble2mqtt_t *ble2mqtt_create();
7578

7679
/**
7780
* Initialize ble2mqtt struct
7881
* @return - true if init success
7982
*/
80-
bool ble2mqtt_init(ble2mqtt_t *ble2mqtt);
83+
bool ble2mqtt_init(ble2mqtt_t *ble2mqtt, b2mconfig_t *b2mconfig);
8184

8285
#endif

src/main.c

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
#include "esp_event.h"
1111
#include "esp_log.h"
1212
#include "nvs_flash.h"
13+
#include "driver/gpio.h"
1314

1415
#include "twifi.h"
1516
#include "tbt.h"
1617
#include "tmqtt.h"
18+
#include "tb2mconfig.h"
1719
#include "ble2mqtt/ble2mqtt.h"
1820
#ifndef DISABLETDEBUG
1921
#include "tdebug.h"
@@ -45,33 +47,56 @@ void app_main(void)
4547
ESP_ERROR_CHECK(nvs_flash_erase());
4648
ret = nvs_flash_init();
4749
}
50+
4851
ESP_ERROR_CHECK(ret);
4952
ESP_ERROR_CHECK(esp_event_loop_create_default());
5053

51-
ble2mqtt_t *ble2mqtt = ble2mqtt_create();
52-
if (!ble2mqtt)
54+
ESP_ERROR_CHECK(gpio_set_direction(GPIO_NUM_4, GPIO_MODE_INPUT));
55+
int reset_config = gpio_get_level(GPIO_NUM_4);
56+
57+
b2mconfig_t *b2mconfig = b2mconfig_create();
58+
if (b2mconfig == NULL)
5359
{
5460
ESP_LOGE(TAG, "malloc error!");
5561
while (1)
5662
;
5763
}
58-
if (ble2mqtt_init(ble2mqtt) == false)
64+
else
5965
{
60-
ESP_LOGE(TAG, "ble2mqtt_init error!");
61-
while (1)
62-
;
66+
b2mconfig_load(b2mconfig);
6367
}
6468

69+
if (reset_config == 1 || b2mconfig->not_initialized)
70+
{
71+
xTaskCreate(vTaskB2MConfig, "task_b2mconfig", (2048 * 6), (void *)b2mconfig, 8, NULL);
72+
}
73+
else
74+
{
75+
ble2mqtt_t *ble2mqtt = ble2mqtt_create();
76+
if (!ble2mqtt)
77+
{
78+
ESP_LOGE(TAG, "malloc error!");
79+
while (1)
80+
;
81+
}
82+
if (ble2mqtt_init(ble2mqtt, b2mconfig) == false)
83+
{
84+
ESP_LOGE(TAG, "ble2mqtt_init error!");
85+
while (1)
86+
;
87+
}
88+
6589
#ifndef DISABLETWIFI
66-
xTaskCreate(vTaskWifi, "task_wifi", (2048 * 6), (void *)ble2mqtt, 8, NULL);
90+
xTaskCreate(vTaskWifi, "task_wifi", (2048 * 6), (void *)ble2mqtt, 8, NULL);
6791
#endif
6892
#ifndef DISABLETMQTT
69-
xTaskCreate(vTaskMqtt, "task_mqtt", (2048 * 6), (void *)ble2mqtt, 10, NULL);
93+
xTaskCreate(vTaskMqtt, "task_mqtt", (2048 * 6), (void *)ble2mqtt, 10, NULL);
7094
#endif
7195
#ifndef DISABLETBT
72-
xTaskCreate(vTaskBt, "task_bt", (2048 * 6), (void *)ble2mqtt, 12, NULL);
96+
xTaskCreate(vTaskBt, "task_bt", (2048 * 6), (void *)ble2mqtt, 12, NULL);
7397
#endif
7498
#ifndef DISABLETDEBUG
75-
xTaskCreate(vTaskDebug, "task_debug", 2048, (void *)ble2mqtt, 20, NULL);
99+
xTaskCreate(vTaskDebug, "task_debug", 2048, (void *)ble2mqtt, 20, NULL);
76100
#endif
101+
}
77102
}

0 commit comments

Comments
 (0)