Get Informed out of Data

Full width home advertisement



Post Page Advertisement [Top]

ESP8266 - NEO 7m GPS and Node-Red through MQTT in Raspberry pi

ESP8266 - NEO 7m GPS and Node-Red through MQTT in Raspberry pi
As mentioned before, this article will be based on previous basic tutorial on setting for Node-red server for GPS tracking.

What needs to be done?

ESP8266 - a microcontroller with Wifi and GPIO on board, it can be more than that with respect to UART and serial communication.

But, we will make ESP8266 coupled with Neo 7m GPS as a tracker object in the client side and Raspberry pi 4 which will host Node-Red acting as a web-server.

For communication between the ESP8266 and Raspberry pi, the MQTT protocol will be used. To know more about MQTT -see here. 


Generally, most products will be choosen based on ease of use in DIY projects which leads to some trouble in practical usage.

But, here we opt for ESP8266 than a raspberry pi zero because of not only cost but also the power usage, Raspberry pi zero will be working on 5v or even more with latest raspbian OS.because raspberry pi 4 GPIO's gives around 12V output now-a-days too.

ESP8266 will be working on 3.3v and also micocontrollers have less processing load will results in low ampere rating. Hence, Power consumption is low.

Since, it is just for GPS, Neo 7M costs around Rs.800 with ESP8266 it will covered under Rs.1200 on the client side.

How to do?

With respect to previous content, we need to install, Node-Red and MQTT server on raspberry pi to receive GPS co-ordinate values.

To get it run, 

Node-Red Installation:
  • bash <(curl -sL
MQTT Installation:
  • sudo apt install -y mosquitto mosquitto-clients
  • sudo systemctl enable mosquitto.service
here, Mosquitto MQTT is preferred since its open source.

Now, Log into Node-Red and pic the MQTT IN node from the Pallete and connect it with debug. This ends the initial stage config of Node-Red.

Default port of Mosquitto MQTT is 1883, run the local server, if needed for security we can use username and password.

Please note down the topic on MQTT IN which you prefer using, as this has to be matched in ESP8266.

Will show the process in Pics,

The Next step is to making the ESP8266 ready for MQTT Server. To do it, we need Arduino IDE, GPS to be connectred to ESP8266. which will be continues in the next part.

For now, here i will be sharing the ESP8266 code for you.

* ESP8266 with ublox gps sending mqtt publish lattitude in json format

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

//WIFI Settings
const char* ssid = "xxxxxx"; // Wifi SSID
const char* password = "xxxxx"; // Wifi Password

const unsigned int writeInterval = 25000; // write interval (in ms)
//ESP8266 PINOUT RX and TX
static const int RXPin = D2, TXPin = D1;
// GPS-Module baud setting
static const uint32_t GPSBaud = 9600;
//MQTT server config
const char* mqttServer = "raspi-ip or raspi hosted domain name";
const int mqttPort = 1883;
const char* mqttUser = "xxxx";
const char* mqttPassword = "xxxxxx";
const char* pubTopic = "publish/gps";

// objects
WiFiClient espClient;
PubSubClient client(espClient);
TinyGPSPlus gps; // The TinyGPS++ object
SoftwareSerial ss(RXPin, TXPin); // The serial connection to the GPS device

// setup
void setup() {
Serial.println("Program Start : ESP8266 publishes GPS position over MQTT");
Serial.print("Connected to the WiFi network");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to WiFi.");
Serial.println("->WiFi connected");
Serial.println("->IP address: ");

client.setServer(mqttServer, mqttPort);
// GPS baud rate

// loop
void loop() {

if (!client.connected())
// This sketch displays information every time a new sentence is correctly encoded.
while (ss.available() > 0)
if (gps.encode(

if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS detected: check wiring."));

// GPS displayInfo
void displayInfo() {

if (gps.location.isValid()) {
double latitude = (;
double longitude = (gps.location.lng());

Serial.println("********** Publish MQTT data");
char mqtt_payload[55] = "";
snprintf (mqtt_payload, 55, "{\"latitude\": %lf,\"longitude\": %lf}", latitude, longitude);
Serial.print("Publish message: ");
client.publish(pubTopic, mqtt_payload);
Serial.println("> MQTT data published");
Serial.println("********** End ");

delay(writeInterval);// delay
} else {


//MQTT callback
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print("] ");
for (int i = 0; i < length; i++) {
//MQTT reconnect
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("********** Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("-> MQTT client connected");
} else {
Serial.print("failed, rc=");
Serial.println("-> try again in 5 seconds");
// Wait 5 seconds before retrying

No comments:

Post a Comment

Bottom Ad [Post Page]