找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1383|回复: 0
打印 上一主题 下一主题
收起左侧

51单片机+8266WiFi控制家用电器程序

[复制链接]
跳转到指定楼层
楼主
只有程序,其他文件太大,无法发布

单片机源程序如下:
  1. #include "reg52.h"
  2. #include "uart.h"
  3. #include "stdio.h"

  4. #include "dht11.h"  
  5. #include "string.h"
  6. //管脚声明
  7. unsigned char Timer0_count;
  8. unsigned char Timer0_miao;
  9. unsigned char miao1;
  10. unsigned char flag_init;
  11. unsigned char RsPoint;
  12. unsigned char xdata RsBuf[100];
  13. sbit LED2 = P1^2;        //灯光控制输出
  14. sbit LED3 = P1^3;        //灯光控制输出
  15. sbit LED4 = P1^4;        //灯光控制输出
  16. sbit LED1 = P1^1;        //灯光控制输出
  17. unsigned char Key_num;
  18. unsigned char Flag_rec;
  19. unsigned char Rec_stop;
  20. #include "lcd.h"




  21. void main()
  22. {
  23.         char *str2="AT+CIPSEND=14\r\n";
  24.         unsigned char i;

  25.         memset(&RsBuf[1],' ',18);
  26.     Uart_init();
  27.     lcd_init();
  28.         delay(3000);
  29.          SendString("+++");
  30.         delay(500);
  31.         SendString("+++\r\n");
  32.         delay(500);
  33. //        write_1602com(0xc0);
  34. //        for(i=0;i<16;i++)
  35. //        write_1602dat(RsBuf[i+2]);
  36.         Rec_stop=0;
  37.         
  38.         memset(&RsBuf[1],' ',18);
  39.         SendString("AT+CWMODE=1\r\n");
  40.         write_1602com(0x01);
  41.         write_string(0x80,"AT+CWMODE=1     ",0);
  42.         while(Rec_stop==0) ;
  43.         Rec_stop=0;
  44. //        write_1602com(0xc0);
  45. //        delay(1);
  46. //        for(i=0;i<2;i++)
  47. //        write_1602dat(RsBuf[i+2]);
  48.         delay(2000);

  49.         memset(&RsBuf[1],' ',18);
  50.         SendString("AT+RST\r\n");
  51.         write_1602com(0x01);
  52.     write_string(0x80,"AT+RST          ",0);
  53.         while(Rec_stop==0&&RsBuf[0]!='W');
  54.         Rec_stop=0;
  55. //        write_1602com(0xc0);
  56. //        for(i=0;i<16;i++)
  57. //        write_1602dat(RsBuf[i+2]);
  58.            delay(3000);

  59.           memset(&RsBuf[1],' ',18);
  60.     SendString("ATE0\r\n");
  61.         write_string(0x80,"ATE0            ",0);
  62.         write_string(0xc0,"                ",0);
  63.         while(Rec_stop==0) ;
  64.         Rec_stop=0;
  65. //        write_1602com(0xc0);
  66. //        delay(1);
  67. //        for(i=0;i<2;i++)
  68.         write_1602dat(RsBuf[i+2]);
  69.          delay(2000);

  70.            memset(&RsBuf[1],' ',18);
  71.         SendString("ATE0\r\n");
  72.         write_1602com(0x01);
  73.         write_string(0x80,"ATE0            ",0);
  74.         while(Rec_stop==0) ;
  75.         Rec_stop=0;
  76. //        write_1602com(0xc0);
  77. //        delay(1);
  78. //        for(i=0;i<2;i++)
  79. //        write_1602dat(RsBuf[i+2]);
  80.                  delay(2000);

  81.         memset(&RsBuf[1],' ',18);
  82.     SendString("AT+CIPMUX=0\r\n");
  83.         write_1602com(0x01);
  84.     write_string(0x80,"AT+CIPMUX=0      ",0);
  85.         while(Rec_stop==0);
  86.         Rec_stop=0;
  87. //        write_1602com(0xc0);
  88. //        for(i=0;i<2;i++)
  89. //        write_1602dat(RsBuf[i+2]);
  90.            delay(100);

  91.             memset(&RsBuf[1],' ',18);
  92.         SendString("AT+CIPSTART=\"TCP\",\"114.215.204.246\",9999\r\n");
  93.         write_1602com(0x01);
  94.     write_string(0x80,"AT+CISTART=TCP   ",0);
  95.         while(Rec_stop==0&&RsBuf[2]=='O');
  96.         Rec_stop=0;
  97. //        write_1602com(0xc0);
  98. //        for(i=0;i<2;i++)
  99. //        write_1602dat(RsBuf[i+2]);
  100.          delay(2000);


  101.             memset(&RsBuf[1],' ',18);
  102.         SendString("AT+CIPMODE=1\r\n");
  103.         write_1602com(0x01);
  104.     write_string(0x80,"AT+CIPMODE=1     ",0);
  105.         while(Rec_stop==0);
  106.         Rec_stop=0;
  107. //        write_1602com(0xc0);
  108. //        for(i=0;i<2;i++)
  109. //        write_1602dat(RsBuf[i+2]);
  110.          delay(2000);

  111.         RsPoint=0;
  112.         memset(&RsBuf[1],' ',18);
  113.     SendString("AT+CIPSEND\r\n");
  114.         write_1602com(0x01);
  115.     write_string(0x80,"AT+CIPSEND       ",0);
  116.         while(RsPoint==0);
  117. //        write_1602com(0xc0);
  118. //        for(i=0;i<16;i++)
  119. //        write_1602dat(RsBuf[i]);
  120.            delay(2000);
  121.         SendString("AT+CWJAP=\"wodewifi\",\"nidemima\"\r\n");                        
  122.          //在这里填入你的路由器名称和密码 分别替代wodewifi和 nidemima 双引号和斜杠不能删除
  123.         delay(5000);
  124.         
  125.          write_string(0x80,"Te:   C   Hu:  %",0);
  126.         write_string(0xc0,"STATUS:0000     ",0);
  127.         write_1602com(0x85);
  128.         write_1602dat(0xdf);

  129.         while(1)
  130.         {
  131.                   if(FlagStart==1)
  132.                 {
  133.                      TR0=0;
  134.              RH();
  135.                           FlagStart = 0;
  136.                          write_1602com(0x83);
  137.                          write_1602dat(temperature%100/10+0x30);
  138.                          write_1602dat(temperature%100%10+0x30);
  139.                          write_1602com(0x8d);
  140.                          write_1602dat(humidity%100/10+0x30);
  141.                          write_1602dat(humidity%100%10+0x30);
  142.                          SendString("{\"UN\":\"xxxxx@qqcom\",\"VER\":\"A1.1\",\"ID\":\"23502\",\"MAC\":\"18fe34f1cd9a\",\"TP\":\"");
  143.                          //784551388@qq.com是账号          234100是ID
  144.                  SendASC(temperature%100/10+0x30);          //1
  145.                  SendASC(temperature%10+0x30);                //1
  146.                  SendString("\",\"HM\":\"");                   //  8
  147.                  SendASC(humidity%100/10+0x30);          //1
  148.                  SendASC(humidity%10+0x30);                //1
  149.                  SendString("\",\"SOC\":\"99\",\"RET\":\"1\"}\r\n");         
  150.                          TR0=1;
  151.                          Timer0_count=0;
  152.                          Timer0_miao=0;
  153.                 }
  154.          
  155.       
  156.                 write_1602com(0xc7);
  157.                 if(LED1)
  158.             write_1602dat('0');
  159.                 else
  160.                 write_1602dat('1');
  161.                 if(LED2)
  162.             write_1602dat('0');
  163.                 else
  164.                 write_1602dat('2');
  165.                 if(LED3)
  166.             write_1602dat('0');
  167.                 else
  168.                 write_1602dat('3');
  169.                 if(LED4)
  170.             write_1602dat('0');
  171.                 else
  172.                 write_1602dat('4');

  173.                 delay(100);
  174.         }

  175.    
  176. }

  177. //  通讯中断接收程序   中断函数无返回值
  178.   void uart_rx(void)  interrupt 4   //放在这里 和放在main()里面是一样的
  179. {
  180.    
  181.         unsigned char dat;
  182.     if(RI)                         //是否接收中断
  183.     {
  184.      RI=0;
  185.          dat = SBUF;
  186.          if(SBUF=='\r')
  187.          {
  188.            RsPoint=0;
  189.            Rec_stop=1;
  190.           }
  191.           RsBuf[RsPoint]=SBUF;
  192.           RsPoint++;
  193.          if(Flag_rec==1)
  194.          {
  195.                  switch (Key_num)
  196.                  {
  197.                   case 'A': if(dat=='0')LED1=0; else LED1=1;break;
  198.                   case 'B': if(dat=='0')LED2=0; else LED2=1; break;
  199.                   case 'C': if(dat=='0')LED3=0; else LED3=1; break;
  200.                   case 'D': if(dat=='0')LED4=0; else LED4=1; break;
  201.                  }
  202.                  Flag_rec=0;
  203.      }  
  204.          if(dat=='A'||dat=='B'||dat=='C'||dat=='D')
  205.          {
  206.           Flag_rec=1;
  207.           Key_num=dat;
  208.          }
  209.         }

  210. }

  211. void Timer0_ISR (void) interrupt 1 using 0
  212. {
  213.   TH0=0x4c;
  214.   TL0=0x00;
  215.   Timer0_count++;
  216.   if(Timer0_count>=20)
  217.   {
  218.    Timer0_count=0;
  219.    Timer0_miao++;

  220.    if(Timer0_miao>=2)
  221.    {
  222.     Timer0_miao=0;
  223.           FlagStart=1;
  224.    }
  225.   }
  226. }  
复制代码

所有资料51hei提供下载:
1-程序 - 修改.zip (59.75 KB, 下载次数: 14)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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