December 2017
Intermediate to advanced
296 pages
5h 56m
English
Now that the relay is connected to the Raspberry Pi, we will write the logic that will send the on/off command to the socket topic. Open api-engine/server/mqtt/index.js and update it, as follows:
var Data = require('../api/data/data.model'); var mqtt = require('mqtt'); var config = require('../config/environment'); var client = mqtt.connect({ port: config.mqtt.port, protocol: 'mqtts', host: config.mqtt.host, clientId: config.mqtt.clientId, reconnectPeriod: 1000, username: config.mqtt.clientId, password: config.mqtt.clientId, keepalive: 300, rejectUnauthorized: false }); client.on('connect', function() { console.log('Connected to Mosca at ' + config.mqtt.host + ' on port ' + config.mqtt.port); client.subscribe('api-engine'); ...