diff --git a/backend/index.js b/backend/index.js index e090012..9912bf7 100644 --- a/backend/index.js +++ b/backend/index.js @@ -17,9 +17,7 @@ const setupID = require('./Models/setupIDModel'); const Employee = require('./Models/employeeModel'); const modeModel = require('./Models/modeModel'); -let list = []; - -var client = mqtt.connect('mqtt://hivemq.dock.moxd.io'); +var client = mqtt.connect(`mqtt://${process.env.MQTT_BROKER}`); Mongoose.connect(process.env.MONGODB_CONNECTION, { useUnifiedTopology: true, useNewUrlParser: true, @@ -32,29 +30,29 @@ client.on('connect', function () { client.subscribe('mittelkonsole/order/+', function (err) { console.error(err); }); - client.subscribe('thkoeln/IoT/bmw/montage/mittelkonsole/action/+', function ( + client.subscribe( `${process.env.BASETOPIC}/action/+`, function ( err, ) { console.error(err); }); - client.subscribe('thkoeln/IoT/bmw/montage/mittelkonsole/list', function ( + client.subscribe(`${process.env.BASETOPIC}/list`, function ( err, ) { console.error(err); }); client.subscribe( - 'thkoeln/IoT/bmw/montage/mittelkonsole/actionList', + `${process.env.BASETOPIC}/actionList`, function (err) { console.error(err); }, ); - client.subscribe('thkoeln/IoT/bmw/montage/mittelkonsole/mode', function ( + client.subscribe( `${process.env.BASETOPIC}/mode`, function ( err, ) { console.error(err); }); - client.subscribe('thkoeln/IoT/setup', (err) => { + client.subscribe(`${process.env.BASETOPIC}/setup`, (err) => { console.error(err); }); }); @@ -70,7 +68,7 @@ client.on('message', async function (topic, message) { console.log(err); } // console.log('Message:' + message.toString()); - if (topic == 'thkoeln/IoT/bmw/montage/mittelkonsole/list') { + if (topic == `${process.env.BASETOPIC}/list`) { console.log('on RECIVE:' + JSON.stringify(computedMessage)); productList.remove({}, (err) => { if (err) throw err; @@ -79,7 +77,7 @@ client.on('message', async function (topic, message) { console.log("Sending new List to Client...") io.emit('productList', data); }); - } else if (topic == 'thkoeln/IoT/bmw/montage/mittelkonsole/actionList') { + } else if (topic == `${process.env.BASETOPIC}/actionList`) { Actions.remove({}, (err) => { if (err) throw err; }); @@ -87,7 +85,7 @@ client.on('message', async function (topic, message) { console.log("Sending new List to Client...") io.emit('actions', data); }); - } else if (topic == 'thkoeln/IoT/bmw/montage/mittelkonsole/mode') { + } else if (topic == `${process.env.BASETOPIC}/mode`) { let newMode = new Mode({ mode: computedMessage }); Mode.remove({}, (err) => { if (err) throw err; @@ -101,7 +99,7 @@ client.on('message', async function (topic, message) { }); io.emit('mode', computedMessage); } else if ( - topic.startsWith('thkoeln/IoT/bmw/montage/mittelkonsole/action/') + topic.startsWith(`${process.env.BASETOPIC}/action/`) ) { let eID = topic.split('/'); eID = eID[eID.length - 1]; @@ -122,7 +120,7 @@ client.on('message', async function (topic, message) { let name = mitarbeiter.find((elem) => elem.eID == orderedAction.eID); orderedAction.employee = name ? name.name : orderedAction.eID; io.emit('orderedAction', orderedAction); - } else if (topic.startsWith('mittelkonsole/order/')) { + } else if (topic.startsWith(`${process.env.BASETOPIC}/order`)) { let id = topic.split('/'); id = id[id.length - 1]; let newOrder = new Orders({ @@ -143,7 +141,7 @@ client.on('message', async function (topic, message) { let name = mitarbeiter.find((elem) => elem.eID == newOrder.eID); newOrder.employee = name ? name.name : newOrder.eID; io.emit('orderedProduct', newOrder); - } else if (topic == 'thkoeln/IoT/setup') { + } else if (topic == `${process.env.BASETOPIC}/setup`) { let device = await setupID.findOne({ SetupId: computedMessage }); if (device) { io.emit('setupID', device); @@ -216,7 +214,7 @@ io.on('connection', async (socket) => { socket.on('mode_change', (msg) => { client.publish( - 'thkoeln/IoT/bmw/montage/mittelkonsole/mode', + `${process.env.BASETOPIC}/mode`, msg.toString(), { retain: true }, ); @@ -230,7 +228,7 @@ io.on('connection', async (socket) => { quantity: msg.quantity, step: msg.step, }); - client.publish('thkoeln/IoT/bmw/montage/mittelkonsole/list', JSON.stringify(products), { + client.publish( `${process.env.BASETOPIC}/list`, JSON.stringify(products), { retain: true, }); }); @@ -239,7 +237,7 @@ io.on('connection', async (socket) => { actions.push({ name: msg.name, }); - client.publish('thkoeln/IoT/bmw/montage/mittelkonsole/actionList', JSON.stringify(actions), { + client.publish( `${process.env.BASETOPIC}/actionList`, JSON.stringify(actions), { retain: true, }); }); @@ -249,7 +247,7 @@ io.on('connection', async (socket) => { }); Actions.find({}, '-_id -__v').then((data) => { const newList = JSON.stringify(data); - client.publish('thkoeln/IoT/bmw/montage/mittelkonsole/actionList', newList, { + client.publish( `${process.env.BASETOPIC}/actionList`, newList, { retain: true, }); }); @@ -261,7 +259,7 @@ io.on('connection', async (socket) => { }); productList.find({}, '-_id -__v').then((data) => { const newList = JSON.stringify(data); - client.publish('thkoeln/IoT/bmw/montage/mittelkonsole/list', newList, { + client.publish( `${process.env.BASETOPIC}/list`, newList, { retain: true, }); }); @@ -287,7 +285,7 @@ io.on('connection', async (socket) => { const newEmployee = new Employee({ name: msg.name, eID: msg.eID, setupID }); await newEmployee.save(); await setupID.deleteOne({ SetupId: msg.setupID }); - client.publish('thkoeln/IoT/setup/' + msg.setupID, msg.eID.toString(), { + client.publish( `${process.env.BASETOPIC}/setup/` + msg.setupID, msg.eID.toString(), { retain: true, }); console.log('message: ' + msg); diff --git a/esp32-IoT-Button/src/main.cpp b/esp32-IoT-Button/src/main.cpp index 7565802..99c8612 100644 --- a/esp32-IoT-Button/src/main.cpp +++ b/esp32-IoT-Button/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -82,6 +83,7 @@ byte bottomRightCheck[] = { B11000, B10000}; + /*** Initales verbinden mit WLAN ***/ void setupWifi() { @@ -116,12 +118,14 @@ void setupWifi() void initialSetup() { + std::string ssetup =baseTopic+ std::string("/setup/"); //Adresse des Brokers + const char *setupTopic = ssetup.c_str(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Init Setup"); Serial.println("Intial Setup"); - client.publish("thkoeln/IoT/setup", WiFi.macAddress().c_str()); - String in = "thkoeln/IoT/setup/"; + client.publish(setupTopic, WiFi.macAddress().c_str()); + String in = setupTopic; in += WiFi.macAddress(); Serial.println(in); const char *c = in.c_str(); @@ -129,12 +133,19 @@ void initialSetup() } /*** Connenct/Reconnect to MQTT Broker in Case of Connection loss ***/ -const char *broker = "hivemq.dock.moxd.io"; //Adresse des Brokers -const char *orderList = "thkoeln/IoT/bmw/montage/mittelkonsole/list"; //Ein Topic -const char *outTopic = "mittelkonsole/order/"; -const char *actions = "thkoeln/IoT/bmw/montage/mittelkonsole/actionList"; -const char *modeTopic = "thkoeln/IoT/bmw/montage/mittelkonsole/mode"; -const char *actionOut = "thkoeln/IoT/bmw/montage/mittelkonsole/action/"; + + +const char *broker = mqtt_broker; +std::string sorderList =baseTopic+ std::string("/list"); //Adresse des Brokers +const char *orderList = sorderList.c_str(); //Ein Topic +std::string soutTopic =baseTopic+ std::string("/order/"); +const char *outTopic = soutTopic.c_str(); +std::string sactions =baseTopic+ std::string("/actionList"); +const char *actions = sactions.c_str(); +std::string smodeTopic =baseTopic+ std::string("/mode"); +const char *modeTopic =smodeTopic.c_str(); +std::string sactionOut =baseTopic+ std::string("/action/"); +const char *actionOut =sactionOut.c_str(); void reconnect() { // Loop until we're reconnected @@ -168,11 +179,15 @@ void reconnect() /*** Funktion welche ausgeführt wird, wenn eine Nachricht auf einem abbonierten Topic ankommt ***/ void callback(char *topic, byte *payload, unsigned int length) { + std::string smodeTopic =baseTopic+ std::string("/mode"); + const char *modeTopic =smodeTopic.c_str(); + std::string sactions =baseTopic+ std::string("/actionList"); + const char *actions = sactions.c_str(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Updating..."); Serial.println(topic); - String setup = "thkoeln/IoT/setup/"; + String setup = modeTopic; setup += WiFi.macAddress(); Serial.println(setup); const char *c = setup.c_str(); @@ -194,7 +209,7 @@ void callback(char *topic, byte *payload, unsigned int length) mitarbeiterID = value; } } - if (strcmp(topic, "thkoeln/IoT/bmw/montage/mittelkonsole/mode") == 0) + if (strcmp(topic, modeTopic) == 0) { char buffer[128]; memcpy(buffer, payload, length); @@ -222,7 +237,7 @@ void callback(char *topic, byte *payload, unsigned int length) Serial.println(mode); } } - if (strcmp(topic, "thkoeln/IoT/bmw/montage/mittelkonsole/list") == 0 && mode == ORDER_MODE) + if (strcmp(topic,modeTopic) == 0 && mode == ORDER_MODE) { for (int i = 0; i < length; i++) { @@ -235,7 +250,7 @@ void callback(char *topic, byte *payload, unsigned int length) if (hits >= len) hits = len - 1; } - else if (strcmp(topic, "thkoeln/IoT/bmw/montage/mittelkonsole/actionList") == 0 && mode == ACTION_MODE) + else if (strcmp(topic,actions) == 0 && mode == ACTION_MODE) { for (int i = 0; i < length; i++) {