@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
// needs global define
|
// needs global define
|
||||||
#define DATA_STOP_BYTE 0x00
|
#define DATA_STOP_BYTE 0x00
|
||||||
|
#define DATA_RESET_BYTE 0xFF
|
||||||
#define HEADING_TRIGGER_BIT 1
|
#define HEADING_TRIGGER_BIT 1
|
||||||
#define HEADING_HIGH_BIT 2
|
#define HEADING_HIGH_BIT 2
|
||||||
#define ALTITUDE_TRIGGER_BIT 4
|
#define ALTITUDE_TRIGGER_BIT 4
|
||||||
@ -14,15 +15,17 @@
|
|||||||
|
|
||||||
#define HEADING_SIM_UP "sim/autopilot/heading_up"
|
#define HEADING_SIM_UP "sim/autopilot/heading_up"
|
||||||
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
|
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
|
||||||
// XXX: double check altitude values
|
|
||||||
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
|
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
|
||||||
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
|
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
|
||||||
#define AIRPSEED_SIM_UP "sim/autopilot/airspeed_up"
|
#define AIRPSEED_SIM_UP "sim/autopilot/airspeed_up"
|
||||||
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
||||||
|
|
||||||
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
|
#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_SIM_VALUE_TOPIC "/xplane/rref/#"
|
||||||
|
|
||||||
|
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
||||||
|
|
||||||
struct device {
|
struct device {
|
||||||
byte address;
|
byte address;
|
||||||
String name;
|
String name;
|
||||||
@ -41,7 +44,9 @@ int port = 1883;
|
|||||||
const char topic[] = MQTT_SIM_VALUE_TOPIC;
|
const char topic[] = MQTT_SIM_VALUE_TOPIC;
|
||||||
|
|
||||||
EthernetClient client;
|
EthernetClient client;
|
||||||
|
char localIP[16];
|
||||||
MqttClient mqttClient(client);
|
MqttClient mqttClient(client);
|
||||||
|
unsigned long mqttLastKeepAlive = millis();
|
||||||
|
|
||||||
void onMqttMessage(int messageSize) {
|
void onMqttMessage(int messageSize) {
|
||||||
// // we received a message, print out the topic and contents
|
// // we received a message, print out the topic and contents
|
||||||
@ -77,7 +82,8 @@ void setup() {
|
|||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
// print your local IP address:
|
// 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
|
// MQTT auth
|
||||||
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
||||||
@ -95,6 +101,7 @@ void setup() {
|
|||||||
// subscribe to MQTT topic
|
// subscribe to MQTT topic
|
||||||
mqttClient.onMessage(onMqttMessage);
|
mqttClient.onMessage(onMqttMessage);
|
||||||
mqttClient.subscribe(topic);
|
mqttClient.subscribe(topic);
|
||||||
|
mqttClient.setKeepAliveInterval(10 * 1000L);
|
||||||
|
|
||||||
// start I2C
|
// start I2C
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
@ -104,6 +111,13 @@ void setup() {
|
|||||||
digitalWrite(LED_TX, HIGH);
|
digitalWrite(LED_TX, HIGH);
|
||||||
pinMode(LED_RX, OUTPUT);
|
pinMode(LED_RX, OUTPUT);
|
||||||
digitalWrite(LED_RX, HIGH);
|
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() {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
// needs global define
|
// needs global define
|
||||||
#define DATA_STOP_BYTE 0x00
|
#define DATA_STOP_BYTE 0x00
|
||||||
|
#define DATA_RESET_BYTE 0xFF
|
||||||
#define ROTATOR1_TRIGGER_BIT 1
|
#define ROTATOR1_TRIGGER_BIT 1
|
||||||
#define ROTATOR1_HIGH_BIT 2
|
#define ROTATOR1_HIGH_BIT 2
|
||||||
#define ROTATOR2_TRIGGER_BIT 4
|
#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;
|
uint8_t address = 0;
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
@ -120,6 +132,7 @@ void setup() {
|
|||||||
|
|
||||||
Wire.begin(address);
|
Wire.begin(address);
|
||||||
Wire.onRequest(i2cRequest);
|
Wire.onRequest(i2cRequest);
|
||||||
|
Wire.onReceive(i2cReceive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
Reference in New Issue
Block a user