Skip to content

Commit 6c92c16

Browse files
committed
Added Bluetooth serial support for ESP32
1 parent bf2f103 commit 6c92c16

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Software/Arduino code/OpenAstroTracker/Configuration_adv.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@
216216
#define ESPBOARD
217217
#undef HEADLESS_CLIENT
218218
#define HEADLESS_CLIENT 1
219+
// #define HEADLESS_BLUETOOTH 1
219220
#define WIFI_ENABLED
220221
#if defined(ESP8266)
221222
#undef RUN_STEPPERS_IN_MAIN_LOOP

Software/Arduino code/OpenAstroTracker/src/b_setup.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ LcdButtons lcdButtons(0);
1414

1515
#ifdef ESP32
1616
DRAM_ATTR Mount mount(RAStepsPerDegree, DECStepsPerDegree, &lcdMenu);
17+
#ifdef HEADLESS_BLUETOOTH
18+
#include "BluetoothSerial.h"
19+
BluetoothSerial SerialBT;
20+
#endif
1721
#else
1822
Mount mount(RA_STEPS_PER_DEGREE, DEC_STEPS_PER_DEGREE, &lcdMenu);
1923
#endif
@@ -134,6 +138,9 @@ void setup() {
134138
// end microstepping -------------------
135139

136140
Serial.begin(57600);
141+
#ifdef HEADLESS_BLUETOOTH
142+
SerialBT.begin("OpenAstroTracker");
143+
#endif
137144
//BT.begin(9600);
138145

139146
LOGV2(DEBUG_ANY, F("Hello, universe, this is OAT %s!"), VERSION);

Software/Arduino code/OpenAstroTracker/src/f_serial.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#include "MeadeCommandProcessor.hpp"
77

88
void processSerialData();
9+
#ifdef HEADLESS_BLUETOOTH
10+
bool bt_connected = false;
11+
void processSerialBTData();
12+
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
13+
bool setupCallback = false;
14+
#endif
915

1016
////////////////////////////////////////////////
1117
// The main loop when under serial control
@@ -16,11 +22,22 @@ void serialLoop()
1622

1723
#ifdef ESPBOARD
1824
processSerialData();
25+
26+
#ifdef HEADLESS_BLUETOOTH
27+
if (!setupCallback) {
28+
SerialBT.register_callback(bt_callback);
29+
setupCallback = true;
30+
}
31+
if (bt_connected) {
32+
processSerialBTData();
33+
}
34+
#endif
1935
#endif
2036

2137
#ifdef WIFI_ENABLED
2238
wifiControl.loop();
2339
#endif
40+
2441
}
2542

2643
//////////////////////////////////////////////////
@@ -55,4 +72,56 @@ void processSerialData() {
5572
}
5673
}
5774

75+
#ifdef HEADLESS_BLUETOOTH
76+
void processSerialBTData() {
77+
char buffer[2];
78+
while (SerialBT.available() > 0) {
79+
if (SerialBT.readBytes(buffer, 1) == 1) {
80+
if (buffer[0] == 0x06) {
81+
LOGV1(DEBUG_SERIAL, F("SerialBT: Received: ACK request, replying"));
82+
SerialBT.print('1');
83+
} else {
84+
String inCmd = String(buffer[0]) + SerialBT.readStringUntil('#');
85+
LOGV2(DEBUG_SERIAL, F("SerialBT: Received: %s"), inCmd.c_str());
86+
if (inCmd == "Ka") {
87+
SerialBT.print('1');
88+
continue;
89+
}
90+
if (inCmd == "K*") {
91+
SerialBT.print('1');
92+
continue;
93+
}
94+
if (inCmd.charAt(1) == 0x06) {
95+
SerialBT.print('P');
96+
continue;
97+
}
98+
99+
100+
String retVal = MeadeCommandProcessor::instance()->processCommand(inCmd);
101+
if (retVal != "") {
102+
LOGV2(DEBUG_SERIAL, F("SerialBT: Replied: %s"), retVal);
103+
SerialBT.print(retVal);
104+
} else {
105+
LOGV2(DEBUG_SERIAL, F("Didnt reply %s"), retVal);
106+
}
107+
}
108+
} else {
109+
LOGV1(DEBUG_SERIAL, F("SerialBT: Read nothing"));
110+
}
111+
112+
mount.loop();
113+
}
114+
}
115+
void bt_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
116+
switch (event) {
117+
case ESP_SPP_SRV_OPEN_EVT:
118+
bt_connected = true;
119+
break;
120+
case ESP_SPP_CLOSE_EVT:
121+
bt_connected = false;
122+
break;
123+
}
124+
}
125+
#endif
126+
58127
#endif

0 commit comments

Comments
 (0)