Skip to content

Commit

Permalink
Merge pull request #178 from X-Ryl669/control_pan_tilt_from_mqtt
Browse files Browse the repository at this point in the history
Add control of pan and tilt from MQTT topics
  • Loading branch information
roleoroleo authored Dec 18, 2024
2 parents f55c5da + 4e77d12 commit 041803a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/mqtt/mqtt-sonoff/include/mqtt-sonoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define MQTT_SONOFF_SNAPSHOT "/mnt/mmc/sonoff-hack/bin/snapshot"
#define CONF2MQTT_SCRIPT "/mnt/mmc/sonoff-hack/script/conf2mqtt.sh"
#define IPC_CMD "/mnt/mmc/sonoff-hack/bin/ipc_cmd"
#define PTZ_CMD "/mnt/mmc/sonoff-hack/bin/ptz"


#define MOTION_ALARM_FILE "/tmp/onvif_notify_server/motion_alarm"

Expand Down
37 changes: 37 additions & 0 deletions src/mqtt/mqtt-sonoff/src/mqtt-sonoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,43 @@ static void send_ha_discovery() {
mqtt_send_message(&msg, retain);
free(msg.msg);

// Send pan config
msg.msg=print_switch_json("pan_left", "mdi:video");
cJSON_Minify(msg.msg);
msg.len=strlen(msg.msg);

sprintf(topic, "%s/switch/%s_pan_left/config", mqtt_sonoff_conf.ha_conf_prefix, mqtt_sonoff_conf.device_id);
mqtt_send_message(&msg, retain);
free(msg.msg);

// Send pan_right config
msg.msg=print_switch_json("pan_right", "mdi:video");
cJSON_Minify(msg.msg);
msg.len=strlen(msg.msg);

sprintf(topic, "%s/switch/%s_pan_right/config", mqtt_sonoff_conf.ha_conf_prefix, mqtt_sonoff_conf.device_id);
mqtt_send_message(&msg, retain);
free(msg.msg);

// Send pan_up config
msg.msg=print_switch_json("pan_up", "mdi:video");
cJSON_Minify(msg.msg);
msg.len=strlen(msg.msg);

sprintf(topic, "%s/switch/%s_pan_up/config", mqtt_sonoff_conf.ha_conf_prefix, mqtt_sonoff_conf.device_id);
mqtt_send_message(&msg, retain);
free(msg.msg);

// Send pan_down config
msg.msg=print_switch_json("pan_down", "mdi:video");
cJSON_Minify(msg.msg);
msg.len=strlen(msg.msg);

sprintf(topic, "%s/switch/%s_pan_down/config", mqtt_sonoff_conf.ha_conf_prefix, mqtt_sonoff_conf.device_id);
mqtt_send_message(&msg, retain);
free(msg.msg);


// Send motion_detection config
msg.msg=print_switch_json("motion_detection", "mdi:motion-sensor");
cJSON_Minify(msg.msg);
Expand Down
32 changes: 32 additions & 0 deletions src/mqtt/mqtt-sonoff/src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,38 @@ static void message_callback(struct mosquitto *mosq, void *obj, const struct mos
sprintf(cmd_line, "%s -t off", IPC_CMD);
system(cmd_line);
}
} else if (strcasecmp("pan_left", param) == 0) {
if ((strcasecmp("on", (char *) message->payload) == 0) || (strcasecmp("yes", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop; %s -a left", PTZ_CMD, PTZ_CMD);
system(cmd_line);
} else if ((strcasecmp("off", (char *) message->payload) == 0) || (strcasecmp("no", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop", PTZ_CMD);
system(cmd_line);
}
} else if (strcasecmp("pan_right", param) == 0) {
if ((strcasecmp("on", (char *) message->payload) == 0) || (strcasecmp("yes", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop; %s -a right", PTZ_CMD, PTZ_CMD);
system(cmd_line);
} else if ((strcasecmp("off", (char *) message->payload) == 0) || (strcasecmp("no", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop", PTZ_CMD);
system(cmd_line);
}
} else if (strcasecmp("pan_up", param) == 0) {
if ((strcasecmp("on", (char *) message->payload) == 0) || (strcasecmp("yes", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop; %s -a up", PTZ_CMD, PTZ_CMD);
system(cmd_line);
} else if ((strcasecmp("off", (char *) message->payload) == 0) || (strcasecmp("no", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop", PTZ_CMD);
system(cmd_line);
}
} else if (strcasecmp("pan_down", param) == 0) {
if ((strcasecmp("on", (char *) message->payload) == 0) || (strcasecmp("yes", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop; %s -a down", PTZ_CMD, PTZ_CMD);
system(cmd_line);
} else if ((strcasecmp("off", (char *) message->payload) == 0) || (strcasecmp("no", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -a stop", PTZ_CMD);
system(cmd_line);
}
} else if (strcasecmp("motion_detection", param) == 0) {
if ((strcasecmp("on", (char *) message->payload) == 0) || (strcasecmp("yes", (char *) message->payload) == 0)) {
sprintf(cmd_line, "%s -m on", IPC_CMD);
Expand Down

0 comments on commit 041803a

Please sign in to comment.