Compare commits
4 Commits
5cea555e77
...
5c1a32cda2
Author | SHA1 | Date | |
---|---|---|---|
5c1a32cda2 | |||
afffd831f4 | |||
73e4e7d8e2 | |||
6c1ad80e86 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
config.h
|
@ -1,4 +1,7 @@
|
||||
#include <Wire.h>
|
||||
#include <ArduinoMqttClient.h>
|
||||
#include <Ethernet.h>
|
||||
#include "config.h"
|
||||
|
||||
#define READ_BYTES_FROM_WIRE 1
|
||||
|
||||
@ -9,6 +12,17 @@
|
||||
#define ALTITUDE_TRIGGER_BIT 4
|
||||
#define ALTITUDE_HIGH_BIT 8
|
||||
|
||||
#define HEADING_SIM_UP "sim/autopilot/heading_up"
|
||||
#define HEADING_SIM_DOWN "sim/autopilot/heading_down"
|
||||
// XXX: double check altitude values
|
||||
#define ALTITUDE_SIM_UP "sim/autopilot/altitude_up"
|
||||
#define ALTITUDE_SIM_DOWN "sim/autopilot/altitude_down"
|
||||
#define AIRPSEED_SIM_UP "sim/autopilot/airspeed_up"
|
||||
#define AIRSPEED_SIM_DOWN "sim/autopilot/airspeed_down"
|
||||
|
||||
#define MQTT_SIM_COMMAND_TOPIC "/xplane/meta/cmnd"
|
||||
#define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#"
|
||||
|
||||
struct device {
|
||||
byte address;
|
||||
String name;
|
||||
@ -20,11 +34,72 @@ struct device devices[] = {
|
||||
|
||||
int numDevices = -1;
|
||||
|
||||
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;
|
||||
MqttClient mqttClient(client);
|
||||
|
||||
void onMqttMessage(int messageSize) {
|
||||
// // we received a message, print out the topic and contents
|
||||
// Serial.println("Received a message with topic '");
|
||||
// Serial.print(mqttClient.messageTopic());
|
||||
// Serial.print("', length ");
|
||||
// Serial.print(messageSize);
|
||||
// Serial.println(" bytes:");
|
||||
//
|
||||
// // use the Stream interface to print the contents
|
||||
// while (mqttClient.available()) {
|
||||
// Serial.print((char)mqttClient.read());
|
||||
// }
|
||||
// Serial.println();
|
||||
//
|
||||
// Serial.println();
|
||||
}
|
||||
|
||||
void sendMqttMessage(char *topic, char *message) {
|
||||
mqttClient.beginMessage(topic);
|
||||
mqttClient.print(message);
|
||||
mqttClient.endMessage();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// count devices for loop
|
||||
numDevices = sizeof(devices) / sizeof(device);
|
||||
|
||||
// start the Ethernet connection:
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// no point in carrying on, so do nothing forevermore:
|
||||
while (1);
|
||||
}
|
||||
// print your local IP address:
|
||||
Serial.println(Ethernet.localIP());
|
||||
|
||||
// MQTT auth
|
||||
mqttClient.setUsernamePassword(MQTT_USER, MQTT_PASS);
|
||||
|
||||
// MQTT connection
|
||||
if (!mqttClient.connect(broker, port)) {
|
||||
Serial.print("MQTT connection failed! Error code = ");
|
||||
Serial.println(mqttClient.connectError());
|
||||
|
||||
while (1);
|
||||
} else {
|
||||
Serial.println("MQTT connected");
|
||||
}
|
||||
|
||||
// subscribe to MQTT topic
|
||||
mqttClient.onMessage(onMqttMessage);
|
||||
mqttClient.subscribe(topic);
|
||||
|
||||
// start I2C
|
||||
Wire.begin();
|
||||
|
||||
// for debugging
|
||||
pinMode(LED_TX, OUTPUT);
|
||||
digitalWrite(LED_TX, HIGH);
|
||||
pinMode(LED_RX, OUTPUT);
|
||||
@ -42,15 +117,15 @@ void loop() {
|
||||
if (data != DATA_STOP_BYTE) {
|
||||
if (data & ALTITUDE_TRIGGER_BIT) {
|
||||
if (data & ALTITUDE_HIGH_BIT) {
|
||||
Serial.println("Altitude up");
|
||||
sendMqttMessage(MQTT_SIM_COMMAND_TOPIC, ALTITUDE_SIM_UP);
|
||||
} else {
|
||||
Serial.println("Altitude down");
|
||||
sendMqttMessage(MQTT_SIM_COMMAND_TOPIC, ALTITUDE_SIM_DOWN);
|
||||
}
|
||||
} else if (data & HEADING_TRIGGER_BIT) {
|
||||
if (data & HEADING_HIGH_BIT) {
|
||||
Serial.println("Heading right");
|
||||
sendMqttMessage(MQTT_SIM_COMMAND_TOPIC, HEADING_SIM_UP);
|
||||
} else {
|
||||
Serial.println("Heading left");
|
||||
sendMqttMessage(MQTT_SIM_COMMAND_TOPIC, HEADING_SIM_DOWN);
|
||||
}
|
||||
} else {
|
||||
// who are you?
|
||||
|
@ -1,10 +1,10 @@
|
||||
#include <Wire.h>
|
||||
|
||||
#define PIN_HEADING_WHITE PD3
|
||||
#define PIN_HEADING_RED PD6
|
||||
#define PIN_ROTATOR1_WHITE 2
|
||||
#define PIN_ROTATOR1_GREEN 4
|
||||
|
||||
#define PIN_ALTITUDE_WHITE PD2
|
||||
#define PIN_ALTITUDE_RED PD5
|
||||
#define PIN_ROTATOR2_WHITE 3
|
||||
#define PIN_ROTATOR2_GREEN 5
|
||||
|
||||
#define VALUE_BUFFER 30
|
||||
|
||||
@ -12,10 +12,13 @@
|
||||
|
||||
// needs global define
|
||||
#define DATA_STOP_BYTE 0x00
|
||||
#define HEADING_TRIGGER_BIT 1
|
||||
#define HEADING_HIGH_BIT 2
|
||||
#define ALTITUDE_TRIGGER_BIT 4
|
||||
#define ALTITUDE_HIGH_BIT 8
|
||||
#define ROTATOR1_TRIGGER_BIT 1
|
||||
#define ROTATOR1_HIGH_BIT 2
|
||||
#define ROTATOR2_TRIGGER_BIT 4
|
||||
#define ROTATOR2_HIGH_BIT 8
|
||||
#define PIN_ADDRESS_1 11
|
||||
#define PIN_ADDRESS_2 12
|
||||
#define PIN_ADDRESS_3 13
|
||||
|
||||
int lastClk = HIGH;
|
||||
|
||||
@ -46,12 +49,12 @@ void falling(uint8_t pin, byte triggerBit, byte highBit) {
|
||||
}
|
||||
}
|
||||
|
||||
void headingFalling() {
|
||||
falling(PIN_HEADING_RED, HEADING_TRIGGER_BIT, HEADING_HIGH_BIT);
|
||||
void rotator1Falling() {
|
||||
falling(PIN_ROTATOR1_GREEN, ROTATOR1_TRIGGER_BIT, ROTATOR1_HIGH_BIT);
|
||||
}
|
||||
|
||||
void altitudeFalling() {
|
||||
falling(PIN_ALTITUDE_RED, ALTITUDE_TRIGGER_BIT, ALTITUDE_HIGH_BIT);
|
||||
void rotator2Falling() {
|
||||
falling(PIN_ROTATOR2_GREEN, ROTATOR2_TRIGGER_BIT, ROTATOR2_HIGH_BIT);
|
||||
}
|
||||
|
||||
int eventCount = 0;
|
||||
@ -92,18 +95,30 @@ void i2cRequest() {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t address = 0;
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(PIN_HEADING_WHITE, INPUT_PULLUP);
|
||||
pinMode(PIN_HEADING_RED, INPUT_PULLUP);
|
||||
pinMode(PIN_ALTITUDE_WHITE, INPUT_PULLUP);
|
||||
pinMode(PIN_ALTITUDE_RED, INPUT_PULLUP);
|
||||
// setup rotator GPIOs
|
||||
pinMode(PIN_ROTATOR1_WHITE, INPUT_PULLUP);
|
||||
pinMode(PIN_ROTATOR1_GREEN, INPUT_PULLUP);
|
||||
pinMode(PIN_ROTATOR2_WHITE, INPUT_PULLUP);
|
||||
pinMode(PIN_ROTATOR2_GREEN, INPUT_PULLUP);
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_ALTITUDE_WHITE), altitudeFalling, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_HEADING_WHITE), headingFalling, FALLING);
|
||||
// setup address selector GPIOs
|
||||
pinMode(PIN_ADDRESS_1, INPUT_PULLUP);
|
||||
pinMode(PIN_ADDRESS_2, INPUT_PULLUP);
|
||||
pinMode(PIN_ADDRESS_3, INPUT_PULLUP);
|
||||
|
||||
Wire.begin(0x01);
|
||||
// calculate address by LOW GPIOs
|
||||
address = digitalRead(PIN_ADDRESS_1) == LOW;
|
||||
address |= (digitalRead(PIN_ADDRESS_2) == LOW) << 1;
|
||||
address |= (digitalRead(PIN_ADDRESS_3) == LOW) << 2;
|
||||
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_ROTATOR1_WHITE), rotator1Falling, FALLING);
|
||||
attachInterrupt(digitalPinToInterrupt(PIN_ROTATOR2_WHITE), rotator2Falling, FALLING);
|
||||
|
||||
Wire.begin(address);
|
||||
Wire.onRequest(i2cRequest);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user