Ahsan Masood
Published

Capturing & Visualizing GPS Data Using ESP8266 & Ubidots

Collect location context with ease along with sensor data from your various sensor nodes.

IntermediateProtip3,488
Capturing & Visualizing GPS Data Using ESP8266 & Ubidots

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
You could use any variant of ESP8266 boards
×1
NEO-6M GPS module
Tested ESP8266 with NEO-6M GPS module
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
Optional
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

neo6m-1024x542_qBrPbbTcJJ.jpg

Code

Untitled file

C/C++
#include "UbidotsMicroESP8266.h"
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define TOKEN  "Your_token_here"  // Put here your Ubidots TOKEN
#define WIFISSID "Your_WiFi_SSID" // Put here your Wi-Fi SSID
#define PASSWORD "Your_WiFi_Password" // Put here your Wi-Fi password

Ubidots client(TOKEN);

static const int RXPin = D1, TXPin = D2;
static const uint32_t GPSBaud = 9600; // Change according to your device

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

double _lat, _lng;
int dummySensorValue = 22;
char context[25];

unsigned long previousMillis = 0;     // last time data was send
const long interval = 5000;           // data transfer interval 

void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);
  
  client.wifiConnection(WIFISSID, PASSWORD);
}

void loop()
{ 
  while (ss.available() > 0) {
    if (gps.encode(ss.read())) {
      unsigned long currentMillis = millis();
  
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        
        if (gps.location.isValid())
        {
          _lat = gps.location.lat();
          _lng = gps.location.lng();
          sprintf(context, "lat=%.2f$lng=%.2f", _lat, _lng);
  
          client.add("Dummy Sensor", dummySensorValue, context);
          client.sendAll(true);
          
          Serial.print(F("Location: ")); 
          Serial.print(_lat);
          Serial.print(F(","));
          Serial.print(_lng);
        }
        else
        {
          Serial.print(F("INVALID"));
        }
        
        Serial.println();
      }
    }
  }

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

Credits

Ahsan Masood

Ahsan Masood

1 project • 2 followers

Comments