mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-10 16:31:44 +00:00
Update mqtt-dashboard, adopt new UI, break OTA for now..
This commit is contained in:
1 parent
daa47e8272
commit
a86bde7b3f
16 files changed
+399
-297
No files matched your search
@@ -1,8 +1,8 @@
|
||||
PROG ?= ./example # Program we are building
|
||||
DELETE ?= rm -rf # Command to remove files
|
||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||
SOURCES = main.c net.c mongoose.c # Source code files
|
||||
CFLAGS ?= -W -Wall -Wextra -g -I. # Build options
|
||||
PROG ?= ./example # Program we are building
|
||||
DELETE ?= rm -rf # Command to remove files
|
||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||
SOURCES = main.c net.c hal.c mongoose.c # Source code files
|
||||
CFLAGS ?= -W -Wall -Wextra -g -I. # Build options
|
||||
|
||||
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
|
||||
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "hal.h"
|
||||
|
||||
#define MAX_PIN_NO 144
|
||||
|
||||
// Mocked device pins
|
||||
static bool s_pins[MAX_PIN_NO];
|
||||
|
||||
bool gpio_write(uint16_t pin, bool status) {
|
||||
bool ok = false;
|
||||
if (pin >= 0 && pin < MAX_PIN_NO) {
|
||||
s_pins[pin] = status;
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool gpio_read(uint16_t pin) {
|
||||
return (pin >= 0 && pin < MAX_PIN_NO) ? s_pins[pin] : false;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
bool gpio_write(uint16_t pin, bool status);
|
||||
bool gpio_read(uint16_t pin);
|
||||
@@ -2,6 +2,7 @@
|
||||
// All rights reserved
|
||||
|
||||
#include "net.h"
|
||||
#include "hal.h"
|
||||
|
||||
// Handle interrupts, like Ctrl-C
|
||||
static int s_signo;
|
||||
@@ -9,22 +10,6 @@ static void signal_handler(int signo) {
|
||||
s_signo = signo;
|
||||
}
|
||||
|
||||
// Mocked device pins
|
||||
static bool s_pins[NUM_PINS];
|
||||
|
||||
bool hal_gpio_write(int pin, bool status) {
|
||||
bool ok = false;
|
||||
if (pin >= 0 && pin < NUM_PINS) {
|
||||
s_pins[pin] = status;
|
||||
ok = true;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool hal_gpio_read(int pin) {
|
||||
return (pin >= 0 && pin < NUM_PINS) ? s_pins[pin] : false;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
struct mg_mgr mgr;
|
||||
int i;
|
||||
@@ -32,7 +17,7 @@ int main(int argc, char *argv[]) {
|
||||
// Parse command-line flags
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-u") == 0 && argv[i + 1] != NULL) {
|
||||
g_url = argv[++i];
|
||||
g_mqtt_server_url = argv[++i];
|
||||
} else if (strcmp(argv[i], "-i") == 0 && argv[i + 1] != NULL) {
|
||||
g_device_id = strdup(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-t") == 0 && argv[i + 1] != NULL) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include "net.h"
|
||||
|
||||
char *g_url = MQTT_SERVER_URL;
|
||||
char *g_mqtt_server_url = MQTT_SERVER_URL;
|
||||
char *g_root_topic = MQTT_ROOT_TOPIC;
|
||||
char *g_device_id;
|
||||
|
||||
@@ -11,9 +11,12 @@ static uint8_t s_qos = 1; // MQTT QoS
|
||||
static struct mg_connection *s_conn; // MQTT Client connection
|
||||
static struct mg_rpc *s_rpc = NULL; // List of registered RPC methods
|
||||
|
||||
#define MAX_PINS 20
|
||||
struct device_config {
|
||||
int pins[NUM_PINS]; // State of the GPIO pins
|
||||
int log_level; // Device logging level, 0-4
|
||||
int log_level; // Device logging level, 0-4
|
||||
int pin_count; // Number of pins to handle
|
||||
uint16_t pin_map[MAX_PINS]; // Pins to handle
|
||||
bool pin_state[MAX_PINS]; // State of the GPIO pins
|
||||
};
|
||||
|
||||
static struct device_config s_device_config;
|
||||
@@ -75,12 +78,16 @@ static void publish_status(struct mg_connection *c) {
|
||||
|
||||
// Print JSON notification into the io buffer
|
||||
mg_xprintf(mg_pfn_iobuf, &io,
|
||||
"{%m:%m,%m:{%m:%m,%m:%d,%m:[%M],%m:%M,%m:%M}}", //
|
||||
"{%m:%m,%m:{%m:%m,%m:%d,%m:%d,%m:[%M],%m:[%M],%m:%M,%m:%M}}", //
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"), //
|
||||
MG_ESC("status"), MG_ESC("online"), //
|
||||
MG_ESC(("log_level")), s_device_config.log_level, //
|
||||
MG_ESC(("pins")), print_ints, s_device_config.pins, NUM_PINS, //
|
||||
MG_ESC(("crnt_fw")), print_fw_status, MG_FIRMWARE_CURRENT, //
|
||||
MG_ESC(("pin_count")), s_device_config.pin_count, //
|
||||
MG_ESC(("pin_map")), print_ints, s_device_config.pin_map,
|
||||
s_device_config.pin_count, //
|
||||
MG_ESC(("pin_state")), print_ints, s_device_config.pin_state,
|
||||
s_device_config.pin_count, //
|
||||
MG_ESC(("crnt_fw")), print_fw_status, MG_FIRMWARE_CURRENT, //
|
||||
MG_ESC(("prev_fw")), print_fw_status, MG_FIRMWARE_PREVIOUS);
|
||||
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
@@ -90,7 +97,6 @@ static void publish_status(struct mg_connection *c) {
|
||||
pub_opts.qos = s_qos;
|
||||
pub_opts.retain = true;
|
||||
mg_mqtt_pub(c, &pub_opts);
|
||||
MG_INFO(("%lu PUBLISHED %s -> %.*s", c->id, topic, io.len, io.buf));
|
||||
mg_iobuf_free(&io);
|
||||
}
|
||||
|
||||
@@ -103,7 +109,6 @@ static void publish_response(struct mg_connection *c, char *buf, size_t len) {
|
||||
pub_opts.message = mg_str_n(buf, len);
|
||||
pub_opts.qos = s_qos;
|
||||
mg_mqtt_pub(c, &pub_opts);
|
||||
MG_INFO(("%lu PUBLISHED %s -> %.*s", c->id, topic, len, buf));
|
||||
}
|
||||
|
||||
static void subscribe(struct mg_connection *c) {
|
||||
@@ -120,20 +125,25 @@ static void subscribe(struct mg_connection *c) {
|
||||
|
||||
static void rpc_config_set(struct mg_rpc_req *r) {
|
||||
struct device_config dc = s_device_config;
|
||||
dc.pin_count = (int) mg_json_get_long(r->frame, "$.params.pin_count", -1);
|
||||
dc.log_level = (int) mg_json_get_long(r->frame, "$.params.log_level", -1);
|
||||
|
||||
if (dc.log_level < 0 || dc.log_level > MG_LL_VERBOSE) {
|
||||
mg_rpc_err(r, -32602, "Log level must be from 0 to 4");
|
||||
} else if (dc.pin_count <= 0 || dc.pin_count > MAX_PINS) {
|
||||
mg_rpc_err(r, -32602, "Pin count must be from 1 to %d", MAX_PINS);
|
||||
} else {
|
||||
int i, val;
|
||||
for (i = 0; i < NUM_PINS; i++) {
|
||||
char path[20];
|
||||
mg_snprintf(path, sizeof(path), "$.params.pins[%lu]", i);
|
||||
val = (int) mg_json_get_long(r->frame, path, -1);
|
||||
if (val >= 0 && val != dc.pins[i]) {
|
||||
dc.pins[i] = val;
|
||||
hal_gpio_write((int) i, val);
|
||||
for (i = 0; i < dc.pin_count; i++) {
|
||||
char path[50];
|
||||
mg_snprintf(path, sizeof(path), "$.params.pin_map[%d]", i);
|
||||
dc.pin_map[i] = (uint16_t) mg_json_get_long(r->frame, path, 0);
|
||||
mg_snprintf(path, sizeof(path), "$.params.pin_state[%d]", i);
|
||||
if ((val = (int) mg_json_get_long(r->frame, path, -1)) >= 0) {
|
||||
gpio_write(dc.pin_map[i], val);
|
||||
}
|
||||
dc.pin_state[i] = gpio_read(dc.pin_map[i]);
|
||||
// MG_INFO(("%d %d %d", i, dc.pin_map[i], dc.pin_state[i]));
|
||||
}
|
||||
mg_log_set(dc.log_level);
|
||||
s_device_config = dc;
|
||||
@@ -201,7 +211,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
MG_ERROR(("%lu ERROR %s", c->id, (char *) ev_data));
|
||||
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||
// MQTT connect is successful
|
||||
MG_INFO(("%lu CONNECTED to %s", c->id, g_url));
|
||||
MG_INFO(("%lu CONNECTED to %s", c->id, g_mqtt_server_url));
|
||||
subscribe(c);
|
||||
publish_status(c);
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
@@ -244,7 +254,7 @@ static void timer_reconnect(void *arg) {
|
||||
opts.keepalive = MQTT_KEEPALIVE_SEC;
|
||||
opts.retain = true;
|
||||
opts.message = mg_str(message);
|
||||
s_conn = mg_mqtt_connect(mgr, g_url, &opts, fn, NULL);
|
||||
s_conn = mg_mqtt_connect(mgr, g_mqtt_server_url, &opts, fn, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -257,8 +267,14 @@ void web_init(struct mg_mgr *mgr) {
|
||||
int i, ping_interval_ms = MQTT_KEEPALIVE_SEC * 1000 - 500;
|
||||
set_device_id();
|
||||
s_device_config.log_level = (int) mg_log_level;
|
||||
for (i = 0; i < NUM_PINS; i++) {
|
||||
s_device_config.pins[i] = hal_gpio_read(i);
|
||||
s_device_config.pin_count = 5;
|
||||
s_device_config.pin_map[0] = 10;
|
||||
s_device_config.pin_map[1] = 11;
|
||||
s_device_config.pin_map[2] = 12;
|
||||
s_device_config.pin_map[3] = 13;
|
||||
s_device_config.pin_map[4] = 25;
|
||||
for (i = 0; i < s_device_config.pin_count; i++) {
|
||||
s_device_config.pin_state[i] = gpio_read(s_device_config.pin_map[i]);
|
||||
}
|
||||
|
||||
// Configure JSON-RPC functions we're going to handle
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "mongoose.h"
|
||||
#include "hal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -12,18 +13,14 @@ extern "C" {
|
||||
#define MQTT_KEEPALIVE_SEC 60
|
||||
#define MQTT_SERVER_URL "mqtt://broker.hivemq.com:1883"
|
||||
#define MQTT_ROOT_TOPIC "mg_mqtt_dashboard"
|
||||
#define NUM_PINS 30
|
||||
|
||||
extern char *g_url;
|
||||
extern char *g_mqtt_server_url;
|
||||
extern char *g_device_id;
|
||||
extern char *g_root_topic;
|
||||
|
||||
void web_init(struct mg_mgr *mgr);
|
||||
void web_free(void);
|
||||
|
||||
bool hal_gpio_write(int pin, bool status);
|
||||
bool hal_gpio_read(int pin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user