This node.js application is a bridge between the your IP Cameras (with sound or motion detection) and a mqtt server. That way your can have your home respond to sound detection events.
It's intended as a building block in heterogenous smart home environments where an MQTT message broker is used as the centralized message bus. See MQTT Smarthome on Github for a rationale and architectural overview.
Using ipcam2mqtt is really easy, but it requires at least Node.js v6 or higher. (This app is tested against node v10, v13 and the latest lts version).
npm install -g ipcam2mqtt
ipcam2mqtt 0.0.0-development Usage: index.js [options] Options: -m, --mqtt mqtt broker url. See https://github.com/svrooij/ipcam2mqtt#mqtt-url [default: "mqtt://127.0.0.1"] -i, --instance instance name. used as mqtt client id and as topic prefix [default: "cameras"] -p, --port The port to run on [default: 8000] --timeout The timeout in seconds for resetting back to inactive, -1 for no reset [default: 10] -k, --keep-images Set this if you want to keep the images in mqtt [boolean] -h, --help Show help [boolean] -v, --verbosity Set the verbosity. [choices: "error", "warn", "info", "debug"] [default: "info"] --version Show version number [boolean]
Use the MQTT url to connect to your specific mqtt server. Check out mqtt.connect for the full description.
Connection without port (port 1883 gets used) [protocol]://[address] (eg. mqtt://127.0.0.1) Connection with port [protocol]://[address]:[port] (eg. mqtt://127.0.0.1:1883) Secure connection with username/password and port [protocol]://[username]:[password]@[address]:[port] (eg. mqtts://myuser:firstname.lastname@example.org:8883)
Configure your cameras
You now have and FTP server running on your computer. Now you can configure the cameras to send FTP snapshots to it when it detects movement or sound. The username you supply will be used as the device name.
Every message starts with the instance name (specified with the
-i argument), which defaults to
cameras so we'll asume the default.
This bridge uses the
cameras/connected topic to send retained connection messages. Use this topic to check your if your ipcam2mqtt bridge is still running.
0or missing is not connected (set by will functionality).
1is connected to mqtt, but have not received an image.
2is connected to mqtt and received our first image from a camera.
If there is motion detected (eg. a file is received over FTP), you will see two messages on your mqtt server.
A motion message on
cameras/username/motion with the following properties
nameThe username used for the connection
valcurrent state of the device.
filenameThe filename of the uploaded image
kindThe guessed kind of detection (based on the filename)
tstimestamp of last update.
And an image message on
cameras/username/image, this will just contain the raw image data. And can be displayed by various sources.
PM2 to run in backgroundUse
If everything works as expected, you should make the app run in the background automatically. Personally I use PM2 for this. And they have a great guide for this.
To start ipcam2mqtt with PM2, you have to use this command.
pm2 start ipcam2mqtt -x -- [regular-options] # the -x -- part is to tell pm2 you want to specify arguments to the script. example: pm2 start ipcam2mqtt -x -- -i cameras -m mqtt://your.mqtt.host:1883
You can also run this bridge on docker. Be sure to specify your own mqtt connection string! This command connects port
8021 (you can change this) to the container where the bridge runs at
You can also set the other properties by using the
-e "IPCAM2MQTT_...=newvalue" argument. All the properties can be set with the prefix
IPCAM2MQTT_ followed by the full name.
docker run -d -e "IPCAM2MQTT_MQTT=mqtt://your.mqtt.nl:1883" -p 8021:8021 --name ipcam2mqtt svrooij/ipcam2mqtt:latest # Open (and follow) the logs docker logs ipcam2mqtt -f
The actual FTP server part is mostly copied, improved and simplified from mqtt-camera-ftpd. It wasn't really working anymore and it wasn't a true CLI tool hence this new and improved version.
This bridge took me a lot of hours to build, so I invite everyone using it check out my brand new Github sponsor page