Skip to content

Commit 64e3e95

Browse files
committed
Wifi support for AP mode as well as fail over, Adding stop commands to Meade
1 parent 095cb59 commit 64e3e95

File tree

3 files changed

+92
-29
lines changed

3 files changed

+92
-29
lines changed

Software/Arduino code/OpenAstroTracker/MeadeCommandProcessor.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@
156156
// This stops all motors, including tracking. Note that deceleration curves are still followed.
157157
// Returns: 1 when all motors have stopped.
158158
//
159+
// :Qd#
160+
// Stop slew in specified direction where d is n, s, e, w
161+
// Returns: nothing
162+
//
159163
// -- QUIT MOVEMENT Extensions --
160164
// :Qq#
161165
// Disconnect, Quit Control mode
@@ -304,10 +308,6 @@ String MeadeCommandProcessor::handleMeadeSyncControl(String inCmd) {
304308
// SET INFO
305309
/////////////////////////////
306310
String MeadeCommandProcessor::handleMeadeSetInfo(String inCmd) {
307-
if (inCmd.length() < 6) {
308-
return "0";
309-
}
310-
311311
if ((inCmd[0] == 'd') && (inCmd.length() == 10)) {
312312
// Set DEC
313313
// 0123456789
@@ -401,7 +401,7 @@ String MeadeCommandProcessor::handleMeadeSetInfo(String inCmd) {
401401
{
402402
return "1";
403403
}
404-
else if (inCmd[0] == 'D') { // Set Date (MM/DD/YY) :SC04/30/20#
404+
else if (inCmd[0] == 'C') { // Set Date (MM/DD/YY) :SC04/30/20#
405405
return "1Updating Planetary Data#"; //
406406
}
407407
else {
@@ -541,18 +541,33 @@ String MeadeCommandProcessor::handleMeadeExtraCommands(String inCmd) {
541541
String MeadeCommandProcessor::handleMeadeQuit(String inCmd) {
542542
// :Q# stops a motors - remains in Control mode
543543
// :Qq# command does not stop motors, but quits Control mode
544-
if ((inCmd.length() == 0) || (inCmd[0] != 'q')) {
544+
if (inCmd.length() == 0) {
545545
_mount->stopSlewing(ALL_DIRECTIONS | TRACKING);
546546
_mount->waitUntilStopped(ALL_DIRECTIONS);
547547
return "1";
548548
}
549-
else {
550-
inSerialControl = false;
551-
_lcdMenu->setCursor(0, 0);
552-
_lcdMenu->updateDisplay();
553-
_mount->startSlewing(TRACKING);
554-
return "";
549+
550+
switch (inCmd[0]) {
551+
case 'e':
552+
_mount->stopSlewing(EAST);
553+
break;
554+
case 'w':
555+
_mount->stopSlewing(WEST);
556+
break;
557+
case 'n':
558+
_mount->stopSlewing(NORTH);
559+
break;
560+
case 's':
561+
_mount->stopSlewing(SOUTH);
562+
break;
563+
case 'q':
564+
inSerialControl = false;
565+
_lcdMenu->setCursor(0, 0);
566+
_lcdMenu->updateDisplay();
567+
break;
555568
}
569+
570+
return "";
556571
}
557572

558573
/////////////////////////////

Software/Arduino code/OpenAstroTracker/WifiControl.cpp

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,57 @@ WifiControl::WifiControl(Mount* mount, LcdMenu* lcdMenu)
77
{
88
_mount = mount;
99
_lcdMenu = lcdMenu;
10-
11-
switch (WIFI_MODE) {
12-
case 0: // startup Infrastructure Mode
13-
break;
14-
case 1: // startup AP mode
15-
break;
16-
case 2: // Attempt Infra, fail over to AP
17-
break;
18-
}
1910
}
2011

2112
void WifiControl::setup() {
13+
14+
#ifdef DEBUG_MODE
15+
Serial.printf("Starting up Wifi As Mode %d\n", WIFI_MODE);
16+
#endif
17+
2218
_cmdProcessor = MeadeCommandProcessor::instance();
19+
20+
switch (WIFI_MODE) {
21+
case 0: // startup Infrastructure Mode
22+
startInfrastructureMode();
23+
break;
24+
case 1: // startup AP mode
25+
startAccessPointMode();
26+
break;
27+
case 2: // Attempt Infra, fail over to AP
28+
startInfrastructureMode();
29+
_infraStart = millis();
30+
break;
31+
}
2332
}
2433

2534

2635
void WifiControl::startInfrastructureMode()
2736
{
37+
#ifdef DEBUG_MODE
38+
Serial.println("Starting Infrastructure Mode Wifi");
39+
#endif
40+
WiFi.hostname(HOSTNAME);
2841
WiFi.begin(INFRA_SSID, INFRA_WPAKEY);
2942
}
3043

44+
void WifiControl::startAccessPointMode()
45+
{
46+
#ifdef DEBUG_MODE
47+
Serial.println("Starting AP Mode Wifi");
48+
#endif
49+
IPAddress local_ip(192, 168, 1, 1);
50+
IPAddress gateway(192, 168, 1, 1);
51+
IPAddress subnet(255, 255, 255, 0);
52+
53+
WiFi.hostname(HOSTNAME);
54+
WiFi.softAP(HOSTNAME, OAT_WPAKEY);
55+
WiFi.softAPConfig(local_ip, gateway, subnet);
56+
}
57+
3158
void WifiControl::loop()
3259
{
60+
3361
if (_status != WiFi.status()) {
3462
_status = WiFi.status();
3563
#ifdef DEBUG_MODE
@@ -55,12 +83,30 @@ void WifiControl::loop()
5583
}
5684
_mount->loop();
5785

58-
if (_status != WL_CONNECTED) return;
86+
if (_status != WL_CONNECTED) {
87+
infraToAPFailover();
88+
return;
89+
}
5990

6091
tcpLoop();
6192
udpLoop();
6293
}
6394

95+
void WifiControl::infraToAPFailover() {
96+
if (_infraStart != 0 &&
97+
!WiFi.isConnected() &&
98+
_infraStart + _infraWait < millis()) {
99+
100+
WiFi.disconnect();
101+
startAccessPointMode();
102+
_infraStart = 0;
103+
104+
#ifdef DEBUG_MODE
105+
Serial.println("Could not connect to Infra, Starting AP.");
106+
#endif
107+
}
108+
}
109+
64110
void WifiControl::tcpLoop() {
65111
if (client && client.connected()) {
66112
while (client.available()) {
@@ -91,9 +137,7 @@ void WifiControl::udpLoop()
91137
int packetSize = _udp->parsePacket();
92138
if (packetSize)
93139
{
94-
String lookingFor = "skyfi:";
95-
lookingFor += HOSTNAME;
96-
lookingFor += "?";
140+
String lookingFor = "skyfi:";;
97141

98142
String reply = "skyfi:";
99143
reply += HOSTNAME;
@@ -104,12 +148,12 @@ void WifiControl::udpLoop()
104148
#endif
105149
char incomingPacket[255];
106150
int len = _udp->read(incomingPacket, 255);
107-
incomingPacket[lookingFor.length()] = 0;
151+
incomingPacket[len] = 0;
108152
#ifdef DEBUG_MODE
109153
Serial.printf("Received: %s\n", incomingPacket);
110154
#endif
111-
112-
155+
156+
incomingPacket[lookingFor.length()] = 0;
113157
if (lookingFor.equalsIgnoreCase(incomingPacket)) {
114158
_udp->beginPacket(_udp->remoteIP(), 4031);
115159
/*unsigned char bytes[255];

Software/Arduino code/OpenAstroTracker/WifiControl.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class WifiControl {
1717

1818
private:
1919
void startInfrastructureMode();
20-
//void startApMode();
20+
void startAccessPointMode();
21+
void infraToAPFailover();
2122
void tcpLoop();
2223
void udpLoop();
2324
wl_status_t _status;
@@ -28,5 +29,8 @@ class WifiControl {
2829
WiFiServer* _tcpServer;
2930
WiFiUDP* _udp;
3031
WiFiClient client;
32+
33+
long _infraStart = 0;
34+
int _infraWait = 30000; // 30 second timeout for
3135
};
3236
#endif

0 commit comments

Comments
 (0)