0

I am working on IoT based project and I want to control the device using mobile App. Everything works fine, I can switch the load and get the status back on mobile app, but I want to use my ESP8266 as a GPS using google API. I have googling many many times but people work with standalone ESP8266 as a GPS using google API. Here there are the two github project for Standalone ESP8266 as GPS:

  1. Google GeoLocation API wrapper for Arduino MKR1000, ESP8266 and ESP32
  2. Google Geolocation API for fetching location on ESP8266 board without using any GPS hardware

My Question is: how can I use my ESP8266 for IoT application (Controlling the Arduino pins) as well as GPS module using Google API?

In my case, I need to use an Arduino Mega/Uno because much more sensors were used in this project which I not mentioned here.

I attached my code, its working perfect, means I controlled the Arduino pins and Arduino send the status using ESP8266 on my mobile App.

String server = ""; //variable for sending data to webpage
boolean No_IP = false; //variable to check for ip Address
String IP = ""; //variable to store ip Address
char data;
char values = 'c';
int flag = 1;

 void setup() 
{
Serial.begin(9600);
Serial1.begin(9600);
wifi_init();
delay(200);
showIP();

}

    void loop()
{
  delay(50);
  String temp = "";
while(Serial1.available()) // Check the Esp Serial
{
  temp = Serial1.readString(); // Read the String from Esp Serial
  Serial.println(temp);
  delay(50);
}
if ((temp.substring(11) == "a" or temp.substring(11) == "A") and flag == 1) // If "a" or "A" received
{
  digitalWrite(13,HIGH); // Load conected on pin 13 ON
  Serial.println("Char received High");
  flag = 0;
  sendData("Light On");
  delay(1000);
}

else if ((temp.substring(11) == "a" or temp.substring(11) == "A") and flag == 0)
{
  digitalWrite(13,LOW); // Load conected on pin 13 OFF
  Serial.println("Char received Low");
  flag = 1;
  sendData("Light Off");
  delay(1000);
}
else
{

}

delay(200);

} // Void loop End

These are the functions which is used to established the connection.

Note: I used the ESP 8266 in Station mode Check in void wifi_init() function, `

void findIp(int time1) //check for the availability of IP Address
{
  int time2 = millis();
  while (time2 + time1 > millis())  {
    while (Serial1.available() > 0)    {
      if (Serial1.find("IP has been read"))      {
        No_IP = true;
      }
    }
  }
}

void showIP()//Display the IP Address
{
  IP = "";
  char ch = 0;
  while (1)
  {
    Serial1.println("AT+CIFSR");
    while (Serial1.available() > 0)
    {
      if (Serial1.find("STAIP,"))
      {
        delay(1000);
        Serial.print("IP Address:");
        while (Serial1.available() > 0)
        {
          ch = Serial1.read();
          if (ch == '+')
            break;
          IP += ch;
        }
      }
      if (ch == '+')
        break;
    }
    if (ch == '+')
      break;
    delay(1000);
  }
  Serial.print(IP);
  Serial.print("Port:");
  Serial.println(80);
}

void establishConnection(String Serial1and, int timeOut) //Define the process for sending AT Serial1ands to module
{
  int q = 0;
  while (1)
  {
    Serial.println(Serial1and);
    Serial1.println(Serial1and);
    while (Serial1.available())
    {
      if (Serial1.find("OK"))
        q = 8;
    }
    delay(timeOut);
    if (q > 5)
      break;
    q++;
  }
  if (q == 8)
    Serial.println("OK");
  else
    Serial.println("Error");
}

void wifi_init() //send AT Serial1ands to module
{

  establishConnection("AT", 100);

  establishConnection("AT+CWMODE=1", 100);
  establishConnection("AT+CWQAP", 100);
  findIp(5000);
  if (!No_IP)
  {
    Serial.println("Connecting Wifi....");
    establishConnection("AT+CWJAP=\"SSID\",\"PASSWORD\"", 7000);        //provide your WiFi username and password here
  }
  else
  {
  }
  Serial.println("Wifi Connected");
  // showIP();
  establishConnection("AT+CIPMUX=1", 5000);
  establishConnection("AT+CIPSERVER=1,80", 7000);
}

void sendData(String server1)//send data to module
{
  int p = 0;
  while (1)
  {
    unsigned int l = server1.length();
    Serial.print("AT+CIPSEND=0,");
    Serial1.print("AT+CIPSEND=0,");
    Serial.println(l + 2);
    Serial1.println(l + 2);
    delay(100);
    Serial.println(server1);
    Serial1.println(server1);
    while (Serial1.available())
    {
      Serial.print(Serial.read());
      if (Serial1.find("OK"))
      {
        p = 11;
        break;
      }
    }
    if (p == 11)
      break;
    delay(100);
  }
}

Is it possible to use ESP8266 for IoT applications as well as GPS module using google API?

  • @Juraj problem is that How to GPS values (long and lat) get using google API?. because in the above I mention, #include "ESP8266WiFi.h" used in first link and others, and in the second link #include <WifiLocation.h> is used. Both are not compatable with Arduino mega. these libraries are used with NodeMCU and ESP8266 standalone..... – Abdul Basit Jan 8 at 10:09
  • sorry. you use AT commands. hard to see in you snippets for what platform the code is. then start to code for the esp8266. put the networking part and location in the esp8266 and exchange data with Mega over Serial – Juraj Jan 8 at 10:14
  • @Juraj I can't understand your comment, have you any solution, so please can you share code snippet on answer?. that how ESP 8266 works the above functionality and also getting the GPS location from google API and then exchange with Arduino with Serial communication ? – Abdul Basit Jan 8 at 10:51
  • the google api uses https, so SSL encryption. I don't know about an AVR + esp8266 solution for that with firmware in esp8266. you must write your own sketch for the esp8266 to do the location with google api – Juraj Jan 8 at 12:21
  • @Juraj I think you guide me on right path, I still trying multiple ways, and let you know......Thanks – Abdul Basit Jan 8 at 12:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.