|
|
本帖最后由 freevector 于 2012-8-24 23:23 编辑
这是我在DFRobotDFRobot做实习时所做的一个小小的应用,现在实习完了难得闲下来,借此机会将其写下来,权当纪念。本项目中利用GSM模块实现短信远程控制,本来制作了一个Video,但上传速度比较慢,以后有机会再传视频吧,先上几张图片,我通过发短信控制接在Arduino针脚上的LED灯的开灭,当然很容易通过继电器等实现对其它devices的控制,从而实现一定意义上的智能家居。
模 块 数量
Arduino board (DFR0109) 1
GPS/GSM/GPRS模块(TEL0051) 1
锂电池(7.4V左右,FIT0137) 1
以及一张SIM卡,我所使用的当然是DFRobot公司自己生产的板子及模块,性能非常好,强烈推荐。
上图为所用的Arduino板子及GSM模块
通过手机发送控制指令到GSM模块
很Q的房子
源代码- //-------------------From DFrobot.com------------------------//
- //---using this program,we can control the pins on Arduino---//
- #include <Wire.h>
- #include <Arduino.h>
- #define S_IDLE 0
- #define S_ATOK 1
- #define S_GETMESSAGE 2
- #define S_READMESSAGE 3
- #define M_ATOK "OK"
- #define M_GETMESSAGE "+CMTI"
- #define M_READMESSAGE "+CMGR"
- #define M_CALLREADY "Call Ready"
- #define M_FLAG "DF" //DF the the flag to position the Command in the data recevied by Serial port.you also can change it using other flag.
- char messageCMD[2]=""; //using to hold Command,for example,if the value is H8,stands the pin 8 will output a high level.
- String comdata = "";
- int gsmDriverPin[3] = {10,11,12};
- int messageControlPin[7]={3,4,5,6,7,8,9};
- void setup(){
- Serial.begin(19200); // The rate should not too high.It's ralated with the delay time of reading data from the Serial port.
- InitGsmMode(); //Initial GSM Module
- delay(100);
- for(int i=0;i<7;i++)
- {
- pinMode(messageControlPin[i],OUTPUT);//Initial the controlled pin
- }
- }
- void loop(){
- String event_message="";
- int at_event=0;
- while(1)
- {
- event_message=SerialDataRead(event_message);
- event_message=SerialMessageCheck(event_message,&at_event);
- MessageCommand(&at_event,messageCMD);
- }
- }
- //-------------------------Initial------------------------------------------//
- void InitGsmMode(){
- for(int i = 0 ; i < 3; i++)
- {
- pinMode(gsmDriverPin[i],OUTPUT);
- }
- digitalWrite(10,LOW); //Enable the GSM mode
- digitalWrite(11,HIGH); //Disable the GPS mode
- digitalWrite(12,HIGH);
- delay(1500);
- digitalWrite(12,LOW);
- delay(1500);//need test
- Serial.print("AT");
- Serial.write(0x0D);
- Serial.print("ATE0"); // echo off
- Serial.write(0x0D);
- delay(20);
- Serial.print("AT+CMGF=1"); //Set the format of message to Text Mode
- Serial.write(0x0D);
- delay(20);
- Serial.print("AT+CNMI=2,1,0,0,0"); //Set new SMS message indications
- Serial.write(0x0D);
- delay(20);
- }
- //---------------------------Read data------------------------------------//
- String SerialDataRead(String MessageData){
- while(Serial.available()>0) //serial data read
- {
- char CharRead=Serial.read();
- if(CharRead!=10&&CharRead!=13)
- {
- MessageData=MessageData+CharRead;
- }
- delay(5);
- }
- return MessageData;
- }
- //-----------------------------Check data---------------------------------------//
- String SerialMessageCheck(String Message,int *Event){
- if(Message.indexOf(M_ATOK)!=-1) //check AT OK
- {
- Message="";
- *Event=S_IDLE;
- }
- else if(Message.indexOf(M_CALLREADY)!=-1) //check CALL READY
- {
- Message="";*Event=S_ATOK;
- }
- else if(Message.indexOf(M_GETMESSAGE)!=-1)
- {
- Message="";
- *Event=S_GETMESSAGE;
- delay(20);
- }
- else ;
- return Message;
- }
- //----------------------The most important part!----------------------------//
- void MessageCommand(int *Event,char *messageCMD) {
- if(*Event==S_GETMESSAGE) // when get new SMS,execute follows
- {
- Serial.print("AT+CMGR=1"); //Attention,we will read the message from zone No.1
- Serial.write(0x0D);
- delay(20);
- while(Serial.available()<1);
- while(Serial.available()>0) //Read out the data that has just been print,AT+CMGR=1.
- {
- char CharRead=Serial.read();
- delay(5);
- }
- comdata="";
- while(Serial.available()<1); //waiting data.......
- while(Serial.available())
- {
- char CharRead=char(Serial.read());
- if(CharRead!=10&&CharRead!=13)
- {
- comdata +=CharRead;
- }
- delay(2);
- }
- int messageIndex1=comdata.lastIndexOf(M_FLAG);
- if( messageIndex1!=-1)
- {
- for(int i=0;i<2;i++)
- {
- messageCMD[i]=comdata[messageIndex1+2+i];
- }
- comdata="";
- }
- *Event=S_READMESSAGE;
- if(*Event==S_READMESSAGE)
- {
- if(messageCMD[0]=='H'||messageCMD[0]=='h')
- {
- digitalWrite(int(messageCMD[1]-'0'),HIGH);
- }
- if(messageCMD[0]=='L'||messageCMD[0]=='l')
- {
- digitalWrite(int(messageCMD[1]-'0'),LOW);
- }
- }
- Serial.print("AT+CMGD=1"); //deleted the Received SMS message in Card.
- Serial.write(0x0D);
- delay(20);
- *Event=0;
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|