Compare commits
3 Commits
08990a7a93
...
107297f162
Author | SHA1 | Date | |
---|---|---|---|
107297f162 | |||
d8fe68679a | |||
53bf588ba3 |
@ -1,6 +1,7 @@
|
|||||||
#define DATA_STOP_BYTE 0x00
|
#define DATA_STOP_BYTE 0x00
|
||||||
#define DATA_INIT_BYTE_1 0x01
|
#define DATA_INIT_BYTE_1 0x01
|
||||||
#define DATA_INIT_BYTE_2 0x02
|
#define DATA_INIT_BYTE_2 0x02
|
||||||
|
#define DATA_BYTE 0x03
|
||||||
#define DATA_RESET_BYTE 0xFF
|
#define DATA_RESET_BYTE 0xFF
|
||||||
#define TRIGGER_BIT_1 1
|
#define TRIGGER_BIT_1 1
|
||||||
#define HIGH_BIT_1 2
|
#define HIGH_BIT_1 2
|
||||||
|
@ -15,27 +15,52 @@
|
|||||||
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
||||||
|
|
||||||
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
|
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
|
||||||
#define MQTT_SIM_DEVICE_TOPIC "/xplane/meta/device"
|
#define MQTT_LOG_TOPIC "/xplane/meta/log"
|
||||||
#define MQTT_SIM_LOG_TOPIC "/xplane/meta/log"
|
|
||||||
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
|
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
|
||||||
|
#define MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX "/xplane/rref/"
|
||||||
|
#define MQTT_SIM_VALUE_SUBSCRIBE_TOPIC "/xplane/rref"
|
||||||
|
|
||||||
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
||||||
|
|
||||||
struct device {
|
struct device {
|
||||||
byte address; // I2C address of device
|
byte address; // I2C address of device
|
||||||
char *name; // name of this device
|
char *name; // name of this device
|
||||||
char name1[3]; // first name to send on init of device
|
char name1[3]; // first name to send on init of device
|
||||||
char *highCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is set
|
char *highCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is set
|
||||||
char *lowCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is not set
|
char *lowCommand1; // command to use if TRIGGER_BIT_1 is set and HIGH_BIT_1 is not set
|
||||||
|
int rrefRefresh1; // refresh rate of subscribing rref
|
||||||
|
char *rref1; // rref to subscribe to
|
||||||
char name2[3]; // second name to send on init of device
|
char name2[3]; // second name to send on init of device
|
||||||
char *highCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is set
|
char *highCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is set
|
||||||
char *lowCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is not set
|
char *lowCommand2; // command to use if TRIGGER_BIT_2 is set and HIGH_BIT_2 is not set
|
||||||
|
int rrefRefresh2; // refresh rate of subscribing rref
|
||||||
|
char *rref2; // rref to subscribe to
|
||||||
};
|
};
|
||||||
|
|
||||||
struct device devices[] = {
|
struct device devices[] = {
|
||||||
{ 0x08, "Heading Speed", {'H', 'D', 'G'}, HEADING_SIM_UP, HEADING_SIM_DOWN, {'S', 'P', 'D'}, AIRSPEED_SIM_UP, AIRSPEED_SIM_DOWN }
|
{
|
||||||
|
0x08,
|
||||||
|
"Heading Speed",
|
||||||
|
{'H', 'D', 'G'},
|
||||||
|
HEADING_SIM_UP,
|
||||||
|
HEADING_SIM_DOWN,
|
||||||
|
200,
|
||||||
|
"sim/cockpit/autopilot/heading_mag",
|
||||||
|
{'S', 'P', 'D'},
|
||||||
|
AIRSPEED_SIM_UP,
|
||||||
|
AIRSPEED_SIM_DOWN,
|
||||||
|
200,
|
||||||
|
"sim/cockpit/autopilot/airspeed"
|
||||||
|
}
|
||||||
};
|
};
|
||||||
int numDevices = -1; // gets calculated in setup()
|
const int numDevices = sizeof(devices) / sizeof(device);
|
||||||
|
|
||||||
|
struct rrefSubscription {
|
||||||
|
byte address;
|
||||||
|
byte triggerBit;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct rrefSubscription rrefSubscriptions[numDevices * 2];
|
||||||
|
|
||||||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||||
EthernetClient client;
|
EthernetClient client;
|
||||||
@ -43,20 +68,21 @@ EthernetClient client;
|
|||||||
MqttClient mqttClient(client);
|
MqttClient mqttClient(client);
|
||||||
|
|
||||||
void onMqttMessage(int messageSize) {
|
void onMqttMessage(int messageSize) {
|
||||||
// // we received a message, print out the topic and contents
|
// assume, that we always receive topic beginning with MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX
|
||||||
// Serial.println("Received a message with topic '");
|
// otherwise, this will _HORRIBLY_ fail
|
||||||
// Serial.print(mqttClient.messageTopic());
|
|
||||||
// Serial.print("', length ");
|
char *rrefNoStr = &mqttClient.messageTopic()[13];
|
||||||
// Serial.print(messageSize);
|
int rrefNo = atoi(rrefNoStr);
|
||||||
// Serial.println(" bytes:");
|
struct rrefSubscription rref = rrefSubscriptions[rrefNo - 1];
|
||||||
//
|
|
||||||
// // use the Stream interface to print the contents
|
Wire.beginTransmission(rref.address);
|
||||||
// while (mqttClient.available()) {
|
Wire.write(DATA_BYTE);
|
||||||
// Serial.print((char)mqttClient.read());
|
Wire.write(rref.triggerBit);
|
||||||
// }
|
Wire.write(messageSize);
|
||||||
// Serial.println();
|
while (mqttClient.available()) {
|
||||||
//
|
Wire.write((char)mqttClient.read());
|
||||||
// Serial.println();
|
}
|
||||||
|
Wire.endTransmission(rref.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendMqttMessage(char *topic, int n, ...) {
|
void sendMqttMessage(char *topic, int n, ...) {
|
||||||
@ -71,7 +97,7 @@ void sendMqttMessage(char *topic, int n, ...) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sendI2CInit(struct device d) {
|
void sendI2CInit(struct device d) {
|
||||||
sendMqttMessage(MQTT_SIM_LOG_TOPIC, 2, "Sending init to ", d.name);
|
sendMqttMessage(MQTT_LOG_TOPIC, 2, "Sending init to ", d.name);
|
||||||
Wire.beginTransmission(d.address);
|
Wire.beginTransmission(d.address);
|
||||||
Wire.write(DATA_RESET_BYTE);
|
Wire.write(DATA_RESET_BYTE);
|
||||||
Wire.write(DATA_INIT_BYTE_1);
|
Wire.write(DATA_INIT_BYTE_1);
|
||||||
@ -81,10 +107,28 @@ void sendI2CInit(struct device d) {
|
|||||||
Wire.endTransmission(d.address);
|
Wire.endTransmission(d.address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void subscribeRrefs(struct device *d, int deviceNo) {
|
||||||
// count devices for loop
|
char subscribeMsg[256];
|
||||||
numDevices = sizeof(devices) / sizeof(device);
|
|
||||||
|
|
||||||
|
// subscribe no 1
|
||||||
|
sprintf(subscribeMsg, "%03d %03d %s", deviceNo + 1, d->rrefRefresh1, d->rref1); // subscription no is index-1 based
|
||||||
|
sendMqttMessage(MQTT_SIM_VALUE_SUBSCRIBE_TOPIC, 1, subscribeMsg);
|
||||||
|
rrefSubscriptions[deviceNo] = { d->address, TRIGGER_BIT_1 };
|
||||||
|
|
||||||
|
// subscribe no 2
|
||||||
|
sprintf(subscribeMsg, "%03d %03d %s", deviceNo + 2, d->rrefRefresh2, d->rref2); // subscription no is index-1 based
|
||||||
|
sendMqttMessage(MQTT_SIM_VALUE_SUBSCRIBE_TOPIC, 1, subscribeMsg);
|
||||||
|
rrefSubscriptions[deviceNo + 1] = { d->address, TRIGGER_BIT_2 };
|
||||||
|
}
|
||||||
|
|
||||||
|
void initializeDevices() {
|
||||||
|
for (int i = 0; i < numDevices; i++) {
|
||||||
|
sendI2CInit(devices[i]);
|
||||||
|
subscribeRrefs(&devices[i], i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
// start the Ethernet connection:
|
// start the Ethernet connection:
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
Serial.println("Failed to configure Ethernet using DHCP");
|
||||||
@ -107,7 +151,7 @@ void setup() {
|
|||||||
while (1);
|
while (1);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("MQTT connected");
|
Serial.println("MQTT connected");
|
||||||
sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, "Master online");
|
sendMqttMessage(MQTT_LOG_TOPIC, 2, "Master online ", localIP);
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribe to MQTT topic
|
// subscribe to MQTT topic
|
||||||
@ -118,16 +162,7 @@ void setup() {
|
|||||||
// start I2C
|
// start I2C
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
|
|
||||||
// for debugging
|
initializeDevices();
|
||||||
pinMode(LED_TX, OUTPUT);
|
|
||||||
digitalWrite(LED_TX, HIGH);
|
|
||||||
pinMode(LED_RX, OUTPUT);
|
|
||||||
digitalWrite(LED_RX, HIGH);
|
|
||||||
|
|
||||||
// initialize devices
|
|
||||||
for (int i = 0; i < numDevices; i++) {
|
|
||||||
sendI2CInit(devices[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -143,7 +178,7 @@ void loop() {
|
|||||||
continue;
|
continue;
|
||||||
} else if (data == DATA_RESET_BYTE) {
|
} else if (data == DATA_RESET_BYTE) {
|
||||||
// device needs initialization
|
// device needs initialization
|
||||||
sendMqttMessage(MQTT_SIM_LOG_TOPIC, 2, "got reset byte from ", devices[i].name);
|
sendMqttMessage(MQTT_LOG_TOPIC, 2, "got reset byte from ", devices[i].name);
|
||||||
sendI2CInit(devices[i]);
|
sendI2CInit(devices[i]);
|
||||||
} else {
|
} else {
|
||||||
// handle as payload
|
// handle as payload
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
#define VALUE_BUFFER 30
|
#define VALUE_BUFFER 30
|
||||||
|
|
||||||
#define SKIP_ROTARY_INPUTS 50
|
#define SKIP_ROTARY_INPUTS 40
|
||||||
|
|
||||||
#define INTERNAL_STATE_UNKNOWN 0
|
#define INTERNAL_STATE_UNKNOWN 0
|
||||||
#define INTERNAL_STATE_INITIALIZED 1
|
#define INTERNAL_STATE_INITIALIZED 1
|
||||||
@ -160,6 +160,26 @@ void i2cReceive(int bytes) {
|
|||||||
for (int i = 0; i < 3 && Wire.available(); i++) {
|
for (int i = 0; i < 3 && Wire.available(); i++) {
|
||||||
name2[i] = Wire.read();
|
name2[i] = Wire.read();
|
||||||
}
|
}
|
||||||
|
} else if (data == DATA_BYTE) {
|
||||||
|
// next byte will be trigger bit
|
||||||
|
byte triggerBit = Wire.read();
|
||||||
|
|
||||||
|
// next byte will be length of data
|
||||||
|
byte dataLength = Wire.read();
|
||||||
|
|
||||||
|
// next byte(s) will be the data itself
|
||||||
|
char dataBytes[dataLength];
|
||||||
|
for (int i = 0; i < dataLength && Wire.available(); i++) {
|
||||||
|
dataBytes[i] = Wire.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (triggerBit & TRIGGER_BIT_1) {
|
||||||
|
updateDisplay1(dataBytes, dataLength);
|
||||||
|
} else if (triggerBit & TRIGGER_BIT_2) {
|
||||||
|
updateDisplay2(dataBytes, dataLength);
|
||||||
|
} else {
|
||||||
|
Serial.println("Unknown trigger bit");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// not yet implemented?
|
// not yet implemented?
|
||||||
Serial.println("Received unknown");
|
Serial.println("Received unknown");
|
||||||
@ -177,6 +197,35 @@ void displayTestScreen(Adafruit_SSD1306 display) {
|
|||||||
display.display();
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void updateDisplay1(char *data, byte len) {
|
||||||
|
updateDisplay(&display1, name1, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateDisplay2(char *data, byte len) {
|
||||||
|
updateDisplay(&display2, name2, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateDisplay(Adafruit_SSD1306 *display, char *name, char *data, byte len) {
|
||||||
|
display->clearDisplay();
|
||||||
|
display->setTextColor(SSD1306_WHITE); // Draw white text
|
||||||
|
display->cp437(true); // Use full 256 char 'Code Page 437' font
|
||||||
|
|
||||||
|
display->setTextSize(1); // Normal 1:1 pixel scale
|
||||||
|
display->setCursor(0, 8); // Start at top-left corner
|
||||||
|
display->setFont(NULL);
|
||||||
|
display->write(name[0]);
|
||||||
|
display->write(name[1]);
|
||||||
|
display->write(name[2]);
|
||||||
|
|
||||||
|
display->setTextSize(2); // Normal 1:1 pixel scale
|
||||||
|
display->setCursor(0, 56); // Start at top-left corner
|
||||||
|
display->setFont(&FreeMonoBold18pt7b);
|
||||||
|
for (byte i = 0; i < len; i++) {
|
||||||
|
display->write(data[i]);
|
||||||
|
}
|
||||||
|
display->display();
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t address = I2C_ADDRESS_START;
|
uint8_t address = I2C_ADDRESS_START;
|
||||||
void setup() {
|
void setup() {
|
||||||
// https://support.arduino.cc/hc/en-us/articles/4839084114460-If-your-board-runs-the-sketch-twice
|
// https://support.arduino.cc/hc/en-us/articles/4839084114460-If-your-board-runs-the-sketch-twice
|
||||||
@ -219,46 +268,11 @@ void setup() {
|
|||||||
displayTestScreen(display2);
|
displayTestScreen(display2);
|
||||||
// make sure, test screen is at least displayed 2 seconds
|
// make sure, test screen is at least displayed 2 seconds
|
||||||
delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
|
updateDisplay1("---", 3);
|
||||||
|
updateDisplay2("---", 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
display1.clearDisplay();
|
|
||||||
display1.setTextColor(SSD1306_WHITE); // Draw white text
|
|
||||||
display1.cp437(true); // Use full 256 char 'Code Page 437' font
|
|
||||||
|
|
||||||
display1.setTextSize(1); // Normal 1:1 pixel scale
|
|
||||||
display1.setCursor(0, 8); // Start at top-left corner
|
|
||||||
display1.setFont(NULL);
|
|
||||||
display1.write(name1[0]);
|
|
||||||
display1.write(name1[1]);
|
|
||||||
display1.write(name1[2]);
|
|
||||||
|
|
||||||
display1.setTextSize(2); // Normal 1:1 pixel scale
|
|
||||||
display1.setCursor(0, 56); // Start at top-left corner
|
|
||||||
display1.setFont(&FreeMonoBold18pt7b);
|
|
||||||
display1.write('8');
|
|
||||||
display1.write('9');
|
|
||||||
display1.write('0');
|
|
||||||
display1.display();
|
|
||||||
|
|
||||||
display2.clearDisplay();
|
|
||||||
display2.setTextColor(SSD1306_WHITE); // Draw white text
|
|
||||||
display2.cp437(true); // Use full 256 char 'Code Page 437' font
|
|
||||||
|
|
||||||
display2.setTextSize(1); // Normal 1:1 pixel scale
|
|
||||||
display2.setCursor(0, 8); // Start at top-left corner
|
|
||||||
display2.setFont(NULL);
|
|
||||||
display2.write(name2[0]);
|
|
||||||
display2.write(name2[1]);
|
|
||||||
display2.write(name2[2]);
|
|
||||||
|
|
||||||
display2.setTextSize(2); // Normal 1:1 pixel scale
|
|
||||||
display2.setCursor(0, 56); // Start at top-left corner
|
|
||||||
display2.setFont(&FreeMonoBold18pt7b);
|
|
||||||
display2.write('1');
|
|
||||||
display2.write('2');
|
|
||||||
display2.write('3');
|
|
||||||
display2.display();
|
|
||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
1
simulatesimulator/.gitignore
vendored
Normal file
1
simulatesimulator/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
main
|
1
simulatesimulator/build.sh
Executable file
1
simulatesimulator/build.sh
Executable file
@ -0,0 +1 @@
|
|||||||
|
gcc -o main mqtt.c -lmosquitto main.c
|
104
simulatesimulator/main.c
Normal file
104
simulatesimulator/main.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <mosquitto.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "mqtt.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#define MAX_AIRSPEED 385
|
||||||
|
#define MAX_MQTT_PAYLOAD_SIZE 512
|
||||||
|
|
||||||
|
struct mosquitto *mqtt;
|
||||||
|
|
||||||
|
int airspeed = 0;
|
||||||
|
int heading = 0;
|
||||||
|
|
||||||
|
struct test {
|
||||||
|
char *foo;
|
||||||
|
};
|
||||||
|
|
||||||
|
void onMessage(struct mosquitto *mqtt, void *obj, const struct mosquitto_message *message) {
|
||||||
|
char *mqttTopic;
|
||||||
|
char mqttPayload[MAX_MQTT_PAYLOAD_SIZE];
|
||||||
|
|
||||||
|
if (strcmp(message->topic, "/xplane/meta/rref") == 0) {
|
||||||
|
//requestRrefFromXPlane(message);
|
||||||
|
} else if (strcmp(message->topic, "/xplane/meta/cmnd") == 0) {
|
||||||
|
mqttTopic = NULL;
|
||||||
|
memset(mqttPayload, 0, sizeof(mqttPayload));
|
||||||
|
|
||||||
|
printf("CMD: %s\n", (char *)message->payload);
|
||||||
|
if (strcmp((char *)message->payload, "sim/autopilot/airspeed_up") == 0) {
|
||||||
|
airspeed++;
|
||||||
|
if (airspeed > MAX_AIRSPEED) {
|
||||||
|
airspeed = MAX_AIRSPEED;
|
||||||
|
} else {
|
||||||
|
// airspeed changed
|
||||||
|
mqttTopic = "/xplane/rref/2";
|
||||||
|
sprintf(mqttPayload, "%03d", airspeed);
|
||||||
|
}
|
||||||
|
printf("Airspeed: %d\n", airspeed);
|
||||||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/airspeed_down") == 0) {
|
||||||
|
airspeed--;
|
||||||
|
if (airspeed < 0) {
|
||||||
|
airspeed = 0;
|
||||||
|
} else {
|
||||||
|
mqttTopic = "/xplane/rref/2";
|
||||||
|
sprintf(mqttPayload, "%03d", airspeed);
|
||||||
|
}
|
||||||
|
printf("Airspeed: %d\n", airspeed);
|
||||||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/heading_up") == 0) {
|
||||||
|
heading++;
|
||||||
|
if (heading > 360) {
|
||||||
|
heading = 1;
|
||||||
|
}
|
||||||
|
mqttTopic = "/xplane/rref/1";
|
||||||
|
sprintf(mqttPayload, "%03d", heading);
|
||||||
|
printf("Heading: %d\n", heading);
|
||||||
|
} else if (strcmp((char *)message->payload, "sim/autopilot/heading_down") == 0) {
|
||||||
|
heading--;
|
||||||
|
if (heading < 0) {
|
||||||
|
heading = 359;
|
||||||
|
}
|
||||||
|
mqttTopic = "/xplane/rref/1";
|
||||||
|
sprintf(mqttPayload, "%03d", heading);
|
||||||
|
printf("Heading: %d\n", heading);
|
||||||
|
} else {
|
||||||
|
printf("unkown command %s\n", (char *)message->payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mqttTopic != NULL && mqttPayload != NULL) {
|
||||||
|
sendMqtt(mqtt, mqttTopic, mqttPayload, strlen(mqttPayload));
|
||||||
|
}
|
||||||
|
} else if (strcmp(message->topic, "/xplane/meta/log") == 0) {
|
||||||
|
printf("Log: %s\n", (char *)message->payload);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Unknown topic %s\n", message->topic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void doit(struct test *blah) {
|
||||||
|
blah->foo = "moep";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
struct test bar = { "baz" };
|
||||||
|
doit(&bar);
|
||||||
|
printf("foo is %s\n", bar.foo);
|
||||||
|
|
||||||
|
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
||||||
|
if (mqtt == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
recvMqtt(mqtt, "/xplane/meta/cmnd");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
mosquitto_loop(mqtt, 0, 1);
|
||||||
|
usleep(10 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectMqtt(mqtt);
|
||||||
|
}
|
93
simulatesimulator/mqtt.c
Normal file
93
simulatesimulator/mqtt.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#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, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: I am hard coded :(
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
mosquitto_message_callback_set(mqtt, messageCallback);
|
||||||
|
|
||||||
|
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 to %s (%d): %s\n", topic, err, mosquitto_strerror(err));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int recvMqtt(struct mosquitto *mqtt, char *topic)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
err = mosquitto_subscribe(mqtt, NULL, topic, 0);
|
||||||
|
if (err) {
|
||||||
|
fprintf(stderr, "Error subscribing to %s (%d): %s", topic, err, mosquitto_strerror(err));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void disconnectMqtt(struct mosquitto *mqtt)
|
||||||
|
{
|
||||||
|
// ignore errors
|
||||||
|
mosquitto_disconnect(mqtt);
|
||||||
|
mosquitto_destroy(mqtt);
|
||||||
|
mosquitto_lib_cleanup();
|
||||||
|
}
|
4
simulatesimulator/mqtt.h
Normal file
4
simulatesimulator/mqtt.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
struct mosquitto * connectMqtt(char *user, char *pass, void (*messageCallback)(struct mosquitto *, void *, const struct mosquitto_message *));
|
||||||
|
int sendMqtt(struct mosquitto *mqtt, char *topic, char *payload, int payloadLength);
|
||||||
|
int recvMqtt(struct mosquitto *mqtt, char *topic);
|
||||||
|
void disconnectMqtt(struct mosquitto *mqtt);
|
Reference in New Issue
Block a user