Compare commits

...

3 Commits

Author SHA1 Message Date
107297f162 send rref data to salve and display 2025-05-17 19:57:15 +02:00
d8fe68679a simulation of simulator... yeah... 2025-05-17 19:56:49 +02:00
53bf588ba3 extra device topic not needed anymore 2025-05-17 15:35:21 +02:00
8 changed files with 327 additions and 74 deletions

View File

@ -1,6 +1,7 @@
#define DATA_STOP_BYTE 0x00
#define DATA_INIT_BYTE_1 0x01
#define DATA_INIT_BYTE_2 0x02
#define DATA_BYTE 0x03
#define DATA_RESET_BYTE 0xFF
#define TRIGGER_BIT_1 1
#define HIGH_BIT_1 2

View File

@ -15,9 +15,10 @@
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
#define MQTT_SIM_DEVICE_TOPIC "/xplane/meta/device"
#define MQTT_SIM_LOG_TOPIC "/xplane/meta/log"
#define MQTT_LOG_TOPIC "/xplane/meta/log"
#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
@ -27,15 +28,39 @@ struct 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 *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 *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
int rrefRefresh2; // refresh rate of subscribing rref
char *rref2; // rref to subscribe to
};
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 };
EthernetClient client;
@ -43,20 +68,21 @@ EthernetClient client;
MqttClient mqttClient(client);
void onMqttMessage(int messageSize) {
// // we received a message, print out the topic and contents
// Serial.println("Received a message with topic '");
// Serial.print(mqttClient.messageTopic());
// Serial.print("', length ");
// Serial.print(messageSize);
// Serial.println(" bytes:");
//
// // use the Stream interface to print the contents
// while (mqttClient.available()) {
// Serial.print((char)mqttClient.read());
// }
// Serial.println();
//
// Serial.println();
// assume, that we always receive topic beginning with MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX
// otherwise, this will _HORRIBLY_ fail
char *rrefNoStr = &mqttClient.messageTopic()[13];
int rrefNo = atoi(rrefNoStr);
struct rrefSubscription rref = rrefSubscriptions[rrefNo - 1];
Wire.beginTransmission(rref.address);
Wire.write(DATA_BYTE);
Wire.write(rref.triggerBit);
Wire.write(messageSize);
while (mqttClient.available()) {
Wire.write((char)mqttClient.read());
}
Wire.endTransmission(rref.address);
}
void sendMqttMessage(char *topic, int n, ...) {
@ -71,7 +97,7 @@ void sendMqttMessage(char *topic, int n, ...) {
}
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.write(DATA_RESET_BYTE);
Wire.write(DATA_INIT_BYTE_1);
@ -81,10 +107,28 @@ void sendI2CInit(struct device d) {
Wire.endTransmission(d.address);
}
void setup() {
// count devices for loop
numDevices = sizeof(devices) / sizeof(device);
void subscribeRrefs(struct device *d, int deviceNo) {
char subscribeMsg[256];
// 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:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
@ -107,7 +151,7 @@ void setup() {
while (1);
} else {
Serial.println("MQTT connected");
sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, "Master online");
sendMqttMessage(MQTT_LOG_TOPIC, 2, "Master online ", localIP);
}
// subscribe to MQTT topic
@ -118,16 +162,7 @@ void setup() {
// start I2C
Wire.begin();
// for debugging
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]);
}
initializeDevices();
}
void loop() {
@ -143,7 +178,7 @@ void loop() {
continue;
} else if (data == DATA_RESET_BYTE) {
// 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]);
} else {
// handle as payload

View File

@ -20,7 +20,7 @@
#define VALUE_BUFFER 30
#define SKIP_ROTARY_INPUTS 50
#define SKIP_ROTARY_INPUTS 40
#define INTERNAL_STATE_UNKNOWN 0
#define INTERNAL_STATE_INITIALIZED 1
@ -160,6 +160,26 @@ void i2cReceive(int bytes) {
for (int i = 0; i < 3 && Wire.available(); i++) {
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 {
// not yet implemented?
Serial.println("Received unknown");
@ -177,6 +197,35 @@ void displayTestScreen(Adafruit_SSD1306 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;
void setup() {
// https://support.arduino.cc/hc/en-us/articles/4839084114460-If-your-board-runs-the-sketch-twice
@ -219,46 +268,11 @@ void setup() {
displayTestScreen(display2);
// make sure, test screen is at least displayed 2 seconds
delay(2000);
updateDisplay1("---", 3);
updateDisplay2("---", 3);
}
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);
}

1
simulatesimulator/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
main

1
simulatesimulator/build.sh Executable file
View File

@ -0,0 +1 @@
gcc -o main mqtt.c -lmosquitto main.c

104
simulatesimulator/main.c Normal file
View 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
View 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
View 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);