Problems configuring Arduino UNO serial port

I'm having problems configuring serial ports for different modules.

The problem is when I add the settings for the SIM800l module in this code snippet.

`SoftwareSerial serialSIM800L(4, 7);//RX, TX`

I realized that when I try to configure any digital port as a serial port using SoftwareSerial to use with the SIM800l all my GPS code that previously worked for

and when I remove this line the code works again.

I don't know why this is happening and I would like some help to understand it better.

I am using the following hardware!!!

Arduino UNO
modulo GPS GY-NEO6MV2
modulo GPRS GSM SIM800L

In the GPS module, I connected the TX pin to port 10 of the Arduino (Arduino port 10 configured as RX). and for the RX pin, I connected it to port 11 of the Arduino (port 11 configured as TX).

and for the SIM800L module I made the same TX pin on port 4 configured as RX, RX pin on port 7 configured as TX

as shown in the code below.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <ArduinoJson.h>

SoftwareSerial serialGPS(10, 11); // RX, TX para comunicação com o GPS
SoftwareSerial serialSIM800L(4, 7); // RX, TX para comunicação com o SIM800L 5,6

TinyGPS gps1; // Objeto para lidar com os dados do GPS

StaticJsonDocument<256> jsonDocument; // Objeto para armazenar o JSON

void setup() {
  Serial.begin(9600); // Inicializa a comunicação serial com o computador
  serialGPS.begin(9600); // Inicializa a comunicação com o GPS
  serialSIM800L.begin(9600); // Inicializa a comunicação com o SIM800L

  Serial.println("Aguardando sinal dos satelites...");
  inicializaSIM800L();
  delay(5000);
  serialSIM800L.println("AT"); // Testa comunicação com o SIM800L
  delay(1000);
  serialSIM800L.println("AT+CGATT=1"); // Ativa o GPRS
  delay(1000);
  serialSIM800L.println("AT+CGATT?"); // Verifica o status do GPRS
  delay(1000);
  serialSIM800L.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""); // Configura o tipo de conexão
  delay(1000);
  serialSIM800L.println("AT+SAPBR=3,1,\"APN\",\"timbrasil\""); // Configura o APN
  delay(1000);
  serialSIM800L.println("AT+SAPBR=1,1"); // Ativa o PDP context
  delay(5000);
  serialSIM800L.println("AT+SAPBR=2,1"); // Verifica o IP
  delay(2000);
}

void loop() {
  static unsigned long delayPrint;
  bool recebido = false;
  
  while (serialGPS.available()) {
    char cIn = serialGPS.read();
    recebido = (gps1.encode(cIn) || recebido);
  }

  if (recebido && (millis() - delayPrint > 2000)) {
    delayPrint = millis();

    jsonDocument.clear();
    
    float latitude, longitude;
    unsigned long idadeInfo;
    gps1.f_get_position(&latitude, &longitude, &idadeInfo);

    if (latitude != TinyGPS::GPS_INVALID_F_ANGLE) {
      jsonDocument["Latitude"] = latitude;
    }

    if (longitude != TinyGPS::GPS_INVALID_F_ANGLE) {
      jsonDocument["Longitude"] = longitude;
    }

    if ((latitude != TinyGPS::GPS_INVALID_F_ANGLE) && (longitude != TinyGPS::GPS_INVALID_F_ANGLE)) {
      jsonDocument["Link"] = "https://maps.google.com/maps/?&z=10&q=" + String(latitude, 6) + "," + String(longitude, 6);
    }

    int ano;
    byte mes, dia, hora, minuto, segundo, centesimo;
    gps1.crack_datetime(&ano, &mes, &dia, &hora, &minuto, &segundo, &centesimo, &idadeInfo);

    jsonDocument["Data"] = String(dia) + "/" + String(mes) + "/" + String(ano);
    jsonDocument["Hora"] = String(hora) + ":" + String(minuto) + ":" + String(segundo);

    float altitudeGPS = gps1.f_altitude();
    if ((altitudeGPS != TinyGPS::GPS_INVALID_ALTITUDE) && (altitudeGPS != 1000000)) {
      jsonDocument["Altitude_cm"] = altitudeGPS;
    }

    float velocidade = gps1.f_speed_kmph();
    jsonDocument["Velocidade_kmph"] = velocidade;

    unsigned long sentido = gps1.course();
    jsonDocument["Sentido_grau"] = float(sentido) / 100;

    unsigned short satelites = gps1.satellites();
    unsigned long precisao = gps1.hdop();

    if (satelites != TinyGPS::GPS_INVALID_SATELLITES) {
      jsonDocument["Satelites"] = satelites;
    }

    if (precisao != TinyGPS::GPS_INVALID_HDOP) {
      jsonDocument["Precisao_cent"] = precisao;
    }

    String jsonString;
    serializeJson(jsonDocument, jsonString);
    Serial.println(jsonString);

    // Envia o JSON para a API
  }
}

As a general rule - it strictly not recommended to use a more than one SoftwareSerial instance in the code.
If you are sure to need a two - you must use a special technique to access a ports. See the manual (Limitation section and two port example)
https://docs.arduino.cc/learn/built-in-libraries/software-serial/

PS The recommended solution is to use a board with the appropriate number of hardware UARTs.

2 Likes

Please, refrain from creating multiple topics with the same subject.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.