Hi all,
I am Prashant.
I am creating a GPS tracker with the following things
1) Arduino Uno (ATmega328P)
2) GPS Module (BN-220)
3) GSM Module (SIM-800L)
GPS module will give me location details and the GSM module will send that location details to the server using a GPRS HTTP connection.
When I am connecting both modules separately to Arduino they work as expected.
But wiring them together does not yield the expected result.
PROBLEM: SoftwareSerial can be used by only one module. i.e. either GPS module or GSM module can use it.
Because of this, I am not able to carry out synchronization with GPS and GSM.
I tried to read GPS Serial data first and then switch to GSM Serial to write data, but it still did not work.
How can I solve this problem ?
@prashantgaykar
How about using an Arduino MEGA, they have multiple serial ports.
Anything seems possible when you don't know what you're talking about.
PROBLEM: SoftwareSerial can be used by only one module. i.e. either GPS module or GSM module can use it.
I don't think your statement is correct.
Connect your GPS serial and GSM serial to different Arduino pins and try something like the follow pin connection (you can change the actual pins) and create to different serial connections
SoftwareSerial myGPSserial(2,3)
SoftwareSerial myGSMserial(4,5)
Then use myGPSserial or myGSMserial as appropriate in your code
I am using different pins for both GPS and GSM as shown by you, but when I run the program only one module seems to work.
On top of that for GPS I am using the AltSoftSerial library. Still no luck.
Please find below code for the same. Let me know if there is something wrong with this code.
#include SoftwareSerial.h #include AltSoftSerial.h #include Arduino.h #define CLIENT_KEY "ABC123" #define SERVER_URL "http://google.com" #define BAUD_RATE 9600 SoftwareSerial gsm_serial(2, 3); // RX: 2, TX:3 AltSoftSerial gps_serial(8, 9); // RX: 8, TX:9 int GSM_DELAY = 150; void setup() { gsm_serial.begin(BAUD_RATE);// set GSM baud rate gps_serial.begin(BAUD_RATE);// set GPS baud rate Serial.begin(BAUD_RATE); while (!Serial) continue; Serial.println("Serial init done."); delay(100); } void loop() { String payload = collectDataFromGps(); sendDataToServer(payload); } String collectDataFromGps() { Serial.println("\nRECEIVE"); delay(500); String payload = CLIENT_KEY; payload.concat(','); payload.concat(readGpsData()); return payload; } void sendDataToServer(String payload) { Serial.println("\nSEND"); gsm_serial.listen(); delay(100); sendGSMCommand("AT"); sendGSMCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); sendGSMCommand("AT+SAPBR=3,1,\"APN\",\"apn\""); //Set APN sendGSMCommand("AT+SAPBR=1,1");// Open Bearer sendGSMCommand("AT+SAPBR=2,1");// Query Bearer sendGSMCommand("AT+HTTPINIT"); // Initialize HTTP sendGSMCommand("AT+HTTPPARA=\"CID\",1"); String url_cmd = "AT+HTTPPARA=\"URL\",\""; url_cmd.concat(SERVER_URL); url_cmd.concat("\""); sendGSMCommand(url_cmd); //Server address delay(GSM_DELAY); sendGSMCommand("AT+HTTPDATA=" + String(payload.length()) + ",10000"); Serial.println("PAYLOAD - " + payload); sendGSMCommand(payload); sendGSMCommand("AT+HTTPACTION=1"); delay(100); sendGSMCommand("AT+HTTPTERM"); //Terminate HTTP } String sendGSMCommand(String cmd) { gsm_serial.println(cmd); return readGSMSerialData(); } String readGSMSerialData() { delay(100); String gsmResponse = ""; gsm_serial.listen(); while (gsm_serial.available() != 0) { char raw = gsm_serial.read(); gsmResponse.concat(raw); Serial.write(raw); } gsmResponse.trim(); return gsmResponse; } String readGpsData() { String content = ""; gps_serial.listen(); while (gps_serial.available() > 0) { char data = gps_serial.read(); content.concat(data); } return content; }
@will I am mostly concerned with the microcontroller which allows me to use 2 serial ports at the same time. Does Arduino MEGA come with such a microcontroller?
Hi @prashantgaykar ,
Using the power of Google:
https://www.arduino.cc/en/Main/arduinoBoardMega2560 says:
- The Mega 2560 is an update to the Arduino Mega, which it replaces.
- The Mega 2560 is a microcontroller board based on the ATmega2560.
- It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), ....
-----------
Of course, this doesn't conclusively prove it will meet your needs, but the suggestion of 4 UARTs implies they can all be used at the same time in appropriate circumstances, as otherwise there would be little point in fitting them.
Any limitations are more likely to be related to processor performance arising from handling the incoming data from multiple streams. If they are both 9600 Baud Rate, then the rate of incoming characters is around 1000 per second, which intuitively sounds modest. Perhaps the most common problem with serial ports is that the incoming data must fetched from the UART port buffer before the serial port buffer overflows and characters are lost. Achieving this depends upon the software design of the entire processing task the microcontroller is required to do.
Others may be able to share direct experience, but the only true test is to build your specific application and try it.
Good luck. Dave
Please find below code for the same. Let me know if there is something wrong with this code.
A quick look - and some quick comments:
1. #include <xxxx.h> or #include "xxxx.h" not #include xxxx.h
2. SoftwareSerial can be used to interface with more than one serial port, but only one at a time. So read gps then send to GSM, then back to read gps, but of course gps messages that could have been read when sending the GSM will be lost (though this should not be a problem, just get the latest gps data). AltSoftSerial seems a nice library (I've not used it) where its possible to transmit and receive at the same time. Perhaps consider just using either the SoftwareSerial or the AltSoftSerial, but not both together, at least for initial test purposes.
3. reading the serial from a GPS sensor usually means using a gps library for the sensor which will give NEMA sentences. Usually create a buffer to hold the sentence, read into buffer, parse buffer, etc. Perhaps just reading the GPS serial characters means you just get a never ending stream of characters.
4. if things are not working you should include many a:
#define DEBUG 1
#ifdef DEBUG
Serial.print("something useful")
#endif
I'm sure you will find out whats amiss with your code if you take care to debug it in small steps that lets you see whats going on as your program proceeds. 👍
Perhaps the most common problem with serial ports is that the incoming data must fetched from the UART port buffer before the serial port buffer overflows and characters are lost. Achieving this depends upon the software design of the entire processing task the microcontroller is required to do.
Hey, @davee thank you for the valuable information. I will try this with Arduino MEGA also.
Hey @byron,
Thank you very much for your input. I guess I need to play more with this Serial stuff.