Sending AT Command in void setup versus void loop

I'm using an Arduino Uno with a module attached.

I can send AT commands from the serial monitor (i.e. type them in by hand and hit send) and everything works great. But when I try to have an arduino sketch send the same command, strange things happen.

If I put the AT command in the void setup area, it works fine. But if I put the exact same AT command in the void loop section, nothing happens.

Any suggestions would be greatly appreciated! :slight_smile:

You have to remember that the loop is running over and over again super quickly. It might be that the problem is caused by you hammering the HM-10 so hard that it can't respond. Try this:

#include <SoftwareSerial.h>

SoftwareSerial HM10(2, 3); //HM10(Receive Pin, Transmit Pin)
unsigned long timestamp;

void setup()
{
  Serial.begin(57600);  // Begin the Serial Monitor connection at 57600bps
  HM10.begin(57600);  // Begin the HM-10 connection at 57600bps
  HM10.write("AT-DISI?"); //this command in this setup location, results in the serial monitor returning a correct response from the HM-10 module.
}

void loop()
{
  if (millis() - timestamp > 2000) { //run every 2 seconds
    HM10.write("AT-DISI?"); //but the exact same command in this loop section returns nothing, i.e. no response from the HM-10 or perhaps it responds but everything is moving too fast to display???
    timestamp = millis();
  }

  if (HM10.available()) // Read from HM-10 and send to Serial Monitor
    Serial.write(HM10.read());

  if (Serial.available()) // Read from Serial Monitor and send to HM-10
    HM10.write(Serial.read());
}

I think you need a \r\n or \n at the end of the AT commands or use println instead of write

Running software serial at 57600 is usually fatal.

I believe the HM-10 is configured at 9600 anyway, just like an HC-06

What you propose is quite feasible. It is the only method I have used. You just need some delays

  delay(2000);
  Serial.println(command);
  command = ""; // No repeats
  Serial.println("AT+PIN1234");
  Serial1.print("AT+PIN1234");        //CHANGE PASSWORD
  if (Serial1.available()) {
    while(Serial1.available()) { // While there is more to be read, keep reading.
        delay(3);
      command += (char)Serial1.read();  
    }
  }

Your problem is the way you read your serial input. you should never bet on how to time perfectly the arrival of the chars. You should not use delays, you should have a while loop until you have received -DISIS for example. Also you should forget about using the String class, that will get you in memory trouble. Use plain char arrays.