Strange behaviour of serial and software serial

I am experiencing strange problem related to serial port on Arduino UNO and usage of SoftwareSerial. Behaviour is even different using Arduino IDE or Arduino WEB for compiling and uploading.

I use Arduino UNO with Arduino compatible GSM module (shield) which is communicating with Arduino using library SoftwareSerial over pins 7 and 8. I also use Serial monitor for debugging purposes.

First problem that I have is automatic reset of Arduino each time that I open Serial monitor on IDE or WEB. Why is this happening? It does not happen for example with simple code that turns Arduino into serial interface for GSM module when code reads Softwareserial and prints information to Serial monitor.

Second problem is that behaivour of Serial monitor and Software serial is totally different when using IDE or WEB editor for compilling and uploading code to Arduino. If done by IDE then serial monitor is showing no information while on WEB it is working just fine. In some cases even communication over Sofware serial is corrupted if I had used IDE for compilling and uploading versus WEB version where everything works fine.

Code example is below. I have paste just part of it.

#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <DHT.h>

DHT dht(DHTPIN, DHTTYPE);


SoftwareSerial gsm(7, 8);

int debugMode = 1;

void setup()
{
  gsmPowerOn();
  pinMode(swOnePin, OUTPUT);                
  pinMode(sigSwOnePin, OUTPUT);
  pinMode(buttonOnePin, INPUT);          
  if (debugMode == 1) {
    Serial.begin(9600);
  }
  gsm.begin(19200);                    
  gsm.print("\r");
  gsmSetup();
  attachInterrupt(digitalPinToInterrupt(buttonOnePin), readButtonOne, CHANGE);

  
  dht.begin();

void gsmPowerOn()
{
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  delay(1000);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(3000);
}

void gsmSetup()
{
  if(gprsEnabled) {
    gsm.print("AT+CFUN=1\r");
    delay(5000);
    gsm.print("AT+CSTT=\"internet\",\"mobitel\",\"internet\"\r");
    delay(5000);
    gsm.print("AT+CIICR\r");
    delay(5000);
  }
  gsm.print("AT+CMGF=1\r");
  delay(100);
  gsm.print("AT+CNMI=2,2,0,0,0\r");
  delay(1000);
}

}

First problem that I have is automatic reset of Arduino each time that I open Serial monitor on IDE or WEB. Why is this happening?

That is by design. There are ways to disable (permanently or temporarily) that behavior. While it is disabled, uploading new code becomes a lot more difficult.

It does not happen for example with simple code that turns Arduino into serial interface for GSM module when code reads Softwareserial and prints information to Serial monitor.

Yes, it does. You just are not aware that the Arduino is resetting.

Second problem is that behaivour of Serial monitor and Software serial is totally different when using IDE or WEB editor for compilling and uploading code to Arduino.

That I find extremely difficult to believe, if you are using anywhere near the same version of the IDE on your PC as is being used on the web.

The Arduino has NO idea where the executable file came from. If there are differences in the executable file as a result of creating it on the PC vs. creating it on the web server, they are because of different versions of libraries on the two computers and from different tool chains being used. The different tool chains should have minimal impact.

I have paste just part of it.

Then I'm only going to paste part of the answer.

  attachInterrupt(digitalPinToInterrupt(buttonOnePin), readButtonOne, CHANGE);

Using an interrupt to react two a change in state of a switch pressed by a human almost always indicates that the program is poorly written, containing blocking code that prevents proper polling of the switch. The code you have posted contains lots of useless delays, for force asynchronous communication to appear to be synchronous. A very poor design.

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

You may also get some ideas from Planning and Implementing a Program

...R

@PaulS
I know it is hard to believe but yes, Serial port and Software serial port are working differently if I compile and upload with IDE (1.8.7) or WEB. My Arduino receives commands in sms which are read and action is taken based on keyword. And text comming out of GSM module over software serial is somehow corrupted when i compile with IDE. And command is not recognized as a result.

I posted just a part of code because it is too big to be accepted on forum.

I know my code is not the best written out there but weren't we all beginners at the beginning?

@Robin2
The delays that you see in code are just in part for initializing GSM module to give it time to be functional. There is delay between sending AT command and receiving an answer, GSM module takes its time to become ready.

I know my code is not the best written out there but weren't we all beginners at the beginning?

Who criticized your code, causing you to get all defensive? The ONLY comment made about your code was that it was not complete.

The other comments were about the incorrect approach you are taking to programming, not about the results of that approach.

Get defensive about the right thing!

marmla:
The delays that you see in code are just in part for initializing GSM module to give it time to be functional. There is delay between sending AT command and receiving an answer, GSM module takes its time to become ready.

I understand the need for an interval, but you don't have to use the delay() function to implement it.

...R

Robi2, even if until GSM module is ready no other code should be run? So if its better to use millis in this situation i will definitely do it. thank you!

marmla:
Robi2, even if until GSM module is ready no other code should be run?

If there really is nothing else to be done then you can use delay(). Keep in mind, however, that changing from delay() to millis() if it becomes necessary at a later stage is a lot of hard work.

...R