#include #include #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "esp_event_loop.h" #include "cmd_mqtt.h" #include "nvs.h" /** Arguments used by 'join' function */ static struct { struct arg_str *broker; struct arg_str *topic; struct arg_end *end; } mqtt_args; static int mqttcmd(int argc, char** argv) { int nerrors = arg_parse(argc, argv, (void**) &mqtt_args); if (nerrors != 0) { arg_print_errors(stderr, mqtt_args.end, argv[0]); return 1; } nvs_handle_t hnvs; ESP_ERROR_CHECK( nvs_open("mqtt", NVS_READWRITE, &hnvs) ); if (mqtt_args.broker->count) { nvs_set_str(hnvs, "broker", mqtt_args.broker->sval[0]); } if (mqtt_args.topic->count) { nvs_set_str(hnvs, "topic", mqtt_args.topic->sval[0]); } // read current config size_t topic_len = 128; char topic[128] = ""; nvs_get_str(hnvs, "topic", topic, &topic_len); size_t broker_len = 128; char broker[128] = ""; nvs_get_str(hnvs, "broker", broker, &broker_len); printf("Topic = %s\n", topic); printf("Broker = %s\n", broker); nvs_close(hnvs); printf("Any changes are applied after restart.\n"); return 0; } void console_register_mqtt() { mqtt_args.topic = arg_str0("t", "topic", "", "Set base of the MQTT topic (including slash)"); mqtt_args.broker = arg_str0("b", "broker", "", "Set MQTT broker IP addr"); mqtt_args.end = arg_end(2); const esp_console_cmd_t mqtt_cmd = { .command = "mqtt", .help = "Configure MQTT", .hint = NULL, .func = &mqttcmd, .argtable = &mqtt_args }; ESP_ERROR_CHECK( esp_console_cmd_register(&mqtt_cmd) ); }