Post topics, source code that relate to the Arduino Platform

User avatar
By fayzanhr
#72914 Hey,

So i've been looking for a way to do this, but I can't seem to find it. I'm basically constructing a user-following car, where the user has a GPS, and so does the car. Taking both GPS datas and using some arithmetic, I plan for the car to follow the user, or at least get the car to the users position quickly.

Continuing, I've been able to connect a GPS and an ESP8266 to my Arduino Mega, and I've done the same with another Mega, however it is without a GPS (haven't bought it yet). My idea is that I use an ESP8266 Thing as an AP, and use the first mega to send data to a webpage on my local AP via an ESP8266, and then receive that GPS data from the AP into my other Mega. I would like my data to be transferred between the Arduinos constantly - new data transferred between the two every 3 seconds. Finally, I want the location of both devices on to LCDs that will be connected to each arduino - this is not necessary but would be a good add to the project.

Could someone please point me in the right direction for this? I've been looking online for a couple of days now, and can't seem to get much of an answer from google and other forums.

TL;DR - I need to transfer GPS data coming onto the serial monitor from one arduino to another, continuously and wirelessly, without the use of the internet. Preferred mode is using two WiFi modules as clients, and one as AP server.

N.B. I need a certain amount of range that's why I'm using WiFi, instead of bluetooth, however, I will consider it if all else fails (can not use internet at all).

I'm using:
1. Arduino Mega 2560:-
ESP8266-01 https://www.sparkfun.com/products/13678
2. PMOD GPS Receiver:-
http://store.digilentinc.com/pmod-gps-gps-receiver/
3. 3.2' TFTLCD SHIELD HX8357c:-
https://www.ebay.co.uk/p/3-2-Inch-IPS-T ... 3007842482
4. Sparkfun ESP8266 Thing as AP


This is my current code to get GPS data and WiFi connection.

Code: Select all
/* 
  Connect to WiFi using ESP8266-01 and PMOD GPS, and display on LCD
  Working - 3/01/2018
 

 */

#include <TFT_HX8357.h> // Hardware-specific library
#include "ESP8266.h"
#include "PmodGPS.h"

TFT_HX8357 tft = TFT_HX8357();       // Invoke custom library
TFT_HX8357 myGLCD = TFT_HX8357();       // Invoke custom library

//GPS Connections
#define _3DFpin   7 //pin 40, JF-01
#define _1PPSpin  6 //pin43, JF-04
//#define RSTpin    37 //pin 37, JE-08

//Define colors
#define TFT_GREY 0x5AEB
#define ST7735_BLACK   0x0000
#define ST7735_GRAY    0x8410
#define ST7735_WHITE   0xFFFF
#define ST7735_RED     0xF800
#define ST7735_ORANGE  0xFA60
#define ST7735_YELLOW  0xFFE0 
#define ST7735_LIME    0x07FF
#define ST7735_GREEN   0x07E0
#define ST7735_CYAN    0x07FF
#define ST7735_AQUA    0x04FF
#define ST7735_BLUE    0x001F
#define ST7735_MAGENTA 0xF81F
#define ST7735_PINK    0xF8FF

//Define SSID, Password
#define SSID        "HR"
#define PASSWORD    "3215101791"

ESP8266 wifi(Serial1);

typedef enum{
  RESTART,
  NOTFIXED,
  FIXED
}STATE;


GPS myGPS;
char* LAT;
char* LONG;
NMEA mode;

STATE state=RESTART;

void setup(void) {
  tft.init();
  tft.setRotation(2);
 
  Serial.begin(115200);   
  Serial1.begin(115200);
  Serial2.begin(9600);
  myGPS.GPSinit(Serial2, 9600, _3DFpin, _1PPSpin);
  Serial.print("setup begin\r\n");

  Serial.print("FW Version: ");
  Serial.println(wifi.getVersion().c_str());
   if (wifi.setOprToStation()) {
       Serial.print("to station ok\r\n");
   } else {
       Serial.print("to station err\r\n");
   }

   if (wifi.joinAP(SSID, PASSWORD)) {
       Serial.print("Join AP success\r\n");
       Serial.print("IP: ");       
       Serial.println(wifi.getLocalIP().c_str());
   } else {
       Serial.print("Join AP failure\r\n");
   }
   
   Serial.print("setup end\r\n");

    // Fill screen with random colour so we can see the effect of printing with and without
  // a background colour defined
  tft.fillScreen(0x0000);
 
  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0, 2);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE);  tft.setTextSize(2);
  // We can now plot text on screen using the "print" class
 tft.println("GPS Connection");
 tft.println("Loading...");
  tft.println(wifi.getVersion().c_str());
   if (wifi.joinAP(SSID, PASSWORD)) {
       tft.print("IP: ");       
       tft.println(wifi.getLocalIP().c_str());
   } else {
       tft.print("Unsuccessfull\r\n");
   }
   delay(2000);

 


}

void loop() {
  // Fill screen with random colour so we can see the effect of printing with and without
  // a background colour defined
  tft.fillScreen((0x0000));
 
  // Set "cursor" at top left corner of display (0,0) and select font 2
  // (cursor will move to next line automatically during printing with 'tft.println'
  //  or stay on the line is there is room for the text with tft.print)
  tft.setCursor(0, 0, 2);
  // Set the font colour to be white with a black background, set text size multiplier to 1
  tft.setTextColor(TFT_WHITE,TFT_BLACK);  tft.setTextSize(2);
  // We can now plot text on screen using the "print" class
 

    //State machine for GPS
  switch (state)
  {
    tft.println("GPS Connection");
    case(RESTART):
        state=NOTFIXED;
        break;
    case(NOTFIXED)://Look for satellites, display how many the GPS is connected to
      mode = myGPS.getData(Serial2);//Receive data from GPS
      if (mode == GGA){//If GGAdata was received
        tft.println("GPS Connection");
        tft.println("Connected to:");
        tft.println(SSID);
        Serial.print("Number of Satellites: ");Serial.println(myGPS.getNumSats());
        tft.println("Number of Satellites: "); tft.println(myGPS.getNumSats());\

        delay(3000);
        if (myGPS.isFixed()){//When it is fixed, continue
          state=FIXED;

         

       
        }
      }
        break;
    case(FIXED):
        if(myGPS.isFixed()){//Update data while there is a position fix
          mode = myGPS.getData(Serial2);
          if (mode == GGA)//If GGAdata was received
          {
              Serial.print("Latitude: ");Serial.println(myGPS.getLatitude());
              Serial.print("Longitude: ");Serial.println(myGPS.getLongitude());
              Serial.print("Altitude: ");Serial.println(myGPS.getAltitudeString());
              Serial.print("Number of Satellites: ");Serial.println(myGPS.getNumSats());
              Serial.print("\n");
             
              tft.print("Latitude: ");tft.println(myGPS.getLatitude());
              tft.print("Longitude: ");tft.println(myGPS.getLongitude());
              tft.print("Altitude: ");tft.println(myGPS.getAltitudeString());
              tft.print("Number of Satellites: ");tft.println(myGPS.getNumSats());
              tft.print("\n");
          }
         
        }
        else {
          state=RESTART;//If PFI = 0, re-enter connecting state
        }
      break;
     
  }
 
 
}



High Regards