-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtracker.cpp
138 lines (121 loc) · 4.32 KB
/
tracker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Sgp4.h>
#include "tracker.h"
float sat_latitude;
float sat_longitude;
float sat_altitude;
float sat_azimuth;
float sat_elevation;
float sat_distance;
String catalog_number = "25544";
uint16_t get_error = 0;
int http_code = 0;
Sgp4 sat;
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
data.replace("\r", "");
for (int i = 0; i <= maxIndex && found <= index; i++)
{
if (data.charAt(i) == separator || i == maxIndex)
{
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void getTLE(String catalog_number)
{
// WiFiClient client; // http
WiFiClientSecure client; // https
HTTPClient http;
if (WiFi.status() == WL_CONNECTED)
{
// https insecure
client.setInsecure();
// if (http_timeout > 0) http.setTimeout(http_timeout); // ms
Serial.print("Getting TLE ... ");
if (http.begin(client, "https://celestrak.org/NORAD/elements/gp.php?CATNR=" + catalog_number))
{
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
Serial.println("success");
String line1 = getValue(payload, '\n', 0);
String line2 = getValue(payload, '\n', 1);
String line3 = getValue(payload, '\n', 2);
line1.toCharArray(tle_name, 32);
line2.toCharArray(tle_line1, 70);
line3.toCharArray(tle_line2, 70);
sat.init(tle_name, tle_line1, tle_line2); // initialize satellite parameters
}
else
{
Serial.println("failed");
// Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
if (get_error < 65500)
get_error++;
}
http.end();
http_code = httpCode;
}
}
}
void trackerSetup()
{
Serial.print("Loading TLE ... ");
sat.site(latitude, longitude, altitude); // set site latitude[°], longitude[°] and altitude[m]
sat.init(tle_name, tle_line1, tle_line2); // initialize satellite parameters
Serial.println("done");
// Serial.println("latitude = " + String(latitude, 7) + " longitude = " + String(longitude, 7) + " altitude = " + String(altitude, 2));
// Serial.println("sat_name = " + String(sat_name));
// Serial.println("tle_line1 = " + String(tle_line1));
// Serial.println("tle_line2 = " + String(tle_line2));
// sat.findsat(timeNow);
// sat.findsat(unixtime);
// invjday(sat.satJd, timeZone, true, year, mon, day, hr, minute, sec);
// Serial.println("\nLocal time: " + String(day) + '/' + String(mon) + '/' + String(year) + ' ' + String(hr) + ':' + String(minute) + ':' + String(sec));
// Serial.println("azimuth = " + String(sat.satAz) + " elevation = " + String(sat.satEl) + " distance = " + String(sat.satDist));
// Serial.println("latitude = " + String(sat.satLat) + " longitude = " + String(sat.satLon) + " altitude = " + String(sat.satAlt));
// Serial.println("AZStep pos: " + String(stepperAZ.currentPosition()));
}
void trackerLoop()
{
// int timezone = 8 ; //utc + 8
// int _year;
// int _month;
// int _day;
// int _hour;
// int _minute;
// double _second;
sat.findsat((unsigned long)epoch);
// invjday(sat.satJd, timezone, true, _year, _month, _day, _hour, _minute, _second);
// Serial.println(String(day) + '/' + String(mon) + '/' + String(year) + ' ' + String(hr) + ':' + String(minute) + ':' + String(sec));
// Serial.println("azimuth = " + String(sat.satAz) + " elevation = " + String(sat.satEl) + " distance = " + String(sat.satDist));
// Serial.println("latitude = " + String(sat.satLat) + " longitude = " + String(sat.satLon) + " altitude = " + String(sat.satAlt));
// Serial.println("AZStep pos: " + String(stepperAZ.currentPosition()));
sat_latitude = sat.satLat;
sat_longitude = sat.satLon;
sat_altitude = sat.satAlt;
sat_azimuth = sat.satAz;
sat_elevation = sat.satEl;
sat_distance = sat.satDist;
// second = _second;
// minute = _minute;
// hour = _hour;
// day = _day;
// month = _month;
// year = _year;
if (auto_control)
{
setAz(round(sat_azimuth)); // 0 - 360
setEl(round(sat_elevation)); // 0 - 90
}
}