I’m into a project that uses GPS module NEO-6M and ESP8266_01. The projects takes gps data and send them to thingspeak.
Independently, the arduino is working well with the two modules but when i tries to combine the two code, it fails to work. Instead of the arduino sending the latitude to cloud it keeps sending 0.
I’m using software serial to communicate with both the neo6m and the esp.
The full code is given below
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#define RX 2
#define TX 3
int RXPin = 4;
int TXPin = 5;
double getSensorData();
void sendCommand(String command, int maxTime, char readReplay[]);
int GPSBaud = 9600;
TinyGPSPlus gps;
String AP = "SAMUEL"; // AP NAME
String PASS = "adedayo2020"; // AP PASSWORD
String API = "PTHARH9GLC0R1IJM"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
double valSensor = 1;
SoftwareSerial esp8266(RX,TX);
SoftwareSerial gpsSerial(RXPin, TXPin);
void setup() {
Serial.begin(9600);
gpsSerial.begin(GPSBaud);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
valSensor = getSensorData();
String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
esp8266.listen();
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
double getSensorData(){
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
if (gps.location.isValid())
{
return gps.location.lat();
}
// If 5000 milliseconds pass and there are no characters coming in
// over the software serial port, show a "No GPS detected" error
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while(true);
}
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
I come to discover that i can’t use two software serials in the same program so i decided to read the NMEA data of the GPS using the 0 and 1 pins. the code is given below but he data is not flowing whereas it worked well when i use software serial
String a;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
while(Serial.available()) {
a= Serial.readString();// read the incoming data as string
Serial.println(a);
}
}
Is there a way i can get around the problem since i have to use the two modules