SIM900 AT来电显示开启,一些代码

时间:2023-12-31 18:10:32
/*Note: this code is a demo for how to using gprs shield to send sms message, dial a voice call and
send a http request to the website, upload data to pachube.com by TCP connection, The microcontrollers Digital Pin 7 and hence allow unhindered
communication with GPRS Shield using SoftSerial Library.
IDE: Arduino 1.0 or later
Replace the following items in the code:
1.Phone number, don't forget add the country code
2.Replace the Access Point Name
3. Replace the Pachube API Key with your personal ones assigned
to your account at cosm.com
*/ #include <SoftwareSerial.h>
#include <String.h> SoftwareSerial mySerial(, ); void setup()
{
mySerial.begin(); // the GPRS baud rate
Serial.begin(); // the GPRS baud rate
delay();
} void loop()
{
//after start up the program, you can using terminal to connect the serial of gprs shield,
//if you input 't' in the terminal, the program will execute SendTextMessage(), it will show how to send a sms message,
//if input 'd' in the terminal, it will execute DialVoiceCall(), etc. if (Serial.available())
switch(Serial.read())
{
case 't':
SendTextMessage();
break;
case 'd':
DialVoiceCall();
break;
case 'h':
SubmitHttpRequest();
break;
case 's':
Send2Pachube();
break;
}
if (mySerial.available())
Serial.write(mySerial.read());
} ///SendTextMessage()
///this function is to send a sms message
void SendTextMessage()
{
mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay();
mySerial.println("AT + CMGS = \"+8613616761237\"");//send sms message, be careful need to add a country code before the cellphone number
delay();
mySerial.println("A test message!");//the content of the message
delay();
mySerial.println((char));//the ASCII code of the ctrl+z is 26
delay();
mySerial.println();
} ///DialVoiceCall
///this function is to dial a voice call
void DialVoiceCall()
{
mySerial.println("ATD+8613616761237;");//dial the number
delay();
mySerial.println();
} ///SubmitHttpRequest()
///this function is submit a http request
///attention:the time of delay is very important, it must be set enough
void SubmitHttpRequest()
{
mySerial.println("AT+CSQ");
delay(); ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too. mySerial.println("AT+CGATT?");
delay(); ShowSerialData(); mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(); ShowSerialData(); mySerial.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server
delay(); ShowSerialData(); mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(); ShowSerialData(); mySerial.println("AT+HTTPINIT"); //init the HTTP request delay();
ShowSerialData(); mySerial.println("AT+HTTPPARA=\"URL\",\"http://122.226.151.4:6543/DHT11data.ashx?c=20.1&h=45.5\"");// setting the httppara, the second parameter is the website you want to access
delay(); ShowSerialData(); mySerial.println("AT+HTTPACTION=0");//submit the request
delay();//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available()); ShowSerialData(); mySerial.println("AT+HTTPREAD");// read the data from the website you access
delay(); ShowSerialData(); mySerial.println("");
delay();
} ///send2Pachube()///
///this function is to send the sensor data to the pachube, you can see the new value in the pachube after execute this function///
void Send2Pachube()
{
mySerial.println("AT+CGATT?");
delay(); ShowSerialData(); mySerial.println("AT+CSTT=\"CMNET\"");//start task and setting the APN,
delay(); ShowSerialData(); mySerial.println("AT+CIICR");//bring up wireless connection
delay(); ShowSerialData(); mySerial.println("AT+CIFSR");//get local IP adress
delay(); ShowSerialData(); mySerial.println("AT+CIPSPRT=0");
delay(); ShowSerialData(); mySerial.println("AT+CIPSTART=\"tcp\",\"api.cosm.com\",\"8081\"");//start up the connection
delay(); ShowSerialData(); mySerial.println("AT+CIPSEND");//begin send data to remote server
delay();
ShowSerialData();
String humidity = "";//these 4 line code are imitate the real sensor data, because the demo did't add other sensor, so using 4 string variable to replace.
String moisture = "";//you can replace these four variable to the real sensor data in your project
String temperature = "";//
String barometer = "60.56";//
mySerial.print("{\"method\": \"put\",\"resource\": \"/feeds/42742/\",\"params\"");//here is the feed you apply from pachube
delay();
ShowSerialData();
mySerial.print(": {},\"headers\": {\"X-PachubeApiKey\":");//in here, you should replace your pachubeapikey
delay();
ShowSerialData();
mySerial.print(" \"_cXwr5LE8qW4a296O-cDwOUvfddFer5pGmaRigPsiO0");//pachubeapikey
delay();
ShowSerialData();
mySerial.print("jEB9OjK-W6vej56j9ItaSlIac-hgbQjxExuveD95yc8BttXc");//pachubeapikey
delay();
ShowSerialData();
mySerial.print("Z7_seZqLVjeCOmNbEXUva45t6FL8AxOcuNSsQS\"},\"body\":");
delay();
ShowSerialData();
mySerial.print(" {\"version\": \"1.0.0\",\"datastreams\": ");
delay();
ShowSerialData();
mySerial.println("[{\"id\": \"01\",\"current_value\": \"" + barometer + "\"},");
delay();
ShowSerialData();
mySerial.println("{\"id\": \"02\",\"current_value\": \"" + humidity + "\"},");
delay();
ShowSerialData();
mySerial.println("{\"id\": \"03\",\"current_value\": \"" + moisture + "\"},");
delay();
ShowSerialData();
mySerial.println("{\"id\": \"04\",\"current_value\": \"" + temperature + "\"}]},\"token\": \"lee\"}"); delay();
ShowSerialData(); mySerial.println((char));//sending
delay();//waitting for reply, important! the time is base on the condition of internet
mySerial.println(); ShowSerialData(); mySerial.println("AT+CIPCLOSE");//close the connection
delay();
ShowSerialData();
} void ShowSerialData()
{
while(mySerial.available()!=)
Serial.write(mySerial.read());
}
#include <SoftwareSerial.h>
#include <String.h> SoftwareSerial GPRS(, );
String currentLine = ""; // string to hold the text from server
String triggerNo="";
String mobile = "";
boolean readingNo=false;
long lastTriggerTime=;
boolean fireing=false;
int led=;
void setup()
{
GPRS.begin(); // the GPRS baud rate
Serial.begin(); // the GPRS baud rate
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
delay();
powerUpOrDown();
}
void loop()
{
if(fireing){
if( millis() - lastTriggerTime >= * ){
Serial.println("stop fire!");
digitalWrite(led,LOW);
fireing=false;
}else{
return;
}
} while(GPRS.available()){
char inChar=GPRS.read();
currentLine +=inChar; if(inChar=='\n')currentLine=""; if(currentLine.endsWith( "+CLIP: \"")){
readingNo=true;
mobile="";
} if(readingNo){ if(inChar !=','){
if(inChar !='"')mobile+=inChar;
}else{
readingNo=false;
Serial.println(mobile);
if(mobile==triggerNo){
lastTriggerTime=millis();
Serial.println("fire!!");
fireing=true; currentLine="";
mobile="";
delay();
digitalWrite(led,HIGH);
delay(); GPRS.println("ATH");
while(GPRS.available()){
GPRS.read();
} } }
}
}
} void powerUpOrDown()
{
pinMode(, OUTPUT);
digitalWrite(,LOW);
delay();
digitalWrite(,HIGH);
delay();
digitalWrite(,LOW);
delay();
}

发送:AT+COLP=1,设置被叫号码显示

参考:
http://www.51hei.com/bbs/dpj-30516-1.html
http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0