Skip to content

Commit

Permalink
fixed warnings and reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Mast3rwaf1z committed Nov 9, 2023
1 parent ee6b360 commit 877105a
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1,794 deletions.
157 changes: 78 additions & 79 deletions main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -1,92 +1,91 @@
menu "WiFi Configuration"
menu "Measurement Station Configuration"
menu "WiFi Configuration"
config ESP_WIFI_SSID
string "WiFi SSID"
default "*EDITME*"
help
SSID (network name) for the example to connect to.

config ESP_WIFI_SSID
string "WiFi SSID"
default "*EDITME*"
help
SSID (network name) for the example to connect to.
config ESP_WIFI_PASSWORD
string "WiFi Password"
default "*EDITME*"
help
WiFi password (WPA or WPA2) for the example to use.

config ESP_WIFI_PASSWORD
string "WiFi Password"
default "*EDITME*"
help
WiFi password (WPA or WPA2) for the example to use.
choice ESP_WIFI_SAE_MODE
prompt "WPA3 SAE mode selection"
default ESP_WPA3_SAE_PWE_BOTH
help
Select mode for SAE as Hunt and Peck, H2E or both.
config ESP_WPA3_SAE_PWE_HUNT_AND_PECK
bool "HUNT AND PECK"
config ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT
bool "H2E"
config ESP_WPA3_SAE_PWE_BOTH
bool "BOTH"
endchoice

choice ESP_WIFI_SAE_MODE
prompt "WPA3 SAE mode selection"
default ESP_WPA3_SAE_PWE_BOTH
help
Select mode for SAE as Hunt and Peck, H2E or both.
config ESP_WPA3_SAE_PWE_HUNT_AND_PECK
bool "HUNT AND PECK"
config ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT
bool "H2E"
config ESP_WPA3_SAE_PWE_BOTH
bool "BOTH"
endchoice
config ESP_WIFI_PW_ID
string "PASSWORD IDENTIFIER"
depends on ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT|| ESP_WPA3_SAE_PWE_BOTH
default ""
help
password identifier for SAE H2E

config ESP_WIFI_PW_ID
string "PASSWORD IDENTIFIER"
depends on ESP_WPA3_SAE_PWE_HASH_TO_ELEMENT|| ESP_WPA3_SAE_PWE_BOTH
default ""
help
password identifier for SAE H2E
config ESP_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.

config ESP_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
prompt "WiFi Scan auth mode threshold"
default ESP_WIFI_AUTH_WPA2_PSK
help
The weakest authmode to accept in the scan mode.
This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present and ESP_WIFI_AUTH_OPEN is used.
Please select ESP_WIFI_AUTH_WEP/ESP_WIFI_AUTH_WPA_PSK incase AP is operating in WEP/WPA mode.

choice ESP_WIFI_SCAN_AUTH_MODE_THRESHOLD
prompt "WiFi Scan auth mode threshold"
default ESP_WIFI_AUTH_WPA2_PSK
help
The weakest authmode to accept in the scan mode.
This value defaults to ESP_WIFI_AUTH_WPA2_PSK incase password is present and ESP_WIFI_AUTH_OPEN is used.
Please select ESP_WIFI_AUTH_WEP/ESP_WIFI_AUTH_WPA_PSK incase AP is operating in WEP/WPA mode.
config ESP_WIFI_AUTH_OPEN
bool "OPEN"
config ESP_WIFI_AUTH_WEP
bool "WEP"
config ESP_WIFI_AUTH_WPA_PSK
bool "WPA PSK"
config ESP_WIFI_AUTH_WPA2_PSK
bool "WPA2 PSK"
config ESP_WIFI_AUTH_WPA_WPA2_PSK
bool "WPA/WPA2 PSK"
config ESP_WIFI_AUTH_WPA3_PSK
bool "WPA3 PSK"
config ESP_WIFI_AUTH_WPA2_WPA3_PSK
bool "WPA2/WPA3 PSK"
config ESP_WIFI_AUTH_WAPI_PSK
bool "WAPI PSK"
endchoice

config ESP_WIFI_AUTH_OPEN
bool "OPEN"
config ESP_WIFI_AUTH_WEP
bool "WEP"
config ESP_WIFI_AUTH_WPA_PSK
bool "WPA PSK"
config ESP_WIFI_AUTH_WPA2_PSK
bool "WPA2 PSK"
config ESP_WIFI_AUTH_WPA_WPA2_PSK
bool "WPA/WPA2 PSK"
config ESP_WIFI_AUTH_WPA3_PSK
bool "WPA3 PSK"
config ESP_WIFI_AUTH_WPA2_WPA3_PSK
bool "WPA2/WPA3 PSK"
config ESP_WIFI_AUTH_WAPI_PSK
bool "WAPI PSK"
endchoice
endmenu

endmenu

menu "MQTT Configuration"
config MQTT_URI
string "MQTT server URI"
default "*EDITME*"
help
URI to the MQTT server
menu "MQTT Configuration"
config MQTT_URI
string "MQTT server URI"
default "*EDITME*"
help
URI to the MQTT server

config INITIAL_MESSAGE_TOPIC
string "Initial MQTT message topic"
default "placeholder"
help
Initial message topic to be sent by MQTT client
config INITIAL_MESSAGE_TOPIC
string "Initial MQTT message topic"
default "placeholder"
help
Initial message topic to be sent by MQTT client

config INITIAL_MESSAGE_PAYLOAD
string "Initial MQTT message payload"
default "placeholder"
help
Initial message payload to be sent by MQTT client
endmenu
config INITIAL_MESSAGE_PAYLOAD
string "Initial MQTT message payload"
default "placeholder"
help
Initial message payload to be sent by MQTT client
endmenu

menu "Measurement Station Configuration"
config MEASUREMENT_SEND_PERIOD
int "Define the period between measurements"
default 100
Expand All @@ -102,4 +101,4 @@ menu "Measurement Station Configuration"
config DEBUG_MODE_OFF
bool "off"
endchoice
endmenu
endmenu
76 changes: 41 additions & 35 deletions main/main.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#include "wifi.c"
#include "mqtt.c"
#include "wifi.c"

void measurement_loop(void){
esp_err_t measurement_loop(int argc, char **argv) {
ESP_LOGI(TAG, "starting measurement loop");
while(true){
while (true) {
srand(time(NULL));
char* measurement[sizeof(int)];
char measurement[sizeof(int)];
sprintf(measurement, "%d", rand());
mqtt_send_message("example measurement", measurement);
long now, then;
now = then = clock();
while(now-then < (CLOCKS_PER_SEC/1000)*CONFIG_MEASUREMENT_SEND_PERIOD) now = clock();

vTaskDelay((CONFIG_MEASUREMENT_SEND_PERIOD*CLOCKS_PER_SEC)/1000);
}
return ESP_OK;
}

void measurement_loop_task(char **args){
measurement_loop(0, NULL);
}

static struct {
Expand All @@ -20,60 +24,62 @@ static struct {
struct arg_end *end;
} send_args;

int send(int argc, char** argv){
arg_parse(argc, argv, (void**) &send_args);
ESP_LOGI(TAG, "Topic: %s", send_args.topic->sval[0]);
ESP_LOGI(TAG, "Payload: %s", send_args.payload->sval[0]);
mqtt_send_message(send_args.topic->sval[0], send_args.payload->sval[0]);
return 0;
esp_err_t send(int argc, char **argv) {
arg_parse(argc, argv, (void **)&send_args);
const char* topic = *(send_args.topic->sval);
const char* payload = *(send_args.payload->sval);
ESP_LOGI(TAG, "Topic: %s", topic);
ESP_LOGI(TAG, "Payload: %s", payload);
mqtt_send_message(topic, payload);
return ESP_OK;
}

int state = 0;

int state_machine(void){
esp_err_t state_machine(int argc, char **argv) {
switch(state){
case 0:
nvs_init();
break;
case 1:
wifi_init();
wifi_init(0, NULL);
break;
case 2:
wifi_start();
wifi_start(0, NULL);
break;
case 3:
mqtt_init();
mqtt_init(0, NULL);
break;
case 4:
mqtt_start();
mqtt_start(0, NULL);
break;
case 5:
measurement_loop();
measurement_loop(0, NULL);
break;
default:
ESP_LOGW(TAG, "All states in state machine has been covered!");
return 1;
return ESP_ERR_NOT_SUPPORTED;
}
state++;
return 0;
return ESP_OK;
}

int init_all(){
wifi_init();
wifi_start();
mqtt_init();
mqtt_start();
esp_err_t init_all(int argc, char **argv) {
wifi_init(0, NULL);
wifi_start(0, NULL);
mqtt_init(0, NULL);
mqtt_start(0, NULL);
state = 5;
return 0;
return ESP_OK;
}

void app_main(void){
nvs_init();
if(DEBUG_MODE){
void app_main(void) {
nvs_init();
if(DEBUG_MODE){
ESP_LOGI(TAG, "Entering debug mode");
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.prompt = "esp-debug >";
repl_config.prompt = "esp-debug >";
repl_config.max_cmdline_length = 15;
ESP_ERROR_CHECK(esp_console_register_help_command());
const esp_console_cmd_t run_cmd = {
Expand Down Expand Up @@ -147,9 +153,9 @@ void app_main(void){
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
ESP_ERROR_CHECK(esp_console_start_repl(repl));
}
else{
init_all();
measurement_loop();
}
else {
init_all(0, NULL);
xTaskCreate(measurement_loop_task, "measurement_loop", 4096, NULL, 10, NULL);
}
}
38 changes: 16 additions & 22 deletions main/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

esp_mqtt_client_handle_t mqtt_client;


static void mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data){
esp_mqtt_event_t *event = (esp_mqtt_event_t*)event_data;
esp_mqtt_client_handle_t client = event->client;
static void mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
esp_mqtt_event_t *event = (esp_mqtt_event_t *)event_data;
ESP_LOGI(TAG, "MQTT event handler called");
switch(event_id){
switch (event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT Connected!");
break;
Expand All @@ -25,31 +23,27 @@ static void mqtt_event_handler(void *arg, esp_event_base_t event_base, int32_t e
}
}

void mqtt_init(void){
esp_err_t mqtt_init(int argc, char **argv) {
ESP_LOGI(TAG, "Configuring MQTT client");
const esp_mqtt_client_config_t mqtt_config = {
.broker = {
.address = {
.uri = CONFIG_MQTT_URI
}
}
};
const esp_mqtt_client_config_t mqtt_config = {
.broker = {.address = {.uri = CONFIG_MQTT_URI}}};

ESP_LOGI(TAG, "Initializing MQTT client");
mqtt_client = esp_mqtt_client_init(&mqtt_config);
ESP_LOGI(TAG, "Initializing MQTT client");
mqtt_client = esp_mqtt_client_init(&mqtt_config);

ESP_ERROR_CHECK(esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_PUBLISHED, mqtt_event_handler, mqtt_client));
ESP_ERROR_CHECK(esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_PUBLISHED, mqtt_event_handler, mqtt_client));
ESP_ERROR_CHECK(esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_DATA, mqtt_event_handler, mqtt_client));
ESP_ERROR_CHECK(esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_DISCONNECTED, mqtt_event_handler, mqtt_client));

return ESP_OK;
}

void mqtt_start(void){
esp_err_t mqtt_start(int argc, char **argv) {
ESP_LOGI(TAG, "Starting MQTT client");
ESP_ERROR_CHECK(esp_mqtt_client_start(mqtt_client));
esp_mqtt_client_enqueue(mqtt_client, CONFIG_INITIAL_MESSAGE_TOPIC, CONFIG_INITIAL_MESSAGE_PAYLOAD, 0, 0, 0, true);
ESP_ERROR_CHECK(esp_mqtt_client_start(mqtt_client));
esp_mqtt_client_enqueue(mqtt_client, CONFIG_INITIAL_MESSAGE_TOPIC, CONFIG_INITIAL_MESSAGE_PAYLOAD, 0, 0, 0, true);
return ESP_OK;
}

void mqtt_send_message(char* topic, char* payload){
void mqtt_send_message(const char *topic, const char *payload) {
esp_mqtt_client_enqueue(mqtt_client, topic, payload, 0, 0, 0, true);
}
}
Loading

0 comments on commit 877105a

Please sign in to comment.