数码之家

 找回密码
 立即注册
搜索
查看: 3321|回复: 8

[C51] 基于ESP8266+STC12C5612AD+腾讯云的七彩小夜灯

[复制链接]
发表于 2021-3-29 18:18:14 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
     跟风也买了个ESP8266模块来玩,结果到要开发的时候,才发现开发环境搭建起来太难了,好不容易搭建起来,又发现写程序更麻烦,只因C语言只学了个皮毛,根本看不懂。没办法,只好用熟悉的STC+AT指令控制,本想着有STM32的例程,移植就好,一看也头大。还是按着自己的想法写一个得了。
       研究了几天腾讯云的教程,终于一步步的写通了程序,下面是主 程序,串口(波特率是115200)头文件,EEPROM头文件请自行添加。
       程序完成了灯的开关、亮度、颜色控制并保存数据,。手机端的程序用微信中的腾讯连连。
      最好用3V的单片机

  1. #include<STC12C56.h>
  2. //#include "reg51.h"


  3. unsigned char xdata uart_wifi[256];
  4. unsigned char uart_num,wifi_num,xx;//uart_w,
  5. unsigned char clientToken,power_switch_eep;
  6. unsigned char brightness,brightness0,color,color_temp;//灯的亮度,颜色,色温
  7. bit power_switch,pub_ok,control_ok;//灯开关,是控制命令
  8. bit brightness_dis_ok,power_switch_dis_ok,color_dis_ok;//灯命令数据好

  9. unsigned char del,uart_ld,uart_num,uart_num0;//要填空的数据计数,还要接收数据的个数,合计接收的个数
  10. unsigned char uart_disnum,uart_disnum_de;// 用于数据显示
  11. unsigned char uart_ld,uart_num0;//要填空的数据计数,还要接收数据的个数,合计接收的个数

  12. bit lcd_del,uart_start,uart_lcddisplay,uart_read_ok,RI_ok,esp8266_ok,eeprom_save;//数据开始,数据显示


  13. void wifi_read(void);

  14. void delay_nms(unsigned int z);

  15. //#include "intrins.h"
  16. //#include"cgm12864g.h"
  17. #include<uart_56_115200.h>
  18. //#include<ADC.h>
  19. #include<STC12C54_C56_EEPROM.h>

  20. sbit ledG = P1^3;        //绿呼吸灯
  21. sbit ledR = P1^2;        //红呼吸灯
  22. sbit ledB = P1^4;        //蓝呼吸灯


  23. void delay_nms(unsigned int z)//???M晶振延时1ms
  24. {
  25. unsigned int y;
  26.   while(z--)
  27.   {
  28.     for(y=0;y<650;y++)
  29.         {
  30.                 //if(!P30)
  31.                 //{
  32.                         //IAP_CONTR=0X20;//软件复位,系统复位
  33.                 //        ISP_CONTR=0X60;//软件复位,下载程序
  34.                 //}
  35.         }
  36. }
  37. }






  38. void wifi_read()//读取控制数据
  39. {
  40.         if(uart_wifi[uart_disnum]==0x3a) //:当是冒号时,处理数据,如果担心条件有重复,可多加判断条件
  41.         {
  42.           wifi_num++;
  43.                 if(wifi_num==1&&uart_wifi[uart_disnum-3]==80)// 第1个冒号且往前第3个字符是P
  44.                  pub_ok=1;//
  45.                 if(wifi_num==2&&uart_wifi[uart_disnum+2]==99)// 第2个冒号且往后第2个字符是c
  46.                  control_ok=1;//        
  47.                 if(pub_ok&&control_ok)
  48.                 {
  49.                         if(wifi_num==5||wifi_num==6||wifi_num==7||wifi_num==8||wifi_num==9)
  50.                         {
  51.                                 if(uart_wifi[uart_disnum-2]==104)//h(power_switch)//灯开关
  52.                                 {
  53.                                         if(uart_wifi[uart_disnum+1]==49)
  54.                                         {
  55.                                                 power_switch=1;
  56.                                          
  57.                                         }
  58.                                         else
  59.                                         {
  60.                                                 power_switch=0;
  61.                                                 
  62.                                         }
  63.                                         power_switch_dis_ok=1;
  64.                                         eeprom_save=1;
  65.                                 }
  66.                                 else if(uart_wifi[uart_disnum-2]==114)//r(color)//灯颜色
  67.                                 {
  68.                                         color=uart_wifi[uart_disnum+1]-48;
  69.                                         color_dis_ok=1;
  70.                                         eeprom_save=1;
  71.                                 }
  72.                                 else if(uart_wifi[uart_disnum-2]==115)//s(brightness)//灯亮度
  73.                                 {        
  74.                                         brightness=0;
  75.                                         brightness0=uart_disnum;
  76.                                         do{
  77.                                                 brightness0++;
  78.                                                 brightness=brightness*10+(uart_wifi[brightness0]-48);
  79.                                                 }
  80.                                           while(uart_wifi[brightness0+1]!=44&&uart_wifi[brightness0+1]!=125);//下一个是冒号:或闭花括号}时完成
  81.                                         brightness_dis_ok=1;
  82.                                         eeprom_save=1;        
  83.                                 }
  84.                                 
  85.                  }
  86.                         
  87.                 }
  88.         }
  89.                
  90.                
  91. }

  92. void timer0() interrupt 1  //定时器0中断
  93. {
  94.   //ET0 = 0;                //关定时器0中断
  95.   TR0 = 0;//定时器0停止计数
  96.   TL0=TH0=0;//用于定时清除串口数据位,从头接收
  97.   xx=0;
  98.         uart_disnum_de=0;
  99.         wifi_num=0;
  100.         pub_ok=0;
  101.         control_ok=0;
  102.         for(uart_disnum=0;uart_disnum<uart_num;uart_disnum++)
  103.                         {//将接收到的数据显示并处理想要的数据
  104.                                  
  105.                                         wifi_read();//读取控制数据
  106.                                 }
  107.                         
  108.                 uart_num=0;         
  109. }

  110. void stc12c56_save_eeprom()
  111. {//因为用是的12AD,且没有独立的EEPROM,所以从新下载程序时所有数据会复位到到0xff
  112.         
  113.         ISP_Erase_Sector(0x2e00);//最后的一个扇区,
  114.                                                                                                                
  115.         if(power_switch)
  116.           power_switch_eep=1;
  117.         else
  118.          power_switch_eep=0;
  119.         ISP_Write_Byte(0x2e00,power_switch_eep);
  120.         ISP_Write_Byte(0x2e01,brightness);
  121.         ISP_Write_Byte(0x2e02,color);
  122.         ISP_Write_Byte(0x2e03,color_temp);
  123.         ISP_Write_Byte(0x2e04,clientToken);
  124. }

  125. void main()
  126. {
  127. unsigned char brightness_on,brightness_off;
  128. unsigned char brightness1,brightness10,brightness100;
  129. unsigned char color_temp1,color_temp10,color_temp100;
  130. unsigned char clientToken1,clientToken10,clientToken100;
  131.         delay_nms(500);
  132.         delay_nms(1000);

  133.         InitUart();//串口初始化
  134.         
  135.         delay_nms(3000);
  136.         
  137.         //SendString("AT+CWMODE=1\r\n"); //设置WIFI模式:0关,1,网卡,2路由,3网上和路由
  138.         //delay_nms(500);
  139.         //SendString("AT+CWJAP="wifi名称","wifi密码"\r\n"); //
  140.         //delay_nms(2000);
  141.         //SendString("AT+TCDEVINFOSET=1\,\设备ID"\,"设备名称"\,"设备密钥"\r\n"); //发送设备信息,首次使用时发一次就可
  142.         //delay_nms(5000);
  143.         //SendString("AT+TCSTARTSMART\r\n"); // 以smart config (网卡)方式进行WiFi 配网;配网中WIFI指示灯会闪,完成后常亮(IO口低电平)
  144.         //delay_nms(20000);                                                                                                                                                                //(指示灯接在哪个IO口可用AT+TCMODINFOSET?\r\n命令查看)
  145.         delay_nms(3000);
  146.         SendString("AT+TCMQTTCONN=1,5000,240,0,1\r\n");//连接腾讯云
  147.         
  148.         delay_nms(3000);
  149.         
  150.         SendString("AT+TCMQTTSUB="$thing/down/property/设备ID/设备名称",0\r\n");//订阅主题
  151.         delay_nms(3000);
  152.         
  153.         //SendString("AT+CIPSNTPCFG=1,8\r\n");  // 设置STNP,0为关;(1,8)为开,时区为8
  154.         //delay_nms(2000);
  155.         //SendString("AT+CIPSNTPTIME?\r\n"); // 查询STNP时间
  156.         //        delay_nms(2000);
  157.         //SendString("AT+CIPSNTPTIME?\r\n"); // 查询STNP时间 ,只查询一次可能时间不准确
  158.   // esp8266_ok=1;//esp8266初始化完成
  159.         
  160.         /***************发送设备状态(开或关,亮度,颜色)建议上报,这样开机时,手机端知道灯的状态*************************/
  161.         /***************如果整条信息大于256个字符,将会报错********************/
  162.         delay_nms(1000);
  163.         power_switch_eep=ISP_Read_Byte(0x2e00);//读取保存的开关标志
  164.         if(power_switch_eep>1)
  165.         {power_switch=1;power_switch_eep=1;}
  166.         else if(power_switch_eep==1)
  167.                 power_switch=1;
  168.         else
  169.                 power_switch=0;
  170.         brightness=ISP_Read_Byte(0x2e01);//读取保存的亮度
  171.                 if(brightness>100)
  172.                         brightness=1;
  173.                 brightness100=brightness/100%10;
  174.                 brightness10=brightness/10%10;
  175.                 brightness1=brightness%10;
  176.                
  177.         color=ISP_Read_Byte(0x2e02);//读取保存的颜色
  178.                 if(color>6)
  179.                         color=6;
  180.         color_temp=ISP_Read_Byte(0x2e03);//读取保存的色温
  181.                 if(color_temp>100)
  182.                         color_temp=50;
  183.                 color_temp100=color_temp/100%10;
  184.                 color_temp10=color_temp/10%10;
  185.                 color_temp1=color_temp%10;
  186.         
  187.         clientToken=ISP_Read_Byte(0x2e04);//读取保存的上报计数
  188.                 clientToken++;
  189.                 clientToken100=clientToken/100%10;
  190.                 clientToken10=clientToken/10%10;
  191.                 clientToken1=clientToken%10;
  192.         //SendString("AT+TCMQTTPUB="$thing/up/property/产品ID/设备名称",0,"{\\"method\\":\\"report\\"\\\,\\"clientToken\\":\\"0\\"\\\,\\"timestamp\\":1234567890\\\,\\"params\\":{\\"power_switch\\":1\\\,\\"brightness\\":89\\\,\\"color\\":6\\\,\\"color_temp\\":9 } }"\r\n");
  193.         SendString("AT+TCMQTTPUB="$thing/up/property/产品ID/设备名称",0,"{\\"method\\":\\"report\\"\\\,\\"clientToken\\":\\"");
  194.         if(clientToken100>0);//百位大于0
  195.                 SendData(clientToken100+48);//百位
  196.         if(clientToken100>0||clientToken10>0);//百位大于        0或十位大于0
  197.          SendData(clientToken10+48);//十位
  198.         SendData(clientToken1+48);//个位,发送信息条数,用于上下行消息配对标识,0—255
  199.         SendString("\\"\\\,\\"timestamp\\":1234567890\\\,\\"params\\":{\\"power_switch\\":");//timestamp(属性上报的时间,无实际意义)
  200.         SendData(power_switch_eep+48);//开(1)关(0)标识
  201.         SendString("\\\,\\"brightness\\":");
  202.         if(brightness100>0)        
  203.           SendData(brightness100+48);//亮度数据百位
  204.         if(brightness100>0||brightness10>0)
  205.                 SendData(brightness10+48);//亮度数据十位
  206.         SendData(brightness1+48);//亮度数据个位
  207.         SendString("\\\,\\"color\\":");
  208.         SendData(color+48);//颜色数据
  209.         SendString("\\\,\\"color_temp\\":");
  210.         if(color_temp100>0)
  211.          SendData(color_temp100+48);//色温数据百位
  212.         if(color_temp100>0||color_temp10>0)
  213.          SendData(color_temp10+48);//色温数据十位
  214.         SendData(color_temp1+48);//色温数据个位
  215.         SendString("} }"\r\n");
  216.         /****************************************************************************/
  217.         delay_nms(1000);
  218.         /*******************发送设备信息(可选,发一次即可),如果整条信息大于256个字符,将会报错**
  219.         clientToken++;//读取保存的上报计数
  220.                 clientToken100=clientToken/100%10;
  221.                 clientToken10=clientToken/10%10;
  222.                 clientToken1=clientToken%10;
  223.         SendString("AT+TCMQTTPUB="$thing/up/property/产品ID/设备名称",0,"{\\"method\\":\\"report_info\\"\\\,\\"clientToken\\":\\"");
  224.         if(clientToken100>0);//百位大于0
  225.                 SendData(clientToken100+48);//百位
  226.         if(clientToken100>0||clientToken10>0);//百位大于        0或十位大于0
  227.          SendData(clientToken10+48);//十位
  228.         SendData(clientToken1+48);//个位,发送信息条数,用于上下行消息配对标识,0—255
  229.         //SendData(6+48);//发送信息条数,用于上下行消息配对标识,0—255
  230.         SendString("\\"\\\,\\"params\\":{\\"module_hardinfo\\":\\"ESP-12F\\"\\\,");//模组具体硬件型号(云端和APP端无有效标识),非必填项(moduled_softinfo、module_hardinfo、fw_ver 至少传一个)
  231.         SendString("\\"mac\\":\\"ff:ff:ff:ff:ff:ff\\"");//MAC地址
  232.         SendString("\\\,\\"fw_ver\\":\\"QCloud_AT_ESP8266_v2.0.0\\"");//固件版本
  233.         SendString("}}"\r\n");//扩展信息,\\"device_label\\":{\\"append_info\\":\\"zj97czb\\"}
  234.         /*******************************************************************/
  235.         
  236.         delay_nms(1000);
  237.         stc12c56_save_eeprom();
  238.         
  239.         
  240.     while(1)
  241.                 {
  242.                          // delay_nms(1);
  243.                                 if(eeprom_save)
  244.                                 {
  245.                                         eeprom_save=0;
  246.                                         stc12c56_save_eeprom();
  247.                                        
  248.                                 }
  249.                         
  250.                                 if(power_switch&&brightness>0)//开灯,亮度大于0
  251.                                          {
  252.                                      switch(color)
  253.                                                         {
  254.                                                                 /**********常亮灯******************/
  255.                                                          case 0:ledR=0;ledB=ledG=1;break;//红
  256.                                                          case 1:ledG=0;ledB=ledR=1;break;//绿
  257.                                                          case 2:ledB=0;ledG=ledR=1;break;//蓝
  258.                                                          case 3:ledR=ledG=0;ledB=1;break;//黄
  259.                                                          case 4:ledR=ledB=0;ledG=1;break;//紫
  260.                                                          case 5:ledG=ledB=0;ledR=1;break;//青
  261.                                                          case 6:ledB=ledG=ledR=0;break;// 白
  262.                                                          
  263.                                                                                 break;//
  264.                                                          }
  265.                                                  }
  266.                                         else
  267.                                                  ledB=ledG=ledR=1;//全灭
  268.                                 
  269.                                         brightness_on=brightness;
  270.                                         brightness_off=100-brightness;
  271.                                   while(brightness_on--);//亮的时间
  272.                                         if(brightness_off>0)//亮度为100时
  273.                                          ledB=ledG=ledR=1;        
  274.                                    while(brightness_off--);//灭的时间
  275.                         }
  276.         
  277.         }

复制代码


打赏

参与人数 3家元 +100 收起 理由
kkdkj + 20 光评价不加分不够意思哟
cushion + 20 原創內容
家睦 + 60

查看全部打赏

发表于 2021-3-29 20:37:05 | 显示全部楼层
可以用arduino的,比较简单
回复 支持 反对

使用道具 举报

发表于 2021-3-29 23:30:32 | 显示全部楼层
ESP8266也支持arduino呀
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-30 09:38:46 | 显示全部楼层
akikyoua 发表于 2021-3-29 20:37
可以用arduino的,比较简单

arduino也是单片机加ESP8266方案呀。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-30 09:39:52 | 显示全部楼层
elecfunwb 发表于 2021-3-29 23:30
ESP8266也支持arduino呀

程序都是人家的,哪有自已的用来方便
回复 支持 反对

使用道具 举报

发表于 2021-3-30 16:00:39 | 显示全部楼层

ESP的SDK开发,可以参考这

https://www.mydigit.cn/forum.php ... d=228450&extra=          ESP8266 windows原生编译入门指引

https://www.mydigit.cn/forum.php ... d=228970&extra=  ESP8266 windows原生编译入门指引(续)

https://www.mydigit.cn/forum.php ... d=229376&extra=           ESP8266 windows原生编译入门指引---番外 DIY bash.exe

回复 支持 反对

使用道具 举报

发表于 2021-3-30 17:23:22 | 显示全部楼层
zj97czb 发表于 2021-3-30 09:38
arduino也是单片机加ESP8266方案呀。

是esp8266上直用arduino的ide写程序,程序可以直接在esp8266上跑
回复 支持 反对

使用道具 举报

发表于 2021-3-30 19:39:45 | 显示全部楼层
zj97czb 发表于 2021-3-30 09:39
程序都是人家的,哪有自已的用来方便

那你说错了,即使程序都是人家的,用起来仍然很方便。以前根本看不上Arduino,直到前几天开始研究ESP-12F,也就是8266,发现Arduino是真方便,以前要搞几个小时的东西,Arduino分分钟搞定,越用越爽,一直用一直爽。
回复 支持 反对

使用道具 举报

发表于 2021-3-31 09:30:59 | 显示全部楼层
玩ESP8266绕了一圈,最终还是觉得Arduino靠谱,原因也很简单,Arduino的例程资源丰富很容易上手、出效果。
回复 支持 反对

使用道具 举报

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-26 20:07 , Processed in 0.124800 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

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