Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,29 +30,29 @@ client.on('connect', function () {
client.subscribe('mittelkonsole/order/+', function (err) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fehlt hier nicht die base 🤔

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);
});
});
Expand All @@ -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;
Expand All @@ -79,15 +77,15 @@ 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;
});
Actions.create(computedMessage).then(async (data) => {
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;
Expand All @@ -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];
Expand All @@ -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({
Expand All @@ -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);
Expand Down Expand Up @@ -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 },
);
Expand All @@ -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,
});
});
Expand All @@ -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,
});
});
Expand All @@ -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,
});
});
Expand All @@ -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,
});
});
Expand All @@ -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);
Expand Down
39 changes: 27 additions & 12 deletions esp32-IoT-Button/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>
Expand Down Expand Up @@ -82,6 +83,7 @@ byte bottomRightCheck[] = {
B11000,
B10000};


/*** Initales verbinden mit WLAN ***/
void setupWifi()
{
Expand Down Expand Up @@ -116,25 +118,34 @@ 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();
client.subscribe(c);
}

/*** 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
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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++)
{
Expand All @@ -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++)
{
Expand Down