diff --git a/.gitignore b/.gitignore index 9a614a0..69aa315 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules .env +cert diff --git a/Documentation/mqtt_broker_setup.txt b/Documentation/mqtt_broker_setup.txt index e24a035..9fec0f3 100644 --- a/Documentation/mqtt_broker_setup.txt +++ b/Documentation/mqtt_broker_setup.txt @@ -7,8 +7,19 @@ connection_messages true #log client connect and disconnect password_file /etc/mosquitto/passwd #speicyf path to password file max_connections 5 #demo purpose listener 8883 #port for tls -certfile /etc/letsencrypt/live/mqtt.teeseng.uk/cert.pem -cafile /etc/letsencrypt/live/mqtt.teeseng.uk/chain.pem -keyfile /etc/letsencrypt/live/mqtt.teeseng.uk/privkey.pem +certfile /home/mpuser/letsencrypt-copy/live/mqtt.teeseng.uk-0001/cert.pem +cafile /home/mpuser/letsencrypt-copy/live/mqtt.teeseng.uk-0001/fullchain.pem +keyfile /home/mpuser/letsencrypt-copy/live/mqtt.teeseng.uk-0001/privkey.pem + +#mosquitt.conf +pid_file /run/mosquitto/mosquitto.pid + +persistence true +persistence_location /var/lib/mosquitto/ + +log_type all +log_facility 5 +log_dest file /var/log/mosquitto/mosquitto.log + diff --git a/IoT-sensor/modules/mqtt.js b/IoT-sensor/modules/mqtt.js index e69de29..17375aa 100644 --- a/IoT-sensor/modules/mqtt.js +++ b/IoT-sensor/modules/mqtt.js @@ -0,0 +1,45 @@ +const mqtt = require('mqtt'); +const fs = require('fs'); +const path = require('path') +require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }) + +// Configuration +const brokerUrl = 'mqtt://mqtt.teeseng.uk'; +const options = { + port: 8883, // MQTT broker port with TLS + username: process.env.MQTT_USER, + password: process.env.MQTT_PASS, + protocol: 'mqtts', // Use MQTT over TLS + key: fs.readFileSync(path.resolve(__dirname, '../../cert/privkey.pem')), // Private key for the client + cert: fs.readFileSync(path.resolve(__dirname, '../../cert/cert.pem')), // Client certificate + +}; + +// Create MQTT client +const client = mqtt.connect(brokerUrl, options); + +module.exports = client; +// Event handlers +client.on('connect', () => { + console.log('Connected to MQTT broker'); + client.subscribe('your-topic'); // Subscribe to a topic + // Publish a message + client.publish('your-topic', 'Hello MQTT with TLS!', { qos: 1 }, (err) => { + if (err) { + console.error('Error publishing message:', err); + } else { + console.log('Message published'); + } + client.end(); // Close the connection after publishing + }); +}); + +client.on('message', (topic, message) => { + console.log(`Received message on topic ${topic}: ${message}`); + client.end(); // Close the connection after receiving a message +}); + +client.on('error', (err) => { + console.error('Error:', err); + client.end(); // Close the connection in case of an error +}); diff --git a/Web-Server/modules/mqtt.js b/Web-Server/modules/mqtt.js index e69de29..33135cc 100644 --- a/Web-Server/modules/mqtt.js +++ b/Web-Server/modules/mqtt.js @@ -0,0 +1,37 @@ +const mqtt = require('mqtt'); +const fs = require('fs'); +const path = require('path') +require('dotenv').config({ path: path.resolve(__dirname, '../../.env') }) + +// Configuration +const brokerUrl = 'mqtt://mqtt.teeseng.uk'; +const options = { + port: 8883, // MQTT broker port with TLS + username: process.env.MQTT_USER, + password: process.env.MQTT_PASS, + protocol: 'mqtts', // Use MQTT over TLS + key: fs.readFileSync(path.resolve(__dirname, '../../cert/privkey.pem')), // Private key for the client + cert: fs.readFileSync(path.resolve(__dirname, '../../cert/cert.pem')), // Client certificate + +}; + +// Create MQTT client +const client = mqtt.connect(brokerUrl, options); + +module.exports = client; + +// Event handlers +client.on('connect', () => { + console.log('Connected to MQTT broker'); + client.subscribe('your-topic'); // Subscribe to a topic +}); + +client.on('message', (topic, message) => { + console.log(`Received message on topic ${topic}: ${message}`); + // Additional processing for received message +}); + +client.on('error', (err) => { + console.error('Error:', err); + client.end(); // Close the connection in case of an error +});