diff --git a/display/.gitignore b/display/.gitignore new file mode 100644 index 0000000..0e56cf2 --- /dev/null +++ b/display/.gitignore @@ -0,0 +1 @@ +config.h diff --git a/display/display.ino b/display/display.ino new file mode 100644 index 0000000..cc38093 --- /dev/null +++ b/display/display.ino @@ -0,0 +1,125 @@ +#include +#include +#include +#include "config.h" + +WiFiClient client; +MqttClient mqttClient(client); + +//U8G2_SSD1306_128X64_NONAME_F_HW_I2C headingDisplay(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // High speed I2C +//U8G2_SSD1306_128X64_NONAME_F_HW_I2C speedDisplay(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); + +U8G2_SSD1306_128X64_NONAME_F_HW_I2C headingDisplay(U8G2_R0); // High speed I2C +U8G2_SSD1306_128X64_NONAME_F_HW_I2C speedDisplay(U8G2_R0); + + +const char broker[] = "mqtt.sugarland.lan"; +int port = 1883; +const char topic[] = "/xplane/rref/#"; + +void onMqttMessage(int messageSize) { + if (mqttClient.messageTopic() == "/xplane/rref/1") { + char buf[3] = ""; + char c = '\0'; + for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) { + c = (char)mqttClient.read(); + if (c != '.') { + buf[i] = c; + } + } + + if (buf[1] == '\0') { + buf[1] = buf[0]; + buf[0] = '0'; + } + + if (buf[2] == '\0') { + buf[2] = buf[1]; + buf[1] = buf[0]; + buf[0] = '0'; + } + + headingDisplay.clearBuffer(); + headingDisplay.setFont(u8g2_font_fub11_tf); + headingDisplay.drawStr(0, 11, "HDG"); + headingDisplay.setFont(u8g2_font_fub42_tn); + headingDisplay.drawStr(10, 63, buf); + headingDisplay.sendBuffer(); + } else if (mqttClient.messageTopic() == "/xplane/rref/2") { + char buf[3] = ""; + char c = '\0'; + for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) { + c = (char)mqttClient.read(); + if (c != '.') { + buf[i] = c; + } + } + + if (buf[1] == '\0') { + buf[1] = buf[0]; + buf[0] = '0'; + } + + if (buf[2] == '\0') { + buf[2] = buf[1]; + buf[1] = buf[0]; + buf[0] = '0'; + } + + speedDisplay.clearBuffer(); + speedDisplay.setFont(u8g2_font_fub11_tf); + speedDisplay.drawStr(0, 11, "SPD"); + speedDisplay.setFont(u8g2_font_fub42_tn); + speedDisplay.drawStr(10, 63, buf); + speedDisplay.sendBuffer(); + + } +} + +void setup() { + // put your setup code here, to run once: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + WiFi.begin(WLAN_SSID, WLAN_PASS); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println(WiFi.localIP()); + + mqttClient.setUsernamePassword(MQTT_USER3, MQTT_PASS); + if (!mqttClient.connect(broker, port)) { + Serial.print("MQTT connection failed! Error code = "); + Serial.println(mqttClient.connectError()); + + while (1); + } else { + Serial.println("MQTT connected"); + } + + headingDisplay.begin(); + speedDisplay.setI2CAddress(0x7A); + speedDisplay.begin(); + + mqttClient.onMessage(onMqttMessage); + mqttClient.subscribe(topic); + + mqttClient.beginMessage("/xplane/meta/rref"); + mqttClient.print("001 200 sim/cockpit/autopilot/heading_mag"); + mqttClient.endMessage(); + mqttClient.beginMessage("/xplane/meta/rref"); + mqttClient.print("002 200 sim/cockpit/autopilot/airspeed"); + mqttClient.endMessage(); +} + +void loop() { + // call poll() regularly to allow the library to receive MQTT messages and + // send MQTT keep alives which avoids being disconnected by the broker + mqttClient.poll(); +} diff --git a/test/test.ino b/test/test.ino index 05502b1..2305039 100644 --- a/test/test.ino +++ b/test/test.ino @@ -1,5 +1,3 @@ -#include - #include #include #include @@ -30,7 +28,6 @@ MqttClient mqttClient(client); byte lastRotInput_1A; MCP23017 mcp1 = MCP23017(ROT_ADDR_1, 100); -U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // High speed I2C void setup() { // put your setup code here, to run once: @@ -49,7 +46,7 @@ void setup() { // print your local IP address: Serial.println(Ethernet.localIP()); - mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS); + mqttClient.setUsernamePassword(MQTT_USER2, MQTT_PASS); if (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); @@ -75,15 +72,6 @@ void setup() { mcp1.setPortPullUp(0b00000000, ROT_PORT_A); lastRotInput_1A = mcp1.getPort(ROT_PORT_A); - - u8g2.begin(); - - mqttClient.beginMessage("/xplane/meta/rref"); - mqttClient.print("001 010 sim/cockpit/autopilot/heading_mag"); - mqttClient.endMessage(); - mqttClient.beginMessage("/xplane/meta/rref"); - mqttClient.print("002 010 sim/cockpit/autopilot/airspeed"); - mqttClient.endMessage(); } bool isBitDown(byte input, byte reference) { @@ -149,37 +137,6 @@ void loop() { } void onMqttMessage(int messageSize) { - if (mqttClient.messageTopic() == "/xplane/rref/1") { - char buf[3] = ""; - char c = '\0'; - for (int i = 0; i < 3 && mqttClient.available() && c != '.'; i++) { - c = (char)mqttClient.read(); - if (c != '.') { - buf[i] = c; - } - } - - if (buf[1] == '\0') { - buf[1] = buf[0]; - buf[0] = '0'; - } - - if (buf[2] == '\0') { - buf[2] = buf[1]; - buf[1] = buf[0]; - buf[0] = '0'; - } - - u8g2.clearBuffer(); - u8g2.setFont(u8g2_font_fub11_tf); - u8g2.drawStr(0, 11, "HDG"); - u8g2.setFont(u8g2_font_fub42_tn); - u8g2.drawStr(10, 63, buf); - u8g2.sendBuffer(); - } - - // TODO: speed display - // // we received a message, print out the topic and contents // Serial.println("Received a message with topic '"); // Serial.print(mqttClient.messageTopic());