کد کامل sim800L
کد:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3);
#define GSMReset 5
void setup() {
Serial.begin(9600); // for serial monitor
SIM900.begin(9600); // for GSM shield
gsmPower();
gsmConfig();
}
void loop() {
commandDetect();
smsProcess();
}
//*********************************************************************************
void gsmConfig() {
pinMode(GSMReset, OUTPUT);
sendCommand("AT+CMGF=1\r", 100); // set SMS mode to text
sendCommand("AT+CNMI=2,2,0,0,0\r", 100);
sendCommand("AT+CFUN=1", 2000);
sendCommand("AT+CREG?", 1000);
Serial.println("The end of Config process");
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms");
}
//**********************************************************************************
void showSms() {
char incoming_char = 0;
if (SIM900.available() > 0)
{
incoming_char = SIM900.read(); //Get the character from the cellular serial port.
Serial.print(incoming_char); //Print the incoming character to the terminal.
}
}
//***********************************************************************************
void smsProcess() {
char incoming_char = 0;
if (SIM900.available() > 0)
{
incoming_char = SIM900.read();
if (incoming_char == 'H')
{
delay(10);
Serial.print("horaaaaaaaaaaaaaaaaaaay");
}
}
}
//***********************************************************************************
void gsmPower() {
Serial.println("Turning GSM ON");
digitalWrite(GSMReset, HIGH);
delay(10);
digitalWrite(GSMReset, LOW);
delay(100);
digitalWrite(GSMReset, HIGH);
delay(7000);
Serial.println("config process...");
}
//**********************************************************************************
void sendCommand(String cmd, int t)
{
SIM900.println(cmd);
delay(t);
readSerial();
}
//**************************************************************************************
void readSerial() {
while (SIM900.available())
Serial.write(SIM900.read());
}
//***********************************************************************************
void dialNumber(String number) {
Serial.println("Dialing...");
sendCommand("ATD" + number + ";", 100);
Serial.println("End of Dial process");
delay(2000);
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms");
}
//************************************************************************************
void ensms()
{
Serial.println("Sending SMS");
sendCommand("AT+CSCS=\"IRA\"", 200);
sendCommand("AT+CMGF=1", 200);
sendCommand("AT+CSMP=17,167,0,0", 200);
sendCommand("AT+CMGS=\"09331424592\"", 500);
sendCommand("Salam\r\n AftabRayaneh !\x1A", 0);
Serial.println("End of English SMS process");
delay(2000);
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms");
}
//****************************************************************************************
void farsisms()
{
Serial.println("Sending SMS");
sendCommand("AT+CSCS=\"HEX\"", 100);
sendCommand("AT+CMGF=1", 100);
sendCommand("AT+CSMP=17,167,0,8", 100);
sendCommand("AT+CMGS=\"+09331424592\"", 500);
sendCommand("0633064406270645060C002006220641062A0627062800200631062706CC062706460647\x1A", 0);
Serial.println("End of Persian SMS process");
delay(2000);
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms");
}
//*******************************************************************************************
void commandDetect() {
if (Serial.available() > 0)
{
char data = Serial.read();
switch (data)
{
case 'd' : dialNumber("09331424592");
break;
case 'e' : ensms();
break;
case 'f' : farsisms();
break;
}
}
}