找回密码
 立即注册

QQ登录

只需一步,快速开始

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

esp8266 HTTP协议WiFi智能点灯

[复制链接]
跳转到指定楼层
楼主
这是我试验过的HTTP协议点灯的程序真的很不错


  1. #define MAIN_Fosc                22118400L        

  2.                 #include        "STC15Fxxxx.H"

  3.                 #define                Baudrate1        115200UL
  4.                 #define                Baudrate2        115200UL
  5.                

  6.                 #define        UART1_BUF_LENGTH        8
  7.                 #define        UART2_BUF_LENGTH        64

  8.                 u8        TX1_Cnt;        
  9.                 u8        RX1_Cnt;        
  10.                 u8        TX2_Cnt;        
  11.                 u8        RX2_Cnt;        
  12.                 bit        B_TX1_Busy;        
  13.                 bit        B_TX2_Busy;        
  14.                 bit fa_song;
  15.                 bit jieshou_ok;

  16.                 u8         idata RX1_Buffer[UART1_BUF_LENGTH];        
  17.                 u8         idata RX2_Buffer[UART2_BUF_LENGTH];        

  18.                 void        UART1_config(u8 brt);        
  19.                 void        UART2_config(u8 brt);        
  20.                 void         PrintString1(u8 *puts);
  21.                 void         PrintString2(u8 *puts);
  22.                 void Delay500ms();               
  23.                 void Delayms(unsigned int m);

  24.                 void qidong()
  25.                 {
  26.                          PrintString2("AT+CWMODE=3\r\n");
  27.                          Delay500ms();
  28.                          PrintString2("AT+CWJAP=\"302\",\"123456789.\"\r\n");
  29.                          Delayms(8000);
  30.                          PrintString2("AT+CIFSR\r\n");
  31.                          Delayms(1000);
  32.                          PrintString2("AT+CIPMUX=0\r\n");
  33.                          Delayms(1000);
  34.                          PrintString2("AT+CIPMODE=1\r\n");
  35.                          Delayms(1000);
  36.                          PrintString2("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n");
  37.                          Delayms(1000);
  38.                          PrintString2("AT+CIPSEND\r\n");
  39.                          Delayms(1000);
  40.                          fa_song=1;
  41.                          PrintString2("GET /devices/3255597/datapoints HTTP/1.1\r\napi-key: zoZkCvP7XI=bUQrBNFCPIIqB94Q=\r\nHost:api.heclouds点com\r\n\r\n");
  42.                          jieshou_ok=0;
  43.                          Delayms(5000);
  44.                
  45.                          fa_song=0;
  46.                
  47.                


  48.                 }

  49.                 void chong_qi()
  50.                 {
  51.                        
  52.                         PrintString2("AT+RST\r\n");
  53.                         Delayms(6000);
  54.                         qidong();

  55.                 }

  56.                 void du_shuju()
  57.                 {
  58.                          fa_song=1;
  59.                          PrintString2("GET /devices/3255597/datapoints HTTP/1.1\r\napi-key: zoZkCvP7XI=bUQrBNFCPIIqB94Q=\r\nHost:api.heclouds点com\r\n\r\n");
  60.                          jieshou_ok=0;
  61.                          P23=1;
  62.                          Delayms(5000);
  63.                
  64.                          fa_song=0;
  65.                 }
  66.                
  67.                
  68.                
  69.                
  70.                 void main(void)
  71.                 {
  72.                         P0M1 = 0;        P0M0 = 0;        
  73.                         P1M1 = 0;        P1M0 = 0;        
  74.                         P2M1 = 0;        P2M0 = 0;        
  75.                         P3M1 = 0;        P3M0 = 0;        
  76.                         P4M1 = 0;        P4M0 = 0;        
  77.                         P5M1 = 0;        P5M0 = 0;        
  78.                         P6M1 = 0;        P6M0 = 0;        
  79.                         P7M1 = 0;        P7M0 = 0;        

  80.                         UART1_config(1);        
  81.                         UART2_config(2);        
  82.                         EA = 1;                                


  83.                                 Delayms(5000);
  84.                

  85.                                 qidong();

  86.                         while (1)
  87.                         {

  88.                                  if(RX2_Buffer[7]=='6'&&RX2_Buffer[8]=='0')
  89.                                  {P27=0; }

  90.                                         if(RX2_Buffer[7]=='5'&&RX2_Buffer[8]=='0')
  91.                                  {P27=1; }

  92.                                  if(!jieshou_ok)
  93.                                  {
  94.                               
  95.                                 PrintString2("+++");
  96.                                  Delayms(4000);

  97.                                  chong_qi();
  98.                                  }

  99.                                  du_shuju();

  100.                         }
  101.                 }
  102.                
  103.                
  104.                
  105.                
  106.                
  107.                 void PrintString1(u8 *puts)
  108.                 {
  109.                                 for (; *puts != 0;        puts++)           
  110.                         {
  111.                                 SBUF = *puts;
  112.                                 B_TX1_Busy = 1;
  113.                                 while(B_TX1_Busy);
  114.                         }

  115.                 }
  116.                
  117.                
  118.                
  119.                
  120.                
  121.                 void PrintString2(u8 *puts)
  122.                 {
  123.                                 for (; *puts != 0;        puts++)           
  124.                         {
  125.                                 S2BUF = *puts;
  126.                                 B_TX2_Busy = 1;
  127.                                 while(B_TX2_Busy);
  128.                         }

  129.                 }
  130.                
  131.                
  132.                
  133.                
  134.                
  135.                 void        SetTimer2Baudraye(u16 dat)
  136.                 {
  137.                         AUXR &= ~(1<<4);        
  138.                         AUXR &= ~(1<<3);        
  139.                         AUXR |=  (1<<2);        
  140.                         TH2 = dat / 256;
  141.                         TL2 = dat % 256;
  142.                         IE2  &= ~(1<<2);        
  143.                         AUXR |=  (1<<4);        
  144.                 }
  145.                
  146.                
  147.                
  148.                
  149.                
  150.                 void        UART1_config(u8 brt)
  151.                 {
  152.                         
  153.                         if(brt == 2)
  154.                         {
  155.                                 AUXR |= 0x01;               
  156.                                 SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate1);
  157.                         }

  158.                         
  159.                         else
  160.                         {
  161.                                 TR1 = 0;
  162.                                 AUXR &= ~0x01;               
  163.                                 AUXR |=  (1<<6);        
  164.                                 TMOD &= ~(1<<6);        
  165.                                 TMOD &= ~0x30;               
  166.                                 TH1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) / 256);
  167.                                 TL1 = (u8)((65536UL - (MAIN_Fosc / 4) / Baudrate1) % 256);
  168.                                 ET1 = 0;        
  169.                                 INT_CLKO &= ~0x02;        
  170.                                 TR1  = 1;
  171.                         }
  172.                         
  173.                         SCON = (SCON & 0x3f) | 0x40;        
  174.                
  175.                         ES  = 1;        
  176.                         REN = 1;        
  177.                         P_SW1 &= 0x3f;
  178.                         P_SW1 |= 0x00;               
  179.                

  180.                         B_TX1_Busy = 0;
  181.                         TX1_Cnt = 0;
  182.                         RX1_Cnt = 0;
  183.                 }
  184.                
  185.                
  186.                
  187.                
  188.                
  189.                 void        UART2_config(u8 brt)        
  190.                 {
  191.                         if(brt == 2)
  192.                         {
  193.                                 SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2);

  194.                                 S2CON &= ~(1<<7);        
  195.                                 IE2   |= 1;                        
  196.                                 S2CON |= (1<<4);        
  197.                                 P_SW2 &= ~0x01;      
  198.                                 P_SW2 |= 0;                        

  199.                                 B_TX2_Busy = 0;
  200.                                 TX2_Cnt = 0;
  201.                                 RX2_Cnt = 0;
  202.                         }
  203.                 }
  204.                
  205.                
  206.                
  207.                
  208.                 void UART1_int (void) interrupt UART1_VECTOR
  209.                 {
  210.                         if(RI)
  211.                         {
  212.                                 RI = 0;
  213.                
  214.                                 S2BUF=SBUF;
  215.                
  216.                         }

  217.                         if(TI)
  218.                         {
  219.                                 TI = 0;
  220.                                 B_TX1_Busy = 0;
  221.                         }
  222.                 }
  223.                
  224.                
  225.                
  226.                
  227.                 void UART2_int (void) interrupt UART2_VECTOR
  228.                 {
  229.                         if((S2CON & 1) != 0)
  230.                         {
  231.                                 S2CON &= ~1;        

  232.                                                 SBUF = S2BUF;
  233.                                          if(fa_song)
  234.                                 {
  235.                                         RX2_Buffer[RX2_Cnt] = S2BUF;
  236.                                 if(RX2_Buffer[0]=='v')
  237.                                 {
  238.                                  
  239.                                  if(++RX2_Cnt >= UART2_BUF_LENGTH)        RX2_Cnt = 0;               
  240.                                 }
  241.                                 else
  242.                                 {RX2_Cnt = 0;}

  243.                                 if((RX2_Buffer[0]=='v')&&(RX2_Buffer[1]=='a'))
  244.                                 {
  245.                
  246.                                  RX2_Buffer[1]=0;
  247.                                  jieshou_ok=1;
  248.                                  P23=0;
  249.                                 }

  250.                                 }
  251.                         }

  252.                         if((S2CON & 2) != 0)
  253.                         {
  254.                                 S2CON &= ~2;        
  255.                                 B_TX2_Busy = 0;
  256.                         }

  257.                 }
  258.                 void Delay500ms()               
  259.                 {
  260.                         unsigned char i, j, k;

  261.                         _nop_();
  262.                         _nop_();
  263.                         i = 43;
  264.                         j = 6;
  265.                         k = 203;
  266.                         do
  267.                         {
  268.                                 do
  269.                                 {
  270.                                         while (--k);
  271.                                 } while (--j);
  272.                         } while (--i);
  273.                 }

  274.                 void Delayms(unsigned int m)               
  275.                 {
  276.                         unsigned char i, j;
  277.                         unsigned int n;
  278.                          for(n=0;n<m;n++)
  279.                          {        _nop_();
  280.                                         _nop_();
  281.                                         i = 22;
  282.                                         j = 128;
  283.                                         do
  284.                                         {
  285.                                 while (--j);
  286.                                         } while (--i);
  287.                         }
  288.                 }

复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:502774 发表于 2019-5-19 20:25 来自手机 | 只看该作者
我也有个esp8266一直没有时间完
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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