当前位置:   article > 正文

C51_day6:WIFI模块_c语言 wifi模组 at指令框架

c语言 wifi模组 at指令框架

wifi模块01_AT指令

先通过连接wifi从而连接网络,再连接服务器

  1. #include "reg52.h"
  2. #include "intrins.h"
  3. #include <string.h>
  4. #define SIZE 12
  5. sfr AUXR = 0x8E;
  6. sbit D5 = P3^7;
  7. char cmd[SIZE];
  8. code char LJWL[] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n";
  9. code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n";
  10. char TCMS[] = "AT+CIPMODE=1\r\n";
  11. char SJCS[] = "AT+CIPSEND\r\n";
  12. void UartInit(void) //9600bps@11.0592MHz
  13. {
  14. AUXR = 0x01;
  15. SCON = 0x50; //配置串口工作方式1,REN使能接收
  16. TMOD &= 0xF0;
  17. TMOD |= 0x20;//定时器1工作方式位8位自动重装
  18. TH1 = 0xFD;
  19. TL1 = 0xFD;//9600波特率的初值
  20. TR1 = 1;//启动定时器
  21. EA = 1;//开启总中断
  22. ES = 1;//开启串口中断
  23. }
  24. void Delay1000ms() //@11.0592MHz
  25. {
  26. unsigned char i, j, k;
  27. _nop_();
  28. i = 8;
  29. j = 1;
  30. k = 243;
  31. do
  32. {
  33. do
  34. {
  35. while (--k);
  36. } while (--j);
  37. } while (--i);
  38. }
  39. void sendByte(char data_msg)
  40. {
  41. SBUF = data_msg;
  42. while(!TI);
  43. TI = 0;
  44. }
  45. void sendString(char* str)
  46. {
  47. while( *str != '\0'){
  48. sendByte(*str);
  49. str++;
  50. }
  51. }
  52. void main()
  53. {
  54. D5 = 1;
  55. //配置C51串口的通信方式
  56. UartInit();
  57. while(1){
  58. //Delay1000ms();
  59. //往发送缓冲区写入数据,就完成数据的发送
  60. //sendString("chenlichen shuai\r\n");
  61. sendString(LJWL);
  62. Delay1000ms();
  63. Delay1000ms();
  64. Delay1000ms();
  65. Delay1000ms();
  66. Delay1000ms();
  67. sendString(LJFWQ);
  68. Delay1000ms();
  69. Delay1000ms();
  70. Delay1000ms();
  71. Delay1000ms();
  72. Delay1000ms();
  73. sendString(TCMS);
  74. Delay1000ms();
  75. Delay1000ms();
  76. Delay1000ms();
  77. Delay1000ms();
  78. Delay1000ms();
  79. sendString(SJCS);
  80. Delay1000ms();
  81. Delay1000ms();
  82. Delay1000ms();
  83. Delay1000ms();
  84. Delay1000ms();
  85. }
  86. }
  87. void Uart_Handler() interrupt 4
  88. {
  89. static int i = 0;//静态变量,被初始化一次
  90. if(RI)//中断处理函数中,对于接收中断的响应
  91. {
  92. RI = 0;//清除接收中断标志位
  93. cmd[i] = SBUF;
  94. i++;
  95. if(i == SIZE){
  96. i = 0;
  97. }
  98. if(strstr(cmd,"en")){
  99. D5 = 0;//点亮D5
  100. i = 0;
  101. memset(cmd,'\0',SIZE);
  102. }
  103. if(strstr(cmd,"se")){
  104. D5 = 1;//熄灭D5
  105. i = 0;
  106. memset(cmd,'\0',SIZE);
  107. }
  108. }
  109. if(TI);
  110. }

 wifi模块02_通过TCP通信点灯

  1. #include "reg52.h"
  2. #include "intrins.h"
  3. #include <string.h>
  4. #define SIZE 12
  5. sfr AUXR = 0x8E;
  6. sbit D5 = P3^7;
  7. char cmd[SIZE];
  8. code char LJWL[] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n";
  9. code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n";
  10. char TCMS[] = "AT+CIPMODE=1\r\n";
  11. char SJCS[] = "AT+CIPSEND\r\n";
  12. void UartInit(void) //9600bps@11.0592MHz
  13. {
  14. AUXR = 0x01;
  15. SCON = 0x50; //配置串口工作方式1,REN使能接收
  16. TMOD &= 0xF0;
  17. TMOD |= 0x20;//定时器1工作方式位8位自动重装
  18. TH1 = 0xFD;
  19. TL1 = 0xFD;//9600波特率的初值
  20. TR1 = 1;//启动定时器
  21. EA = 1;//开启总中断
  22. ES = 1;//开启串口中断
  23. }
  24. void Delay1000ms() //@11.0592MHz
  25. {
  26. unsigned char i, j, k;
  27. _nop_();
  28. i = 8;
  29. j = 1;
  30. k = 243;
  31. do
  32. {
  33. do
  34. {
  35. while (--k);
  36. } while (--j);
  37. } while (--i);
  38. }
  39. void sendByte(char data_msg)
  40. {
  41. SBUF = data_msg;
  42. while(!TI);
  43. TI = 0;
  44. }
  45. void sendString(char* str)
  46. {
  47. while( *str != '\0'){
  48. sendByte(*str);
  49. str++;
  50. }
  51. }
  52. void main()
  53. {
  54. int mark = 0;
  55. D5 = 1;
  56. //配置C51串口的通信方式
  57. UartInit();
  58. while(1){
  59. //Delay1000ms();
  60. //往发送缓冲区写入数据,就完成数据的发送
  61. //sendString("chenlichen shuai\r\n");
  62. if(mark == 0){
  63. sendString(LJWL);
  64. Delay1000ms();
  65. Delay1000ms();
  66. Delay1000ms();
  67. Delay1000ms();
  68. Delay1000ms();
  69. sendString(LJFWQ);
  70. Delay1000ms();
  71. Delay1000ms();
  72. Delay1000ms();
  73. Delay1000ms();
  74. Delay1000ms();
  75. sendString(TCMS);
  76. Delay1000ms();
  77. Delay1000ms();
  78. Delay1000ms();
  79. Delay1000ms();
  80. Delay1000ms();
  81. sendString(SJCS);
  82. Delay1000ms();
  83. Delay1000ms();
  84. Delay1000ms();
  85. Delay1000ms();
  86. Delay1000ms();
  87. mark = 1;
  88. }else{
  89. sendString("chenlichen shuai\r\n");
  90. Delay1000ms();
  91. }
  92. }
  93. }
  94. void Uart_Handler() interrupt 4
  95. {
  96. static int i = 0;//静态变量,被初始化一次
  97. if(RI)//中断处理函数中,对于接收中断的响应
  98. {
  99. RI = 0;//清除接收中断标志位
  100. cmd[0] = SBUF;
  101. if(cmd[0] == '1'){
  102. D5 = 0;//点亮D5
  103. }
  104. if(cmd[0] == '0'){
  105. D5 = 1;//熄灭D5
  106. }
  107. }
  108. if(TI);
  109. }

 wifi模块03_优化连接过程

  1. #include "reg52.h"
  2. #include "intrins.h"
  3. #include <string.h>
  4. #define SIZE 12
  5. sfr AUXR = 0x8E;
  6. sbit D5 = P3^7;
  7. sbit D6 = P3^6;
  8. char buffer[SIZE];
  9. code char LJWL[] = "AT+CWJAP=\"TP-LINK_3E30\",\"18650711783\"\r\n"; //入网指令
  10. code char LJFWQ[] = "AT+CIPSTART=\"TCP\",\"192.168.0.113\",8880\r\n"; //连接服务器指令
  11. char TCMS[] = "AT+CIPMODE=1\r\n"; //透传指令
  12. char SJCS[] = "AT+CIPSEND\r\n"; //数据传输开始指令
  13. char RESET[] = "AT+RST\r\n"; //重启模块指令
  14. char AT_OK_Flag = 0; //OK返回值的标志位
  15. char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
  16. void UartInit(void) //9600bps@11.0592MHz
  17. {
  18. AUXR = 0x01;
  19. SCON = 0x50; //配置串口工作方式1,REN使能接收
  20. TMOD &= 0xF0;
  21. TMOD |= 0x20;//定时器1工作方式位8位自动重装
  22. TH1 = 0xFD;
  23. TL1 = 0xFD;//9600波特率的初值
  24. TR1 = 1;//启动定时器
  25. EA = 1;//开启总中断
  26. ES = 1;//开启串口中断
  27. }
  28. void Delay1000ms() //@11.0592MHz
  29. {
  30. unsigned char i, j, k;
  31. _nop_();
  32. i = 8;
  33. j = 1;
  34. k = 243;
  35. do
  36. {
  37. do
  38. {
  39. while (--k);
  40. } while (--j);
  41. } while (--i);
  42. }
  43. void sendByte(char data_msg)
  44. {
  45. SBUF = data_msg;
  46. while(!TI);
  47. TI = 0;
  48. }
  49. void sendString(char* str)
  50. {
  51. while( *str != '\0'){
  52. sendByte(*str);
  53. str++;
  54. }
  55. }
  56. void main()
  57. {
  58. int mark = 0;
  59. D5 = D6 = 1;//灭状态灯
  60. //配置C51串口的通信方式
  61. UartInit();
  62. Delay1000ms();//给espwifi模块上电时间
  63. //发送联网AT指令并等待成功
  64. sendString(LJWL);
  65. //while(!AT_Connect_Net_Flag);
  66. while(!AT_OK_Flag);
  67. AT_OK_Flag = 0;
  68. //发送连服务器指令并等待成功
  69. sendString(LJFWQ);
  70. while(!AT_OK_Flag);
  71. AT_OK_Flag = 0;
  72. //发送透传模式指令并等待成功
  73. sendString(TCMS);
  74. while(!AT_OK_Flag);
  75. AT_OK_Flag = 0;
  76. //发送数据传输指令并等待成功
  77. sendString(SJCS);
  78. while(!AT_OK_Flag);
  79. if(AT_Connect_Net_Flag){
  80. D5 = 0;//点亮D5,代表入网成功
  81. }
  82. if(AT_OK_Flag){
  83. D6 = 0;//点亮D6,代表连接服务器并打开透传模式成功
  84. }
  85. while(1){
  86. Delay1000ms();
  87. //“心跳包”
  88. sendString("chenlichen shuai\r\n");
  89. }
  90. }
  91. void Uart_Handler() interrupt 4
  92. {
  93. static int i = 0;//静态变量,被初始化一次
  94. char tmp;
  95. if(RI)//中断处理函数中,对于接收中断的响应
  96. {
  97. RI = 0;//清除接收中断标志位
  98. tmp = SBUF;
  99. if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == 'F'){
  100. i = 0;
  101. }
  102. buffer[i++] = tmp;
  103. //入网成功的判断依据WIFI GOT IP
  104. if(buffer[0] == 'W' && buffer[5] == 'G'){
  105. AT_Connect_Net_Flag = 1;
  106. memset(buffer, '\0', SIZE);
  107. }
  108. //连接服务器等OK返回值指令的判断
  109. if(buffer[0] == 'O' && buffer[1] == 'K'){
  110. AT_OK_Flag = 1;
  111. memset(buffer, '\0', SIZE);
  112. }
  113. //联网失败出现FAIL字样捕获
  114. if(buffer[0] == 'F' && buffer[1] == 'A'){
  115. for(i=0;i<5;i++){
  116. D5 = 0;
  117. Delay1000ms();
  118. D5 = 1;
  119. Delay1000ms();
  120. }
  121. sendString(RESET);
  122. memset(buffer, '\0', SIZE);
  123. }
  124. //灯控指令
  125. if(buffer[0] == 'L' && buffer[2] == '1'){
  126. D5 = 0;//点亮D5
  127. memset(buffer, '\0', SIZE);
  128. }
  129. if(buffer[0] == 'L' && buffer[2] == '0'){
  130. D5 = 1;//熄灭D5
  131. memset(buffer, '\0', SIZE);
  132. }
  133. if(i == 12) i = 0;
  134. }
  135. }

 wifi模块04_esp工作在路由服务器模式

  1. #include "reg52.h"
  2. #include "intrins.h"
  3. #include <string.h>
  4. #define SIZE 12
  5. sfr AUXR = 0x8E;
  6. sbit D5 = P3^7;
  7. sbit D6 = P3^6;
  8. char buffer[SIZE];
  9. //1 工作在路由模式
  10. char LYMO[] = "AT+CWMODE=2\r\n";
  11. //2 使能多链接
  12. char DLJ[] = "AT+CIPMUX=1\r\n";
  13. //3 建立TCPServer
  14. char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333
  15. //发送数据
  16. char FSSJ[] = "AT+CIPSEND=0,5\r\n";
  17. char AT_OK_Flag = 0; //OK返回值的标志位
  18. char AT_Connect_Net_Flag = 0; //WIFI GOT IP返回值的标志位
  19. char Client_Connect_Flag = 0;
  20. void UartInit(void) //9600bps@11.0592MHz
  21. {
  22. AUXR = 0x01;
  23. SCON = 0x50; //配置串口工作方式1,REN使能接收
  24. TMOD &= 0xF0;
  25. TMOD |= 0x20;//定时器1工作方式位8位自动重装
  26. TH1 = 0xFD;
  27. TL1 = 0xFD;//9600波特率的初值
  28. TR1 = 1;//启动定时器
  29. EA = 1;//开启总中断
  30. ES = 1;//开启串口中断
  31. }
  32. void Delay1000ms() //@11.0592MHz
  33. {
  34. unsigned char i, j, k;
  35. _nop_();
  36. i = 8;
  37. j = 1;
  38. k = 243;
  39. do
  40. {
  41. do
  42. {
  43. while (--k);
  44. } while (--j);
  45. } while (--i);
  46. }
  47. void sendByte(char data_msg)
  48. {
  49. SBUF = data_msg;
  50. while(!TI);
  51. TI = 0;
  52. }
  53. void sendString(char* str)
  54. {
  55. while( *str != '\0'){
  56. sendByte(*str);
  57. str++;
  58. }
  59. }
  60. void main()
  61. {
  62. int mark = 0;
  63. D5 = D6 = 1;//灭状态灯
  64. //配置C51串口的通信方式
  65. UartInit();
  66. Delay1000ms();//给espwifi模块上电时间
  67. sendString(LYMO);
  68. while(!AT_OK_Flag);
  69. AT_OK_Flag = 0;
  70. sendString(DLJ);
  71. while(!AT_OK_Flag);
  72. AT_OK_Flag = 0;
  73. sendString(JLFW);
  74. while(!Client_Connect_Flag);
  75. AT_OK_Flag = 0;
  76. if(Client_Connect_Flag){
  77. D5 = 0;//点亮D5,代表有客户端接入
  78. D6 = 0;
  79. }
  80. while(1){
  81. //4 发送数据
  82. sendString(FSSJ);
  83. Delay1000ms();
  84. Delay1000ms();
  85. sendString("Hello");
  86. Delay1000ms();
  87. Delay1000ms();
  88. }
  89. }
  90. void Uart_Handler() interrupt 4
  91. {
  92. static int i = 0;//静态变量,被初始化一次
  93. char tmp;
  94. if(RI)//中断处理函数中,对于接收中断的响应
  95. {
  96. RI = 0;//清除接收中断标志位
  97. tmp = SBUF;
  98. if(tmp == 'W' || tmp == 'O' || tmp == 'L' || tmp == '0' || tmp == ':'){
  99. i = 0;
  100. }
  101. buffer[i++] = tmp;
  102. //入网成功的判断依据WIFI GOT IP
  103. if(buffer[0] == 'W' && buffer[5] == 'G'){
  104. AT_Connect_Net_Flag = 1;
  105. memset(buffer, '\0', SIZE);
  106. }
  107. //连接服务器等OK返回值指令的判断
  108. if(buffer[0] == 'O' && buffer[1] == 'K'){
  109. AT_OK_Flag = 1;
  110. memset(buffer, '\0', SIZE);
  111. }
  112. if(buffer[0] == '0' && buffer[2] == 'C'){
  113. Client_Connect_Flag = 1;
  114. memset(buffer, '\0', SIZE);
  115. }
  116. //灯控指令
  117. if(buffer[0] == ':' && buffer[1] == 'o' && buffer[2] == 'p'){
  118. D5 = 0;//点亮D5
  119. memset(buffer, '\0', SIZE);
  120. }
  121. if(buffer[0] == ':' && buffer[1] == 'c' && buffer[2] == 'l'){
  122. D5 = 1;//熄灭D5
  123. memset(buffer, '\0', SIZE);
  124. }
  125. if(i == 12) i = 0;
  126. }
  127. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/410593
推荐阅读
相关标签
  

闽ICP备14008679号