cmdgateway

1.0.0 • Public • Published

Thu, 13 Apr. 2017 01:38 PM

Gateway per progetto "Cantiere Smart"

si parte dall'immagine del sistema operativo in Desktop/Backup: SDCardBackupPI3_5.img

Modifica del mosquitto bridging

per collegare il broker MQTT (servizio mosquitto già presente e gia configurato per essere collegato ad aws) bisogna effettuare i seguenti passaggi:

creare la dir: /home/pi/components/PlantGlue/cloudmqtt_conf.d

dentro questa dir creare il file: cloudmqtt.conf

con il seguente contenuto:

connection cloudmqtt
address m20.cloudmqtt.com:17203
remote_username publisher
remote_password publisher
try_private false
start_type automatic
topic # both

poi modificare il file: /etc/mosquitto/mosquitto.conf

in modo tale che abbia il seguente contenuto:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence false
#persistence_location /var/lib/mosquitto/

log_dest none

include_dir /home/pi/components/PlantGlue/cloudmqtt_conf.d

listener 1883
listener 1884
protocol websockets

Software per command gateway verso attuatori telecamera.

creare la dir: /home/pi/components/CmdGateway

in questa dir creare i seguenti files:

CmdGateway.js

'use strict'

var mqtt = require('mqtt')
var net = require('net');

//var HOST = '192.168.1.3';
var HOST = process.argv[2] || 'localhost';
var PORT = process.argv[3] || 8888;

var JsonSocket = require('json-socket');
var tcpclient = new JsonSocket(new net.Socket());

var clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)

var host = 'mqtt://m20.cloudmqtt.com'

var options = {
  port: 17203,
  keepalive: 10,
  clientId: clientId,
  protocolId: 'MQTT',
  protocolVersion: 4,
  clean: true,
  reconnectPeriod: 1000,
  connectTimeout: 30 * 1000,
  will: {
    topic: 'WillMsg',
    payload: 'Connection Closed abnormally..!',
    qos: 0,
    retain: false
  },
  username: 'publisher',
  password: 'publisher',
  rejectUnauthorized: false
}

var client = mqtt.connect(host, options)

client.on('connect', function () {
  client.subscribe('Pan')
  client.subscribe('Tilt')
  client.subscribe('Zoom')
  client.subscribe('AttA')
  client.subscribe('AttB')
  client.subscribe('AttC')
  client.subscribe('AttD')
})

client.on('message', function (topic, message) {
  // message is Buffer
  var mymessage="{\"Command\": \""+topic+"\", \"Value\": "+message.toString()+"}";  
  console.log(mymessage);
  // Write a message to the socket as soon as the client is connected, the server will
  // receive it as message from the client 
  tcpclient.sendMessage({Command: topic, Value: parseFloat(message.toString())});
})

tcpclient.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});

ServerForTest

var net = require('net');

var HOST = 'localhost';
var PORT = 8888;

net.createServer(function(sock) {

    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function(data) {

        console.log('DATA ' + sock.remoteAddress + ': ' + data);        
    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

package.json

{
  "name": "CmdGateway",
  "version": "1.0.0",
  "description": "",
  "main": "CmdGateway.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mqtt": "^2.4.0",
    "net": "^1.0.2"
  }
}

Installare le librerie necessarie al CmdGateway

dare i seguenti comandi

cd /home/pi/components/CmdGateway
npm install

per provare senza l'hardware della telecamera aprire 2 terminal e far partire rispettivamente in ogni terminal nell'ordine:

node ServerForTest.js

node GmdGateway.js

Installare CmdGateway.js come servizio con partenza al Boot

sudo forever-service install CmdGateway -s /home/pi/components/CmdGateway/CmdGateway.js -o " 127.0.0.1 8888"
sudo service CmdGateway status
sudo service CmdGateway start
sudo service CmdGateway stop

(For Testing Installare ServerForTest.js come servizio con partenza al Boot

sudo forever-service install CameraEmulator -s /home/pi/components/CmdGateway/ServerForTest.js
sudo service CameraEmulator status
sudo service CameraEmulator start
sudo service CameraEmulator stop

Readme

Keywords

none

Package Sidebar

Install

npm i cmdgateway

Weekly Downloads

0

Version

1.0.0

License

ISC

Last publish

Collaborators

  • tcafiero