From 2cc737131bce7e2dd3814615c4cdd0b2464c02bd Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Tue, 11 Aug 2020 23:59:43 +0200 Subject: [PATCH 1/6] added dotenv on root folder for env --- package-lock.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9e58b58 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,11 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + } + } +} From c842822c6c5af8ac5dbcd4fa76fd1eb6aa4f16fe Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Wed, 12 Aug 2020 12:18:46 +0200 Subject: [PATCH 2/6] topics now use baseTopic var from .env file --- esp32-IoT-Button/src/main.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/esp32-IoT-Button/src/main.cpp b/esp32-IoT-Button/src/main.cpp index 7565802..5648de9 100644 --- a/esp32-IoT-Button/src/main.cpp +++ b/esp32-IoT-Button/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -129,12 +130,14 @@ 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/"; +std::string baseTopic=getenv("BASETOPIC"); + +const char *broker = getenv("MQTT_BROKER"); //Adresse des Brokers +const char *orderList = (baseTopic+="/list").c_str(); //Ein Topic +const char *outTopic = (baseTopic+="/order/").c_str(); +const char *actions = (baseTopic+="/actionList").c_str(); +const char *modeTopic =(baseTopic+="/mode").c_str(); +const char *actionOut =(baseTopic+="/action/").c_str(); void reconnect() { // Loop until we're reconnected @@ -172,7 +175,7 @@ void callback(char *topic, byte *payload, unsigned int length) lcd.setCursor(0, 0); lcd.print("Updating..."); Serial.println(topic); - String setup = "thkoeln/IoT/setup/"; + String setup =(baseTopic+="/setup/").c_str(); setup += WiFi.macAddress(); Serial.println(setup); const char *c = setup.c_str(); @@ -194,7 +197,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,(baseTopic+="/mode").c_str()) == 0) { char buffer[128]; memcpy(buffer, payload, length); @@ -222,7 +225,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,(baseTopic+="/list").c_str()) == 0 && mode == ORDER_MODE) { for (int i = 0; i < length; i++) { @@ -235,7 +238,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,(baseTopic+="/actionList").c_str()) == 0 && mode == ACTION_MODE) { for (int i = 0; i < length; i++) { From 3fce5bab08e7e661ea3ff814eff44ce4d8dfd6a6 Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Wed, 12 Aug 2020 13:03:21 +0200 Subject: [PATCH 3/6] topics now defined by baseTopic in .env --- backend/index.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) 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); From 6f3fab2c4b21c316a2a5fdb079e3b398c900ddd9 Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Wed, 12 Aug 2020 13:10:10 +0200 Subject: [PATCH 4/6] added missing baseTopic uses --- esp32-IoT-Button/src/main.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/esp32-IoT-Button/src/main.cpp b/esp32-IoT-Button/src/main.cpp index 5648de9..dd42152 100644 --- a/esp32-IoT-Button/src/main.cpp +++ b/esp32-IoT-Button/src/main.cpp @@ -83,6 +83,7 @@ byte bottomRightCheck[] = { B11000, B10000}; +std::string baseTopic=getenv("BASETOPIC"); /*** Initales verbinden mit WLAN ***/ void setupWifi() { @@ -121,8 +122,8 @@ void initialSetup() 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((baseTopic+="/setup/").c_str(), WiFi.macAddress().c_str()); + String in = (baseTopic+="/setup/").c_str(); in += WiFi.macAddress(); Serial.println(in); const char *c = in.c_str(); @@ -130,7 +131,7 @@ void initialSetup() } /*** Connenct/Reconnect to MQTT Broker in Case of Connection loss ***/ -std::string baseTopic=getenv("BASETOPIC"); + const char *broker = getenv("MQTT_BROKER"); //Adresse des Brokers const char *orderList = (baseTopic+="/list").c_str(); //Ein Topic From ba58b4cfb627df322fe8a13216f281a5dbb65b08 Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Wed, 12 Aug 2020 18:01:27 +0200 Subject: [PATCH 5/6] deleted package-json in root --- package-lock.json | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 9e58b58..0000000 --- a/package-lock.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "dotenv": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", - "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" - } - } -} From b142bffa388913ce3f619d6561d77d51aecce1f5 Mon Sep 17 00:00:00 2001 From: George Iyawe Date: Wed, 12 Aug 2020 18:01:59 +0200 Subject: [PATCH 6/6] rewrote modular topics --- esp32-IoT-Button/src/main.cpp | 37 +++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/esp32-IoT-Button/src/main.cpp b/esp32-IoT-Button/src/main.cpp index dd42152..99c8612 100644 --- a/esp32-IoT-Button/src/main.cpp +++ b/esp32-IoT-Button/src/main.cpp @@ -83,7 +83,7 @@ byte bottomRightCheck[] = { B11000, B10000}; -std::string baseTopic=getenv("BASETOPIC"); + /*** Initales verbinden mit WLAN ***/ void setupWifi() { @@ -118,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((baseTopic+="/setup/").c_str(), WiFi.macAddress().c_str()); - String in = (baseTopic+="/setup/").c_str(); + client.publish(setupTopic, WiFi.macAddress().c_str()); + String in = setupTopic; in += WiFi.macAddress(); Serial.println(in); const char *c = in.c_str(); @@ -133,12 +135,17 @@ void initialSetup() /*** Connenct/Reconnect to MQTT Broker in Case of Connection loss ***/ -const char *broker = getenv("MQTT_BROKER"); //Adresse des Brokers -const char *orderList = (baseTopic+="/list").c_str(); //Ein Topic -const char *outTopic = (baseTopic+="/order/").c_str(); -const char *actions = (baseTopic+="/actionList").c_str(); -const char *modeTopic =(baseTopic+="/mode").c_str(); -const char *actionOut =(baseTopic+="/action/").c_str(); +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 @@ -172,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 =(baseTopic+="/setup/").c_str(); + String setup = modeTopic; setup += WiFi.macAddress(); Serial.println(setup); const char *c = setup.c_str(); @@ -198,7 +209,7 @@ void callback(char *topic, byte *payload, unsigned int length) mitarbeiterID = value; } } - if (strcmp(topic,(baseTopic+="/mode").c_str()) == 0) + if (strcmp(topic, modeTopic) == 0) { char buffer[128]; memcpy(buffer, payload, length); @@ -226,7 +237,7 @@ void callback(char *topic, byte *payload, unsigned int length) Serial.println(mode); } } - if (strcmp(topic,(baseTopic+="/list").c_str()) == 0 && mode == ORDER_MODE) + if (strcmp(topic,modeTopic) == 0 && mode == ORDER_MODE) { for (int i = 0; i < length; i++) { @@ -239,7 +250,7 @@ void callback(char *topic, byte *payload, unsigned int length) if (hits >= len) hits = len - 1; } - else if (strcmp(topic,(baseTopic+="/actionList").c_str()) == 0 && mode == ACTION_MODE) + else if (strcmp(topic,actions) == 0 && mode == ACTION_MODE) { for (int i = 0; i < length; i++) {