Hello I am making a gps device with esp8266 for web. when i try my code with localhost it works smoothly but when i try with my hosted site my gps device doesnt works,, gps device is GPS NEO 6M. it doest return any think... not even finds satelight,, please help me how to solve that!!
Here is the Hosted site code which doesnt works..
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
TinyGPSPlus gps;
SoftwareSerial SerialGPS(4, 5);
const char* ssid = "********";
const char* password = "********";
float Latitude , Longitude;
int id = 1;
String LatitudeString="0", LongitudeString="0";
WiFiServer server(80);
void setup()
{
Serial.begin(9600);
SerialGPS.begin(9600);
Serial.println();
Serial.print("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop()
{
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
Latitude = gps.location.lat();
LatitudeString = String(Latitude , 6);
Longitude = gps.location.lng();
LongitudeString = String(Longitude , 6);
Serial.println(LongitudeString);
}
}
WiFiClient client;
HTTPClient http;
String serverPath = "http://******.org/api/locationUpdate/" + String(id) + "/" + LongitudeString + "/" + LatitudeString + "/3";
http.begin(client, serverPath);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.println(LongitudeString);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(200);
}
This one on localhost works very well..
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
TinyGPSPlus gps;
SoftwareSerial SerialGPS(4, 5);
const char* ssid = "********";
const char* password = "********";
float Latitude , Longitude;
int year , month , date, hour , minute , second, id = 1;
String DateString , TimeString , LatitudeString="0", LongitudeString="0";
const char* serverName = "http://192.168.43.254/lut2ms/sensor-data.php";
String apiKeyValue = "xxxxx";
WiFiServer server(80);
void setup()
{
Serial.begin(9600);
SerialGPS.begin(9600);
Serial.println();
Serial.print("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}
void loop()
{
while (SerialGPS.available() > 0)
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
Latitude = gps.location.lat();
LatitudeString = String(Latitude , 6);
Longitude = gps.location.lng();
LongitudeString = String(Longitude , 6);
}
if (gps.date.isValid())
{
DateString = "";
date = gps.date.day();
month = gps.date.month();
year = gps.date.year();
if (date < 10)
DateString = '0';
DateString += String(date);
DateString += " / ";
if (month < 10)
DateString += '0';
DateString += String(month);
DateString += " / ";
if (year < 10)
DateString += '0';
DateString += String(year);
}
if (gps.time.isValid())
{
TimeString = "";
hour = gps.time.hour()+ 5; //adjust UTC
minute = gps.time.minute();
second = gps.time.second();
if (hour < 10)
TimeString = '0';
TimeString += String(hour);
TimeString += " : ";
if (minute < 10)
TimeString += '0';
TimeString += String(minute);
TimeString += " : ";
if (second < 10)
TimeString += '0';
TimeString += String(second);
}
}
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&id=" + id
+ "&latitude=" + LatitudeString + "&longitude=" + LongitudeString
+ "&date=" + DateString + "&time=" + TimeString + "";
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(200);
}
Please help me solve this!! Do I need to add someting to the code to use it with hosted website or its similar as localhost?