#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
String coordLt;
String coordLn;
String vel;
String alt;
String presc;
String sat;
String tiempo;
//subprograma que lee y pone bonito el $GPGGA
void leerGPS(String linea) {
int comaInicio = 0;
int posicion = 0;
for (int i = 0; i < linea.length(); i++) {
if (linea.charAt(i) == ',') {
String dato = linea.substring(comaInicio, i);
// Asignar los datos según su posición
switch (posicion) {
case 1: tiempo = dato; break;
case 2: coordLt = dato; break;
case 3: coordLt += "," + dato; break;
case 4: coordLn = dato; break;
case 5: coordLn += "," + dato; break;
case 7: sat = dato; break;
case 8: presc = dato; break;
case 9: alt = dato; break;
}
comaInicio = i + 1;
posicion++;
}
}
}
// constantes del MQTT
// direccion broker, puerto, y nombre cliente
const char *MQTT_BROKER_ADRESS = "test.mosquitto.org";
const uint16_t MQTT_PORT = 1883;
const char *MQTT_CLIENT_NAME = "Pablo_GPS";
// instanciar objetos
WiFiClient espClient;
PubSubClient mqttClient(espClient);
void ConnectWiFi()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void InitMqtt()
{
mqttClient.setServer(MQTT_BROKER_ADRESS, MQTT_PORT);
}
void ConnectMqtt()
{
while (!mqttClient.connected())
{
Serial.print("Starting MQTT connection...");
if (mqttClient.connect(MQTT_CLIENT_NAME))
{
Serial.print("Client connected");
}
else
{
Serial.print("Failed MQTT connection, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void HandleMqtt()
{
if (!mqttClient.connected())
{
ConnectMqtt();
}
mqttClient.loop();
}
//publica en X topic el dato correspondiente
void PublishMqtt(String topic, String data)
{
mqttClient.publish(topic.c_str(), data.c_str());
}
void setup(void)
{
Serial.begin(115200);
ConnectWiFi();
InitMqtt();
//parte que saca por pantalla el $GPGGA que le das
String ejemplo = "$GPGGA,110827.00,41XX.32485,N,00831.79799,W,1,07,0.99,123.1,M,50.1,M,,*48";
leerGPS(ejemplo);
//aqui en vez de ejemplo hay que meterle el $GPGGA de cada momento
//creo que deberia hacer un timer de 1s y una interrupcion que se lanza cada ese segundo para adquirir los datos
Serial.println("--------------------------");
Serial.println("Hora: " + tiempo + " XX h-XX m-XX s---UTC");
Serial.println("Latitud: " + coordLt);
Serial.println("Longitud: " + coordLn);
Serial.println("Sats: " + sat);
Serial.println("Pres: " + presc);
Serial.println("Altura: " + alt);
Serial.flush();
}
void loop()
{
HandleMqtt();
PublishMqtt("PabloGPS/hora", tiempo);
PublishMqtt("PabloGPS/latitud", coordLt);
PublishMqtt("PabloGPS/longitud", coordLn);
PublishMqtt("PabloGPS/satelites", sat);
PublishMqtt("PabloGPS/precision", presc);
PublishMqtt("PabloGPS/altura", alt);
delay(1000); // envía los datos cada segundo
}