Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Secret.h
.DS_Store
Secret.h
37 changes: 18 additions & 19 deletions rover-gnss/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ char user[32] = "rover-gnss-tester";
char passwd[32] = "";

// Network configuration
IPAddress server(192, 168, 1, 100);
IPAddress tcp_server(192, 168, 1, 100);
int port = 80;
const char* udpAddress = "192.168.1.255";
const int udpPort = 9999;
int trans = 3;

// Timing intervals
const unsigned long wifiReconnectInterval = 10000; // 10 seconds
Expand All @@ -40,7 +39,7 @@ const unsigned long ntripReconnectInterval = 10000; // 10 seconds

// Global variables
WiFiClient espClient;
PubSubClient client(espClient);
PubSubClient mqtt_client(espClient);
TinyGPSPlus gps;
NTRIPClient ntrip_c;
WiFiUDP udp;
Expand Down Expand Up @@ -72,51 +71,51 @@ float distance = 0 ;
void loadPreferences() {
preferences.begin("config", true); // read-only
String temp;

temp = preferences.getString("ssid", ssid);
temp.toCharArray(ssid, sizeof(ssid));

temp = preferences.getString("password", password);
temp.toCharArray(password, sizeof(password));

temp = preferences.getString("mqtt_server", mqtt_server);
temp.toCharArray(mqtt_server, sizeof(mqtt_server));

mqtt_port = preferences.getInt("mqtt_port", mqtt_port);

temp = preferences.getString("mqtt_output", mqtt_output);
temp.toCharArray(mqtt_output, sizeof(mqtt_output));

temp = preferences.getString("mqtt_input", mqtt_input);
temp.toCharArray(mqtt_input, sizeof(mqtt_input));

temp = preferences.getString("mqtt_log", mqtt_log);
temp.toCharArray(mqtt_log, sizeof(mqtt_log));

temp = preferences.getString("mqtt_user", mqtt_user);
temp.toCharArray(mqtt_user, sizeof(mqtt_user));

temp = preferences.getString("mqtt_password", mqtt_password);
temp.toCharArray(mqtt_password, sizeof(mqtt_password));

publish_freq = preferences.getInt("publish_freq", publish_freq);

temp = preferences.getString("host", host);
temp.toCharArray(host, sizeof(host));

httpPort = preferences.getInt("httpPort", httpPort);

temp = preferences.getString("mntpnt", mntpnt);
temp.toCharArray(mntpnt, sizeof(mntpnt));

temp = preferences.getString("user", user);
temp.toCharArray(user, sizeof(user));

temp = preferences.getString("passwd", passwd);
temp.toCharArray(passwd, sizeof(passwd));

mqtt_enabled = preferences.getBool("mqtt_enabled", true);

preferences.end();
}

Expand Down
16 changes: 13 additions & 3 deletions rover-gnss/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@

#define LOG_LEVEL LOG_LEVEL_DEBUG

// Connection to use to transmit data
// RS2323_SERIAL : and connect tx f9p directly to rs232 module
// UDP_SERVER
// TCP_MQTT
// RS2323_MYSERIAL
#define LOG 0
#define UDP_SERVER 1
#define TCP_SERVER 2
#define MYSERIAL 3
#define TRANSMITION_MODE MYSERIAL

// Pinouts
#define PIN_TX 18 //esp32
#define PIN_RX 19 //esp32
Expand Down Expand Up @@ -52,11 +63,10 @@ extern char user[32];
extern char passwd[32];

// Network configuration
extern IPAddress server;
extern IPAddress tcp_server;
extern int port;
extern const char* udpAddress;
extern const int udpPort;
extern int trans;

// Timing intervals
extern const unsigned long wifiReconnectInterval;
Expand All @@ -65,7 +75,7 @@ extern const unsigned long ntripReconnectInterval;

// Global variables
extern WiFiClient espClient;
extern PubSubClient client;
extern PubSubClient mqtt_client;
extern TinyGPSPlus gps;
extern NTRIPClient ntrip_c;
extern WiFiUDP udp;
Expand Down
24 changes: 12 additions & 12 deletions rover-gnss/Connectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ void connectToWiFi() {
isAPMode = false; // Reset AP mode flag

// Reinitialize MQTT and NTRIP connections after WiFi reconnects
if (!isAPMode) {
if (client.connected()) {
client.disconnect();
if (!isAPMode) {
if (mqtt_client.connected()) {
mqtt_client.disconnect();
}
reconnectMQTT();

Expand All @@ -124,20 +124,20 @@ void connectToWiFi() {
void reconnectMQTT() {
if (!mqtt_enabled || isAPMode) return;
unsigned long startAttemptTime = millis();
while (!client.connected() && millis() - startAttemptTime < mqttReconnectInterval) {
while (!mqtt_client.connected() && millis() - startAttemptTime < mqttReconnectInterval) {
logMessage(LOG_LEVEL_INFO, "Attempting MQTT connection...");
if (client.connect(mqtt_UUID, mqtt_user, mqtt_password)) {
if (mqtt_client.connect(mqtt_UUID, mqtt_user, mqtt_password)) {
logMessage(LOG_LEVEL_INFO, "connected");
client.subscribe(mqtt_input);
mqtt_client.subscribe(mqtt_input);
break;
} else {
logMessage(LOG_LEVEL_ERROR, "failed, rc=", client.state());
logMessage(LOG_LEVEL_ERROR, "failed, rc=", mqtt_client.state());
logMessage(LOG_LEVEL_INFO, " try again in 5 seconds");
vTaskDelay(500 / portTICK_PERIOD_MS); // Utiliser vTaskDelay au lieu de delay()
}
}

if (!client.connected()) {
if (!mqtt_client.connected()) {
logMessage(LOG_LEVEL_ERROR, "MQTT connection failed");
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ void handleRoot() {
<label for="password">Password:</label>
<input type="password" id="password" name="password" value=")rawliteral" + String(password) + R"rawliteral("><br>
<button type="button" onclick="togglePasswordVisibility('password')">Show/Hide</button><br>

<h2>NTRIP Configuration</h2>
<label for="host">NTRIP Host:</label>
<input type="text" id="host" name="host" value=")rawliteral" + String(host) + R"rawliteral("><br>
Expand All @@ -257,7 +257,7 @@ void handleRoot() {
<label for="passwd">NTRIP Password:</label>
<input type="password" id="passwd" name="passwd" value=")rawliteral" + String(passwd) + R"rawliteral("><br>
<button type="button" onclick="togglePasswordVisibility('passwd')">Show/Hide</button><br>

<h2>MQTT Configuration</h2>
<label for="mqtt_enabled">Enable MQTT:</label>
<input type="checkbox" id="mqtt_enabled" name="mqtt_enabled" )rawliteral" + String(mqtt_enabled ? "checked" : "") + R"rawliteral(><br>
Expand All @@ -278,7 +278,7 @@ void handleRoot() {
<button type="button" onclick="togglePasswordVisibility('mqtt_password')">Show/Hide</button><br>
<label for="publish_freq">Publish Frequency:</label>
<input type="number" id="publish_freq" name="publish_freq" value=")rawliteral" + String(publish_freq) + R"rawliteral("><br>

<h2>SAVE Configurations</h2>
<input type="submit" value="Save">
</form>
Expand All @@ -293,4 +293,4 @@ void handleRoot() {
</html>
)rawliteral";
webServer.send(200, "text/html", html);
}
}
53 changes: 25 additions & 28 deletions rover-gnss/rover-gnss.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ void setup() {
loadPreferences();

setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
mqtt_client.setServer(mqtt_server, mqtt_port);
mqtt_client.setCallback(callback);

connectToWiFi();
if (!isAPMode) {
if (!isAPMode) {
requestSourceTable();
requestMountPointRawData();
}
Expand Down Expand Up @@ -106,11 +106,11 @@ void loop() {

// Handle MQTT reconnection
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected() && (now - lastMqttReconnectAttempt > mqttReconnectInterval)) {
if (!mqtt_client.connected() && (now - lastMqttReconnectAttempt > mqttReconnectInterval)) {
lastMqttReconnectAttempt = now;
reconnectMQTT();
} else {
client.loop();
mqtt_client.loop();
}
}

Expand Down Expand Up @@ -171,19 +171,19 @@ void readGNSSData() {

//MQTT
void handleMQTTConnection() {
if (!client.connected()) {
if (!mqtt_client.connected()) {
reconnectMQTT();
}
client.loop();
mqtt_client.loop();
}

void publishMQTTData() {
if (mqtt_enabled && client.connected() && gps.location.isValid() && gps.date.isValid() && gps.time.isValid() && gps.altitude.isValid() && gps.satellites.isValid()) {
if (mqtt_enabled && mqtt_client.connected() && gps.location.isValid() && gps.date.isValid() && gps.time.isValid() && gps.altitude.isValid() && gps.satellites.isValid()) {
char timeBuffer[30];
snprintf(timeBuffer, sizeof(timeBuffer), "%04d-%02d-%02d %02d:%02d:%02d,%02d",
gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second(),gps.time.centisecond());

float waterTemp = sensors.getTempCByIndex(0);
if (waterTemp == DEVICE_DISCONNECTED_C) {
waterTemp = 0; // Set temperature to 0 if no sensor is connected
Expand All @@ -206,8 +206,8 @@ void publishMQTTData() {
"\",\"Temp\":\"" + String(waterTemp) +
"\",\"Dist\":\"" + String(distance) +
"\",\"Time\":\"" + String(timeBuffer) + "\"}";
if (client.publish(mqtt_output, json.c_str())) {

if (mqtt_client.publish(mqtt_output, json.c_str())) {
logMessage(LOG_LEVEL_INFO, "Mqtt sent to : ", mqtt_output);
logMessage(LOG_LEVEL_INFO, json.c_str());
} else {
Expand All @@ -216,7 +216,7 @@ void publishMQTTData() {
} else {
if (!isAPMode) {
logMessage(LOG_LEVEL_WARN, "GNSS data not valid. Data not sent.");
} else {
} else {
logMessage(LOG_LEVEL_DEBUG, "Wait...");
}
}
Expand Down Expand Up @@ -334,7 +334,7 @@ void handleNTRIPData() {
}

void handleSerialData() {
WiFiClient client;
WiFiClient wifi_client;
char buffer[512]; // Buffer pour stocker les données série
while (MySerial.available()) {
int len = MySerial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
Expand All @@ -347,33 +347,30 @@ void handleSerialData() {
gps.encode(buffer[i]);
}

switch (trans) {
case 0: // serial out
switch (TRANSMITION_MODE) {
case LOG: // serial out
logMessage(LOG_LEVEL_DEBUG, buffer);
break;
case 1: // udp out
case UDP_SERVER: // udp out
udp.beginPacket(udpAddress, udpPort);
udp.write((uint8_t*)buffer, len);
udp.endPacket();
break;
case 2: // tcp client out
if (!client.connect(server, port)) {
case TCP_SERVER: // tcp client_wifi out
if (!wifi_client.connect(tcp_server, port)) {
logMessage(LOG_LEVEL_ERROR, "connection failed");
return;
}
client.write((uint8_t*)buffer, len);
while (client.connected()) {
while (client.available()) {
char c = client.read();
wifi_client.write((uint8_t*)buffer, len);
while (wifi_client.connected()) {
while (wifi_client.available()) {
char c = wifi_client.read();
logMessage(LOG_LEVEL_DEBUG, String(c).c_str());
}
}
client.stop();
wifi_client.stop();
break;
case 3: // MySerial out
MySerial.write((uint8_t*)buffer, len);
break;
case 4: // MySerial out
case MYSERIAL: // MySerial out
MySerial.write((uint8_t*)buffer, len);
break;
default: // mauvaise config
Expand Down Expand Up @@ -413,4 +410,4 @@ void publishTask(void *parameter) {
logMessage(LOG_LEVEL_WARN, "Failed to take semaphore in publishTask.");
}
}
}
}
Loading