SIM900A模块SMS控制继电器失效问题求助(附Arduino代码)
Hey there, let's figure out why your IF branch isn't triggering! The core issues here are your hardcoded character removal logic (which is unreliable) and unaccounted hidden whitespace/extra characters that break the equality check. Let's break down the fixes step by step:
Key Problems in Your Original Code
- Fixed character removal is fragile: SIM900A's incoming SMS includes variable-length fields (like phone numbers with country codes, timestamps), so stripping exactly 51 characters won't always land you on the actual message content. This shifts your target string and makes the equality check fail.
- Hidden invisible characters: Even if you get close to the message, trailing
\r,\n, or spaces might be present that aren't visible in the Serial Monitor but break theequals()comparison.
Revised Code with Fixes
#include <SoftwareSerial.h> SoftwareSerial SIM900A(9, 10); void setup(){ Serial.begin(9600); SIM900A.begin(9600); pinMode(13, OUTPUT); Serial.println("Program Started"); SIM900A.println("AT+CMGD=ALL"); // Clear old pending messages SIM900A.println("AT+CNMI=2,2,0,0"); // Route incoming SMS directly to serial } void loop(){ if (SIM900A.available() > 0){ String fullResponse = SIM900A.readString(); Serial.println("*** RECEIVED FULL RESPONSE ***"); Serial.println(fullResponse); // Dynamically find the start of the SMS body (after header) // SIM900A separates header and body with a double newline (\r\n\r\n) int messageStart = fullResponse.indexOf("\r\n\r\n"); String smsContent = ""; if (messageStart != -1) { // Extract content after the double newline smsContent = fullResponse.substring(messageStart + 4); } else { // Fallback: if double newline isn't found, grab the last line messageStart = fullResponse.lastIndexOf("\r\n"); if (messageStart != -1) { smsContent = fullResponse.substring(messageStart + 2); } } // Clean up hidden whitespace/invisible characters smsContent.trim(); Serial.println("*** CLEANED SMS CONTENT ***"); Serial.println(smsContent); Serial.print("Length after cleanup: "); Serial.println(smsContent.length()); // Simplified case-insensitive check if (smsContent.equalsIgnoreCase("ON")) { Serial.println("Relay turned ON"); digitalWrite(13, HIGH); delay(5000); } else { Serial.println("Else loop executed - invalid command or mismatched content"); digitalWrite(13, LOW); } Serial.println("*** END SMS ***"); } }
What Changed & Why
- Dynamic content extraction: Instead of stripping a fixed number of characters, we look for the double newline that separates the SMS header (phone number, timestamp) from the actual message body. This works no matter how long the header is.
trim()function: This removes all leading/trailing whitespace (spaces, tabs,\r,\n) that might be leftover from the raw response—these hidden characters were likely breaking your original equality check.equalsIgnoreCase(): This checks for "ON" in any case (ON, On, on) in one line, making the code cleaner and less error-prone than three separateequals()checks.
Additional Debug Tip (If Issues Persist)
If the IF branch still doesn't trigger, print the ASCII values of each character in smsContent to spot hidden non-printable characters:
for (int i = 0; i < smsContent.length(); i++) { Serial.print("Char "); Serial.print(i); Serial.print(": "); Serial.print(smsContent.charAt(i)); Serial.print(" (ASCII: "); Serial.print((int)smsContent.charAt(i)); Serial.println(")"); }
This will reveal any unexpected characters (like non-printable ASCII codes) that are messing up the comparison.
内容的提问来源于stack exchange,提问作者Kaiser
相关产品推荐
相关产品推荐

