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 f2ed4bd
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 288 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
17 changes: 17 additions & 0 deletions examples/mqtt-dashboard/device/hal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "hal.h"

// Mocked device pins
static bool s_pins[MAX_PINS];

bool gpio_write(int pin, bool status) {
bool ok = false;
if (pin >= 0 && pin < MAX_PINS) {
s_pins[pin] = status;
ok = true;
}
return ok;
}

bool gpio_read(int pin) {
return (pin >= 0 && pin < MAX_PINS) ? s_pins[pin] : false;
}
8 changes: 8 additions & 0 deletions examples/mqtt-dashboard/device/hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "mongoose.h"

#define MAX_PINS 144

bool gpio_write(int pin, bool status);
bool gpio_read(int 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
53 changes: 34 additions & 19 deletions examples/mqtt-dashboard/device/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,8 +12,10 @@ static struct mg_connection *s_conn; // MQTT Client connection
static struct mg_rpc *s_rpc = NULL; // List of registered RPC methods

struct device_config {
int pins[NUM_PINS]; // State of the GPIO pins
int log_level; // Device logging level, 0-4
int pin_count; // Number of pins to handle
int pin_map[MAX_PINS]; // Pins to handle
int pin_state[MAX_PINS]; // State of the GPIO pins
int log_level; // Device logging level, 0-4
};

static struct device_config s_device_config;
Expand Down Expand Up @@ -75,12 +77,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 +96,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 +108,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 +124,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] = (int) mg_json_get_long(r->frame, path, -1);
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 +210,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 +253,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 +266,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.

0 comments on commit f2ed4bd

Please sign in to comment.