风筝
发表于: 2017-7-3 12:00:58 | 显示全部楼层

Arduino-SIM900A-GPRS_0.jpg


今天我们在这里介绍一个非常有趣的项目,我们将使用Arduino开发板和GPRS将数据发送到SparkFun服务器。这是一个基于IoT的项目,我们将使用GSM模块SIM900A将一些数据发送到互联网上的Web服务器。


在这个项目中,我们将使用4x4数字键盘输入一些文本,并使用Arduino和GPRS将其发送到SparkFun网站,从那里您可以通过互联网在任何地方看到这些数据。在这里,我们还连接​​了一个16x2液晶显示器,以便在本地查看数据。这种数据发送技术在物联网项目中非常有用,您可以在世界上任何地方监控数据,如监测温度和湿度、监测车辆位置、监测心跳、监测空气污染等等。


所需的组件:

1.    Arduino开发板

2.    GSM模块SIM900A

3.   16x2 LCD显示屏

4.    4x4矩阵键盘

5.    面包板或PCB

6.    连接导线

7.    12V电源

8.    SIM卡


在GSM模块中使用GPRS

这里我们使用了一个带有SIM卡的GSM模块,用于GPRS连接。在这个项目中,GPRS负责向Sparkfun服务器发送数据。以前,我们已经做了很多项目,其中我们使用Wi-Fi模块ESP8266通过互联网将数据发送到不同的服务器。但这次我们使用的是GPRS。


GPRS代表通用分组无线电服务,其是基于分组的无线通信服务,工作在数据速率为56-114kbps并提供到因特网的连接。

对于GPRS,我们不需要购买任何特殊的模块或硬件,因为GSM已经拥有GPRS设备。我们只需要使用以前的项目中用于GSM接口的相同方法或AT命令来访问它。 SIMCOM SIM900A GSM模块数据表中已经提到了许多AT命令。

GSM-Module-SIM900A.jpg


现在通过使用GPRS向服务器发送数据,首先我们需要初始化GSM模块。

GSM初始化命令:

  1. AT: - 该命令用于检查GSM模块是否响应。
  2. AT + CPIN? : - 该命令用于检查SIM卡是否插入GSM模块。
  3. ATE0: - 用于禁用回声
  4. ATE1: - 用于启用回声
复制代码

GPRS互联网初始化命令:

  1. AT + CIPSHUT: - 关闭TCP端口显式地意味着断开连接
  2. AT + CGATT? : - 检查SIM卡是否具有互联网连接
  3. AT + CSTT =“APN”,“userName”,“Pass”: - 连接到互联网
  4. (例如:AT + CSTT =“airtelgprs.com”,“”,“”)
  5. AT + CIICR:提出无线网络。检查SIM卡有数据包或余额
  6. AT + CIFSR: - 获取IP(有时没有这个命令GSM不工作,所以使用这个命令)
  7. AT + CIPSTART =“TCP”,“SERVER IP”,“PORT”: - 用于创建与我们提供的服务器的TCP连接代替SERVER IP
  8. AT + CIPSEND: - 该命令用于向服务器发送数据。输入后,此命令服务器请求数据。
复制代码

输入数据后,我们发送26到服务器。如果一切顺利的话,数据将被成功发布到服务器,并且SparkFun服务器使用通过或失败的字符串进行响应。

sparkfun-logs-data-sent-using-GPRS-GSM-and-arduino.png

send-data-to-sparkfun-using-GPRS-GSM-and-arduino.png


工作原理说明:

从GSM模块GPRS发送数据的过程很简单。在这个项目中,我们通过使用键盘输入一些字符串或单词,并发送到服务器。相同的字符串或单词将出现在LCD上,然后按D / OK将输入字符串发送到服务器。在这里,我们创建了一个字母数字键盘,用于将字符或数字值输入到Arduino或LCD。 C /清除编程为退格。

send-data-to-webserver-using-GPRS-GSM-and-arduino-block-diagram.png


send-data-to-webserver-using-GPRS-GSM-and-arduino.jpg

字母数字是通过使用相同的4x4矩阵键盘输入数字和字母的方法。这里我们编码了同样的4x4键盘,Arduino也接受字母。检查文章末尾的完整代码。

keypad5.jpg 4x4-alphanumeric-keypad.jpg


电路说明

要将数据发送到SparkFun Server,我们需要将键盘和GSM模块连接到Arduino开发板。在这个项目中,我们使用Arduino从键盘输入字符串并发送命令到GSM / GPRS模块。 GSM / GPRS用于与互联网通信,将数据发送到Sparkfun服务器。 GSM模块的Rx和Tx引脚分别与Arduino的引脚D3和D2直接相连(Arduino和GSM的接地必须彼此连接)。 16x2 LCD用于显示输入字符串,并显示欢迎信息和数据发送状态。该16x2 LCD的引脚Rs、en、d4、d5、d6和d7分别与Arduino的引脚号14、15、16、17、18和19连接。4x4键盘用于Arduino的输入字符串,其行引脚R1、R2、R3、R4直接链接到Arduino的引脚11、1、9、8,列引脚C1、C2、C3、C49连接到Arduino的引脚7、6、5、4。

在这里,我们还将GSM Tx引脚连接到Arduino的Tx,以通过串行监视器获取响应数据。

send-data-to-webserver-using-GPRS-GSM-and-arduino-circuit-diagram.png


编程说明:

这个项目的编程部分对于初学者来说有点复杂,但是通过做一点练习,你就可以理解。在本代码中,我们使用键盘库来连接简单的键盘输入数字。为了输入字母,我们使用了相同的库和键盘,但使用了多个功能,使其成为字母数字键盘。意味着我们使用的是每个键的多功能,并可以使用10个键输入所有的字符和整数。

如果我们按键2(ABC2),它将在LCD显示'A',如果我们再次按它,​​它将用'B'替换'A',如果再次按它,​​它将在同一个地方显示'C' 。如果按下键等待一段时间,光标将自动移动到LCD中的下一个位置。然后我们就可以输入下一个字符或数字。同样的程序也适用于其他键。它与旧手机的键盘相同。


下面我们包括Keypad.h库和定义数组矩阵的键:

  1. #include <Keypad.h>               // keypad library for interfacing keypad
  2. const byte ROWS = 4; //four rows
  3. const byte COLS = 4; //four columns
  4. int x=0;
  5. int y=0;
  6. int n=0;
  7. int minValue=0;
  8. int maxValue=0;
  9. char keyPress=0;
  10. int keyPressTime=100;
  11. String msg="";

  12. char hexaKeys[ROWS][COLS] =
  13. {
  14.   {'1','2','3','A'},
  15.   {'4','5','6','B'},
  16.   {'7','8','9','C'},
  17.   {'*','0','#','D'}
  18. };

  19. byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
  20. byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
复制代码

void getkey函数用于输入字母

  1. void getkey(int minValue, int maxValue, char keyPress)
  2. {
  3.    int ch=minValue;
  4.    int pressed=1;
  5.    char key=keyPress;
  6.    lcd.noBlink();
  7.    for(int i=0;i<keyPressTime;i++)
  8.    {     
  9.       if(key==keyPress)
  10.       {
  11.          lcd.setCursor(x,y);
  12.          lcd.print(alpha[ch]);
  13.          ch++;
  14.          if(ch>maxValue)
  15.          ch=minValue;
  16.          i=0;   
  17.       }
  18.       ....
  19.       .....
复制代码

下面的void initGSM()和void initGPRS()函数用于初始化GSM模块和GPRS

  1. void initGSM()
  2. {
  3.   connectGSM("AT","OK");
  4.   connectGSM("ATE1","OK");
  5.   connectGSM("AT+CPIN?","READY");
  6. }
  7. void initGPRS()
  8. {
  9.   connectGSM("AT+CIPSHUT","OK");
  10.   connectGSM("AT+CGATT=1","OK");
  11.   connectGSM("AT+CSTT="airtelgprs.com","",""","OK");
  12.   connectGSM("AT+CIICR","OK");
  13.   delay(1000);
  14.   Serial1.println("AT+CIFSR");
  15.   delay(1000);
  16. }
复制代码

以下部分代码用于创建URL,并通过URL将数据发送到服务器。

  1. else if(key == 'D')
  2.     {
  3.       lcd.clear();
  4.       lcd.noBlink();
  5.       lcd.print("Sending Data");
  6.       lcd.setCursor(0,1);
  7.       lcd.print("To Server");
  8.       url="GET /input/";
  9.       url+=publicKey;
  10.       url+="?private_key=";
  11.       url+=pvtKey;
  12.       url+="&log=";
  13.       url+=msg;
  14.       url+=" HTTP/1.0\r\n\r\n";
  15.       String svr=Start+","+ip+","+port;
  16.       delay(1000);
  17.       connectGSM(svr,"CONNECT");
  18.       delay(1000);
  19.       int len = url.length();
  20.       String str="";
  21.       str=SendCmd+len;
  22.       sendToServer(str);
复制代码

然后,我们可以通过GPRS向SparkFun服务器上发送任何数据,以便从世界任何地方进行监控。


代码:

  1. #include <SoftwareSerial.h>  // serial software library for interfacing gsm module
  2. SoftwareSerial Serial1(2, 3); // RX, TX  // connect gsm Tx at D2 and Rx at D3
  3. #include<LiquidCrystal.h>     // LCD library for interfacing LCD
  4. LiquidCrystal lcd(14,15,16,17,18,19);   // connect rs,en,d4,d5,d6,d7 respectevely
  5. String pvtKey="wY9DPG5vzpH99KNrNkx2";     // private key for posting data to sparkfun
  6. String publicKey="w5nXxM6rp0tww5YVYg3G";  // public key for open page of sparkfun
  7. String url="";
  8. String ip=""data.sparkfun.com"";        // sparkfun server ip or url
  9. int port=80;                              // sparkfun server port
  10. String SendCmd="AT+CIPSEND=";           // sending number of byte command
  11. String Start="AT+CIPSTART="TCP"";     // TCPIP start command
  12. // strings and variables
  13. //String msg="";
  14. String instr="";
  15. String str_sms="";
  16. String str1="";
  17. int i=0,temp=0;
  18. #include <Keypad.h>               // keypad library for interfacing keypad
  19. const byte ROWS = 4; //four rows
  20. const byte COLS = 4; //four columns
  21. int x=0;
  22. int y=0;
  23. int n=0;
  24. int minValue=0;
  25. int maxValue=0;
  26. char keyPress=0;
  27. int keyPressTime=100;
  28. String msg="";
  29. char hexaKeys[ROWS][COLS] =
  30. {
  31.   {'1','2','3','A'},
  32.   {'4','5','6','B'},
  33.   {'7','8','9','C'},
  34.   {'*','0','#','D'}
  35. };
  36. byte rowPins[ROWS] = {11, 10, 9, 8}; //connect to the row pinouts of the keypad
  37. byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
  38. Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  39. String alpha="1!@_$%?ABC2DEF3GHI4JKL5MNO6PQRS7TUV8WXYZ9* 0#";
  40. void setup()
  41. {
  42.   Serial1.begin(9600);    // init serial1 for GSM
  43.   lcd.begin(16,2);        // init LCD
  44.   lcd.print("Sending Data  ");
  45.   lcd.setCursor(0,1);
  46.   lcd.print("to Server");
  47.   delay(2000);
  48.   lcd.clear();
  49.   lcd.print("Circuit Digest");
  50.   lcd.setCursor(0,1);
  51.   lcd.print("Saddam Khan");
  52.   delay(2000);
  53.   lcd.clear();
  54.   lcd.print("Initializing GSM");
  55.   initGSM();      // init GSM module
  56.   lcd.clear();
  57.   lcd.print("Initializing GPRS");
  58.   initGPRS();     // init GPRS in GSM Module
  59.   lcd.clear();
  60.   lcd.print("System Ready");
  61.   delay(2000);
  62. }
  63. void loop()
  64. {
  65.   int n=0;
  66.   lcd.clear();
  67.   lcd.noCursor();
  68.   while(1)
  69.   {
  70.     lcd.cursor();
  71.     char key = customKeypad.getKey();
  72.     if(key=='1')
  73.       getkey(0, 6, key);
  74.      if(key=='2')
  75.       getkey(7, 10, key);
  76.      else if(key=='3')
  77.       getkey(11, 14, key);
  78.      else if(key=='4')
  79.       getkey(15, 18, key);
  80.     else if(key=='5')
  81.       getkey(19, 22, key);
  82.     else if(key=='6')
  83.       getkey(23, 26, key);
  84.     else if(key=='7')
  85.       getkey(27, 31, key);
  86.     else if(key=='8')
  87.       getkey(32,35, key);
  88.     else if(key=='9')
  89.       getkey(36, 40, key);
  90.     else if(key=='*')
  91.       getkey(41, 41, key);
  92.     else if(key=='0')
  93.       getkey(42, 43, key);
  94.     else if(key=='#')
  95.       getkey(44, 44, key);
  96.       
  97.     else if(key == 'C')
  98.     {
  99.          x--;
  100.          lcd.setCursor(x,y);
  101.          lcd.print(" ");
  102.          n--;
  103.          msg[n]=' ';
  104.          lcd.setCursor(x,y);
  105.     }
  106.     else if(key == 'D')
  107.     {
  108.       lcd.clear();
  109.       lcd.noBlink();
  110.       lcd.print("Sending Data");
  111.       lcd.setCursor(0,1);
  112.       lcd.print("To Server");
  113.       url="GET /input/";
  114.       url+=publicKey;
  115.       url+="?private_key=";
  116.       url+=pvtKey;
  117.       url+="&log=";
  118.       url+=msg;
  119.       url+=" HTTP/1.0\r\n\r\n";
  120.       String svr=Start+","+ip+","+port;
  121.       delay(1000);
  122.       connectGSM(svr,"CONNECT");
  123.       delay(1000);
  124.       int len = url.length();
  125.       String str="";
  126.       str=SendCmd+len;
  127.       sendToServer(str);
  128.        Serial1.print(url);
  129.       delay(1000);
  130.       Serial1.write(0x1A);
  131.       delay(1000);
  132.       lcd.clear();
  133.       lcd_status();
  134.        //  clearmsg();
  135.        n=0;
  136.        i=0;
  137.        x=0;
  138.        y=0;
  139.        msg="";
  140.     }
  141.   }
  142. }
  143. void getkey(int minValue, int maxValue, char keyPress)
  144. {
  145.    int ch=minValue;
  146.    int pressed=1;
  147.    char key=keyPress;
  148.    lcd.noBlink();
  149.    for(int i=0;i<keyPressTime;i++)
  150.    {     
  151.       if(key==keyPress)
  152.       {
  153.          lcd.setCursor(x,y);
  154.          lcd.print(alpha[ch]);
  155.          ch++;
  156.          if(ch>maxValue)
  157.          ch=minValue;
  158.          i=0;   
  159.       }
  160.       key=customKeypad.getKey();
  161.       delay(10);
  162.     }
  163.      if(pressed)
  164.      {  
  165.         x++;
  166.         msg+=alpha[ch-1];
  167.         n++;
  168.         if(x>15)
  169.         {
  170.           x=0;
  171.           y=1;
  172.         }
  173.      }
  174.     pressed=0;
  175.     lcd.blink();
  176. }
  177. void lcd_status()
  178. {
  179.   lcd.clear();
  180.   lcd.print("Data Sent to");
  181.   lcd.setCursor(0,1);
  182.   lcd.print("Server");
  183.   delay(2000);
  184.   lcd.clear();
  185. }
  186. void sendToServer(String str)
  187. {
  188.   Serial1.println(str);
  189.   delay(1000);
  190. }
  191. void initGSM()
  192. {
  193.   connectGSM("AT","OK");
  194.   connectGSM("ATE1","OK");
  195.   connectGSM("AT+CPIN?","READY");
  196. }
  197. void initGPRS()
  198. {
  199.   connectGSM("AT+CIPSHUT","OK");
  200.   connectGSM("AT+CGATT=1","OK");
  201.   connectGSM("AT+CSTT="airtelgprs.com","",""","OK");
  202.   connectGSM("AT+CIICR","OK");
  203.   delay(1000);
  204.   Serial1.println("AT+CIFSR");
  205.   delay(1000);
  206. }
  207. void connectGSM (String cmd, char *res)
  208. {
  209.   while(1)
  210.   {
  211.     Serial.println(cmd);
  212.     Serial1.println(cmd);
  213.     delay(500);
  214.     while(Serial1.available()>0)
  215.     {
  216.       if(Serial1.find(res))
  217.       {
  218.         delay(1000);
  219.         return;
  220.       }
  221.     }
  222.     delay(1000);
  223.    }
  224. }
  225. /*
  226. Public URL
  227. http://data.sparkfun.com/streams/w5nXxM6rp0tww5YVYg3G
  228. Public Key
  229. w5nXxM6rp0tww5YVYg3G
  230. Private Key
  231. wY9DPG5vzpH99KNrNkx2
  232. Keep this key secret, and in a safe place.  You will not be able to retrieve it.
  233. Delete Key
  234. xxxxxxxxxxxxx
  235. This key can only be used once.  Keep this key secret, and in a safe place.  You will not be able to retrieve it.
  236. Logging using query string parameters
  237. Format:
  238. http://data.sparkfun.com/input/[publicKey]?private_key=[privateKey]&log=[value]
  239. Example:
  240. http://data.sparkfun.com/input/w5nXxM6rp0tww5YVYg3G?private_key=wY9DPG5vzpH99KNrNkx2&log=22.21
  241. */
复制代码


跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-20 01:50 , Processed in 0.156136 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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