Compare commits
2 Commits
226f2a3e3f
...
08990a7a93
Author | SHA1 | Date | |
---|---|---|---|
08990a7a93 | |||
061a084f1d |
@ -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);
|
||||||
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
|
||||||
@ -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,10 +168,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// transmit MQTT keepalive message if MQTT_KEEPALIVE_INTERVAL_MS is reached
|
// call poll() regularly to allow the library to receive MQTT messages and
|
||||||
// or millis() is wrapping to 0
|
// send MQTT keep alives which avoids being disconnected by the broker
|
||||||
if (mqttLastKeepAlive + MQTT_KEEPALIVE_INTERVAL_MS < millis() || mqttLastKeepAlive > millis()) {
|
mqttClient.poll();
|
||||||
sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, localIP);
|
|
||||||
mqttLastKeepAlive = millis();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user