add login for mqtt
This commit is contained in:
parent
5d9a1be202
commit
7f4c65eb0a
23
.vscode/launch.json
vendored
23
.vscode/launch.json
vendored
@ -5,7 +5,28 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"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
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -2,7 +2,7 @@
|
|||||||
"makefile.launchConfigurations": [
|
"makefile.launchConfigurations": [
|
||||||
{
|
{
|
||||||
"cwd": "/home/mreenen/Documents/git/relayClient",
|
"cwd": "/home/mreenen/Documents/git/relayClient",
|
||||||
"binaryPath": "/home/mreenen/Documents/git/relayClient/relayClient",
|
"binaryPath": "/home/mreenen/Documents/git/relayClient/mqttClient",
|
||||||
"binaryArgs": []
|
"binaryArgs": []
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
gcc src/main.c src/gpio.c \
|
|
||||||
-lpthread -lpaho-mqtt3a \
|
|
||||||
-o relayClient \
|
|
||||||
-I src -I include/paho-mqtt
|
|
||||||
@ -12,14 +12,9 @@ Group=<username>
|
|||||||
WorkingDirectory=/home/<username>
|
WorkingDirectory=/home/<username>
|
||||||
|
|
||||||
ExecStart=/home/<username>/.local/bin/relayClient
|
ExecStart=/home/<username>/.local/bin/relayClient
|
||||||
# Use SIGINT to stop
|
|
||||||
KillSignal=SIGINT
|
|
||||||
# Auto restart on crash
|
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RestartSec=20
|
RestartSec=20
|
||||||
# Tag things in the log
|
|
||||||
SyslogIdentifier=relayClient
|
SyslogIdentifier=relayClient
|
||||||
#StandardOutput=syslog
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#define ADDRESS "tcp://10.2.0.3:1883"
|
#define ADDRESS "tcp://10.2.0.3:1883"
|
||||||
#define CLIENTID "mqttClient"
|
#define CLIENTID "mqttClient"
|
||||||
|
#define MQTT_USER "mreenen"
|
||||||
|
#define MQTT_PASS "somepass"
|
||||||
#define BASE_TOPIC "/cool/"
|
#define BASE_TOPIC "/cool/"
|
||||||
#define TIMEOUT 10000L
|
#define TIMEOUT 10000L
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,6 @@ void MQTT_subscribe(char* topic, int qos, void (*onMessage)(char* topicName, int
|
|||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
|
||||||
if (Subscriptions == NULL)
|
if (Subscriptions == NULL)
|
||||||
{
|
{
|
||||||
Subscriptions = (Subscription_t *) malloc(sizeof(Subscription_t) * 5);
|
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
|
else
|
||||||
{
|
{
|
||||||
// copy topic to allow passing local variable
|
|
||||||
char *newTopic = malloc(strlen(topic) + 1);
|
char *newTopic = malloc(strlen(topic) + 1);
|
||||||
memcpy(newTopic, topic, strlen(topic) + 1);
|
memcpy(newTopic, topic, strlen(topic) + 1);
|
||||||
sub.topic = newTopic;
|
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));
|
memcpy(Subscriptions + sizeof(Subscription_t)*(Subscriptions_len), &sub, sizeof(Subscription_t));
|
||||||
Subscriptions_len++;
|
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;
|
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
|
||||||
opts.onSuccess = &onSubscribe;
|
opts.onSuccess = &onSubscribe;
|
||||||
@ -204,6 +202,8 @@ clientConf_t* MQTT_connect(void (*onConnect_cb)(void* context)){
|
|||||||
Client->conn_opts.onSuccess = onConn;
|
Client->conn_opts.onSuccess = onConn;
|
||||||
Client->conn_opts.onFailure = onConnectFailure;
|
Client->conn_opts.onFailure = onConnectFailure;
|
||||||
Client->conn_opts.context = &Client;
|
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);
|
printf("connecting to MQTT server (%s)\n", ADDRESS);
|
||||||
rc = MQTTAsync_connect(Client->client, &(Client->conn_opts));
|
rc = MQTTAsync_connect(Client->client, &(Client->conn_opts));
|
||||||
if(rc != MQTTASYNC_SUCCESS){
|
if(rc != MQTTASYNC_SUCCESS){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user