Это старая версия документа!
Перехожу в порт и устанавливаю.
cd /usr/ports/net/mosquitto # make install clean ------------------------------ mosquitto-1.4.10 -------------------------------¬ ¦ ---------------------------------------------------------------------------¬ ¦ ¦ ¦+[x] WS MQTT over Websockets support ¦ ¦ ¦ L--------------------------------------------------------------------------- ¦ +------------------------------------------------------------------------------+ ¦ < OK > <Cancel> ¦ L-------------------------------------------------------------------------------
------------------------------- c-ares-1.12.0 ---------------------------------¬ ¦ ---------------------------------------------------------------------------¬ ¦ ¦ ¦+[x] CONFIG_INFO Add the ares_config_info patch ¦ ¦ ¦ ¦+[ ] DEBUG Build with debugging support ¦ ¦ ¦ ¦+[x] HIDE_SYMBOLS Hide internal library symbols ¦ ¦ ¦ ¦+[ ] OPTIMIZED_CFLAGS Build with compiler optimizations ¦ ¦ ¦ L--------------------------------------------------------------------------- ¦ +------------------------------------------------------------------------------+ ¦ < OK > <Cancel> ¦ L-------------------------------------------------------------------------------
---------------------------- libwebsockets-2.0.2 ------------------------------¬ ¦ ---------------------------------------------------------------------------¬ ¦ ¦ ¦+[x] HTTP2 HTTP protocol version 2.0 support ¦ ¦ ¦ ¦+[ ] HTTP_PROXY HTTP proxy support ¦ ¦ ¦ ¦+[ ] IPV6 IPv6 protocol support ¦ ¦ ¦ ¦+[ ] LIBEV High-performance events support via libev ¦ ¦ ¦ ¦+[ ] LIBUV Asynchronous I/O support via libuv ¦ ¦ ¦ ¦+[x] UNIX_SOCK Unix domain socket support ¦ ¦ ¦ ¦+[x] WEBSERVER Build and/or install internal web server ¦ ¦ ¦ L--------------------------------------------------------------------------- ¦ +------------------------------------------------------------------------------+ ¦ < OK > <Cancel> ¦ L-------------------------------------------------------------------------------
===> SECURITY REPORT:
    This port has installed the following files which may act as network
    servers and may therefore pose a remote security risk to the system.
/usr/local/bin/mosquitto_passwd
/usr/local/sbin/mosquitto
/usr/local/lib/libmosquitto.so.1.4.10
    This port has installed the following startup scripts which may cause
    these network services to be started at boot time.
/usr/local/etc/rc.d/mosquitto
    If there are vulnerabilities in these programs there may be a security
    risk to the system. FreeBSD makes no guarantee about the security of
    ports included in the Ports Collection. Please type 'make deinstall'
    to deinstall the port if this is a concern.
    For more information, and contact details about the security
    status of this software, see the following webpage:
http://mosquitto.org/
===>  Cleaning for c-ares-1.12.0
===>  Cleaning for libwebsockets-2.0.2
===>  Cleaning for libuv-1.10.0
===>  Cleaning for mosquitto-1.4.10
Конфигурационный файл находиться /usr/local/etc/mosquitto/mosquitto.conf
С помощью утилиты mosquitto_passwd создаю файл паролей passwd с логином и паролем для подключения к серверу.
# mosquitto_passwd -c /usr/local/etc/mosquitto/passwd esp8266 Password: Reenter password:
Запускаю mosquitto.
# /usr/local/etc/rc.d/mosquitto onestart
Проверяю
ps -ax | grep mosquitto 10552 ?? Ss 3:47,77 /usr/local/sbin/mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf -d sockstat | grep 1883 nobody mosquitto 10552 3 tcp4 10.215.130.1:1883 *:* nobody mosquitto 10552 4 tcp4 10.215.130.1:1883 10.215.130.186:3387
Для запуска mosquitto при старте системы в rc.config добавил строку
mosquitto_enable="YES"
Для добавления в топики информации с датчиков температуры написал скрипт
#!/bin/sh
while true
do
term0=`/usr/local/bin/digitemp_DS9097 -t 0 -q -c /usr/local/etc/digitemp.conf`
term1=`/usr/local/bin/digitemp_DS9097 -t 1 -q -c /usr/local/etc/digitemp.conf`
term2=`/usr/local/bin/snmpwalk -v1 -c ччччч_ro ХХХ.ХХХ.ХХХ.ХХХ .1.3.6.1.4.1.2021.2000.6.4.1.2.5.116.101.109.112.48.1 | awk -F'["]' '{ print $2 }'`
/usr/local/bin/mosquitto_pub -h localhost -t sensors/ds18b20_0 -u temp -P pft,bcm -m $term0
/usr/local/bin/mosquitto_pub -h localhost -t sensors/ds18b20_1 -u temp -P pft,bcm -m $term1
/usr/local/bin/mosquitto_pub -h localhost -t sensors/ds18b20_2 -u temp -P pft,bcm -m $term2
  sleep 6
done
Конструкция -F'[«]' изменяет разделитель awk с пробела по умолчанию на двойные кавычки.
Чтобы скрипт стартовал как демон написал с папке /usr/local/etc/rc.d ещё одни скрипт
#!/bin/sh
# PROVIDE: mqtt
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable mqtt:
#
# mqtt_enable="YES"
#
#
. /etc/rc.subr
name=mqtt
rcvar=${name}_enable
pidfile="/var/run/${name}.pid"
command=/scripts/mqqt-temp
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"
mqtt_start() {
  /usr/sbin/daemon -P ${pidfile} -r -f  $command
}
mqtt_stop() {
  if [ -e "${pidfile}" ]; then
    kill -s TERM `cat ${pidfile}`
  else
    echo "${name} is not running"
  fi
}
mqtt_status() {
  if [ -e "${pidfile}" ]; then
    echo "${name} is running as pid `cat ${pidfile}`"
  else
    echo "${name} is not running"
  fi
}
load_rc_config ${name}
run_rc_command "$1"
rc скрипт не демонизирует процесс, для демонизации используется строка /usr/sbin/daemon