Arduino+SIM800L发送SMS时出现CMS ERROR 305的解决建议咨询
Fixing CMS ERROR 305 When Sending SMS with Arduino & SIM800L
First, let's break down what CMS ERROR 305 actually means—it translates to Invalid parameters in text mode. This usually pops up when the SIM800L gets unexpected input, or when the sequence of commands you're sending in text mode (AT+CMGF=1) is out of order or incomplete. Let's walk through your code and fix the issues step by step.
Key Problems in Your Current Code
- Ignoring the
>Prompt: When you send theAT+CMGS="phone_number"command, the SIM800L will respond with a>character to signal it's ready to accept the SMS content. Your code uses fixed delays instead of waiting for this prompt, which often leads to sending the message before the module is prepared—this is the most common cause of ERROR 305. - Inconsistent Error Reporting: You first set
at+cmee=0(which disables detailed error messages) then addedat+cmee=1later. Keep error reporting enabled; it's critical for debugging. - Unreliable Delay-Based Flow: Fixed
delay()calls aren't as reliable as waiting for the module to finish responding before sending the next command. Modules can take variable time to process commands depending on signal strength.
Modified & Debugged Code
Here's an improved version of your sends() function that addresses these issues, plus a helper function to make command handling cleaner:
void sends() { Serial.println("Attempting to send SMS..."); // Enable detailed error reporting (keep this on for debugging) sendATCommand("AT+CMEE=1", 1000); // Set GSM character set (matches your original command) sendATCommand("AT+CSCS=\"GSM\"", 1000); // Switch to text mode for SMS sendATCommand("AT+CMGF=1", 1000); // Prepare to send SMS: wait for the > prompt Serial.println("\nSending AT+CMGS command..."); gsm.print("AT+CMGS=\"+98914xxxxxxx\"\r"); // Add \r to properly terminate the command // Wait for the > response (timeout after 3 seconds if no prompt) unsigned long startTime = millis(); bool promptReceived = false; while(millis() - startTime < 3000) { if(gsm.available()) { char c = gsm.read(); Serial.print(c); // Echo module responses for debugging if(c == '>') { promptReceived = true; break; } } } if(promptReceived) { Serial.println("\nSending SMS content..."); gsm.println("HELLO WORLD"); // Send your message content gsm.write(26); // Send Ctrl+Z (ASCII 26) to finalize and send the SMS delay(1000); // Give the module time to process and transmit // Read and print the final response if(gsm.available()) { String response = gsm.readString(); Serial.println("\nFinal module response:"); Serial.println(response); } } else { Serial.println("\nERROR: Never received the > prompt from SIM800L"); } } // Helper function to send AT commands and print their responses void sendATCommand(String cmd, unsigned long waitTime) { gsm.println(cmd); delay(waitTime); if(gsm.available()) { String response = gsm.readString(); Serial.println("Command: " + cmd); Serial.println("Response: " + response); } }
Additional Troubleshooting Steps
- Check Your SIM Card: Make sure your SIM is activated, has enough balance for SMS, and is properly inserted. Some carriers require you to explicitly enable SMS service on new SIMs.
- Verify Phone Number Format: Double-check the international format (
+country_code+number). For Iran,+98914xxxxxxxis correct—just ensure there are no extra spaces or characters. - Power Supply Matters: SIM800L draws a lot of current when sending SMS. Avoid powering it through Arduino's USB port; use a dedicated 5V/2A power supply instead. Weak power often causes weird, hard-to-debug errors.
- Check Signal Strength: Send the
AT+CSQcommand to check signal strength. A value between 10-30 is good; anything below 10 means you need better signal (try moving the module near a window or adding an external antenna).
内容的提问来源于stack exchange,提问作者hesam sadig bayan




