fixed mqtt and sockets and reverse proxy after 5 hours

This commit is contained in:
2024-12-02 13:44:15 +01:00
parent 8aa54805ac
commit dd39bd3021
6 changed files with 40 additions and 7 deletions

View File

@@ -31,7 +31,6 @@
#include <chrono> #include <chrono>
#include <sstream> #include <sstream>
#include "KobukiParser.h" #include "KobukiParser.h"
#include "graph.h"
using namespace std; using namespace std;

View File

@@ -5,6 +5,7 @@ MqttClient::MqttClient(const std::string& address, const std::string& clientId,
//here all the @PARAMS are getting set for the connection //here all the @PARAMS are getting set for the connection
: client_(address, clientId), username_(username), password_(password), callback_(*this) { : client_(address, clientId), username_(username), password_(password), callback_(*this) {
client_.set_callback(callback_); client_.set_callback(callback_);
options.set_clean_session(true); options.set_clean_session(true);
options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1 options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
if (!username_.empty() && !password_.empty()) { if (!username_.empty() && !password_.empty()) {

View File

@@ -1,7 +1,7 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <thread> #include <thread>
#include "KobukiDriver/graph.h" #include <cstdlib> // For unsetenv
#include "MQTT/MqttClient.h" #include "MQTT/MqttClient.h"
#include "KobukiDriver/CKobuki.h" #include "KobukiDriver/CKobuki.h"
@@ -10,7 +10,7 @@ CKobuki robot;
std::string readMQTT(); std::string readMQTT();
void parseMQTT(std::string message); void parseMQTT(std::string message);
//ip, clientID, username, password //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 message = "stop";
std::string serializeKobukiData(const TKobukiData &data); std::string serializeKobukiData(const TKobukiData &data);
void sendKobukiData(TKobukiData &data); void sendKobukiData(TKobukiData &data);
@@ -18,14 +18,18 @@ void sendKobukiData(TKobukiData &data);
void setup() void setup()
{ {
unsigned char *null_ptr(0); 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 //connect mqtt server and sub to commands
client.connect(); client.connect();
client.subscribe("home/commands"); client.subscribe("home/commands");
} }
int main() int main()
{ {
// Unset the http_proxy environment variable
setup(); setup();
std::thread safety([&]() { robot.robotSafety(&message); }); std::thread safety([&]() { robot.robotSafety(&message); });
std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); }); std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); });
@@ -35,7 +39,6 @@ int main()
} }
sendMqtt.join(); sendMqtt.join();
safety.join(); safety.join();
return 0;
} }
std::string readMQTT() std::string readMQTT()
@@ -270,6 +273,7 @@ std::string serializeKobukiData(const TKobukiData &data) {
void sendKobukiData(TKobukiData &data) { void sendKobukiData(TKobukiData &data) {
while (true) { while (true) {
client.publishMessage("kobuki/data", serializeKobukiData(data)); client.publishMessage("kobuki/data", serializeKobukiData(data));
std::cout << "Sent data" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} }
} }

View File

@@ -10,7 +10,7 @@ def on_message(client, userdata, message):
# Create an MQTT client instance # Create an MQTT client instance
mqtt_client = mqtt.Client() mqtt_client = mqtt.Client()
mqtt_client.username_pw_set("server", "serverwachtwoordofzo") 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.loop_start()
mqtt_client.subscribe("kobuki/data") 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 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

View File

@@ -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;
}
}

7
src/config/nginx.conf Normal file
View File

@@ -0,0 +1,7 @@
stream {
server {
listen 9001;
proxy_pass localhost:8080;
}
}