Skip to content

Commit

Permalink
Update mqtt-dashboard, adopt new UI, break OTA for now..
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Feb 14, 2024
1 parent daa47e8 commit a86bde7
Show file tree
Hide file tree
Showing 16 changed files with 392 additions and 290 deletions.
1 change: 0 additions & 1 deletion examples/mqtt-dashboard/dashboard/components.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/mqtt-dashboard/dashboard/history.min.js

This file was deleted.

1 change: 0 additions & 1 deletion examples/mqtt-dashboard/dashboard/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>
<body class="h-full"></body>
<script src="history.min.js"></script>
<script type="module" src="main.js"></script>
</html>
2 changes: 1 addition & 1 deletion examples/mqtt-dashboard/dashboard/main.css

Large diffs are not rendered by default.

485 changes: 317 additions & 168 deletions examples/mqtt-dashboard/dashboard/main.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions examples/mqtt-dashboard/device/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions examples/mqtt-dashboard/device/hal.c
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions examples/mqtt-dashboard/device/hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "mongoose.h"

bool gpio_write(uint16_t pin, bool status);
bool gpio_read(uint16_t pin);
19 changes: 2 additions & 17 deletions examples/mqtt-dashboard/device/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,22 @@
// All rights reserved

#include "net.h"
#include "hal.h"

// Handle interrupts, like Ctrl-C
static int s_signo;
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;

// 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) {
Expand Down
54 changes: 35 additions & 19 deletions examples/mqtt-dashboard/device/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

#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;

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;
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions examples/mqtt-dashboard/device/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include "mongoose.h"
#include "hal.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -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
9 changes: 2 additions & 7 deletions examples/rp2040/pico-w5500/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ include(pico-sdk/pico_sdk_init.cmake)

project(firmware)
pico_sdk_init()

add_executable(firmware
main.c mongoose.c net.c packed_fs.c)

target_include_directories(firmware PUBLIC
.)
add_executable(firmware main.c mongoose.c net.c packed_fs.c)
target_include_directories(firmware PUBLIC .)

target_link_libraries(firmware pico_stdlib hardware_spi pico_rand)
pico_add_extra_outputs(firmware) # create map/bin/hex file etc.
Expand All @@ -19,7 +15,6 @@ pico_enable_stdio_uart(firmware 1) # to the UART, for remote testing
# Mongoose build flags
add_definitions(-DMG_ENABLE_TCPIP=1)
add_definitions(-DMG_ENABLE_PACKED_FS=1)
add_definitions(-DMG_ENABLE_MBEDTLS=0) # TODO(cpq): enable
add_definitions(-DMG_ENABLE_CUSTOM_RANDOM=1)
add_definitions(-DMG_ENABLE_POSIX_FS=0)

Expand Down
1 change: 1 addition & 0 deletions examples/rp2040/pico-w5500/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ifeq ($(OS),Windows_NT)
RM = cmd /C del /Q /F /S
MKBUILD = if not exist build mkdir build
endif
.PHONY: build

all example:
true
Expand Down
63 changes: 0 additions & 63 deletions examples/rp2040/pico-w5500/mbedtls_config.h

This file was deleted.

2 changes: 1 addition & 1 deletion examples/stm32/nucleo-h723zg-make-baremetal-builtin/net.c
2 changes: 1 addition & 1 deletion examples/stm32/nucleo-h723zg-make-baremetal-builtin/net.h

0 comments on commit a86bde7

Please sign in to comment.