Arduino Programming
Today we are going to talk about the GSM Module. We can use this GSM Module (I am using the SIM900A V5.1 Module) to perform the task we want by sending an SMS and we can even send an SMS to a phone we want.
Let's see how the pin is located in this SIM900A V5.1 Module.
Let's first see how we can make sure that this module is working. When a sim is inserted into this module and 5v is given to the vcc pin, the red LED here lights up. If so, we can get a basic idea that this module is working. You can do more work. Make a voice call to the SIM in the module from another mobile phone. If it rings then the SIM900A Module is correctly connected to the network.
Now let's see if this module works correctly with the Arduino board. For that, prepare the devices as follows.
GSM Module VCC - Arduino 5v
GSM Module GND - Arduino GND
GSM Module Tx - Arduino 2 Pin
GSM Module Rx - Arduino 3 Pin
Now try uploading the code below. To upload first remove RX and TX pin. Reconnect after uploading.
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(2,3); //TX and RX pins
char State=' ';
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("Arduino with SIM900A is ready");
SIM900A.begin(9600);
Serial.println("SIM900A started at 9600");
delay(1000);
Serial.println("SIM900A is Ready");
}
void loop() {
if(SIM900A.available()){
State=SIM900A.read();
Serial.write(State);
}
if(Serial.available()){
State=Serial.read();
SIM900A.write(State);
}
}
Now open Serial Monitor and type AT and send. Then AT should be shown again in the Serial Monitor. Now make a voice call to the Sim in the module as mentioned earlier. Then Ring should be displayed in the Serial Monitor. If this happened successfully, your SIM900A Module will communicate correctly with the Arduino board.
Now the next step. Here we are looking at how to turn on and off a light bulb at home using this module through an SMS.
For this we need,
1) Arduino Uno board × 1
2) SIM900A GSM Module × 1
3) Relay Module × 1
4) Bulb × 1
5) Jumper Wires and Wires
Now set up the devices as shown in Figure 4 and upload the following code. Remove RX and TX pin before uploading. Install again after uploading.
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(2, 3); // TX and RX pins
const int Bulb = 6; //Bulb pin
String Message;
void setup() {
pinMode(Bulb, OUTPUT);
digitalWrite(Bulb, LOW);
Serial.begin(9600);
SIM900A.begin(9600);
delay(5000);
Serial.print("SIM900A is Ready");
SIM900A.print("AT+CMGF=1\r");
delay(100);
}
void loop() {
if (SIM900A.available() > 0) {
Message = SIM900A.readString();
Serial.print(Message);
delay(10);
}
if (Message.indexOf("ON") >= 0) { //Bulb On message
digitalWrite(Bulb, HIGH);
Message = "";
} else if (Message.indexOf("OFF") >= 0) { //Bulb Off message
digitalWrite(Bulb, LOW);
Message = "";
}
}
In this case, when a message comes to the Sim in the GSM Module, the string in the SMS is compared with the string in the code and a decision is made as per the code.
The String Message in the 11th line of this code; The said variable is defined to save the string that comes from an SMS.
Here, in the loop part, the part in the 27th line assigns the string that came to the GSM module to the variable called Message.
After that, after the 31st line, if the ON message comes, the bulb will turn on, and if the OFF message comes after the 35th line, the bulb will turn off.
Now let's see the next step. How can we send an SMS to our Mobile Phone using this GSM Module? For that too, set up the devices as above and upload the code below.
/*
***** YS Presents *****
GSM SIM900A Coding
*/
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(2, 3);
const int Bulb = 6;
String Message;
void setup() {
pinMode(Bulb, OUTPUT);
digitalWrite(Bulb, LOW);
Serial.begin(9600);
SIM900A.begin(9600);
delay(5000);
Serial.print("SIM900A is Ready");
SIM900A.print("AT+CMGF=1\r");
delay(100);
}
void loop() {
if (SIM900A.available() > 0) {
Message = SIM900A.readString();
Serial.print(Message);
delay(10);
}
if (Message.indexOf("ON") >= 0) {
digitalWrite(Bulb, HIGH);
Message = "";
sendMessage();
} else if (Message.indexOf("OFF") >= 0) {
digitalWrite(Bulb, LOW);
Message = "";
}
}
void SendMessage() {
Serial.println("Sending Message");
SIM900A.println("AT+CMGF=1");
delay(1000);
Serial.println("Setting Mobile Number");
SIM900A.println("AT+CMGS=\"+94***********\"\r");
delay(1000);
Serial.println("Setting SMS");
SIM900A.println("Hello!! *This Message Sent By SIM900A* Bulb is On");
delay(100);
Serial.println("Done");
SIM900A.println((char)26);
delay(1000);
Serial.println("Message has been Sent Successfully");
}
Here than the previous code
0 Comments