-
Notifications
You must be signed in to change notification settings - Fork 9
/
fake_GPS.ino
100 lines (84 loc) · 2.56 KB
/
fake_GPS.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
extern "C"
{
#include "user_interface.h" // Required for wifi_station_connect() to work
}
#include <ESP8266WiFi.h> // https://github.com/esp8266/Arduino
#include <WiFiUdp.h>
#include <time.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include "TimeClient.h" // https://github.com/arduino-libraries/NTPClient
TimeClient TimeClient;
WiFiManager wifiManager;
time_t locEpoch = 0, netEpoch = 0;
int firstrun = 1;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
wifiManager.setTimeout(180);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "NixieAP"
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("NixieAP"))
{
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
Serial1.begin(9600);
TimeClient.Setup();
askFrequency = 50;
}
void loop()
{
char tstr[128];
unsigned char cs;
unsigned int i;
struct tm *tmtime;
unsigned long amicros, umicros = 0;
for (;;)
{
amicros = micros();
askFrequency = 60 * 60 * 1000;
while (((netEpoch = TimeClient.GetCurrentTime()) == locEpoch) || (!netEpoch))
{
delay(100);
}
if (netEpoch)
{
umicros = amicros;
tmtime = localtime(&netEpoch);
if ((!tmtime->tm_sec) || firstrun) // full minute or first cycle
{
digitalWrite(LED_BUILTIN, HIGH); // blink for sync
sprintf(tstr, "$GPRMC,%02d%02d%02d,A,0000.0000,N,00000.0000,E,0.0,0.0,%02d%02d%02d,0.0,E,S",
tmtime->tm_hour, tmtime->tm_min, tmtime->tm_sec, tmtime->tm_mday, tmtime->tm_mon + 1, tmtime->tm_year - 100);
cs = 0;
for (i = 1; i < strlen(tstr); i++) // calculate checksum
cs ^= tstr[i];
sprintf(tstr + strlen(tstr), "*%02X", cs);
Serial.println(tstr); // send to console
Serial1.println(tstr); // send to clock
delay(100);
digitalWrite(LED_BUILTIN, LOW);
if(tmtime->tm_sec < 57)
{
delay(58000 - ((micros() - amicros) / 1000) - (tmtime->tm_sec * 1000)); // wait for end of minute
}
firstrun = 0;
}
}
delay(200);
if (((amicros - umicros) / 1000000L) > 3600) // if no sync for more than one hour
digitalWrite(LED_BUILTIN, HIGH); // switch off LED
}
}