forked from Hopperpop/Sattrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSattrack.ino
356 lines (288 loc) · 9.39 KB
/
Sattrack.ino
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <Sgp4.h>
#include <Ticker.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <NeoPixelBus.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include "FS.h"
#include "Globals.h"
#include "Config.h"
#include "SatCalc.h"
#include "Webpage.h"
#include "Clients.h"
#include "Pixels.h"
///////////////////////////////////
// Setup //
///////////////////////////////////
void setup() {
initConfig();
WiFi.hostname(config->host);
strip.Begin();
LedStrip.Begin(&strip);
LedStrip.SetAnimColor(0x9f,0x0,0x0);
LedStrip.AnimStart(ANIM_WAIT);
SPIFFS.begin();
pinMode(buttonPin, INPUT);
#ifdef DEBUG
Serial.begin(115200);
Serial.println();
#endif
///Start wifi
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(config->ssid,config->password);
if (!config->dhcp){
WiFi.config(IPAddress(config->IP),IPAddress(config->Gateway),IPAddress(config->Netmask));
}
///Connect to wifi
int timeout = 0;
while (WiFi.status() != WL_CONNECTED && timeout <= 15 ) {
delay(500);
timeout += 1;
#ifdef DEBUG
Serial.print(".");
#endif
}
///Start AP on timeout
if (timeout > 15){
WiFi.mode(WIFI_AP);
WiFi.softAP( config->host , config->ap_password);
LedStrip.SetAnimColor(0,0,0,0x0,0x0,0x9f);
LedStrip.AnimStart(ANIM_WAIT);
dataError = true;
}else{
LedStrip.SetAnimColor(0xff,0x66,0x0);
}
#ifdef DEBUG
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
#endif
///Set site coordinates
sat.site(config->lat,config->lon,config->alt);
sat.setsunrise(config->sunoffset);
//Start MDNS
#ifndef USE_OTA
if (MDNS.begin(config->host, WiFi.localIP())){
#ifdef DEBUG
Serial.printf("mDNS responder started for %s.local\n", config->host);
Serial.println();
#endif
}
#endif
///get tle and time
UDPNTPClient.begin(2390);
if (!dataError){
bool timeFlag = updateTime();
bool TleFlag = getTle(config->satnum);
if (!(timeFlag && TleFlag)){
#ifdef DEBUG
Serial.println("Can't get data");
Serial.println();
#endif
LedStrip.SetAnimColor(0x9f,0x0,0x0);
LedStrip.AnimStart(ANIM_FLASH);
dataError = true;
if (timeFlag && !TleFlag){
updatejdtime = getJulianTime() + 0.01417; // retry update in 15 min
}else{
updatejdtime = getJulianTime() + 0.000694; // retry update in 1 min
}
}
}
///initilize server,websocket,ota
initServer();
initWebsocket();
initMonths();
#ifdef USE_OTA
initOTA();
#endif
if (!dataError) {LedStrip.AnimStop();}
///Start framerate ticker
#ifdef DEBUG_frame
ticker.attach(1,Second_Tick);
framerate = 0;
#endif
state=IDLE;
}
//////////////////////////////////////
// Main loop //
//////////////////////////////////////
void ColorCalc(double jd,double satEl,int16_t satVis);
void loop() {
double jd = getJulianTime();
if(!dataError){
sat.findsat(jd); //find satellite position on system time
if ( sat.satEl > config->offset ){ ///aboven horizon => display
if (LedStrip.CanShow()){
ColorCalc(jd,sat.satEl,sat.satVis);
}
}else{ //below horizon
if (LedStrip.CanShow()){ //clear pixels
strip.ClearTo(RgbColor(0,0,0));
strip.Show();
}
if (passPredictions[0].jdstop <= jd && !predError){ //pred next past
predError = !updatePasses();
uint8_t buf[]="n";webSocket.broadcastBIN(buf,1); //notify clients that there is new data available
}
if ( updatejdtime < jd){ ///update tle and time
if (passPredictions[0].jdstart - 0.5/sat.revpday > jd || passPredictions[0].jdstop + 0.1/sat.revpday < jd){
if (millis() > 2592000000){
ESP.restart();
}
updateTime();
getTle(config->satnum);
}
}
}
if (millis() - socketrate > 33)
{
socketrate = millis();
webSocketSendData();
}
if(jd > orbit.lastJd-orbit.step*(orbit_size/2-1)){
updateOrbit();
webSocketSendOrbit();
}
}
else if(WiFi.getMode() != WIFI_AP){ ///retry when failed getting data
if (updatejdtime < jd){
bool timeFlag = updateTime();
bool TleFlag = getTle(config->satnum);
if (!(timeFlag && TleFlag)){
#ifdef DEBUG
Serial.println("Can't get data");
Serial.println();
#endif
if (timeFlag && !TleFlag){
updatejdtime = getJulianTime() + 0.01417; // retry update in 15 min
}else{
updatejdtime = getJulianTime() + 0.000694; // retry update in 1 min
}
}else{
dataError = false;
LedStrip.AnimStop();
}
}
}
///calculate prediction requests///
if (PredictRequest != NULL){
int params = PredictRequest->params();
if (params > 0 && !predError){ ///check for extra arguments
char *ptr;
bool err;
uint32_t unix = strtoul(PredictRequest->getParam(0)->value().c_str(),&ptr,10); ///read unix time
double jdC = getJulianFromUnix(unix);
///recalc new predictions
LedStrip.AnimStart(ANIM_WAIT);
passinfo Predictions[pred_size];
#ifdef DEBUG
Serial.println("Prediction request: " + PredictRequest->getParam(0)->name() + " " + PredictRequest->getParam(0)->value());
#endif
if (PredictRequest->getParam(0)->name() == "pre"){
err = !predictPasses(Predictions,jdC, true);
}else if (PredictRequest->getParam(0)->name() == "next"){
err = !predictPasses(Predictions,jdC, false);
}else{
err = true; //no correct arguments
}
LedStrip.AnimStop();
senddata(PredictRequest,Predictions,err); /// send new prediction
}else{
senddata(PredictRequest,passPredictions,false); /// send normal prediction
}
PredictRequest = NULL;
}
///statemachine///
switch(state){
case RECALC:
LedStrip.SetAnimColor(0xff,0x66,0x0);
LedStrip.AnimStart(ANIM_WAIT);
sat.site(config->lat,config->lon,config->alt); //set new coordinates
sat.setsunrise(config->sunoffset);
if(!getTle(config->satnum, true)){ //get new tle and recalculate overpasses
LedStrip.SetAnimColor(0x9f,0x0,0x0);
LedStrip.AnimStart(ANIM_FLASH);
dataError = true;
updatejdtime = getJulianTime() + 0.003472; // retry update in 5 min
}else{
LedStrip.AnimStop();
dataError = false;
}
state=IDLE;
break;
case RESTART:
closeAllConnections();
delay(5000);
ESP.restart();
state=IDLE;
break;
case RESEND:
uint8_t buf[]="n";
webSocket.broadcastBIN(buf,1); //notify clients that there is new data available
state=IDLE;
break;
}
///OTA and debug////
#ifdef DEBUG_frame
framerate +=1;
#endif
#ifdef USE_OTA
ArduinoOTA.handle();
#endif
///resetbutton///
bool state = digitalRead(buttonPin);
if (!state && prevButtonState){
pressTime = millis();
}
if (millis() - pressTime > 5000 && !state){ //button pressed for 5 sec
//save defaultconfigs
setDefaultConfig();
EEPROM.getDataPtr();
EEPROM.commit();
//small animation
LedStrip.SetAnimColor(127,0,255);
LedStrip.AnimStart(ANIM_WAIT);
delay(2000);
ESP.restart();
}
prevButtonState = state;
}
//////////////////////////////////////
// Functions //
//////////////////////////////////////
double getJulianTime(){
return jdtime + (millis()-timemillis)/86400000.0;
}
void ColorCalc(double jd,double satEl,int16_t satVis){
RgbColor Color;
if (satVis == -1){ ///daylight
Color = RgbColor::LinearBlend(config->ColorDayL, config->ColorDayH, (satEl-config->offset)/(90.0-config->offset));
}else{ //dark
RgbColor visible = RgbColor::LinearBlend(config->ColorVisL, config->ColorVisH, (satEl-config->offset)/(90.0-config->offset));
RgbColor eclipsed = RgbColor::LinearBlend(config->ColorEclL, config->ColorEclH, (satEl-config->offset)/(90.0-config->offset));
Color = RgbColor::LinearBlend(eclipsed, visible, satVis/1000.0);
}
if (jd <= passPredictions[0].jdstop+0.0001 && jd >= passPredictions[0].jdstart-0.0001 && !predError){
float brightness;
if (jd <= passPredictions[0].jdmax){
brightness = (jd-passPredictions[0].jdstart)/(passPredictions[0].jdmax-passPredictions[0].jdstart);
}else{
brightness = (passPredictions[0].jdstop-jd)/(passPredictions[0].jdstop-passPredictions[0].jdmax);
}
if (brightness < 0.0){brightness = 0.0;}
if (brightness > 1.0){brightness = 1.0;}
Color = RgbColor::LinearBlend(RgbColor(0,0,0), Color, brightness);
}
strip.ClearTo(Color);
strip.Show();
}