关键字:2560+sim900串口通信问题,求大能进,已经通宵3天没搞定..-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9663|回复: 6

[已解决] 关键字:2560+sim900串口通信问题,求大能进,已经通宵3天没搞定..

[复制链接]
发表于 2013-6-22 14:13 | 显示全部楼层 |阅读模式
本帖最后由 magicianbo 于 2013-6-22 14:46 编辑

坛里面大能,能否帮助下我,我用pc接2560,2560接sim900,实现打电话和发短信的功能,在串口调试工具里面,我能控制打电话和发短信,可是每一个AT命令都有返回值的,我串口调试工具却死活接受不到....已经通宵度娘搞了三天,还是无果,只能打扰各位大能,一下为代码块和接线图
[mw_shl_code=c,true]
#include <SoftwareSerial.h>
#include <String.h>

SoftwareSerial mySerial(15, 3);

void setup()
{
  mySerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);    // the GPRS baud rate
  delay(1000);
}

void loop()
{
   
  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(100);
  mySerial.println("AT + CMGS = \"+8615811******\"");//send sms message, be careful need to add a country code before the cellphone number
  delay(100);
  mySerial.println("pa ~pa~pa~!");//the content of the message
  delay(100);
  mySerial.println((char)26);//the ASCII code of the ctrl+z is 26
  delay(100);
  mySerial.println();
  Serial.write(mySerial.read());
}

///DialVoiceCall
///this function is to dial a voice call
void DialVoiceCall()
{
  mySerial.println("ATD + +86158***0****;");//dial the number
  delay(100);
  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(100);

  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(100);

  ShowSerialData();

  mySerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
  delay(1000);

  ShowSerialData();

  mySerial.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server
  delay(4000);

  ShowSerialData();

  mySerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
  delay(2000);

  ShowSerialData();

  mySerial.println("AT+HTTPINIT"); //init the HTTP request

  delay(2000);
  ShowSerialData();

  mySerial.println("AT+HTTPPARA=\"URL\",\"www.baidu.com\"");// setting the httppara, the second parameter is the website you want to access
  delay(1000);

  ShowSerialData();

  mySerial.println("AT+HTTPACTION=0");//submit the request
  delay(10000);//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(300);

  ShowSerialData();

  mySerial.println("");
  delay(100);
}

///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(1000);

  ShowSerialData();

  mySerial.println("AT+CSTT=\"CMNET\"");//start task and setting the APN,
  delay(1000);

  ShowSerialData();

  mySerial.println("AT+CIICR");//bring up wireless connection
  delay(3000);

  ShowSerialData();

  mySerial.println("AT+CIFSR");//get local IP adress
  delay(2000);

  ShowSerialData();

  mySerial.println("AT+CIPSPRT=0");
  delay(3000);

  ShowSerialData();

  mySerial.println("AT+CIPSTART=\"tcp\",\"api.cosm.com\",\"8081\"");//start up the connection
  delay(2000);

  ShowSerialData();

  mySerial.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();
  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";//
  mySerial.print("{\"method\": \"put\",\"resource\": \"/feeds/42742/\",\"params\"");//here is the feed you apply from pachube
  delay(500);
  ShowSerialData();
  mySerial.print(": {},\"headers\": {\"X-PachubeApiKey\":");//in here, you should replace your pachubeapikey
  delay(500);
  ShowSerialData();
  mySerial.print(" \"_cXwr5LE8qW4a296O-cDwOUvfddFer5pGmaRigPsiO0");//pachubeapikey
  delay(500);
  ShowSerialData();
  mySerial.print("jEB9OjK-W6vej56j9ItaSlIac-hgbQjxExuveD95yc8BttXc");//pachubeapikey
  delay(500);
  ShowSerialData();
  mySerial.print("Z7_seZqLVjeCOmNbEXUva45t6FL8AxOcuNSsQS\"},\"body\":");
  delay(500);
  ShowSerialData();
  mySerial.print(" {\"version\": \"1.0.0\",\"datastreams\": ");
  delay(500);
  ShowSerialData();
  mySerial.println("[{\"id\": \"01\",\"current_value\": \"" + barometer + "\"},");
  delay(500);
  ShowSerialData();
  mySerial.println("{\"id\": \"02\",\"current_value\": \"" + humidity + "\"},");
  delay(500);
  ShowSerialData();
  mySerial.println("{\"id\": \"03\",\"current_value\": \"" + moisture + "\"},");
  delay(500);
  ShowSerialData();
  mySerial.println("{\"id\": \"04\",\"current_value\": \"" + temperature + "\"}]},\"token\": \"lee\"}");


  delay(500);
  ShowSerialData();

  mySerial.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet
  mySerial.println();

  ShowSerialData();

  mySerial.println("AT+CIPCLOSE");//close the connection
  delay(100);
  ShowSerialData();
}

void ShowSerialData()
{
  while(mySerial.available()!=0)
    Serial.write(mySerial.read());
}[/mw_shl_code]

2560 的3口接sim900的RX,2560的15口接sim900的TX
发表于 2013-6-22 14:31 | 显示全部楼层
不要用softwareSerial,直接用Serial1,这是最好的方案。
如果要用softwareSerial,把RX脚改到10,TX随意
 楼主| 发表于 2013-6-22 14:43 | 显示全部楼层
大能一出手,就知有没有!!!按照楼上大能所说的将RX改到10,读取SIM反馈数据完美读取.我用了3个通宵,大能用了2分钟.........我这菜到火星了....
再次谢谢奈何col
能再好奇问问为什么改到10就能读出来?我试过改成7,改成15那些都不能..
发表于 2013-6-22 16:35 | 显示全部楼层
softwareSerial通过PCINT实现,2、3引脚不带PCINT中断功能
发表于 2013-6-22 18:06 | 显示全部楼层
奈何col 发表于 2013-6-22 16:35
softwareSerial通过PCINT实现,2、3引脚不带PCINT中断功能

请问有没有这样的资料,各种开发板的各个接口都有哪些功能,我前两天看一篇文章,才发现只有9、10、11三个口带PWM控制舵机的功能

点评

使用servo类库,任意IO口都可以控制舵机  详情 回复 发表于 2013-6-22 20:59
发表于 2013-6-22 20:59 | 显示全部楼层
大米袋 发表于 2013-6-22 18:06
请问有没有这样的资料,各种开发板的各个接口都有哪些功能,我前两天看一篇文章,才发现只有9、10、11三 ...

使用servo类库,任意IO口都可以控制舵机
发表于 2014-1-8 11:09 | 显示全部楼层
这个你要仔细阅读一下SIM900的硬件手册,我这边可以提供技术资料,多多交流 www.minwireless.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-4 03:50 , Processed in 0.073599 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表