Compare commits
26 Commits
226f2a3e3f
...
main
Author | SHA1 | Date | |
---|---|---|---|
8cbd64034b | |||
99c2f4f0d4 | |||
095fca6b3d | |||
69d537bddd | |||
11146fb632 | |||
52e2c96e0e | |||
17a5b457d6 | |||
73c59c732a | |||
2543ac1336 | |||
6af2ab8b1c | |||
f34380bd01 | |||
b8c7b84746 | |||
c8984d8765 | |||
4e33b053f4 | |||
ac6f851b68 | |||
d62548d3e1 | |||
199168550d | |||
17da15d69f | |||
997bb3c5cf | |||
6e1f06f8e2 | |||
95ea99d5e4 | |||
107297f162 | |||
d8fe68679a | |||
53bf588ba3 | |||
08990a7a93 | |||
061a084f1d |
1
controller/.gitignore
vendored
Normal file
1
controller/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
controller
|
22
controller/README.md
Normal file
22
controller/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Controller
|
||||
## links
|
||||
* https://www.kernel.org/doc/Documentation/i2c/dev-interface
|
||||
* https://forum.arduino.cc/t/solved-cannot-get-correct-i2c-data-from-atmega8-into-raspberry-pi/1368337
|
||||
|
||||
## PINs
|
||||
* interal = physical = usage
|
||||
* 5 = 29 = reset peripheral devices
|
||||
* 0 = 10 = I2C SDA
|
||||
* 1 = 9 = I2C SCL
|
||||
|
||||
## I2C
|
||||
* somehow the buildin I2C does not work anymore (maybe already damaged?)
|
||||
* add additional bus by using GPIOs:
|
||||
```
|
||||
$ fgrep i2c /boot/firmware/config.txt
|
||||
dtparam=i2c_arm=on
|
||||
dtoverlay=i2c-gpio,bus=7,i2c_gpio_sda=10,i2c_gpio_scl=9
|
||||
```
|
||||
|
||||
## commands
|
||||
* reset peripheral devices: `gpioset gpiochip0 5=0`
|
1
controller/build.sh
Executable file
1
controller/build.sh
Executable file
@ -0,0 +1 @@
|
||||
gcc -o controller ringbuffer.c controller.c -li2c
|
236
controller/controller.c
Normal file
236
controller/controller.c
Normal file
@ -0,0 +1,236 @@
|
||||
#define RINGBUFFER_THREAD_SAFE
|
||||
|
||||
#include <linux/i2c-dev.h>
|
||||
#include <i2c/smbus.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "global.h"
|
||||
#ifndef RINGBUFFER_H
|
||||
#include "ringbuffer.h"
|
||||
#endif
|
||||
|
||||
#define IIC_DEVICE "/dev/i2c-7"
|
||||
#define BUFFER_BLOCKS 128
|
||||
|
||||
#define HEADING_SIM_UP "sim/autopilot/heading_up"
|
||||
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
|
||||
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
|
||||
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
|
||||
#define AIRSPEED_SIM_UP "sim/autopilot/airspeed_up"
|
||||
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
||||
|
||||
#define ERROR_IIC_OPEN "open failed"
|
||||
|
||||
struct ringBuffer i2cDataRead;
|
||||
struct ringBuffer i2cDataWrite;
|
||||
|
||||
struct blockData {
|
||||
char data[DATA_SIZE];
|
||||
int triggerBit;
|
||||
};
|
||||
|
||||
struct device {
|
||||
uint8_t address; // I2C address of device
|
||||
char *name; // name of this 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 rrefSubscription {
|
||||
uint8_t address;
|
||||
uint8_t triggerBit;
|
||||
};
|
||||
|
||||
const struct device devices[] = {
|
||||
{
|
||||
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"
|
||||
}
|
||||
};
|
||||
const int numDevices = sizeof(devices) / sizeof(struct device);
|
||||
struct rrefSubscription *rrefSubscriptions;
|
||||
|
||||
void _log(char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
int setI2CAddress(int i2c, uint8_t address) {
|
||||
if (ioctl(i2c, I2C_SLAVE, address) < 0) {
|
||||
_log("setting i2c address 0x%02X failed\n", address);
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void sendI2CInit(int i2c, struct device d) {
|
||||
_log("Sending init to %s\n", d.name);
|
||||
|
||||
int bufLen = 1 + 1 + sizeof(d.name1) + 1 + sizeof(d.name2);
|
||||
char buf[bufLen];
|
||||
int i = 0;
|
||||
buf[i++] = DATA_RESET_BYTE;
|
||||
buf[i++] = DATA_INIT_BYTE_1;
|
||||
buf[i++] = d.name1[0];
|
||||
buf[i++] = d.name1[1];
|
||||
buf[i++] = d.name1[2];
|
||||
buf[i++] = DATA_INIT_BYTE_2;
|
||||
buf[i++] = d.name2[0];
|
||||
buf[i++] = d.name2[1];
|
||||
buf[i++] = d.name2[2];
|
||||
|
||||
setI2CAddress(i2c, d.address);
|
||||
i2c_smbus_write_block_data(i2c, 0, bufLen, buf);
|
||||
}
|
||||
|
||||
void sendI2CData(int i2c, struct device d, uint8_t triggerBit, char *data, uint8_t dataLen) {
|
||||
int bufLen = 1 + 1 + 1 + dataLen;
|
||||
char buf[bufLen];
|
||||
int i = 0;
|
||||
buf[i++] = DATA_BYTE;
|
||||
buf[i++] = triggerBit;
|
||||
buf[i++] = dataLen;
|
||||
for (int j = 0; j < dataLen; j++) {
|
||||
buf[i++] = data[j];
|
||||
}
|
||||
|
||||
setI2CAddress(i2c, d.address);
|
||||
i2c_smbus_write_block_data(i2c, 0, bufLen, buf);
|
||||
}
|
||||
|
||||
int tempCounter = 0;
|
||||
int tempGauge = 0;
|
||||
void* mqttHandler(void* arg) {
|
||||
struct blockData *block = malloc(sizeof(struct blockData));
|
||||
|
||||
while (1) {
|
||||
sprintf(block->data, "%03d", tempCounter);
|
||||
block->triggerBit = TRIGGER_BIT_1;
|
||||
ringBufferWrite(&i2cDataWrite, block);
|
||||
tempCounter++;
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
free(block);
|
||||
}
|
||||
|
||||
void* i2cHandler(void* arg) {
|
||||
int i2c, tmp, buzy;
|
||||
__s32 i2cResponse;
|
||||
struct blockData *block = malloc(sizeof(struct blockData));
|
||||
|
||||
i2c = open(IIC_DEVICE, O_RDWR);
|
||||
if (i2c < 0) {
|
||||
_log("Unable to open I2C device %s\n", IIC_DEVICE);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
buzy = 0;
|
||||
for (int i = 0; i < numDevices; i++) {
|
||||
setI2CAddress(i2c, devices[i].address);
|
||||
i2cResponse = i2c_smbus_read_byte_data(i2c, 0);
|
||||
if (i2cResponse < 0) {
|
||||
_log("I2C read from device %s failed: %s (%d)\n", devices[i].name, strerror(errno), errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i2cResponse == DATA_RESET_BYTE) {
|
||||
sendI2CInit(i2c, devices[i]);
|
||||
buzy = 1;
|
||||
} else if (i2cResponse == DATA_STOP_BYTE) {
|
||||
// expect no forther data from device
|
||||
// send data to device
|
||||
do {
|
||||
tmp = ringBufferRead(&i2cDataWrite, block);
|
||||
if (tmp == 0) {
|
||||
// we got something to send
|
||||
sendI2CData(i2c, devices[i], block->triggerBit, block->data, 3);
|
||||
buzy = 1;
|
||||
}
|
||||
} while (tmp == 0);
|
||||
continue;
|
||||
} else {
|
||||
// real data
|
||||
_log("data: 0x%02x\n", i2cResponse);
|
||||
if (i2cResponse & TRIGGER_BIT_2) {
|
||||
if (i2cResponse & HIGH_BIT_2) {
|
||||
tempGauge++;
|
||||
if (tempGauge > 999) {
|
||||
tempGauge = 999;
|
||||
}
|
||||
} else {
|
||||
tempGauge--;
|
||||
if (tempGauge < 0) {
|
||||
tempGauge = 0;
|
||||
}
|
||||
}
|
||||
sprintf(block->data, "%03d", tempGauge);
|
||||
block->triggerBit = TRIGGER_BIT_2;
|
||||
ringBufferWrite(&i2cDataWrite, block);
|
||||
}
|
||||
buzy = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (buzy == 0) {
|
||||
usleep(50 * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
close(i2c);
|
||||
free(block);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t i2cThread, mqttThread;
|
||||
|
||||
// create the ring buffers for i2c devices
|
||||
ringBufferCreate(BUFFER_BLOCKS, sizeof(struct blockData), &i2cDataRead);
|
||||
ringBufferCreate(BUFFER_BLOCKS, sizeof(struct blockData), &i2cDataWrite);
|
||||
|
||||
// alloc
|
||||
rrefSubscriptions = calloc(sizeof(struct device), numDevices);
|
||||
|
||||
// start and join threads
|
||||
pthread_create(&i2cThread, NULL, i2cHandler, NULL);
|
||||
pthread_create(&mqttThread, NULL, mqttHandler, NULL);
|
||||
pthread_join(i2cThread, NULL);
|
||||
pthread_join(mqttThread, NULL);
|
||||
|
||||
// free resources
|
||||
free(rrefSubscriptions);
|
||||
ringBufferDestroy(&i2cDataRead);
|
||||
ringBufferDestroy(&i2cDataWrite);
|
||||
|
||||
return 0;
|
||||
}
|
11
controller/global.h
Normal file
11
controller/global.h
Normal file
@ -0,0 +1,11 @@
|
||||
#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
|
||||
#define TRIGGER_BIT_2 4
|
||||
#define HIGH_BIT_2 8
|
||||
#define I2C_ADDRESS_START 0x08
|
||||
#define DATA_SIZE 8
|
75
controller/ringbuffer.c
Normal file
75
controller/ringbuffer.c
Normal file
@ -0,0 +1,75 @@
|
||||
#ifndef RINGBUFFER_H
|
||||
#include "ringbuffer.h"
|
||||
#endif
|
||||
|
||||
void _ringBufferIncReader(struct ringBuffer *buf) {
|
||||
buf->reader += buf->blockSize;
|
||||
if (buf->reader >= buf->buffer + buf->blocks * buf->blockSize) {
|
||||
buf->reader = buf->buffer;
|
||||
}
|
||||
}
|
||||
|
||||
void _ringBufferIncWriter(struct ringBuffer *buf) {
|
||||
buf->writer += buf->blockSize;
|
||||
if (buf->writer == buf->buffer + buf->blocks * buf->blockSize) {
|
||||
buf->writer = buf->buffer;
|
||||
}
|
||||
|
||||
if (buf->writer == buf->reader) {
|
||||
// we incremented the writer and now it is equal to reader
|
||||
// this means, the writer is overtaking the reader
|
||||
// so push the reader forward, even if we are
|
||||
// loosing data but this is how ring buffers work, eh?
|
||||
_ringBufferIncReader(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out) {
|
||||
out->buffer = malloc(blocks * blockSize);
|
||||
out->blocks = blocks;
|
||||
out->blockSize = blockSize;
|
||||
out->reader = out->buffer;
|
||||
out->writer = out->buffer;
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_init(&out->mutex, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
void ringBufferDestroy(struct ringBuffer *buf) {
|
||||
free(buf->buffer);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_destroy(&buf->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
int ringBufferRead(struct ringBuffer *buf, void *out) {
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_lock(&buf->mutex);
|
||||
#endif
|
||||
if (buf->reader != buf->writer) {
|
||||
// we have data to read
|
||||
memcpy(out, buf->reader, buf->blockSize);
|
||||
_ringBufferIncReader(buf);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
return 0;
|
||||
} else {
|
||||
// nothing to read
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ringBufferWrite(struct ringBuffer *buf, void *in) {
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_lock(&buf->mutex);
|
||||
#endif
|
||||
memcpy(buf->writer, in, buf->blockSize);
|
||||
_ringBufferIncWriter(buf);
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_unlock(&buf->mutex);
|
||||
#endif
|
||||
}
|
25
controller/ringbuffer.h
Normal file
25
controller/ringbuffer.h
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#define RINGBUFFER_H
|
||||
|
||||
struct ringBuffer {
|
||||
void *buffer;
|
||||
int blocks;
|
||||
size_t blockSize;
|
||||
void *reader;
|
||||
void *writer;
|
||||
#ifdef RINGBUFFER_THREAD_SAFE
|
||||
pthread_mutex_t mutex;
|
||||
#endif
|
||||
};
|
||||
|
||||
void ringBufferCreate(int blocks, size_t blockSize, struct ringBuffer *out);
|
||||
void ringBufferDestroy(struct ringBuffer *buf);
|
||||
int ringBufferRead(struct ringBuffer *buf, void *out);
|
||||
void ringBufferWrite(struct ringBuffer *buf, void *in);
|
@ -1,9 +0,0 @@
|
||||
#define DATA_STOP_BYTE 0x00
|
||||
#define DATA_INIT_BYTE_1 0x01
|
||||
#define DATA_INIT_BYTE_2 0x02
|
||||
#define DATA_RESET_BYTE 0xFF
|
||||
#define TRIGGER_BIT_1 1
|
||||
#define HIGH_BIT_1 2
|
||||
#define TRIGGER_BIT_2 4
|
||||
#define HIGH_BIT_2 8
|
||||
#define I2C_ADDRESS_START 0x08
|
1
master/global.h
Symbolic link
1
master/global.h
Symbolic link
@ -0,0 +1 @@
|
||||
../controller/global.h
|
@ -15,55 +15,78 @@
|
||||
#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/meta/rref"
|
||||
|
||||
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
||||
|
||||
#define SLAVE_RESET_PIN 7
|
||||
|
||||
struct 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 *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"
|
||||
}
|
||||
};
|
||||
const int numDevices = sizeof(devices) / sizeof(device);
|
||||
|
||||
struct rrefSubscription {
|
||||
byte address;
|
||||
byte triggerBit;
|
||||
};
|
||||
|
||||
int numDevices = -1;
|
||||
struct rrefSubscription rrefSubscriptions[numDevices * 2];
|
||||
|
||||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||
|
||||
const char broker[] = "openhab.sugarland.lan";
|
||||
int port = 1883;
|
||||
const char topic[] = MQTT_SIM_VALUE_TOPIC;
|
||||
|
||||
EthernetClient client;
|
||||
char localIP[16];
|
||||
|
||||
MqttClient mqttClient(client);
|
||||
unsigned long mqttLastKeepAlive = millis();
|
||||
|
||||
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();
|
||||
Serial.println("Received MQTT message");
|
||||
|
||||
// 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, ...) {
|
||||
@ -78,7 +101,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);
|
||||
@ -88,10 +111,25 @@ 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 * 2] = { 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 * 2) + 1] = { d->address, TRIGGER_BIT_2 };
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// setup slave reset pin and pull it down for a reset
|
||||
pinMode(SLAVE_RESET_PIN, OUTPUT);
|
||||
digitalWrite(SLAVE_RESET_PIN, LOW);
|
||||
|
||||
// start the Ethernet connection:
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
@ -99,6 +137,7 @@ void setup() {
|
||||
while (1);
|
||||
}
|
||||
// print your local IP address:
|
||||
char localIP[16];
|
||||
sprintf(localIP, "%d.%d.%d.%d", Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
|
||||
Serial.println(localIP);
|
||||
|
||||
@ -106,34 +145,27 @@ void setup() {
|
||||
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
||||
|
||||
// MQTT connection
|
||||
if (!mqttClient.connect(broker, port)) {
|
||||
if (!mqttClient.connect(MQTT_SERVER, MQTT_PORT)) {
|
||||
Serial.print("MQTT connection failed! Error code = ");
|
||||
Serial.println(mqttClient.connectError());
|
||||
|
||||
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
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.subscribe(topic);
|
||||
mqttClient.setKeepAliveInterval(10 * 1000L);
|
||||
mqttClient.subscribe(MQTT_SIM_VALUE_TOPIC);
|
||||
mqttClient.setKeepAliveInterval(MQTT_KEEPALIVE_INTERVAL_MS);
|
||||
|
||||
// switch on the slaves and give them a moment to boot
|
||||
digitalWrite(SLAVE_RESET_PIN, HIGH);
|
||||
delay(5000);
|
||||
|
||||
// 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]);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@ -149,8 +181,9 @@ 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]);
|
||||
subscribeRrefs(&devices[i], i);
|
||||
} else {
|
||||
// handle as payload
|
||||
if (data & TRIGGER_BIT_1) {
|
||||
@ -174,10 +207,7 @@ void loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// transmit MQTT keepalive message if MQTT_KEEPALIVE_INTERVAL_MS is reached
|
||||
// or millis() is wrapping to 0
|
||||
if (mqttLastKeepAlive + MQTT_KEEPALIVE_INTERVAL_MS < millis() || mqttLastKeepAlive > millis()) {
|
||||
sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, localIP);
|
||||
mqttLastKeepAlive = millis();
|
||||
}
|
||||
// call poll() regularly to allow the library to receive MQTT messages and
|
||||
// send MQTT keep alives which avoids being disconnected by the broker
|
||||
mqttClient.poll();
|
||||
}
|
||||
|
37
rotator/README.md
Normal file
37
rotator/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# rotator device
|
||||
* two rotators
|
||||
* two displays
|
||||
|
||||
## required libraries
|
||||
* https://github.com/adafruit/Adafruit_SSD1306/
|
||||
* https://github.com/adafruit/Adafruit-GFX-Library
|
||||
* https://github.com/adafruit/Adafruit_BusIO
|
||||
|
||||
## PINs
|
||||
* IDE = Physical = Usage
|
||||
* x = 1 = reset (connecto to master)
|
||||
* 2 = 4 = rotator 1 white
|
||||
* 3 = 5 = rotator 2 white
|
||||
* 4 = 6 = rotator 1 green
|
||||
* 5 = 11 = rotator 2 green
|
||||
* 6 = 12 = display 2 dc
|
||||
* 7 = 13 = display 2 reset
|
||||
* 8 = 14 = display 1 dc
|
||||
* 9 = 15 = display 1 reset
|
||||
* 10 = 16 = display 1 cs
|
||||
* 11 = 17 = SPI MOSI
|
||||
* 12 = 18 = (unused) SPI MISO
|
||||
* 13 = 19 = SPI SCK
|
||||
* 14 / A0 = 23 = address selector bit 1
|
||||
* 15 / A1 = 24 = address selector bit 2
|
||||
* 16 / A2 = 25 = address selector bit 3
|
||||
* 17 / A3 = 26 = display 2 cs
|
||||
* 18 / A4 = 27 = I2C SDA (connect to master)
|
||||
* 19 / A5 = 28 = I2C SCL (connect to master)
|
||||
|
||||
## address selection
|
||||
* no address selector pin set to low = 0x08
|
||||
* address selector bit 1 = low = 0x09
|
||||
* address selector bit 2 = low = 0x0A
|
||||
* address selector bit 1 and 2 = low = 0x0B
|
||||
...
|
@ -1 +1 @@
|
||||
../master/global.h
|
||||
../controller/global.h
|
@ -51,6 +51,11 @@ uint8_t lastClk = HIGH;
|
||||
byte valueBuffer[VALUE_BUFFER] = { 0 };
|
||||
uint8_t readerPos, writerPos = 0;
|
||||
|
||||
// ringbuffer of display data
|
||||
struct {
|
||||
|
||||
}
|
||||
|
||||
void addValue(uint8_t value) {
|
||||
valueBuffer[writerPos++] = value;
|
||||
|
||||
@ -160,6 +165,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 +202,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 +273,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);
|
||||
}
|
||||
|
2
simulatesimulator/.gitignore
vendored
Normal file
2
simulatesimulator/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
main
|
||||
stress
|
2
simulatesimulator/build.sh
Executable file
2
simulatesimulator/build.sh
Executable file
@ -0,0 +1,2 @@
|
||||
gcc -o main mqtt.c -lmosquitto main.c
|
||||
gcc -o stress mqtt.c -lmosquitto stress.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);
|
114
simulatesimulator/stress.c
Normal file
114
simulatesimulator/stress.c
Normal file
@ -0,0 +1,114 @@
|
||||
#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
|
||||
#define LOOPS 1000000
|
||||
|
||||
struct mosquitto *mqtt;
|
||||
|
||||
int airspeed = 100;
|
||||
int heading = 0;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
int loop = 0;
|
||||
int main() {
|
||||
char mqttPayload[MAX_MQTT_PAYLOAD_SIZE];
|
||||
|
||||
mqtt = connectMqtt(MQTT_USER1, MQTT_PASS, onMessage);
|
||||
if (mqtt == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
recvMqtt(mqtt, "/xplane/meta/cmnd");
|
||||
|
||||
while (1) {
|
||||
loop++;
|
||||
if (loop > LOOPS) {
|
||||
sprintf(mqttPayload, "%03d", heading);
|
||||
sendMqtt(mqtt, "/xplane/rref/1", mqttPayload, strlen(mqttPayload));
|
||||
sprintf(mqttPayload, "%03d", airspeed);
|
||||
sendMqtt(mqtt, "/xplane/rref/2", mqttPayload, strlen(mqttPayload));
|
||||
|
||||
heading++;
|
||||
if (heading >= 360) {
|
||||
heading = 0;
|
||||
}
|
||||
|
||||
airspeed++;
|
||||
if (airspeed >= MAX_AIRSPEED) {
|
||||
airspeed = 100;
|
||||
}
|
||||
loop = 0;
|
||||
}
|
||||
|
||||
mosquitto_loop(mqtt, 0, 1);
|
||||
}
|
||||
|
||||
disconnectMqtt(mqtt);
|
||||
}
|
Reference in New Issue
Block a user