keepalive is called by mqtt poll
manual keepliave not needed anymore
This commit is contained in:
@ -19,7 +19,7 @@
|
|||||||
#define MQTT_SIM_LOG_TOPIC "/xplane/meta/log"
|
#define MQTT_SIM_LOG_TOPIC "/xplane/meta/log"
|
||||||
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
|
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
|
||||||
|
|
||||||
#define MQTT_KEEPALIVE_INTERVAL_LOOPS 60000
|
#define MQTT_KEEPALIVE_INTERVAL_MS 15000
|
||||||
|
|
||||||
struct device {
|
struct device {
|
||||||
byte address; // I2C address of device
|
byte address; // I2C address of device
|
||||||
@ -35,19 +35,12 @@ struct device {
|
|||||||
struct device devices[] = {
|
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, {'S', 'P', 'D'}, AIRSPEED_SIM_UP, AIRSPEED_SIM_DOWN }
|
||||||
};
|
};
|
||||||
|
int numDevices = -1; // gets calculated in setup()
|
||||||
int numDevices = -1;
|
|
||||||
|
|
||||||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
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;
|
EthernetClient client;
|
||||||
char localIP[16];
|
|
||||||
MqttClient mqttClient(client);
|
MqttClient mqttClient(client);
|
||||||
int mqttLastKeepAlive = 0;
|
|
||||||
|
|
||||||
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
|
||||||
@ -99,6 +92,7 @@ void setup() {
|
|||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
// print your local IP address:
|
// 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]);
|
sprintf(localIP, "%d.%d.%d.%d", Ethernet.localIP()[0], Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
|
||||||
Serial.println(localIP);
|
Serial.println(localIP);
|
||||||
|
|
||||||
@ -106,7 +100,7 @@ void setup() {
|
|||||||
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
||||||
|
|
||||||
// MQTT connection
|
// MQTT connection
|
||||||
if (!mqttClient.connect(broker, port)) {
|
if (!mqttClient.connect(MQTT_SERVER, MQTT_PORT)) {
|
||||||
Serial.print("MQTT connection failed! Error code = ");
|
Serial.print("MQTT connection failed! Error code = ");
|
||||||
Serial.println(mqttClient.connectError());
|
Serial.println(mqttClient.connectError());
|
||||||
|
|
||||||
@ -118,8 +112,8 @@ void setup() {
|
|||||||
|
|
||||||
// subscribe to MQTT topic
|
// subscribe to MQTT topic
|
||||||
mqttClient.onMessage(onMqttMessage);
|
mqttClient.onMessage(onMqttMessage);
|
||||||
mqttClient.subscribe(topic);
|
mqttClient.subscribe(MQTT_SIM_VALUE_TOPIC);
|
||||||
mqttClient.setKeepAliveInterval(10 * 1000L);
|
mqttClient.setKeepAliveInterval(MQTT_KEEPALIVE_INTERVAL_MS);
|
||||||
|
|
||||||
// start I2C
|
// start I2C
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
@ -174,11 +168,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// transmit MQTT keepalive message every MQTT_KEEPALIVE_INTERVAL_LOOPS times
|
// call poll() regularly to allow the library to receive MQTT messages and
|
||||||
// running the loop
|
// send MQTT keep alives which avoids being disconnected by the broker
|
||||||
mqttLastKeepAlive++;
|
mqttClient.poll();
|
||||||
if (mqttLastKeepAlive >= MQTT_KEEPALIVE_INTERVAL_LOOPS) {
|
|
||||||
sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, localIP);
|
|
||||||
mqttLastKeepAlive = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user