add login for mqtt

This commit is contained in:
mreenen 2023-03-27 19:56:40 +02:00
parent 5d9a1be202
commit 7f4c65eb0a
6 changed files with 28 additions and 16 deletions

23
.vscode/launch.json vendored
View File

@ -5,7 +5,28 @@
"version": "0.2.0",
"configurations": [
{
"type": "cppdbg"
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/mqttClient",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

View File

@ -2,7 +2,7 @@
"makefile.launchConfigurations": [
{
"cwd": "/home/mreenen/Documents/git/relayClient",
"binaryPath": "/home/mreenen/Documents/git/relayClient/relayClient",
"binaryPath": "/home/mreenen/Documents/git/relayClient/mqttClient",
"binaryArgs": []
}
]

View File

@ -1,6 +0,0 @@
#!/bin/bash
gcc src/main.c src/gpio.c \
-lpthread -lpaho-mqtt3a \
-o relayClient \
-I src -I include/paho-mqtt

View File

@ -12,14 +12,9 @@ Group=<username>
WorkingDirectory=/home/<username>
ExecStart=/home/<username>/.local/bin/relayClient
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
RestartSec=20
# Tag things in the log
SyslogIdentifier=relayClient
#StandardOutput=syslog
[Install]
WantedBy=multi-user.target

View File

@ -3,6 +3,8 @@
#define ADDRESS "tcp://10.2.0.3:1883"
#define CLIENTID "mqttClient"
#define MQTT_USER "mreenen"
#define MQTT_PASS "somepass"
#define BASE_TOPIC "/cool/"
#define TIMEOUT 10000L

View File

@ -34,7 +34,6 @@ void MQTT_subscribe(char* topic, int qos, void (*onMessage)(char* topicName, int
{
int rc;
if (Subscriptions == NULL)
{
Subscriptions = (Subscription_t *) malloc(sizeof(Subscription_t) * 5);
@ -63,7 +62,6 @@ void MQTT_subscribe(char* topic, int qos, void (*onMessage)(char* topicName, int
}
else
{
// copy topic to allow passing local variable
char *newTopic = malloc(strlen(topic) + 1);
memcpy(newTopic, topic, strlen(topic) + 1);
sub.topic = newTopic;
@ -72,7 +70,7 @@ void MQTT_subscribe(char* topic, int qos, void (*onMessage)(char* topicName, int
memcpy(Subscriptions + sizeof(Subscription_t)*(Subscriptions_len), &sub, sizeof(Subscription_t));
Subscriptions_len++;
printf("INFO: MQTT_subscribe(): Subscribing to topic %s (QoS%d)\n", sub.topic, sub.qos);
printf("INFO: MQTT_subscribe(): Subscribing to topic '%s' (QoS%d)\n", sub.topic, sub.qos);
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
opts.onSuccess = &onSubscribe;
@ -204,6 +202,8 @@ clientConf_t* MQTT_connect(void (*onConnect_cb)(void* context)){
Client->conn_opts.onSuccess = onConn;
Client->conn_opts.onFailure = onConnectFailure;
Client->conn_opts.context = &Client;
Client->conn_opts.username = MQTT_USER;
Client->conn_opts.password = MQTT_PASS;
printf("connecting to MQTT server (%s)\n", ADDRESS);
rc = MQTTAsync_connect(Client->client, &(Client->conn_opts));
if(rc != MQTTASYNC_SUCCESS){