parent
1bb827c5db
commit
51877aa741
@ -0,0 +1,9 @@ |
||||
wifi.use_dhcpc - int 0/1 - use dynamic IP |
||||
wifi.static_ip - str |
||||
wifi.static_gw - str |
||||
wifi.static_mask - str |
||||
|
||||
mqtt.topic - str, base topic, ends with slash |
||||
mqtt.broker - str, mqtt broker |
||||
|
||||
/* wifi ssid/pw are stored in system nvs */ |
@ -0,0 +1,127 @@ |
||||
/* Console example — WiFi commands
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.) |
||||
|
||||
Unless required by applicable law or agreed to in writing, this |
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
||||
CONDITIONS OF ANY KIND, either express or implied. |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include "esp_log.h" |
||||
#include "esp_console.h" |
||||
#include "argtable3/argtable3.h" |
||||
#include "esp_event_loop.h" |
||||
#include "cmd_ip.h" |
||||
|
||||
#include "nvs.h" |
||||
|
||||
/** Arguments used by 'join' function */ |
||||
static struct { |
||||
struct arg_lit *dhcp_on; |
||||
struct arg_lit *dhcp_off; |
||||
struct arg_str *static_ip; |
||||
struct arg_str *static_gw; |
||||
struct arg_str *static_mask; |
||||
struct arg_str *static_dns1; |
||||
struct arg_str *static_dns2; |
||||
struct arg_end *end; |
||||
} ip_args; |
||||
|
||||
static int ipcmd(int argc, char** argv) |
||||
{ |
||||
esp_err_t rv; |
||||
|
||||
int nerrors = arg_parse(argc, argv, (void**) &ip_args); |
||||
if (nerrors != 0) { |
||||
arg_print_errors(stderr, ip_args.end, argv[0]); |
||||
return 1; |
||||
} |
||||
|
||||
nvs_handle_t hnvs; |
||||
ESP_ERROR_CHECK( nvs_open("wifi", NVS_READWRITE, &hnvs) ); |
||||
|
||||
if (ip_args.dhcp_on->count) { |
||||
nvs_set_u8(hnvs, "use_dhcpc", 1); |
||||
} else if (ip_args.dhcp_off->count) { |
||||
nvs_set_u8(hnvs, "use_dhcpc", 0); |
||||
} |
||||
|
||||
if (ip_args.static_ip->count) { |
||||
nvs_set_str(hnvs, "static_ip", ip_args.static_ip->sval[0]); |
||||
} |
||||
|
||||
if (ip_args.static_gw->count) { |
||||
nvs_set_str(hnvs, "static_gw", ip_args.static_gw->sval[0]); |
||||
} |
||||
|
||||
if (ip_args.static_mask->count) { |
||||
nvs_set_str(hnvs, "static_mask", ip_args.static_mask->sval[0]); |
||||
} |
||||
|
||||
if (ip_args.static_dns1->count) { |
||||
nvs_set_str(hnvs, "static_dns1", ip_args.static_dns1->sval[0]); |
||||
} |
||||
|
||||
if (ip_args.static_dns2->count) { |
||||
nvs_set_str(hnvs, "static_dns2", ip_args.static_dns2->sval[0]); |
||||
} |
||||
|
||||
// read current config
|
||||
|
||||
uint8_t use_dhcpc; |
||||
rv = nvs_get_u8(hnvs, "use_dhcpc", &use_dhcpc); |
||||
if (rv != ESP_OK) { |
||||
use_dhcpc = 1; |
||||
} |
||||
|
||||
char ip[16], gw[16], mask[16], dns1[16], dns2[16]; |
||||
size_t iplen = 16; |
||||
size_t gwlen = 16; |
||||
size_t masklen = 16; |
||||
size_t dns1len = 16; |
||||
size_t dns2len = 16; |
||||
ip[0] = gw[0] = mask[0] = dns1[0] = dns2[0] = 0; |
||||
|
||||
nvs_get_str(hnvs, "static_ip", ip, &iplen); |
||||
nvs_get_str(hnvs, "static_gw", gw, &gwlen); |
||||
nvs_get_str(hnvs, "static_mask", mask, &masklen); |
||||
nvs_get_str(hnvs, "static_dns1", dns1, &dns1len); |
||||
nvs_get_str(hnvs, "static_dns2", dns2, &dns2len); |
||||
|
||||
printf("DHCP = %s\n", use_dhcpc ? "Yes (dynamic IP)" : "No (static IP)"); |
||||
printf("Saved static IP = %s\n", ip); |
||||
printf("Saved static GW = %s\n", gw); |
||||
printf("Saved static MASK = %s\n", mask); |
||||
printf("Saved static DNS1 = %s\n", dns1); |
||||
printf("Saved static DNS2 = %s\n", dns2); |
||||
|
||||
nvs_close(hnvs); |
||||
|
||||
printf("Any changes are applied after restart.\n"); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
void console_register_ip() |
||||
{ |
||||
ip_args.dhcp_on = arg_lit0("d", "dynamic", "Enable DHCP"); |
||||
ip_args.dhcp_off = arg_lit0("s", "static", "Disable DHCP (use static)"); |
||||
ip_args.static_ip = arg_str0("a", "ip", "<IP>", "Set static IP"); |
||||
ip_args.static_gw = arg_str0("g", "gw", "<IP>", "Set static GW"); |
||||
ip_args.static_mask = arg_str0("m", "mask", "<IP>", "Set static MASK (e.g. 255.255.255.0)"); |
||||
ip_args.static_dns1 = arg_str0("n", "dns1", "<DNS>", "Set static nameserver1 (e.g. 8.8.8.8)"); |
||||
ip_args.static_dns2 = arg_str0("N", "dns2", "<DNS>", "Set static nameserver2 (e.g. 8.8.4.4)"); |
||||
ip_args.end = arg_end(5); |
||||
|
||||
const esp_console_cmd_t ip_cmd = { |
||||
.command = "ip", |
||||
.help = "Configure TCP/IP", |
||||
.hint = NULL, |
||||
.func = &ipcmd, |
||||
.argtable = &ip_args |
||||
}; |
||||
|
||||
ESP_ERROR_CHECK( esp_console_cmd_register(&ip_cmd) ); |
||||
} |
@ -0,0 +1,12 @@ |
||||
#pragma once |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
void console_register_ip(); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
@ -0,0 +1,70 @@ |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#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", "<TOPIC>", "Set base of the MQTT topic (including slash)"); |
||||
mqtt_args.broker = arg_str0("b", "broker", "<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) ); |
||||
} |
@ -0,0 +1,12 @@ |
||||
#pragma once |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
void console_register_mqtt(); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
@ -0,0 +1,128 @@ |
||||
/**
|
||||
* TODO file description |
||||
*/ |
||||
|
||||
#include <mqtt_client.h> |
||||
#include <nvs.h> |
||||
#include <esp_log.h> |
||||
#include "mqttpub.h" |
||||
|
||||
#define TAG "mq" |
||||
|
||||
static bool mqtt_inited = false; |
||||
|
||||
esp_mqtt_client_handle_t s_client; |
||||
static char s_basetopic[128]; |
||||
static char s_topicbuf[256]; |
||||
|
||||
static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) |
||||
{ |
||||
esp_mqtt_client_handle_t client = event->client; |
||||
int msg_id; |
||||
// your_context_t *context = event->context;
|
||||
switch (event->event_id) { |
||||
case MQTT_EVENT_CONNECTED: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); |
||||
|
||||
// msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
|
||||
|
||||
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||||
//
|
||||
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
|
||||
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
//
|
||||
// msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
|
||||
// ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
//
|
||||
// msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
|
||||
// ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
|
||||
break; |
||||
case MQTT_EVENT_DISCONNECTED: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); |
||||
break; |
||||
|
||||
case MQTT_EVENT_SUBSCRIBED: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); |
||||
// msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
|
||||
// ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||||
break; |
||||
case MQTT_EVENT_UNSUBSCRIBED: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); |
||||
break; |
||||
case MQTT_EVENT_PUBLISHED: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); |
||||
break; |
||||
case MQTT_EVENT_DATA: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_DATA"); |
||||
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); |
||||
printf("DATA=%.*s\r\n", event->data_len, event->data); |
||||
break; |
||||
case MQTT_EVENT_ERROR: |
||||
ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); |
||||
break; |
||||
default: |
||||
ESP_LOGI(TAG, "Other event id:%d", event->event_id); |
||||
break; |
||||
} |
||||
return ESP_OK; |
||||
} |
||||
|
||||
|
||||
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { |
||||
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); |
||||
mqtt_event_handler_cb(event_data); |
||||
} |
||||
|
||||
void initialize_mqttpub() |
||||
{ |
||||
char brokerurl[128]; |
||||
size_t len; |
||||
esp_err_t rv; |
||||
nvs_handle_t hnvs; |
||||
|
||||
ESP_ERROR_CHECK( nvs_open("mqtt", NVS_READWRITE, &hnvs) ); |
||||
|
||||
len = 128; |
||||
brokerurl[0] = 0; |
||||
rv = nvs_get_str(hnvs, "broker", brokerurl, &len); |
||||
if (rv != ESP_OK || brokerurl[0] == 0) { |
||||
ESP_LOGW(TAG, "Missing MQTT broker!"); |
||||
return; |
||||
} |
||||
|
||||
len = 128; |
||||
s_basetopic[0] = 0; |
||||
rv = nvs_get_str(hnvs, "topic", s_basetopic, &len); |
||||
if (rv != ESP_OK) { |
||||
ESP_LOGW(TAG, "Bad MQTT topic!"); |
||||
return; |
||||
} |
||||
ESP_LOGI(TAG, "mqtt.topic = %s", s_basetopic); |
||||
|
||||
nvs_close(hnvs); |
||||
|
||||
esp_mqtt_client_config_t mqtt_cfg = { |
||||
.uri = brokerurl, |
||||
}; |
||||
|
||||
s_client = esp_mqtt_client_init(&mqtt_cfg); |
||||
ESP_ERROR_CHECK(esp_mqtt_client_register_event(s_client, ESP_EVENT_ANY_ID, mqtt_event_handler, s_client)); |
||||
ESP_ERROR_CHECK(esp_mqtt_client_start(s_client)); |
||||
|
||||
mqtt_inited = true; |
||||
} |
||||
|
||||
void mqtt_publish(const char *topic, const char *payload) |
||||
{ |
||||
if (!mqtt_inited) { |
||||
ESP_LOGE(TAG, "MQTT not inited"); |
||||
return; |
||||
} |
||||
|
||||
snprintf(s_topicbuf, 256, "%s%s", s_basetopic, topic); |
||||
|
||||
ESP_LOGI(TAG, "MQTT pub to %s: %s", s_topicbuf, payload); |
||||
|
||||
|
||||
esp_mqtt_client_publish(s_client, s_topicbuf, payload, (int) strlen(payload), 1, 0); |
||||
} |
@ -0,0 +1,9 @@ |
||||
/**
|
||||
* TODO file description |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
void initialize_mqttpub(); |
||||
|
||||
void mqtt_publish(const char *topic, const char *payload); |
@ -0,0 +1,125 @@ |
||||
/**
|
||||
* TODO file description |
||||
*/ |
||||
|
||||
#include <esp_err.h> |
||||
#include <nvs.h> |
||||
#include <esp_log.h> |
||||
#include <string.h> |
||||
#include <lwip/ip4_addr.h> |
||||
#include <nvs_flash.h> |
||||
#include "nvsconfig.h" |
||||
|
||||
#define TAG_CONFIG "config" |
||||
|
||||
void initialize_nvs() |
||||
{ |
||||
esp_err_t rv; |
||||
nvs_handle_t hnvs; |
||||
|
||||
char tmps[128]; |
||||
size_t len; |
||||
uint8_t u8val; |
||||
|
||||
|
||||
esp_err_t err = nvs_flash_init(); |
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES) { |
||||
ESP_ERROR_CHECK( nvs_flash_erase() ); |
||||
err = nvs_flash_init(); |
||||
} |
||||
ESP_ERROR_CHECK(err); |
||||
|
||||
|
||||
|
||||
/* Wifi namespace */ |
||||
|
||||
ESP_ERROR_CHECK( nvs_open("wifi", NVS_READWRITE, &hnvs) ); |
||||
|
||||
u8val = 99; |
||||
rv = nvs_get_u8(hnvs, "use_dhcpc", &u8val); |
||||
if (rv != ESP_OK || (u8val != 0 && u8val != 1)) { |
||||
ESP_LOGW(TAG_CONFIG, "Invalid dhcp option - set to default"); |
||||
u8val = 1; |
||||
nvs_set_u8(hnvs, "use_dhcpc", u8val); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.use_dhcpc = %d", u8val); |
||||
|
||||
len = 16; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "static_ip", tmps, &len); |
||||
if (rv != ESP_OK || IPADDR_NONE == ipaddr_addr(tmps)) { |
||||
strcpy(tmps, "192.168.56.2"); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid static ip - set to default"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "static_ip", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.static_ip = %s", tmps); |
||||
|
||||
len = 16; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "static_gw", tmps, &len); |
||||
if (rv != ESP_OK || IPADDR_NONE == ipaddr_addr(tmps)) { |
||||
strcpy(tmps, "192.168.0.1"); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid static gw - set to default"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "static_gw", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.static_gw = %s", tmps); |
||||
|
||||
len = 16; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "static_mask", tmps, &len); |
||||
if (rv != ESP_OK || IPADDR_NONE == ipaddr_addr(tmps)) { |
||||
strcpy(tmps, "255.255.255.0"); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid static mask - set to default"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "static_mask", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.static_mask = %s", tmps); |
||||
|
||||
len = 16; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "static_dns1", tmps, &len); |
||||
if (rv != ESP_OK || IPADDR_NONE == ipaddr_addr(tmps)) { |
||||
strcpy(tmps, "8.8.8.8"); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid static DNS1 - set to default"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "static_dns1", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.static_dns1 = %s", tmps); |
||||
|
||||
len = 16; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "static_dns2", tmps, &len); |
||||
if (rv != ESP_OK) { |
||||
strcpy(tmps, ""); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid static DNS2 - set to empty"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "static_dns2", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "wifi.static_dns2 = %s", tmps); |
||||
|
||||
nvs_close(hnvs); |
||||
|
||||
|
||||
/* MQTT namespace */ |
||||
|
||||
ESP_ERROR_CHECK( nvs_open("mqtt", NVS_READWRITE, &hnvs) ); |
||||
|
||||
len = 128; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "broker", tmps, &len); |
||||
if (rv != ESP_OK) { |
||||
strcpy(tmps, ""); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid broker addr - set to empty"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "broker", tmps)); |
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "mqtt.broker = %s", tmps); |
||||
|
||||
len = 128; |
||||
tmps[0] = 0; |
||||
rv = nvs_get_str(hnvs, "topic", tmps, &len); |
||||
if (rv != ESP_OK || tmps[0] == 0) { |
||||
strcpy(tmps, "/meteo/"); |
||||
ESP_LOGW(TAG_CONFIG, "Invalid mqtt topic - set to default"); |
||||
ESP_ERROR_CHECK(nvs_set_str(hnvs, "topic", tmps)); // default
|
||||
} |
||||
ESP_LOGI(TAG_CONFIG, "mqtt.topic = %s", tmps); |
||||
|
||||
nvs_close(hnvs); |
||||
} |
@ -0,0 +1,7 @@ |
||||
/**
|
||||
* TODO file description |
||||
*/ |
||||
|
||||
#pragma once |
||||
|
||||
void initialize_nvs(); |
Loading…
Reference in new issue