کد:
#include <SoftwareSerial.h>
SoftwareSerial SIM800(D2, D1 , 128);
#define GSMReset D3
void setup() {
Serial.begin(9600); // for serial monitor
SIM800.begin(9600); // for GSM shield
gsmPower();
gsmConfig();
dialNumber() ;
}
void loop() {
commandDetect();
}
//*********************************************************************************
void gsmConfig() {
sendCommand("ATZ", 100);
sendCommand("AT+CMGD=1,4", 200);
sendCommand("AT+CMGF=1\r", 100); // set SMS mode to text
sendCommand("AT+CFUN=1", 2000);
sendCommand("AT+CREG?", 1000);
dtmfActive();
anwerCall();
Serial.println("The end of Config process");
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//**********************************************************************************
void showSms() {
char incoming_char = 0;
if ( SIM800.available() > 0)
{
incoming_char = SIM800.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 ( SIM800.available() > 0)
{
incoming_char = SIM800.read();
if (incoming_char == 'A')
{
delay(10);
incoming_char = SIM800.read();
if (incoming_char == 'd')
{
delay(10);
Serial.print("horaaaaaaaaaaaaaaaaaaay");
}
}
}
}
//***********************************************************************************
void gsmPower() {
pinMode(GSMReset, OUTPUT);
Serial.println("Turning GSM ON");
digitalWrite(GSMReset, HIGH);
// delay(1100);
//digitalWrite(GSMReset, LOW);
// delay(4000);
Serial.println("Start config process...");
}
//**********************************************************************************
void sendCommand(String cmd, int t)
{
SIM800.println(cmd);
readSerial();
delay(t);
}
//**************************************************************************************
void readSerial( ) {
while (SIM800.available())
{
Serial.write(SIM800.read());
}
}
//***********************************************************************************
void dialNumber() {
Serial.println("Dialing...");
sendCommand("ATD09177102643;", 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 \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//************************************************************************************
void ensms()
{
Serial.println("Sending SMS");
sendCommand("AT+CSCS=\"GSM\"", 200);
sendCommand("AT+CMGF=1", 200);
sendCommand("AT+CSMP=17,167,0,0", 200);
sendCommand("AT+CMGS=\"09177102643\"", 500);
sendCommand("Salam\n manam !\x1A", 500);
Serial.println("Englisg SMS Sent");
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//****************************************************************************************
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=\"09177102643\"", 500);
sendCommand("0633064406270645060C002006220641062A0627062800200631062706CC062706460647\x1A", 0);
Serial.println("Persian SMS Sent");
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//*******************************************************************************************
void commandDetect() {
if (Serial.available() > 0)
{
char data = Serial.read();
switch (data)
{
case 'd' :
dialNumber();
break;
case 'e' : ensms();
break;
case 'f' : farsisms();
break;
case 'h':
SubmitHttpRequest();
break;
case 's':
Send2Pachube();
break;
}
}
}
//******************************************************************************************************
void SubmitHttpRequest()
{
sendCommand("AT+CSQ", 100);
sendCommand("AT+CGATT?", 100);
sendCommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", 1000); //setting the SAPBR, the connection type is using gprs
sendCommand("AT+SAPBR=3,1,\"APN\",\"CMNET\"", 4000); //setting the APN, the second need you fill in your local apn server
sendCommand("AT+SAPBR=1,1", 2000); //setting the SAPBR, for detail you can refer to the AT command mamual
sendCommand("AT+HTTPINIT", 2000); //init the HTTP request
sendCommand("AT+HTTPPARA=\"URL\",\"www.google.com.hk\"", 1000); // setting the httppara, the second parameter is the website you want to access
sendCommand("AT+HTTPACTION=0", 10000); //submit the request
sendCommand("AT+HTTPREAD", 300); // read the data from the website you access
sendCommand("", 100);
delay(2000);
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//*************************************************************************************************************************
///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()
{
sendCommand("AT+CGATT?", 1000);
sendCommand("AT+CSTT=\"CMNET\"", 1000); //start task and setting the APN,
sendCommand("AT+CIICR", 3000); //bring up wireless connection
sendCommand("AT+CIFSR", 2000); //get local IP adress
sendCommand("AT+CIPSPRT=0", 3000);
sendCommand("AT+CIPSTART=\"tcp\",\"api.cosm.com\",\"8081\"", 2000); //start up the connection
sendCommand("AT+CIPSEND", 4000); //begin send data to remote server
String humidity = "1031";//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 = "1242";//you can replace these four variable to the real sensor data in your project
String temperature = "30";//
String barometer = "60.56";//
sendCommand("{\"method\": \"put\",\"resource\": \"/feeds/42742/\",\"params\"", 500); //here is the feed you apply from pachube
sendCommand(": {},\"headers\": {\"X-PachubeApiKey\":", 500); //in here, you should replace your pachubeapikey
sendCommand(" \"_cXwr5LE8qW4a296O-cDwOUvfddFer5pGmaRigPsiO0", 500); //pachubeapikey
sendCommand("jEB9OjK-W6vej56j9ItaSlIac-hgbQjxExuveD95yc8BttXc", 500); //pachubeapikey
sendCommand("Z7_seZqLVjeCOmNbEXUva45t6FL8AxOcuNSsQS\"},\"body\":", 500);
sendCommand(" {\"version\": \"1.0.0\",\"datastreams\": ", 500);
sendCommand("[{\"id\": \"01\",\"current_value\": \"" + barometer + "\"},", 500);
sendCommand("{\"id\": \"02\",\"current_value\": \"" + humidity + "\"},", 500);
sendCommand("{\"id\": \"03\",\"current_value\": \"" + moisture + "\"},", 500);
sendCommand("{\"id\": \"04\",\"current_value\": \"" + temperature + "\"}]},\"token\": \"lee\"}", 500);
SIM800.println(); ((char)26); //sending
delay(5000);
SIM800.println();
sendCommand("AT+CIPCLOSE", 100); //close the connection
delay(2000);
Serial.println("Ensert\n 'd' for dial \n 'e' for english sms \n 'f' for farsi sms \n 'h' for HttpRequest \n 's' for Send2Pachube ");
}
//******************************************************************************************************************
void dtmfActive() {
sendCommand("AT+DDET=1", 200);
}
//***************************************************************
void anwerCall() {
sendCommand("ATS0=3", 200);
}
void blinkd16(){
pinMode(16,OUTPUT);
digitalWrite(16,HIGH);
delay(100);
digitalWrite(16,LOW);
delay(100);
}
فقط باید شماره پینهای ارتباط سریال نرم افزاریتون رو عوض کنید.