-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESPNTRIPClient.ino
182 lines (154 loc) · 4.36 KB
/
ESPNTRIPClient.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
/**
* ESP NTRIP Client
*
* Copyright 2023 by KLS <https://github.com/klstronics/ESPNTRIPClient>
*
* Licensed under GNU General Public License 3.0 or later.
* Some rights reserved. See COPYING, AUTHORS.
*
* @license GPL-3.0 <http://www.gnu.org/licenses/>
*/
#include <FS.h>
#include <ArduinoJson.h>
#include "NTRIPClient.h"
#include <WiFiManager.h>
#ifdef ESP32
#include <SPIFFS.h>
#else
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#define RX 16
#define TX 4
#define CONFIGSWITCH 17
#define LED 14
#define LED_ON digitalWrite(LED, HIGH);
#define LED_OFF digitalWrite(LED, LOW);
#else
#define CONFIGSWITCH 5
#define LED 2
#define LED_ON digitalWrite(LED, LOW);
#define LED_OFF digitalWrite(LED, HIGH);
#endif
String ntriphost = "";
uint16_t ntripport = 2101;
String ntripmountpoint = "";
String ntripuser = "";
String ntrippassword = "";
NTRIPClient *ntrip;
WiFiManager wm;
bool saved = false;
String gga = "";
String configfile = "/ntrip.json";
void setup() {
#ifdef ESP32
Serial.begin(115200, SERIAL_8N1, RX, TX);
#else
Serial.begin(115200, SERIAL_8N1);
#endif
pinMode(CONFIGSWITCH, INPUT_PULLUP);
#ifdef LED
pinMode(LED, OUTPUT);
LED_OFF
#endif
if (SPIFFS.begin()) {
if (SPIFFS.exists(configfile)) {
if (File file = SPIFFS.open(configfile, "r")) {
StaticJsonDocument<512> doc;
if (!deserializeJson(doc, file)) {
ntriphost = String((const char*) doc["host"]);
ntripport = String((const char*) doc["port"]).toInt();
ntripmountpoint = String((const char*) doc["mountpoint"]);
ntripuser = String((const char*) doc["user"]);
ntrippassword = String((const char*) doc["password"]);
}
file.close();
}
}
}
else {
SPIFFS.format();
ESP.restart();
}
bool configured = wm.getWiFiIsSaved() && ntriphost.length() > 0 && ntripmountpoint.length() > 0;
wm.setDebugOutput(false);
wm.setConfigPortalTimeout(configured ? 25 : 0);
wm.setCaptivePortalEnable(true);
wm.setAPClientCheck(true);
wm.setBreakAfterConfig(false);
wm.setConfigPortalBlocking(true);
wm.setDebugOutput(false);
wm.setSaveParamsCallback(saveParamCallback);
WiFiManagerParameter host("host", "NTRIP Host", ntriphost.c_str(), 32);
WiFiManagerParameter port("port", "NTRIP Port", String(ntripport).c_str(), 6);
WiFiManagerParameter mountpoint("mountpoint", "Mountpoint", ntripmountpoint.c_str(), 32);
WiFiManagerParameter user("user", "User", ntripuser.c_str(), 32);
WiFiManagerParameter password("password", "Password", ntrippassword.c_str(), 32);
wm.addParameter(&host);
wm.addParameter(&port);
wm.addParameter(&mountpoint);
wm.addParameter(&user);
wm.addParameter(&password);
if (!configured || digitalRead(CONFIGSWITCH) == HIGH) {
wm.startConfigPortal("NTRIPConfig");
wm.stopConfigPortal();
wm.disconnect();
if (saved) {
ESP.restart();
}
}
ntrip = new NTRIPClient(ntriphost, ntripport);
}
void loop() {
connectWiFi();
while (Serial.available()) {
char c = Serial.read();
if (c == '$') {
gga = c;
}
else {
gga += c;
}
if (c == '\n' && gga.length() > 40) {
if (!ntrip->connected()) {
ntrip->startStream(ntripmountpoint, gga, ntripuser, ntrippassword);
}
else {
ntrip->sendGGA(gga);
}
gga = "";
}
}
while (ntrip->available()) {
Serial.write(ntrip->read());
}
}
void connectWiFi() {
if (WiFi.status() == WL_CONNECTED) {
LED_ON
return;
}
LED_OFF
WiFi.setHostname("NTRIPClient");
WiFi.mode(WIFI_STA);
WiFi.begin(wm.getWiFiSSID(true).c_str(), wm.getWiFiPass(true).c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
LED_ON
}
void saveParamCallback(){
StaticJsonDocument<512> doc;
doc["host"] = String(wm.server->arg("host"));
doc["port"] = String(wm.server->arg("port"));
doc["mountpoint"] = String(wm.server->arg("mountpoint"));
doc["user"] = String(wm.server->arg("user"));
doc["password"] = String(wm.server->arg("password"));
File file = SPIFFS.open(configfile, "w");
if (file) {
serializeJson(doc, file);
file.close();
}
saved = true;
//serializeJson(doc, Serial);
}