Hi I have a problem with my project.I can get SMS and I can see it on Serial Monitor.Message(R1,R2... R6 etc.) should trigger the relay but ıt doesnt.What is the point I missed?
'#include <Arduino.h>
// Using Serial2 hardware serial port on ESP32
HardwareSerial SIM800(2);
// Pin definitions for SIM800C (connected to ESP32 RX2 and TX2)
#define SIM800_RX_PIN 16 // ESP32 RX2 (GPIO16)
#define SIM800_TX_PIN 17 // ESP32 TX2 (GPIO17)
// Baud rate for SIM800C
#define BAUD_RATE 9600
// Relay pins
#define RELAY1_PIN 21
#define RELAY2_PIN 19
#define RELAY3_PIN 18
#define RELAY4_PIN 5
#define RELAY5_PIN 27
#define RELAY6_PIN 26
void setup() {
// Initialize primary serial port for debugging
Serial.begin(9600);
// Initialize SIM800C serial port
SIM800.begin(BAUD_RATE, SERIAL_8N1, SIM800_RX_PIN, SIM800_TX_PIN);
// Initialize relay pins
pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(RELAY3_PIN, OUTPUT);
pinMode(RELAY4_PIN, OUTPUT);
pinMode(RELAY5_PIN, OUTPUT);
pinMode(RELAY6_PIN, OUTPUT);
// Set all relays to HIGH initially (OFF state)
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
digitalWrite(RELAY3_PIN, HIGH);
digitalWrite(RELAY4_PIN, HIGH);
digitalWrite(RELAY5_PIN, HIGH);
digitalWrite(RELAY6_PIN, HIGH);
// Check connection to the SIM800C module
if (sendATCommand("AT")) {
Serial.println("SIM800C module connected.");
} else {
Serial.println("Failed to connect to SIM800C module.");
}
// Set SMS mode to text
if (sendATCommand("AT+CMGF=1")) {
Serial.println("SMS mode set to text.");
} else {
Serial.println("Failed to set SMS mode.");
}
// Enable new SMS notifications
if (sendATCommand("AT+CNMI=1,2,0,0,0")) {
Serial.println("New SMS notifications enabled.");
} else {
Serial.println("Failed to enable new SMS notifications.");
}
}
void loop() {
// Check for new SMS
readSMS();
}
// Function to send AT commands and wait for a response
bool sendATCommand(const char* command) {
SIM800.println(command); // Send AT command
Serial.print("Sent AT Command: ");
Serial.println(command);
// Timeout for response
unsigned long timeout = millis() + 5000; // 5-second timeout
while (millis() < timeout) {
if (SIM800.available()) {
String response = SIM800.readString(); // Read the response
Serial.print("SIM800 Response: ");
Serial.println(response);
// Check for "OK" or ">" in response to confirm successful command
if (response.indexOf("OK") != -1 || response.indexOf(">") != -1) {
return true;
}
}
delay(100); // Small delay to allow response
}
// Return false if no valid response
return false;
}
// Function to control relays based on SMS commands
void controlRelay(const String& command) {
if (command == "R1") {
digitalWrite(RELAY1_PIN, LOW); // Activate Relay 1
Serial.println("Relay 1 activated.");
} else if (command == "R2") {
digitalWrite(RELAY2_PIN, LOW); // Activate Relay 2
Serial.println("Relay 2 activated.");
} else if (command == "R3") {
digitalWrite(RELAY3_PIN, LOW); // Activate Relay 3
Serial.println("Relay 3 activated.");
} else if (command == "R4") {
digitalWrite(RELAY4_PIN, LOW); // Activate Relay 4
Serial.println("Relay 4 activated.");
} else if (command == "R5") {
digitalWrite(RELAY5_PIN, LOW); // Activate Relay 5
Serial.println("Relay 5 activated.");
} else if (command == "R6") {
digitalWrite(RELAY6_PIN, LOW); // Activate Relay 6
Serial.println("Relay 6 activated.");
} else {
Serial.println("Invalid command received.");
}
}
// Function to read incoming SMS
void readSMS() {
if (SIM800.available()) {
String response = SIM800.readString();
Serial.println("Incoming SMS: ");
Serial.println(response);
// Check for new SMS notification with +CMTI
if (response.indexOf("+CMTI") != -1) {
// Extract SMS index number
int indexStart = response.indexOf(",") + 1;
String indexStr = response.substring(indexStart);
int smsIndex = indexStr.toInt();
// Read the new SMS
SIM800.print("AT+CMGR=");
SIM800.println(smsIndex);
delay(1000); // Wait for SMS content
// Read and display SMS content
if (SIM800.available()) {
String smsResponse = SIM800.readString();
Serial.println("SMS Content: ");
Serial.println(smsResponse);
// Check for relay commands in the SMS content
if (smsResponse.indexOf("R1") != -1) controlRelay("R1");
else if (smsResponse.indexOf("R2") != -1) controlRelay("R2");
else if (smsResponse.indexOf("R3") != -1) controlRelay("R3");
else if (smsResponse.indexOf("R4") != -1) controlRelay("R4");
else if (smsResponse.indexOf("R5") != -1) controlRelay("R5");
else if (smsResponse.indexOf("R6") != -1) controlRelay("R6");
}
}
}
}
`