ESP32-NMEA-Generator-Decoder-Mqtt

ESP32 NMEA Data Generator and Decoder

This project demonstrates how to use an ESP32 microcontroller to generate and decode NMEA (National Marine Electronics Association) data. The ESP32 connects to a WiFi network and communicates with a public MQTT broker to publish and subscribe to specific topics. The project includes generating random NMEA sentences, decoding them, and displaying the results via a web interface using Flask and MQTT.

Features

Components

Getting Started

Prerequisites

Installation

  1. ESP32 Setup:
    • Install the ESP32 board in Arduino IDE.
    • Install the necessary libraries in Arduino IDE:
      #include <WiFi.h>
      #include <PubSubClient.h>
      
  2. Flask and MQTT Setup:
    • Install Flask and paho-mqtt:
      pip install flask paho-mqtt
      

Usage

  1. ESP32 Code:
    • Upload the provided ESP32 code to your ESP32 development board.
    • Example snippet from main.cpp:
      void setup() {
          Serial.begin(115200);
          WiFi.begin(ssid, password);
          while (WiFi.status() != WL_CONNECTED) {
              delay(500);
              Serial.println("Connecting to WiFi..");
          }
          Serial.println("Connected to the Wi-Fi network");
      
          client.setServer(mqtt_broker, mqtt_port);
          client.setCallback(callback);
      
          while (!client.connected()) {
              String client_id = "esp32-client-";
              client_id += String(WiFi.macAddress());
              if (client.connect(client_id.c_str())) {
                  client.subscribe(topic_generate);
                  client.subscribe(topic_decode);
              } else {
                  delay(2000);
              }
          }
          client.publish(topic, "Hi, I'm ESP32 ^^");
      }
      
  2. Flask Server:
    • Run the Flask server:
      python app.py
      
    • Example snippet from app.py:
      @app.route('/generate', methods=['GET'])
      def generate_data():
          global last_response
          last_response = ""
          if mqtt_client_obj:
              result = mqtt_client_obj.publish(topic_generate, "")
              if result[0] == 0:
                  print("Message published successfully")
              else:
                  print("Failed to publish message")
      
              time.sleep(2)  # Ensure sufficient wait time for ESP32 response
      
              return jsonify({"data": last_response})
          else:
              return jsonify({"error": "MQTT client not connected"})
      
  3. Web Interface:
    • Access the web interface by navigating to http://<your_server_ip>:5000 in your web browser.
    • Example snippet from index.html:
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>ESP32 NMEA Data</title>
          <style>
              body { font-family: Arial, sans-serif; text-align: center; }
              .button { display: inline-block; padding: 10px 20px; font-size: 16px; cursor: pointer; }
              .button:hover { background-color: #ddd; }
          </style>
      </head>
      <body>
          <h1>ESP32 NMEA Data</h1>
          <button class="button" onclick="generateData()">Generate Sample Data</button>
          <pre id="output"></pre>
          <script>
              function generateData() {
                  fetch('/generate')
                      .then(response => response.json())
                      .then(data => {
                          document.getElementById('output').textContent = data.data;
                      });
              }
          </script>
      </body>
      </html>
      

Files

Example

After setting up, you can generate random NMEA sentences and see the decoded information on the web interface.

Contributing

If you would like to contribute to this project, please open an issue or submit a pull request.

Profile

Check out my GitHub profile: Esmail Sarhadi

View this project on GitHub: ESP32-NMEA-Generator-Decoder-Mqtt

Donation

If you find this project helpful, consider making a donation:

Crypto donation button by NOWPayments

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments