Hello, i'm connecting both GPS module and GSM module to arduino mega ports
the GSM is connected to Serial2 and the GPS is connected to the Software serial, both are working, but not fast enough.
Here is a brief explanation, i'm building a vehicle that is remotely controlled by a TCP server while you can observe it over a map.
The module connect ot the TCP server fine, GPS is sending coordinates, but when i want to move the vehicle i have to send the command 10 to 15 times to make it happen, i think its something to do with baudrate or delays... dunno what to do with the code to make it work fine.
Here is my code
//GPS SERIAL2 TCP And motor drive sketch
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#define motorpin0 53 // Direction ch1 Right
#define motorpin1 49 // Direction ch2 Left
#define speedpin0 5 //Speed Ch1
#define speedpin1 6 //Speed Ch2
#define FiveVolt 22 //Motor drive logic VCC
#define gnd 24 //Motor drive logic Ground
TinyGPS gps;
//SoftwareSerial gsm(12,13); //SERIAL2 Software serial
SoftwareSerial GPS(10,11); //GPS Software serial
int SPEED=100; //Motor speed intializing
int turn=50;
//byte count=0;
unsigned long start;
int time_to_go;
char data[1024];
int led = 13;
int onModulePin = 2; // the pin to switch on the module (without press on button)
int x = 0;
char command=0;
void switchModule(){
digitalWrite(onModulePin,HIGH);
delay(2000);
digitalWrite(onModulePin,LOW);}
void setup(){
Serial.begin(9600); // UART baud rate
Serial2.begin(9600);
GPS.begin(9600);
//the motor control wires are outputs
pinMode (motorpin0, OUTPUT);
pinMode(motorpin1, OUTPUT);
//PWM Pins are outputs
pinMode(speedpin0, OUTPUT);
pinMode(speedpin1, OUTPUT);
//Logic for motor drive
pinMode(FiveVolt, OUTPUT);
pinMode(gnd,OUTPUT);
delay(2000);
pinMode(led, OUTPUT);
pinMode(onModulePin, OUTPUT);
digitalWrite(FiveVolt,HIGH);
digitalWrite(gnd,LOW);
switchModule(); // switches the module ON
for (int i=0;i < 4;i++){
delay(5000);
}
delay(2000);
Serial2.println("AT+CIPSHUT"); //single connection
do{
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
Serial2.println("AT+CIPMUX=0"); //single connection
do{
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
Serial2.println("AT+CSTT=\"Etisalat\"");
do{
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
Serial2.println("AT+CIICR");
do{
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
Serial2.println("AT+CIFSR");
do{
while(Serial2.available()==0);
}while(Serial2.read()!='.');
do{
while(Serial2.available()==0);
}while(Serial2.read()!='\n');
delay(100);
Serial2.println("AT+CIPSTART=\"TCP\",\"41.36.54.103\",\"3030\"");
do{
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
do{ //waits for connection
while(Serial2.available()==0);
}while(Serial2.read()!='K');
delay(100);
}
void loop()
{
GPS.listen();
bool newData = false;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (GPS.available())
{
char c = GPS.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
float flat, flon;
if(newData)
{
//unsigned long age;
gps.f_get_position(&flat, &flon, NULL);
flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6;
flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;
}
Serial2.println("AT+CIPSEND"); //sending data
do{
while(Serial2.available()==0);
}while(Serial2.read()!='>');
delay(100);
Serial2.print("\n LAT=");
Serial2.print( flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6); //Print the latitude to the TCP server
Serial2.print("\n LON=");
Serial2.print( flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6); //Print the longitude to the TCP server
Serial2.write(0x1A); //EOL character, CTRL+Z Characters
Serial2.write(0x0D);
Serial2.write(0x0A);
while(Serial2.available())
{command = Serial2.read();
}
if (command== 'f')
{
//forward
analogWrite(speedpin0,SPEED);
analogWrite(speedpin1,SPEED);
digitalWrite(motorpin0,0);
digitalWrite(motorpin1,1);
delay(2000);
}
else if (command=='b') //backward
{
analogWrite(speedpin0,SPEED);
analogWrite(speedpin1,SPEED);
digitalWrite(motorpin0,1);
digitalWrite(motorpin1,0);
}
else if (command=='i')
{SPEED= SPEED + 20;
}
else if (command=='d') //decrease speed
{
SPEED= SPEED - 20;
// analogWrite(speedpin0,SPEED);
//analogWrite(speedpin1,SPEED);
}
else if (command=='r') //Turn right
{
analogWrite (speedpin0, 0);
analogWrite (speedpin1, 0);
delay(200);
//turn motors off then turn
analogWrite(speedpin0,20);
analogWrite(speedpin1,100);
digitalWrite(motorpin0,0);
digitalWrite(motorpin1,1);
delay(900);
analogWrite (speedpin0, 0);
analogWrite (speedpin1, 0);
}
else if (command=='l') //Turn left
{
analogWrite (speedpin0,0);
analogWrite (speedpin1,0);
delay(200);
//turn motors off then turn
analogWrite(speedpin1,20);
analogWrite(speedpin0,100);
digitalWrite(motorpin0,1);
digitalWrite(motorpin1,0);
delay(900);
analogWrite (speedpin0, 0);
analogWrite (speedpin1, 0);
}
else if (command=='s')
{
analogWrite (speedpin0, 0);
analogWrite (speedpin1, 0);
}
}