Added mqtt skeleton
This commit is contained in:
parent
c7b801114a
commit
b9840e7882
10
mos.yml
10
mos.yml
|
@ -33,7 +33,15 @@ config_schema:
|
|||
- ["i2c.scl_gpio", 5] # D1
|
||||
- ["flashLight", "o", {title: "Flash light / alarm light settings"}]
|
||||
- ["flashLight.address", "i", 0x30, {title: "i2c address of motor controller TB6612 (e.g. WEMOS)"}]
|
||||
- ["flashLight.mqttCtrlTopic", "s", "flashLight/##boardid##/ctrl", {title: "MQTT channel to subscribe to receive commands"}]
|
||||
- ["flashLight.mqttCtrlTopic", "s", "flashLight/%s/ctrl", {title: "MQTT channel to subscribe to receive commands; %s is replaced by clientId"}]
|
||||
- ["flashLight.mqttStatusTopic", "s", "flashLight/%s/status", {title: "MQTT channel to publish to send status change infos; %s is replaced by clientId"}]
|
||||
|
||||
- ["mqtt.enable", true]
|
||||
- ["mqtt.server", "mqtt.pmpark.de:41883"]
|
||||
- ["mqtt.user", "default"]
|
||||
- ["mqtt.pass", "12345678"]
|
||||
- ["mqtt.will_message", "offline"]
|
||||
- ["mqtt.will_topic", "flashLight/"]
|
||||
|
||||
# These settings get compiled into the C structure, and can be accessed
|
||||
# from the C code this way:
|
||||
|
|
67
src/main.c
67
src/main.c
|
@ -11,9 +11,16 @@
|
|||
#include "mgos_net.h"
|
||||
#include "mgos_sys_config.h"
|
||||
#include "mgos_timers.h"
|
||||
#include "mgos_mqtt.h"
|
||||
#include "common/str_util.h"
|
||||
|
||||
static float pwm;
|
||||
static float flashLightSpeed = 50; /* 0.0 .. 100.0 */
|
||||
static float pwm = 30.0;
|
||||
static float flashLightSpeed = 50.0; /* 0.0 .. 100.0 */
|
||||
static const char * clientId = "";
|
||||
static char commandTopic[256] = {'\0'};
|
||||
#if 0
|
||||
#endif
|
||||
static char pubStatusTopic[256] = {'\0'};
|
||||
|
||||
// Motor numbers
|
||||
//Motor shiled I2C Address: 0x30
|
||||
|
@ -22,18 +29,32 @@ static float flashLightSpeed = 50; /* 0.0 .. 100.0 */
|
|||
#define M1_addr 0x30
|
||||
#define M1_freq 5000
|
||||
|
||||
static void pubStatus(const char *statusString, float percentage) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 30);
|
||||
if (mgos_mqtt_global_connect()) {
|
||||
json_printf(&out, "{statusString: %Q, speed: %f}", statusString, percentage);
|
||||
mgos_mqtt_pub(pubStatusTopic, fb.buf, fb.len, 0, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void motor_timer_cb(void *arg) {
|
||||
if (pwm > flashLightSpeed + 5.0) {
|
||||
pwm = 0.0; // start again
|
||||
} else {
|
||||
wemos_motor_setmotor(M1, _CW, pwm);
|
||||
LOG(LL_INFO, ("M1, pwm=%f", pwm));
|
||||
pubStatus("on", pwm);
|
||||
|
||||
pwm += 0.1;
|
||||
|
||||
if (pwm > flashLightSpeed) {
|
||||
wemos_motor_setmotor(M1, _STOP, 0.0);
|
||||
LOG(LL_INFO, ("Stopped"));
|
||||
pubStatus("off", 0);
|
||||
}
|
||||
}
|
||||
(void) arg;
|
||||
|
@ -58,10 +79,12 @@ static void flashLightOn(struct mg_rpc_request_info *ri, void *cb_arg,
|
|||
|
||||
printf("FlashLight ON\n");
|
||||
wemos_motor_setmotor(M1, _CW, flashLightSpeed);
|
||||
pubStatus("on", flashLightSpeed);
|
||||
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
mbuf_free(&fb);
|
||||
|
||||
|
||||
(void) cb_arg;
|
||||
(void) fi;
|
||||
|
@ -87,6 +110,7 @@ static void flashLightOff(struct mg_rpc_request_info *ri, void *cb_arg,
|
|||
|
||||
printf("FlashLight OFF\n");
|
||||
wemos_motor_setmotor(M1, _STOP, 0);
|
||||
pubStatus("off", 0);
|
||||
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
|
@ -131,17 +155,50 @@ void net_changed(int ev, void *evd, void *arg) {
|
|||
(void) arg;
|
||||
}
|
||||
|
||||
void onMqttConnection(struct mg_connection *c, const char *client_id, struct mg_send_mqtt_handshake_opts *opts, void *fn_arg) {
|
||||
// add MQTT cmd subscription
|
||||
LOG(LL_INFO, ("onMqttConnection handler called with clientId=%s", client_id));
|
||||
|
||||
(void) c;
|
||||
(void) client_id;
|
||||
(void) opts;
|
||||
(void) fn_arg;
|
||||
}
|
||||
#if 0
|
||||
static void mqttCommandHandler(struct mg_connection *c, const char *topic, int topic_len,
|
||||
const char *msg, int msg_len, void *userdata) {
|
||||
LOG(LL_INFO, ("Got message on topic %.*s", topic_len, topic));
|
||||
|
||||
(void) c;
|
||||
(void) topic;
|
||||
(void) topic_len;
|
||||
(void) msg;
|
||||
(void) msg_len;
|
||||
(void) userdata;
|
||||
}
|
||||
#endif
|
||||
enum mgos_app_init_result mgos_app_init(void) {
|
||||
struct mg_rpc *c = mgos_rpc_get_global();
|
||||
mg_rpc_add_handler(c, "FlashLight.On", NULL, flashLightOn, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Off", NULL, flashLightOff, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Speed", "{num: %d}", flashLightSetSpeed, NULL);
|
||||
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_changed, NULL);
|
||||
|
||||
LOG(LL_INFO, ("Initializing MQTT"));
|
||||
// add MQTT cmd subscription
|
||||
// mgos_mqtt_set_connect_fn(onMqttConnection, NULL);
|
||||
clientId = mgos_sys_config_get_mqtt_client_id();
|
||||
clientId = clientId ? clientId : mgos_sys_config_get_device_id();
|
||||
LOG(LL_INFO, ("clientId=%s", clientId));
|
||||
LOG(LL_INFO, ("cmdTopic=%s", mgos_sys_config_get_flashLight_mqttCtrlTopic()));
|
||||
LOG(LL_INFO, ("pubStatusTopic=%s", mgos_sys_config_get_flashLight_mqttStatusTopic()));
|
||||
c_snprintf(commandTopic, sizeof(commandTopic), mgos_sys_config_get_flashLight_mqttCtrlTopic(), clientId);
|
||||
c_snprintf(pubStatusTopic, sizeof(pubStatusTopic), mgos_sys_config_get_flashLight_mqttStatusTopic(), clientId);
|
||||
#if 0
|
||||
mgos_mqtt_sub(commandTopic, mqttCommandHandler, NULL);
|
||||
#endif
|
||||
LOG(LL_INFO, ("Initializing motor controller"));
|
||||
wemos_motor_init();
|
||||
wemos_motor_initMotor(M1, M1_addr, M1_freq);
|
||||
LOG(LL_INFO, ("\r\nTest PWM 30 to 100, step 0.1,CW\r\n"));
|
||||
pwm = 30.0;
|
||||
mgos_set_timer(200, MGOS_TIMER_REPEAT, motor_timer_cb, NULL);
|
||||
return MGOS_APP_INIT_SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue