I'm doing a project about a GSM Switch with voltage monitoring. When I sent a SMS to the GSM Switch, it will replay my phone with SMS that how many voltage the GSM Switch now. However, my problem is when I sent a SMS to the GSM Switch request for the voltage information, it keeps sending the SMS (non-stop) to reply me how many voltage now. I want it reply only one SMS to me when I sent a request.
Anyone can help me to fix this problem, please.
I'm using Arduino Uno, Voltage Sensor, 2 Relay Switch and SIM900 GSM Module.
Here is my code:
#include <SoftwareSerial.h>
// Configure software serial port
SoftwareSerial SIM900(7, 8);
// Variable to store Phone No
// REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
// USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
String PhoneNo = "+xxxxxxxxxxx";
// Variable to store text message
String textMessage;
String VoltageString;
// want SMS show relay off by default
String Relay1State = "Off";
String Relay2State = "Off";
// Voltage Sensor
int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;
const int Relay1 = 11; // Relay1 connected to pin 11
const int Relay2 = 12; // Relay2 connected to pin 12
void setup() {
pinMode(Relay1, OUTPUT); // Set relay as OUTPUT
pinMode(Relay2, OUTPUT); // Set relay as OUTPUT
pinMode(analogInput, INPUT); // Set Voltage sensor as INPUT
// By default the relay is off
digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
SIM900.begin(19200); // Arduino communicates with SIM900 GSM shield at a baud rate of 19200
Serial.begin(9600);
// Give time to your GSM shield log on to network
delay(10000);
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
// Send the SMS
sendSMS();
}
void CheckVoltage() {
// Check voltage
value = analogRead(analogInput); // read the value at analog input
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2/(R1+R2));
VoltageString = String(vin,2); // Convert float to String
VoltageString = (VoltageString + "v");
delay(500);
}
void sendSMS() {
SIM900.println("AT + CMGS = \"" + PhoneNo + "\"");
delay(100);
// REPLACE WITH YOUR OWN SMS MESSAGE CONTENT
SIM900.println("GSM Switch Ready");
delay(100);
// End AT command with a ^Z, ASCII code 26
SIM900.println((char)26);
delay(100);
SIM900.println();
// Give module time to send SMS
delay(5000);
}
// Function that sends State SMS
void sendState(String statemessage) {
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"" + PhoneNo + "\"");
delay(100);
// Send the SMS
SIM900.println(statemessage);
delay(100);
// End AT command with a ^Z, ASCII code 26
SIM900.println((char)26); // ctrl+z
delay(100);
SIM900.println();
// Give module time to send SMS
delay(5000);
}
// Function that sends Voltage SMS
void sendVoltage(String voltagemessage) {
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"" + PhoneNo + "\"");
delay(100);
// Send the SMS
SIM900.println(voltagemessage);
delay(100);
// End AT command with a ^Z, ASCII code 26
SIM900.println((char)26); // ctrl+z
delay(1000);
SIM900.println();
// Give module time to send SMS
delay(5000);
}
void loop() {
// Display any text that the GSM shield sends out on the serial port
if(SIM900.available()>0) {
textMessage = SIM900.readString(); //Get the character from the cellular serial port
Serial.print(textMessage); //Print the incoming character to the terminal
delay(10);
}
// Turn on relay1 and save current state
if(textMessage.indexOf("Relay1ON")>=0) {
digitalWrite(Relay1, LOW);
Relay1State = "On";
Serial.println("Relay1 set to ON");
textMessage = "";
}
// Turn off relay1 and save current state
if(textMessage.indexOf("Relay1OFF")>=0) {
digitalWrite(Relay1, HIGH);
Relay1State = "Off";
Serial.println("Relay1 set to OFF");
textMessage = "";
}
// Turn on relay2 and save current state
if(textMessage.indexOf("Relay2ON")>=0) {
digitalWrite(Relay2, LOW);
Relay2State = "On";
Serial.println("Relay2 set to ON");
textMessage = "";
}
// Turn off relay2 and save current state
if(textMessage.indexOf("Relay2OFF")>=0) {
digitalWrite(Relay2, HIGH);
Relay2State = "Off";
Serial.println("Relay2 set to OFF");
textMessage = "";
}
// Check relay state
if(textMessage.indexOf("STATE")>=0) {
String statemessage = "Relay1 is " + Relay1State + ", Relay2 is " + Relay2State;
sendState(statemessage);
Serial.println("Relay state resquest");
textMessage = "";
}
// Check Voltage
if(textMessage.indexOf("Voltage")>=0) {
CheckVoltage();
String voltagemessage = "Voltage = " + VoltageString;
sendVoltage(voltagemessage);
Serial.println("Voltage resquest");
textMessage = "";
}
}
