mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-23 11:44:53 +00:00
14 files changed
+239
-23
No files matched your search
@@ -12,13 +12,13 @@ static void signal_handler(int signo) {
|
||||
// Mocked device pins
|
||||
static bool s_pins[100];
|
||||
|
||||
void gpio_write(int pin, bool status) {
|
||||
void hal_gpio_write(int pin, bool status) {
|
||||
if (pin >= 0 && pin <= (int) (sizeof(s_pins) / sizeof(s_pins[0]))) {
|
||||
s_pins[pin] = status;
|
||||
}
|
||||
}
|
||||
|
||||
bool gpio_read(int pin) {
|
||||
bool hal_gpio_read(int pin) {
|
||||
return (pin >= 0 && pin <= (int) (sizeof(s_pins) / sizeof(s_pins[0])))
|
||||
? s_pins[pin]
|
||||
: false;
|
||||
|
||||
@@ -46,7 +46,7 @@ static void publish_status(struct mg_connection *c) {
|
||||
struct mg_mqtt_opts pub_opts;
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
pub_opts.topic = pubt;
|
||||
s_device_config.led_status = gpio_read(s_device_config.led_pin);
|
||||
s_device_config.led_status = hal_gpio_read(s_device_config.led_pin);
|
||||
char *device_status_json = mg_mprintf(
|
||||
"{%m:%m,%m:{%m:%m,%m:%s,%m:%hhu,%m:%hhu,%m:%hhu,%m:%M,%m:%M}}",
|
||||
MG_ESC("method"), MG_ESC("status.notify"), MG_ESC("params"),
|
||||
@@ -114,7 +114,7 @@ static void rpc_config_set(struct mg_rpc_req *r) {
|
||||
if (tmp_pin > 0) s_device_config.led_pin = tmp_pin;
|
||||
|
||||
if (tmp_pin > 0 && ok) {
|
||||
gpio_write(s_device_config.led_pin, s_device_config.led_status);
|
||||
hal_gpio_write(s_device_config.led_pin, s_device_config.led_status);
|
||||
}
|
||||
|
||||
mg_rpc_ok(r, "%m", MG_ESC("ok"));
|
||||
@@ -219,7 +219,7 @@ static void timer_fn(void *arg) {
|
||||
.qos = s_qos,
|
||||
.topic = mg_str(status_topic),
|
||||
.version = 4,
|
||||
.keepalive = KEEP_ALIVE_INTERVAL,
|
||||
.keepalive = MQTT_KEEP_ALIVE_INTERVAL,
|
||||
.retain = true,
|
||||
.message = mg_str(msg)};
|
||||
s_conn = mg_mqtt_connect(mgr, g_url, &opts, fn, NULL);
|
||||
@@ -234,9 +234,9 @@ static void timer_keepalive(void *arg) {
|
||||
}
|
||||
|
||||
void web_init(struct mg_mgr *mgr) {
|
||||
int pingreq_interval_ms = KEEP_ALIVE_INTERVAL * 1000 - 500;
|
||||
int pingreq_interval_ms = MQTT_KEEP_ALIVE_INTERVAL * 1000 - 500;
|
||||
if (!g_device_id) generate_device_id();
|
||||
if (!g_root_topic) g_root_topic = DEFAULT_ROOT_TOPIC;
|
||||
if (!g_root_topic) g_root_topic = MQTT_ROOT_TOPIC;
|
||||
s_device_config.log_level = (uint8_t) mg_log_level;
|
||||
|
||||
// Configure JSON-RPC functions we're going to handle
|
||||
|
||||
@@ -13,10 +13,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define DEVICE_ID_LEN 10
|
||||
#define ROOT_TOPIC_LEN 30
|
||||
#define KEEP_ALIVE_INTERVAL 60
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 60
|
||||
#define MQTT_SERVER_URL "mqtt://broker.hivemq.com:1883"
|
||||
#define DEFAULT_ROOT_TOPIC "topic_mg_device"
|
||||
#define MQTT_ROOT_TOPIC "topic_mg_device"
|
||||
|
||||
extern char *g_url;
|
||||
extern char *g_device_id;
|
||||
@@ -25,8 +24,8 @@ extern char *g_root_topic;
|
||||
void web_init(struct mg_mgr *mgr);
|
||||
void web_destroy();
|
||||
|
||||
void gpio_write(int pin, bool status);
|
||||
bool gpio_read(int pin);
|
||||
void hal_gpio_write(int pin, bool status);
|
||||
bool hal_gpio_read(int pin);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -23,6 +23,16 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef MQTT_DASHBOARD
|
||||
void hal_gpio_write(int pin, bool status) { // For MQTT dashboard HAL
|
||||
gpio_write((uint16_t) pin, status);
|
||||
}
|
||||
|
||||
bool hal_gpio_read(int pin) { // For MQTT dashboard HAL
|
||||
return gpio_read((uint16_t) pin);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void timer_fn(void *arg) {
|
||||
gpio_toggle(LED); // Blink LED
|
||||
struct mg_tcpip_if *ifp = arg; // And show
|
||||
@@ -37,13 +47,10 @@ int main(void) {
|
||||
uart_init(UART_DEBUG, 115200); // Initialise debug printf
|
||||
ethernet_init(); // Initialise ethernet pins
|
||||
|
||||
#ifdef MQTT_DASHBOARD
|
||||
// User can customise the MQTT url, device ID or the root topic below
|
||||
#define DEVICE_ID "H723ZG"
|
||||
g_url = MQTT_SERVER_URL;
|
||||
g_device_id = DEVICE_ID;
|
||||
g_root_topic = DEFAULT_ROOT_TOPIC;
|
||||
#endif
|
||||
// User can customise the MQTT url, device ID or the root topic below
|
||||
#ifdef MQTT_DASHBOARD
|
||||
g_device_id = "H723ZG";
|
||||
#endif
|
||||
|
||||
MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));
|
||||
|
||||
|
||||
Reference in new issue
Block a user