send rref to mqtt
This commit is contained in:
parent
60cfcf0bc7
commit
0ac6418870
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
bin/
|
||||
.vscode/
|
||||
.vscode/
|
||||
config.h
|
||||
|
2
Makefile
2
Makefile
@ -6,7 +6,7 @@ command:
|
||||
gcc -o ${BIN}/command network.c command.c
|
||||
|
||||
readref:
|
||||
gcc -o ${BIN}/readref network.c readref.c
|
||||
gcc -o ${BIN}/readref network.c mqtt.c -lmosquitto readref.c
|
||||
|
||||
.PHONY: clean dirs
|
||||
clean:
|
||||
|
74
mqtt.c
Normal file
74
mqtt.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
// https://mosquitto.org/api/files/mosquitto-h.html
|
||||
#include <mosquitto.h>
|
||||
|
||||
struct mosquitto *connectMqtt(char *user, char *pass)
|
||||
{
|
||||
int err;
|
||||
struct mosquitto *mqtt;
|
||||
|
||||
err = mosquitto_lib_init();
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error initalizing mosquitto lib (%d): %s\n", err, mosquitto_strerror(err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mqtt = mosquitto_new(user, true, NULL);
|
||||
if (mqtt == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error creating mosquitto instance (%d): %s\n", errno, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = mosquitto_username_pw_set(mqtt, user, pass);
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error setting username & password (%d): %s\n", err, mosquitto_strerror(err));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err = mosquitto_connect(mqtt, "openhab.sugarland.lan", 1883, 10);
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error on connection of type ");
|
||||
if (err == MOSQ_ERR_ERRNO)
|
||||
{
|
||||
fprintf(stderr, "system (%d): %s\n", errno, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "mosquitto (%d): %s\n", err, mosquitto_strerror(err));
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mqtt;
|
||||
}
|
||||
|
||||
int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = mosquitto_publish(mqtt, NULL, topic, payloadLength, payload, 0, false);
|
||||
if (err)
|
||||
{
|
||||
fprintf(stderr, "Error publishing message (%d): %s\n", err, mosquitto_strerror(err));
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void disconnectMqtt(struct mosquitto *mqtt)
|
||||
{
|
||||
// ignore errors
|
||||
mosquitto_disconnect(mqtt);
|
||||
mosquitto_destroy(mqtt);
|
||||
}
|
3
mqtt.h
Normal file
3
mqtt.h
Normal file
@ -0,0 +1,3 @@
|
||||
struct mosquitto * connectMqtt(char *user, char *pass);
|
||||
int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength);
|
||||
void disconnectMqtt(struct mosquitto *mqtt);
|
25
readref.c
25
readref.c
@ -4,6 +4,9 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "network.h"
|
||||
#include "mqtt.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
struct dref_struct_in
|
||||
{
|
||||
@ -21,6 +24,7 @@ struct dref_struct_out
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int xPlaneSocket;
|
||||
struct mosquitto *mqtt;
|
||||
char *wtf;
|
||||
|
||||
// check argument
|
||||
@ -37,8 +41,10 @@ int main(int argc, char *argv[])
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// char drefsToRead[413 * ((argc - 1) / 3)];
|
||||
// memset(drefsToRead, 0, sizeof(drefsToRead));
|
||||
// mqtt
|
||||
mqtt = connectMqtt(MQTT_USER, MQTT_PASS);
|
||||
|
||||
// request every dref from arguments
|
||||
for (int i = 1; i < argc; i = i + 3)
|
||||
{
|
||||
// prepare struct to send, make sure padding is with NULL bytes
|
||||
@ -53,13 +59,13 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// memcpy(&drefsToRead[((i - 1) / 3) * 413], &drefToRead, sizeof(drefToRead));
|
||||
}
|
||||
|
||||
// read from network
|
||||
char msg[DGRAM_MSG_LENGTH + 1];
|
||||
char payload[RECV_BUFFER];
|
||||
|
||||
char mqttTopic[100], mqttPayload[512];
|
||||
while (1)
|
||||
{
|
||||
int payloadBytes = recvFromXPlane(xPlaneSocket, msg, payload, RECV_BUFFER);
|
||||
@ -74,13 +80,21 @@ int main(int argc, char *argv[])
|
||||
printf("\n");
|
||||
if (strcmp(msg, "RREF") == 0)
|
||||
{
|
||||
// XXX: loop throught multiple drefs
|
||||
if ((payloadBytes - 1) % 8 == 0)
|
||||
{
|
||||
// XXX: loop
|
||||
memset(mqttTopic, 0, sizeof(mqttTopic));
|
||||
memset(mqttPayload, 0, sizeof(mqttPayload));
|
||||
|
||||
struct dref_struct_out drefRead;
|
||||
memcpy(&drefRead, &payload[1], 8);
|
||||
|
||||
printf("%d: %f\n", drefRead.dref_sender_index, drefRead.dref_flt_value);
|
||||
|
||||
sprintf(mqttTopic, "/xplane/rref/%d", drefRead.dref_sender_index);
|
||||
sprintf(mqttPayload, "%f", drefRead.dref_flt_value);
|
||||
|
||||
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -99,6 +113,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
disconnectMqtt(mqtt);
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user