From 8995401782877b4cdb19aaeccb6f5743b1c78db6 Mon Sep 17 00:00:00 2001 From: damage Date: Sun, 11 May 2025 20:29:33 +0200 Subject: [PATCH] MQTT keepalive and reset devices on master boot close #1 --- master/master.ino | 25 +++++++++++++++++++++++-- rotator/rotator.ino | 13 +++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/master/master.ino b/master/master.ino index 24d992c..c2268dd 100644 --- a/master/master.ino +++ b/master/master.ino @@ -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(); + } } diff --git a/rotator/rotator.ino b/rotator/rotator.ino index 4e43ebf..ffcbe35 100644 --- a/rotator/rotator.ino +++ b/rotator/rotator.ino @@ -12,6 +12,7 @@ // needs global define #define DATA_STOP_BYTE 0x00 +#define DATA_RESET_BYTE 0xFF #define ROTATOR1_TRIGGER_BIT 1 #define ROTATOR1_HIGH_BIT 2 #define ROTATOR2_TRIGGER_BIT 4 @@ -95,6 +96,17 @@ void i2cRequest() { } } +void i2cReceive(int bytes) { + for (int i = 0; i < bytes && Wire.available(); i++) { + byte data = Wire.read(); + if (data == DATA_RESET_BYTE) { + writerPos = readerPos = 0; + } else { + // nothing else yet implemented + } + } +} + uint8_t address = 0; void setup() { Serial.begin(115200); @@ -120,6 +132,7 @@ void setup() { Wire.begin(address); Wire.onRequest(i2cRequest); + Wire.onReceive(i2cReceive); } void loop() {