WIP meteostation with ESP8266
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
esp8266-meteo/main/cmd_ip.c

127 lines
3.7 KiB

/* 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) );
}