Hello Everyone!
I hope you all are doing well, I am stuck on a problem in my code ( attached below ). I am using a thingspeak server to receive data, All other aspects are working fine, but I can't seem to make the GPS module work ( I am using ublox Neo 6m ). I receive 0.00 for both longitude and latitude values on the thingspeak Server, but the Gps Module seems to work fine as I see the Led Indicator. In my opinion, there is a problem with the if-else statement, but I Can't Figure it out. Please Help me to get it to work!
#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(2,3);
#include "EmonLib.h"
#include <String.h>
#include <DHT.h>
#include <TinyGPSPlus.h>
int Rtxp = 9;
int Txp = 8;
TinyGPSPlus Gps;
SoftwareSerial ss(Rtxp,Txp);
#define DHTPIN A0
EnergyMonitor emon1;
DHT dht(DHTPIN, DHT11);
float Lati;
float Longi;
float h;
float t;
double Irms;
void setup()
{
gprsSerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
dht.begin();
emon1.current(1, 111.1);
delay(1000);
}
void loop()
{
if (ss.available()>0 && Gps.location.isValid() ){
Lati = Gps.location.lat();
Longi =Gps.location.lng();
h = dht.readHumidity();
t = dht.readTemperature();
Irms = emon1.calcIrms(1480);
delay(100);
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
Serial.println(Irms);
Serial.println("Ampere");
}
else {
Lati = 0.00;
Longi =0.00;
h = dht.readHumidity();
t = dht.readTemperature();
Irms = emon1.calcIrms(1480);
delay(100);
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
Serial.println(Irms);
Serial.println("Ampere");
}
if (gprsSerial.available())
Serial.write(gprsSerial.read());
gprsSerial.println("AT");
delay(1000);
gprsSerial.println("AT+CPIN?");
delay(1000);
gprsSerial.println("AT+CREG?");
delay(1000);
gprsSerial.println("AT+CGATT?");
delay(1000);
gprsSerial.println("AT+CIPSHUT");
delay(1000);
gprsSerial.println("AT+CIPSTATUS");
delay(2000);
gprsSerial.println("AT+CIPMUX=0");
delay(2000);
ShowSerialData();
gprsSerial.println("AT+CSTT=\"ufone.pinternet\"");//start task and setting the APN,
delay(1000);
ShowSerialData();
gprsSerial.println("AT+CIICR");//bring up wireless connection
delay(3000);
ShowSerialData();
gprsSerial.println("AT+CIFSR");//get local IP adress
delay(2000);
ShowSerialData();
gprsSerial.println("AT+CIPSPRT=0");
delay(3000);
ShowSerialData();
gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
delay(6000);
ShowSerialData();
gprsSerial.println("AT+CIPSEND");//begin send data to remote server
delay(4000);
ShowSerialData();
String str="GET https://api.thingspeak.com/update?api_key=MG7I0XOD86XQEPXJ&field1=" + String(t) +"&field2="+String(h)+"&field3=" + String(Irms) + "&field4=" + String(Lati) +"&field5=" + String(Longi);
Serial.println(str);
gprsSerial.println(str);//begin send data to remote server
delay(4000);
ShowSerialData();
gprsSerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
gprsSerial.println();
ShowSerialData();
gprsSerial.println("AT+CIPSHUT");//close the connection
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(gprsSerial.available()!=0)
Serial.write(gprsSerial.read());
delay(5000);
}