Program Won't Run if Serial Monitor is closed

Hello Everyone,
I am working on an IoT-based project, Since I have somewhat achieved what the device(Arduino Uno, DHT 11, and Sim 900a ) is intended to do, I now need to run the device on a power supply, Meaning I won't be able to open the Serial Monitor Through my laptop, and as a result, I suspect the code won't work, because the Serial Buffer will be full and the code won't process further. Can you guys give me any suggestions, I have tried searching the internet but I could not find anything satisfactory.

#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(2,3);
#include "EmonLib.h"                  
#include <String.h>
#include <DHT.h> 
 
#define DHTPIN A0
EnergyMonitor emon1; 
DHT dht(DHTPIN, DHT11);
 
void setup()
{
  gprsSerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);    // the GPRS baud rate 
  dht.begin();
 emon1.current(1, 111.1); 
  delay(1000);
}
 
void loop()
{
      float h = dht.readHumidity();
      float t = dht.readTemperature(); 
      double Irms = emon1.calcIrms(1480);
      delay(100);   
         
      Serial.print("Temperature = ");
      Serial.print(t);
      Serial.println(" °C");
      Serial.print("Humidity = ");
      Serial.print(h);
      Serial.println(" %");    
      Serial.println(Irms);
      Serial.println("Ampere");
   
  if (gprsSerial.available())
    Serial.write(gprsSerial.read());
 
  gprsSerial.println("AT");
  delay(1000);
 
  gprsSerial.println("AT+CPIN?");
  delay(1000);
 
  gprsSerial.println("AT+CREG?");
  delay(1000);
 
  gprsSerial.println("AT+CGATT?");
  delay(1000);
 
  gprsSerial.println("AT+CIPSHUT");
  delay(1000);
 
  gprsSerial.println("AT+CIPSTATUS");
  delay(2000);
 
  gprsSerial.println("AT+CIPMUX=0");
  delay(2000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CSTT=\"ufone.pinternet\"");//start task and setting the APN,
  delay(1000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIICR");//bring up wireless connection
  delay(3000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIFSR");//get local IP adress
  delay(2000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSPRT=0");
  delay(3000);
 
  ShowSerialData();
  
  gprsSerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000);
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();
  
  String str="GET https://api.thingspeak.com/update?api_key=MG7I0XOD86XQEPXJ&field1=" + String(t) +"&field2="+String(h)+"&field3="+String(Irms);
  Serial.println(str);
  gprsSerial.println(str);//begin send data to remote server
  
  delay(4000);
  ShowSerialData();
 
  gprsSerial.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet 
  gprsSerial.println();
 
  ShowSerialData();
 
  gprsSerial.println("AT+CIPSHUT");//close the connection
  delay(100);
  ShowSerialData();
} 
void ShowSerialData()
{
  while(gprsSerial.available()!=0)
  Serial.write(gprsSerial.read());
  delay(5000); 
  
}

The Arduino does not care if anything is listening to the serial port. It'll work just the same when nothing is there to receive.

1 Like

So, It will work fine. Even if the Serial Monitor is closed?
okay let me try it thanks :slight_smile:

No man It does not work, I still think the Serial buffer needs to be empty if the program needs to progress.

Which MCU are you using?

AVR-based Arduinos have no mechanism to detect whether the serial monitor is even connected.

You have some other issue - it would be rather a silly design if the lack of a receiver hung your code.

Try this:

const byte ledPin=13;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin,HIGH);
Serial.println("On");
delay(1000);
digitalWrite(ledPin,LOW);
Serial.println("Off");
delay(1000);
}

It's running on a battery powered Uno on my desk right now, built in LED flashing away.

Yes, it works perfectly! but this requires no communication with another chip, As you can see in the code I posted, I am using AT commands and the Sim900a Module communicates the replies through the Serial Monitor.

I am not familiar with the terms please can you elaborate or refer me to any webpage?

The Arduino Uno has no mechanism to detect whether the serial monitor is connected.

This is the problem, and you need to rewrite the program using one of the software serial libraries.

the Sim900a Module communicates the replies through the Serial Monitor.

There are plenty of examples on line, showing how to do the communication correctly.

1 Like

I'm not sure I understand the issue. Your code should do exactly the same thing if it is not hooked up to your laptop. The AT commands will all be sent using soft serial and the results will be sprayed out of the hardware serial port into the air, since there's no monitor.

What do you mean when you say it doesn't work?

1 Like

Thank you!
I figured it out. The issue was due to the small function Show Serial data, I removed it and now the whole circuit works fine!
Thank You again for your interest

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