@ -7,6 +7,7 @@
|
||||
|
||||
// needs global define
|
||||
#define DATA_STOP_BYTE 0x00
|
||||
#define DATA_RESET_BYTE 0xFF
|
||||
#define HEADING_TRIGGER_BIT 1
|
||||
#define HEADING_HIGH_BIT 2
|
||||
#define ALTITUDE_TRIGGER_BIT 4
|
||||
@ -14,15 +15,17 @@
|
||||
|
||||
#define HEADING_SIM_UP "sim/autopilot/heading_up"
|
||||
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
|
||||
// XXX: double check altitude values
|
||||
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
|
||||
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
|
||||
#define AIRPSEED_SIM_UP "sim/autopilot/airspeed_up"
|
||||
#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_VALUE_TOPIC "/xplane/rref/#"
|
||||
|
||||
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
||||
|
||||
struct device {
|
||||
byte address;
|
||||
String name;
|
||||
@ -41,7 +44,9 @@ 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
|
||||
@ -77,7 +82,8 @@ void setup() {
|
||||
while (1);
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.println(Ethernet.localIP());
|
||||
sprintf(localIP, "%d.%d.%d.%d", Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
|
||||
Serial.println(localIP);
|
||||
|
||||
// MQTT auth
|
||||
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
||||
@ -95,6 +101,7 @@ void setup() {
|
||||
// subscribe to MQTT topic
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.subscribe(topic);
|
||||
mqttClient.setKeepAliveInterval(10 * 1000L);
|
||||
|
||||
// start I2C
|
||||
Wire.begin();
|
||||
@ -104,6 +111,13 @@ void setup() {
|
||||
digitalWrite(LED_TX, HIGH);
|
||||
pinMode(LED_RX, OUTPUT);
|
||||
digitalWrite(LED_RX, HIGH);
|
||||
|
||||
// initialize devices
|
||||
for (int i = 0; i < numDevices; i++) {
|
||||
Wire.beginTransmission(devices[i].address);
|
||||
Wire.write(DATA_RESET_BYTE);
|
||||
Wire.endTransmission(devices[i].address);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@ -134,4 +148,11 @@ 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, localIP);
|
||||
mqttLastKeepAlive = millis();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user