Arduino+GSM模块实现短信远程控制
本帖最后由 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=""; //using to hold Command,for example,if the value is H8,stands the pin 8 will output a high level.
String comdata = "";
int gsmDriverPin = {10,11,12};
int messageControlPin={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,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,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=comdata;
}
comdata="";
}
*Event=S_READMESSAGE;
if(*Event==S_READMESSAGE)
{
if(messageCMD=='H'||messageCMD=='h')
{
digitalWrite(int(messageCMD-'0'),HIGH);
}
if(messageCMD=='L'||messageCMD=='l')
{
digitalWrite(int(messageCMD-'0'),LOW);
}
}
Serial.print("AT+CMGD=1"); //deleted the Received SMS messagein Card.
Serial.write(0x0D);
delay(20);
*Event=0;
}
}
另外大家在使用GSM模块的时候,可以先用USB模式调试,然后再脱机运行,调试的代码在DFRobot的官网都有,我也是先用的USB模式调试,然后才开始尝试用Arduino 控制的。 很酷.........:)我也弄一个.... 您用的GSM模是什么型号啊? wing 发表于 2012-8-25 09:30 static/image/common/back.gif
您用的GSM模是什么型号啊?
SIM548C
具体参数你可以点击网址:http://www.dfrobot.com/index.php?route=product/product&filter_name=GSM&product_id=673
里面有详细的datasheet。呵呵............... 东西不错,价格接受不了 本帖最后由 thelover 于 2012-8-25 21:21 编辑
向你打听点事情,taobao上的那个DFRobot是正牌的代理么?就是你实习的公司么?
我看到他们只有一款二合一的模块在卖,而且还巨贵。 thelover 发表于 2012-8-25 21:11 static/image/common/back.gif
向你打听点事情,taobao上的那个DFRobot是正牌的代理么?就是你实习的公司么?
我看到他们只有一款二合一 ...
恩,应该是的............. 谢谢, 我估计你的判断依据是"巨贵" GSM 模块的话推荐SIM300或者TC35i,性价比比较高~网上的相关资料也比较多! thelover 发表于 2012-8-26 00:48 static/image/common/back.gif
谢谢, 我估计你的判断依据是"巨贵"
同意楼下的,因为该模块功能确实很多,如果只用GSM模块的相关功能的话有点大才小用 DFRobot的东西太贵了。。 不错,是我想要的。先收藏了。 特别想知道你的小房子哪里弄的{:soso_e154:}
页:
[1]