diff --git a/.gitignore b/.gitignore index 2753f7e..c68747a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -Secret.h \ No newline at end of file +.DS_Store +Secret.h diff --git a/rover-gnss/Config.cpp b/rover-gnss/Config.cpp index 963a848..36d459b 100644 --- a/rover-gnss/Config.cpp +++ b/rover-gnss/Config.cpp @@ -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 @@ -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; @@ -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(); } diff --git a/rover-gnss/Config.h b/rover-gnss/Config.h index 8c636e7..23f3a7e 100644 --- a/rover-gnss/Config.h +++ b/rover-gnss/Config.h @@ -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 @@ -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; @@ -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; diff --git a/rover-gnss/Connectivity.cpp b/rover-gnss/Connectivity.cpp index 7f9dc59..9a1a34d 100644 --- a/rover-gnss/Connectivity.cpp +++ b/rover-gnss/Connectivity.cpp @@ -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(); @@ -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"); } } @@ -244,7 +244,7 @@ void handleRoot() {

- +

NTRIP Configuration


@@ -257,7 +257,7 @@ void handleRoot() {

- +

MQTT Configuration


@@ -278,7 +278,7 @@ void handleRoot() {

- +

SAVE Configurations

@@ -293,4 +293,4 @@ void handleRoot() { )rawliteral"; webServer.send(200, "text/html", html); -} \ No newline at end of file +} diff --git a/rover-gnss/rover-gnss.ino b/rover-gnss/rover-gnss.ino index 5ab5e50..3e78537 100644 --- a/rover-gnss/rover-gnss.ino +++ b/rover-gnss/rover-gnss.ino @@ -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(); } @@ -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(); } } @@ -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 @@ -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 { @@ -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..."); } } @@ -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); @@ -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 @@ -413,4 +410,4 @@ void publishTask(void *parameter) { logMessage(LOG_LEVEL_WARN, "Failed to take semaphore in publishTask."); } } -} \ No newline at end of file +} diff --git a/unit_tests/7-Wifi-MQTT-RTK/7-Wifi-MQTT-RTK.ino b/unit_tests/7-Wifi-MQTT-RTK/7-Wifi-MQTT-RTK.ino index 8efe7ba..26fd655 100644 --- a/unit_tests/7-Wifi-MQTT-RTK/7-Wifi-MQTT-RTK.ino +++ b/unit_tests/7-Wifi-MQTT-RTK/7-Wifi-MQTT-RTK.ino @@ -9,8 +9,8 @@ HardwareSerial MySerial(1); #define PIN_RX 16 -#define PIN_TX 17 -#define MYSERIAL_BAUD_RATE 460800 // 115200 Works with F9P config on 2Hz ( not in 5Hz ! ) +#define PIN_TX 17 +#define MYSERIAL_BAUD_RATE 460800 // 115200 Works with F9P config on 2Hz ( not in 5Hz ! ) // #define POWER_PIN 25 @@ -25,10 +25,10 @@ DallasTemperature sensors(&oneWire); // variable to hold device addresses DeviceAddress Thermometer; int deviceCount = 0; -float temp =0; // variable for displaying the temperature +float temp = 0; // variable for displaying the temperature // WIFI Parameters -const char* ssid = WIFI_SSID; +const char* ssid = WIFI_SSID; const char* password = WIFI_PASSWORD; // MQTT Parameters @@ -42,26 +42,26 @@ const char* mqtt_password = MQTT_PASSWORD; const char mqtt_UUID[] = MQTT_UUID; WiFiClient espClient; -PubSubClient client(espClient); +PubSubClient client_mqtt(espClient); long lastMsg = 0; char msg[50]; int value = 0; int timeInterval = 3000; -const long readDuration = 1000; // Duration of NMEA reading in milliseconds -unsigned long previousMillis = 0; // timer -unsigned long currentMillis = 0; // timer +const long readDuration = 1000; // Duration of NMEA reading in milliseconds +unsigned long previousMillis = 0; // timer +unsigned long currentMillis = 0; // timer // TinyGPSPlus instance to store GNSS NMEA data (datetim, position, etc.) TinyGPSPlus gps; TinyGPSCustom gnssFixMode(gps, "GNGGA", 6); IPAddress server(192, 168, 1, 100); // IP address of the server -int port = 80; +int port = 80; char* host = NTRIP_SERVER_HOST; -int httpPort = 2101; // port 2101 is default port of NTRIP caster +int httpPort = 2101; // port 2101 is default port of NTRIP caster char* mntpnt = NTRIP_CASTER_MOUNTPOINT; -char* user = NTRIP_USER; +char* user = NTRIP_USER; char* passwd = NTRIP_PASSWORD; bool sendGGA = true; NTRIPClient ntrip_c; @@ -71,276 +71,293 @@ String ggaMessage = ""; const char* udpAddress = "192.168.1.255"; const int udpPort = 9999; -int trans = 3; //0 = serial, 1 = udp, 2 = tcp client, 3 = MySerial - Choose which out you want use. for rs232 set 0 and connect tx f9p directly to rs232 module +// 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 WiFiUDP udp; void setup() { - // POWER_PIN : This pin controls the power supply of the MICRO PCI card - // pinMode(POWER_PIN, OUTPUT); - // digitalWrite(POWER_PIN, HIGH); - - // Start serial ports - Serial.begin(115200); - delay(100); - MySerial.begin(MYSERIAL_BAUD_RATE, SERIAL_8N1, PIN_RX, PIN_TX); // 115200 Works with - delay(100); - - // Start up the DS18b20 communication protocol - sensors.begin(); - - // Locate devices on the bus - Serial.println("Locating devices..."); - Serial.print("Found "); - deviceCount = sensors.getDeviceCount(); // counting number of devices on the bus - Serial.print(deviceCount, DEC); - Serial.println(" devices."); - Serial.println(""); - - Serial.println("Printing addresses..."); - for (int i = 0; i < deviceCount; i++) { - Serial.print("Sensor "); - Serial.print(i+1); - Serial.print(" : "); - sensors.getAddress(Thermometer, i); // get each DS18b20 64bits address - printAddress(Thermometer); // function at the end of this code - } - - // Setup Wifi & MQTT - setup_wifi(); - client.setServer(mqtt_server, mqtt_port); - client.setCallback(callback); - - Serial.println("Requesting SourceTable."); - if(ntrip_c.reqSrcTbl(host,httpPort)){ - char buffer[512]; - delay(5); - while(ntrip_c.available()){ - ntrip_c.readLine(buffer,sizeof(buffer)); - //Serial.print(buffer); //print sourcetable - } - Serial.print("Requesting SourceTable is OK\n"); - - } else { - Serial.println("SourceTable request error"); - } - ntrip_c.stop(); // Need to call "stop" function for next request. - - Serial.println("Requesting MountPoint's Raw data"); - if(!ntrip_c.reqRaw(host,httpPort,mntpnt,user,passwd)){ - delay(15000); - ESP.restart(); + // POWER_PIN : This pin controls the power supply of the MICRO PCI card + // pinMode(POWER_PIN, OUTPUT); + // digitalWrite(POWER_PIN, HIGH); + + // Start serial ports + Serial.begin(115200); + delay(100); + MySerial.begin(MYSERIAL_BAUD_RATE, SERIAL_8N1, PIN_RX, PIN_TX); // 115200 Works with + delay(100); + + // Start up the DS18b20 communication protocol + sensors.begin(); + + // Locate devices on the bus + Serial.println("Locating devices..."); + Serial.print("Found "); + deviceCount = sensors.getDeviceCount(); // counting number of devices on the bus + Serial.print(deviceCount, DEC); + Serial.println(" devices."); + Serial.println(""); + + Serial.println("Printing addresses..."); + for (int i = 0; i < deviceCount; i++) { + Serial.print("Sensor "); + Serial.print(i + 1); + Serial.print(" : "); + sensors.getAddress(Thermometer, i); // get each DS18b20 64bits address + printAddress(Thermometer); // function at the end of this code + } + + // Setup Wifi & MQTT + setup_wifi(); + if (TRANSMITION_MODE == TCP_MQTT) { + client_mqtt.setServer(mqtt_server, mqtt_port); + client_mqtt.setCallback(callback); + } + + // Ntrip connectinon + Serial.println("Requesting SourceTable."); + if (ntrip_c.reqSrcTbl(host, httpPort)) { + char buffer[512]; + delay(5); + while (ntrip_c.available()) { + ntrip_c.readLine(buffer, sizeof(buffer)); + //Serial.print(buffer); //print sourcetable } - Serial.println("Requesting MountPoint is OK"); + Serial.print("Requesting SourceTable is OK\n"); + + } else { + Serial.println("SourceTable request error"); + } + ntrip_c.stop(); // Need to call "stop" function for next request. + + Serial.println("Requesting MountPoint's Raw data"); + if (!ntrip_c.reqRaw(host, httpPort, mntpnt, user, passwd)) { + delay(15000); + ESP.restart(); + } + Serial.println("Requesting MountPoint is OK"); } void loop() { - - if (sendGGA) { - currentMillis = millis(); - if (currentMillis - previousMillis >= timeInterval) { - previousMillis = currentMillis; - - unsigned long readStartMillis = millis(); - bool ggaFound = false; - while (millis() - readStartMillis < readDuration && !ggaFound) { - while (MySerial.available()) { - char c = MySerial.read(); - gps.encode(c); - if (c == '\n' || c == '\r') { - if (nmeaMessage.startsWith("$GNGGA") || nmeaMessage.startsWith("$GPGGA")) { - // Validation du format GGA - int numFields = 0; - for (char ch : nmeaMessage) { - if (ch == ',') numFields++; - } - if (numFields == 14) { // 14 virgules attendues dans un message GGA complet - ntrip_c.setLastGGA(nmeaMessage); // Stocker le dernier message GGA reçu - Serial.println("Extracted GGA: " + nmeaMessage); // Log du message GGA extrait - ggaFound = true; // Mettre à jour le drapeau pour arrêter la lecture - break; // Sortir de la boucle intérieure - } - } - nmeaMessage = ""; - } else { - nmeaMessage += c; - } - } - } - // Send the last GGA message stored - String lastGGA = ntrip_c.getLastGGA(); - if (lastGGA != "") { - ntrip_c.sendGGA(lastGGA.c_str(), host, httpPort, user, passwd, mntpnt); - Serial.println("Sent GGA: " + lastGGA); // Log sent GGA message - lastGGA = ""; - //Serial.println("Cleaned GGA"); - } else { - Serial.println("No GGA message to send."); + if (sendGGA) { + currentMillis = millis(); + if (currentMillis - previousMillis >= timeInterval) { + previousMillis = currentMillis; + + unsigned long readStartMillis = millis(); + bool ggaFound = false; + while (millis() - readStartMillis < readDuration && !ggaFound) { + while (MySerial.available()) { + char c = MySerial.read(); + gps.encode(c); + if (c == '\n' || c == '\r') { + if (nmeaMessage.startsWith("$GNGGA") || nmeaMessage.startsWith("$GPGGA")) { + // Validation du format GGA + int numFields = 0; + for (char ch : nmeaMessage) { + if (ch == ',') numFields++; + } + if (numFields == 14) { // 14 virgules attendues dans un message GGA complet + ntrip_c.setLastGGA(nmeaMessage); // Stocker le dernier message GGA reçu + Serial.println("Extracted GGA: " + nmeaMessage); // Log du message GGA extrait + ggaFound = true; // Mettre à jour le drapeau pour arrêter la lecture + break; // Sortir de la boucle intérieure + } } + nmeaMessage = ""; + } else { + nmeaMessage += c; + } } + } + + // Send the last GGA message stored + String lastGGA = ntrip_c.getLastGGA(); + if (lastGGA != "") { + ntrip_c.sendGGA(lastGGA.c_str(), host, httpPort, user, passwd, mntpnt); + Serial.println("Sent GGA: " + lastGGA); // Log sent GGA message + lastGGA = ""; + //Serial.println("Cleaned GGA"); + } else { + Serial.println("No GGA message to send."); + } } + } - // NTRIP Data Handling - while (ntrip_c.available()) { - char ch = ntrip_c.read(); - MySerial.print(ch); + // NTRIP Data Handling + while (ntrip_c.available()) { + char ch = ntrip_c.read(); + MySerial.print(ch); + } + + long now = millis(); + if (now - lastMsg > timeInterval) { + lastMsg = now; + if (TRANSMITION_MODE == TCP_MQTT) { + + + if (!client_mqtt.connected()) { + Serial.println("Reconnect to Mqtt"); + reconnect(); + } + client_mqtt.loop(); } + // // While GNSS_SERIAL buffer is not empty + // while (MySerial.available()) + // // Read buffer + // gps.encode(MySerial.read()); + + // Dump GNSS time (UTC) + Serial.print(gps.time.hour()); + Serial.print(':'); + Serial.print(gps.time.minute()); + Serial.print(':'); + Serial.print(gps.time.second()); + Serial.print('.'); + Serial.print(gps.time.centisecond()); + Serial.print(" - LONG = "); + Serial.print(gps.location.lng(), 9); + Serial.print(" - LAT = "); + Serial.print(gps.location.lat(), 9); + Serial.print(" - COURSE = "); + Serial.print(gps.course.value()); + Serial.print(" - SATELLITES = "); + Serial.print(gps.satellites.value()); + Serial.print(" - FIX MODE = "); + Serial.print(gnssFixMode.value()); + Serial.println(); - long now = millis(); - if (now - lastMsg > timeInterval ) { - lastMsg = now; - if (!client.connected()) { - Serial.println("Reconnect to Mqtt"); - reconnect(); - } - client.loop(); - - // // While GNSS_SERIAL buffer is not empty - // while (MySerial.available()) - // // Read buffer - // gps.encode(MySerial.read()); - - // Dump GNSS time (UTC) - Serial.print(gps.time.hour()); - Serial.print(':'); - Serial.print(gps.time.minute()); - Serial.print(':'); - Serial.print(gps.time.second()); - Serial.print('.'); - Serial.print(gps.time.centisecond()); - Serial.print(" - LONG = "); - Serial.print(gps.location.lng(),9); - Serial.print(" - LAT = "); - Serial.print(gps.location.lat(),9); - Serial.print(" - COURSE = "); - Serial.print(gps.course.value()); - Serial.print(" - SATELLITES = "); - Serial.print(gps.satellites.value()); - Serial.print(" - FIX MODE = "); - Serial.print(gnssFixMode.value()); - Serial.println(); - - sensors.requestTemperatures(); // request temperature conversion for all sensors - for (int i = 0; i < deviceCount; i++) { - Serial.print("Sensor "); - Serial.print(i+1); - Serial.print(" : "); - temp = sensors.getTempCByIndex(i); // Get temperature(Celsius) for sensor#i - Serial.print(temp); - Serial.print(" °C"); - Serial.println(""); - } - Serial.println(""); - - String json = "{\"user\":\""+(String)mqtt_user+"\","; - json += "\"Temperature_Water\":\""+(String)sensors.getTempCByIndex(0)+"\","; - json += "\"Lon\":\""+String(gps.location.lng(), 9)+"\","; - json += "\"Lat\":\""+String(gps.location.lat(), 9)+"\","; - json += "\"FIXE\":\""+String(gnssFixMode.value())+"\"}"; - client.publish(mqtt_output, json.c_str() ); - client.disconnect(); - - Serial.println("Mqtt sent to : " + (String)mqtt_output ); - Serial.println(json); + sensors.requestTemperatures(); // request temperature conversion for all sensors + for (int i = 0; i < deviceCount; i++) { + Serial.print("Sensor "); + Serial.print(i + 1); + Serial.print(" : "); + temp = sensors.getTempCByIndex(i); // Get temperature(Celsius) for sensor#i + Serial.print(temp); + Serial.print(" °C"); + Serial.println(""); } + Serial.println(""); - WiFiClient client; // Declare the WiFiClient outside the switch - - while (MySerial.available()) { - String s = MySerial.readStringUntil('\n'); - switch (trans) { - case 0: //serial out - Serial.println(s); - break; - case 1: //udp out - udp.beginPacket(udpAddress, udpPort); - udp.print(s); - udp.endPacket(); - break; - case 2: //tcp client out - if (!client.connect(server, port)) { - Serial.println("connection failed"); - return; - } - client.println(s); - while (client.connected()) { - while (client.available()) { - char c = client.read(); - Serial.print(c); - } - } - client.stop(); - break; - case 3: //MySerial out - MySerial.println(s); - break; - default: //mauvaise config - Serial.println("mauvais choix ou oubli de configuration"); - break; + String json = "{\"user\":\"" + (String)mqtt_user + "\","; + json += "\"Temperature_Water\":\"" + (String)sensors.getTempCByIndex(0) + "\","; + json += "\"Lon\":\"" + String(gps.location.lng(), 9) + "\","; + json += "\"Lat\":\"" + String(gps.location.lat(), 9) + "\","; + json += "\"FIXE\":\"" + String(gnssFixMode.value()) + "\"}"; + if (TRANSMITION_MODE == TCP_MQTT) { + client_mqtt.publish(mqtt_output, json.c_str()); + client_mqtt.disconnect(); + + Serial.println("Mqtt sent to : " + (String)mqtt_output); + } + Serial.println(json); + } + + WiFiClient client_wifi; // Declare the WiFiClient outside the switch + + while (MySerial.available()) { + String s = MySerial.readStringUntil('\n'); + switch (TRANSMITION_MODE) { + case RS2323_SERIAL: //serial out + Serial.println(s); + break; + case UDP_SERVER: //udp out + udp.beginPacket(udpAddress, udpPort); + udp.print(s); + udp.endPacket(); + break; + case TCP_MQTT: //tcp client_wifi out + if (!client_wifi.connect(server, port)) { + Serial.println("connection failed"); + return; + } + client_wifi.println(s); + while (client_wifi.connected()) { + while (client_wifi.available()) { + char c = client_wifi.read(); + Serial.print(c); + } } + client_wifi.stop(); + break; + case RS2323_MYSERIAL: //MySerial out + MySerial.println(s); + break; + default: //mauvaise config + Serial.println("mauvais choix ou oubli de configuration"); + break; } + } - delay(1000); // to reduce busy-looping + delay(1000); // to reduce busy-looping } -void printAddress(DeviceAddress deviceAddress) { - for (uint8_t i = 0; i < 8; i++) { - Serial.print("0x"); - if (deviceAddress[i] < 0x10) Serial.print("0"); - Serial.print(deviceAddress[i], HEX); - if (i < 7) Serial.print(", "); - } - Serial.println(""); +void printAddress(DeviceAddress deviceAddress) { + for (uint8_t i = 0; i < 8; i++) { + Serial.print("0x"); + if (deviceAddress[i] < 0x10) Serial.print("0"); + Serial.print(deviceAddress[i], HEX); + if (i < 7) Serial.print(", "); + } + Serial.println(""); } void setup_wifi() { - delay(10); - // We start by connecting to a WiFi network - Serial.println(); - Serial.print("Connecting to "); - Serial.println(ssid); - - WiFi.begin(ssid, password); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); + delay(10); + // We start by connecting to a WiFi network + Serial.println(); + Serial.print("Connecting to "); + Serial.println(ssid); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); } void reconnect() { - // Loop until we're reconnected - while (!client.connected()) { - delay(100); - Serial.print("Attempting MQTT connection..."); - // Attempt to connect - if (client.connect(mqtt_UUID , mqtt_user, mqtt_password )) { - Serial.println("connected"); - // Subscribe - client.subscribe(mqtt_input); - } else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(3000); - } + // Loop until we're reconnected + while (!client_mqtt.connected()) { + delay(100); + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client_mqtt.connect(mqtt_UUID, mqtt_user, mqtt_password)) { + Serial.println("connected"); + // Subscribe + client_mqtt.subscribe(mqtt_input); + } else { + Serial.print("failed, rc="); + Serial.print(client_mqtt.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(3000); } + } } void callback(char* topic, byte* message, unsigned int length) { - Serial.print("Message arrived on topic: "); - Serial.print(topic); - Serial.print(". Message: "); - String messageTemp; - - for (int i = 0; i < length; i++) { - Serial.print((char)message[i]); - messageTemp += (char)message[i]; - } - Serial.println(); + Serial.print("Message arrived on topic: "); + Serial.print(topic); + Serial.print(". Message: "); + String messageTemp; + + for (int i = 0; i < length; i++) { + Serial.print((char)message[i]); + messageTemp += (char)message[i]; + } + Serial.println(); } diff --git a/unit_tests/7-Wifi-MQTT-RTK/Secret.h b/unit_tests/7-Wifi-MQTT-RTK/Secret.h index cc35bc4..c44491b 100644 --- a/unit_tests/7-Wifi-MQTT-RTK/Secret.h +++ b/unit_tests/7-Wifi-MQTT-RTK/Secret.h @@ -14,3 +14,9 @@ #define NTRIP_USER "YOUR NTRIP User" #define NTRIP_PASSWORD "YOUR NTRIP Password if neede" #define NTRIP_CASTER_MOUNTPOINT "YOUR NTRIP MOUNT POINT" + + +#define RS2323_SERIAL 0 +#define UDP_SERVER 1 +#define TCP_MQTT 2 +#define RS2323_MYSERIAL 3