Get Informed out of Data

Full width home advertisement



Post Page Advertisement [Top]

ESP8266 - NodeRed - GPS(Neo7m) - Google maps on Raspberry pi 4

ESP8266 - NodeRed - GPS(Neo7m) - Google maps on Raspberry pi 4
This is a continuation tutorial of using Node-Red for GPS tracking, kindly see previous two tutorials to follow up.

In the previous artcile, we are upto the course of running MQTT client on ESP32 and sending data to the raspberry pi.

ESP32 with Neo 7M will be connected through 3.3v and GND, for TX and RX,the ESP32 D2 and D1 pins are used.

After that, upload the following code to the ESP32 with your node red hosted Raspberry pi IP or domain name.

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

//TODO: ESP32 MQTT user config
const char* ssid = "mohan"; // Wifi SSID
const char* password = "123456789"; // Wifi Password
const char* username = "new"; // my AskSensors username
const char* pubTopic = "publish/gps"; // publish/username/apiKeyIn
const unsigned int writeInterval = 2500; // write interval (in ms)
static const int RXPin = D1, TXPin = D2;
static const uint32_t GPSBaud = 9600;
//AskSensors MQTT config
const char* mqtt_server = "";
unsigned int mqtt_port = 1883;

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

// setup
void setup() {
Serial.println("********** Program Start : ESP32 publishes NEO-6M GPS position to raspberry piover MQTT");
Serial.print("********** connecting to WIFI : ");

WiFi.begin(ssid, password);

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

client.setServer(mqtt_server, mqtt_port);
// 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());
double spd =(gps.speed.kmph());
Serial.println("********** Publish MQTT data to raspberry pi");
char mqtt_payload[50] = "";
snprintf (mqtt_payload, 50, "%lf,%lf,%lf", latitude,longitude,spd);
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("ESP32Client", username, "")) {
Serial.println("-> MQTT client connected");
} else {
Serial.print("failed, rc=");
Serial.println("-> try again in 5 seconds");
// Wait 5 seconds before retrying

The following code will get the GPS co-ordinates and send them to the raspberry pi via MQTT.
please replace, Wifi SSID and password for your device.

Now, coming on to the Node-Red, we need to place the MQTT INnode with the same topic name which is mentioned in the ESP32 program. Here, publish/GPS is used as Topic.
Then, a function to split the values and assign it to the payload which is the format required for the nodered/worldmap.

The following code will be uploaded to the function node:
var test=msg.payload
var [a,b,c]=test.split(",")
var locate={"name":"esp32","lat":a,"lon":b,"speed":c}
return msg;

these lines will format latitude longitude values for the worldmap.

Now,we need to change the map layer. By default, serveral Openstreet map layer are found on the nodered/worldmap.

But, most places are not found on them when the locality becomes too rural. Hence, we are going to add google maps unofficially.

For doing this, Open terminal,

get into the /.node-red/nodemodules/nodered-contrib-worldmap/worldmap/

then edit, wordlmap.js file,

Find the OpenStreetmap url in the file and replace it with,

  • Google Road:{x}&y={y}&z={z}&s=Ga
  • Google Satellite:{x}&y={y}&z={z}&s=Ga
Now, you could reload the page and see the new google map with all the markings.

No comments:

Post a Comment

Bottom Ad [Post Page]