From dd39bd30219f4f1247337241b485450070dd277a Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Mon, 2 Dec 2024 13:44:15 +0100 Subject: [PATCH] fixed mqtt and sockets and reverse proxy after 5 hours --- src/C++/Driver/src/KobukiDriver/CKobuki.h | 1 - src/C++/Driver/src/MQTT/MqttClient.cpp | 1 + src/C++/Driver/src/main.cpp | 14 +++++++++----- src/Python/flask/web/app.py | 2 +- src/config/nginx-sites.conf | 22 ++++++++++++++++++++++ src/config/nginx.conf | 7 +++++++ 6 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/config/nginx-sites.conf create mode 100644 src/config/nginx.conf diff --git a/src/C++/Driver/src/KobukiDriver/CKobuki.h b/src/C++/Driver/src/KobukiDriver/CKobuki.h index 0567df1..a3fb1c1 100755 --- a/src/C++/Driver/src/KobukiDriver/CKobuki.h +++ b/src/C++/Driver/src/KobukiDriver/CKobuki.h @@ -31,7 +31,6 @@ #include #include #include "KobukiParser.h" -#include "graph.h" using namespace std; diff --git a/src/C++/Driver/src/MQTT/MqttClient.cpp b/src/C++/Driver/src/MQTT/MqttClient.cpp index 7386cb2..50940bb 100644 --- a/src/C++/Driver/src/MQTT/MqttClient.cpp +++ b/src/C++/Driver/src/MQTT/MqttClient.cpp @@ -5,6 +5,7 @@ MqttClient::MqttClient(const std::string& address, const std::string& clientId, //here all the @PARAMS are getting set for the connection : client_(address, clientId), username_(username), password_(password), callback_(*this) { client_.set_callback(callback_); + options.set_clean_session(true); options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1 if (!username_.empty() && !password_.empty()) { diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index b1ba02f..727b6c4 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "KobukiDriver/graph.h" +#include // For unsetenv #include "MQTT/MqttClient.h" #include "KobukiDriver/CKobuki.h" @@ -10,7 +10,7 @@ CKobuki robot; std::string readMQTT(); void parseMQTT(std::string message); //ip, clientID, username, password -MqttClient client("mqtt://145.92.224.21:1884", "KobukiRPI", "rpi", "rpiwachtwoordofzo"); // create a client object +MqttClient client("ws://145.92.224.21/ws/", "KobukiRPI", "rpi", "rpiwachtwoordofzo"); // create a client object std::string message = "stop"; std::string serializeKobukiData(const TKobukiData &data); void sendKobukiData(TKobukiData &data); @@ -18,24 +18,27 @@ void sendKobukiData(TKobukiData &data); void setup() { unsigned char *null_ptr(0); - robot.startCommunication("/dev/ttyUSB0", true, null_ptr); + // robot.startCommunication("/dev/ttyUSB0", true, null_ptr); //connect mqtt server and sub to commands + client.connect(); client.subscribe("home/commands"); } int main() { + // Unset the http_proxy environment variable + + setup(); std::thread safety([&]() { robot.robotSafety(&message); }); std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); }); while(true){ - parseMQTT(readMQTT()); + parseMQTT(readMQTT()); } sendMqtt.join(); safety.join(); - return 0; } std::string readMQTT() @@ -270,6 +273,7 @@ std::string serializeKobukiData(const TKobukiData &data) { void sendKobukiData(TKobukiData &data) { while (true) { client.publishMessage("kobuki/data", serializeKobukiData(data)); + std::cout << "Sent data" << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index 60ddd63..4864394 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -10,7 +10,7 @@ def on_message(client, userdata, message): # Create an MQTT client instance mqtt_client = mqtt.Client() mqtt_client.username_pw_set("server", "serverwachtwoordofzo") -mqtt_client.connect("localhost", 1884, 60) +mqtt_client.connect("145.92.224.21/ws", 80, 60) mqtt_client.loop_start() mqtt_client.subscribe("kobuki/data") mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use diff --git a/src/config/nginx-sites.conf b/src/config/nginx-sites.conf new file mode 100644 index 0000000..e213464 --- /dev/null +++ b/src/config/nginx-sites.conf @@ -0,0 +1,22 @@ +server { + listen 80; + server_name 145.92.224.21; + + # Proxy WebSocket connections for MQTT + location /ws/ { + proxy_pass http://localhost:9001; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + } + + # Proxy HTTP connections for Flask + location / { + proxy_pass http://localhost:5000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } +} diff --git a/src/config/nginx.conf b/src/config/nginx.conf new file mode 100644 index 0000000..0f13dc0 --- /dev/null +++ b/src/config/nginx.conf @@ -0,0 +1,7 @@ +stream { + server { + listen 9001; + proxy_pass localhost:8080; + } +} +