master resets slaves

This commit is contained in:
2025-05-18 15:10:17 +02:00
parent 17da15d69f
commit 199168550d
3 changed files with 131 additions and 13 deletions

View File

@ -18,10 +18,12 @@
#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_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
@ -68,6 +70,8 @@ EthernetClient client;
MqttClient mqttClient(client);
void onMqttMessage(int messageSize) {
Serial.println("Received MQTT message");
// assume, that we always receive topic beginning with MQTT_SIM_VALUE_SUBSCRIBE_TOPIC_PREFIX
// otherwise, this will _HORRIBLY_ fail
@ -113,22 +117,19 @@ void subscribeRrefs(struct device *d, int deviceNo) {
// 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 };
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 + 1] = { d->address, TRIGGER_BIT_2 };
}
void initializeDevices() {
for (int i = 0; i < numDevices; i++) {
sendI2CInit(devices[i]);
subscribeRrefs(&devices[i], i);
}
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");
@ -159,10 +160,12 @@ void setup() {
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();
initializeDevices();
}
void loop() {
@ -180,6 +183,7 @@ void loop() {
// device needs initialization
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) {