|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
跟风也买了个ESP8266模块来玩,结果到要开发的时候,才发现开发环境搭建起来太难了,好不容易搭建起来,又发现写程序更麻烦,只因C语言只学了个皮毛,根本看不懂。没办法,只好用熟悉的STC+AT指令控制,本想着有STM32的例程,移植就好,一看也头大。还是按着自己的想法写一个得了。
研究了几天腾讯云的教程,终于一步步的写通了程序,下面是主 程序,串口(波特率是115200)头文件,EEPROM头文件请自行添加。
程序完成了灯的开关、亮度、颜色控制并保存数据,。手机端的程序用微信中的腾讯连连。
最好用3V的单片机
- #include<STC12C56.h>
- //#include "reg51.h"
- unsigned char xdata uart_wifi[256];
- unsigned char uart_num,wifi_num,xx;//uart_w,
- unsigned char clientToken,power_switch_eep;
- unsigned char brightness,brightness0,color,color_temp;//灯的亮度,颜色,色温
- bit power_switch,pub_ok,control_ok;//灯开关,是控制命令
- bit brightness_dis_ok,power_switch_dis_ok,color_dis_ok;//灯命令数据好
- unsigned char del,uart_ld,uart_num,uart_num0;//要填空的数据计数,还要接收数据的个数,合计接收的个数
- unsigned char uart_disnum,uart_disnum_de;// 用于数据显示
- unsigned char uart_ld,uart_num0;//要填空的数据计数,还要接收数据的个数,合计接收的个数
- bit lcd_del,uart_start,uart_lcddisplay,uart_read_ok,RI_ok,esp8266_ok,eeprom_save;//数据开始,数据显示
- void wifi_read(void);
- void delay_nms(unsigned int z);
- //#include "intrins.h"
- //#include"cgm12864g.h"
- #include<uart_56_115200.h>
- //#include<ADC.h>
- #include<STC12C54_C56_EEPROM.h>
- sbit ledG = P1^3; //绿呼吸灯
- sbit ledR = P1^2; //红呼吸灯
- sbit ledB = P1^4; //蓝呼吸灯
- void delay_nms(unsigned int z)//???M晶振延时1ms
- {
- unsigned int y;
- while(z--)
- {
- for(y=0;y<650;y++)
- {
- //if(!P30)
- //{
- //IAP_CONTR=0X20;//软件复位,系统复位
- // ISP_CONTR=0X60;//软件复位,下载程序
- //}
- }
- }
- }
- void wifi_read()//读取控制数据
- {
- if(uart_wifi[uart_disnum]==0x3a) //:当是冒号时,处理数据,如果担心条件有重复,可多加判断条件
- {
- wifi_num++;
- if(wifi_num==1&&uart_wifi[uart_disnum-3]==80)// 第1个冒号且往前第3个字符是P
- pub_ok=1;//
- if(wifi_num==2&&uart_wifi[uart_disnum+2]==99)// 第2个冒号且往后第2个字符是c
- control_ok=1;//
- if(pub_ok&&control_ok)
- {
- if(wifi_num==5||wifi_num==6||wifi_num==7||wifi_num==8||wifi_num==9)
- {
- if(uart_wifi[uart_disnum-2]==104)//h(power_switch)//灯开关
- {
- if(uart_wifi[uart_disnum+1]==49)
- {
- power_switch=1;
-
- }
- else
- {
- power_switch=0;
-
- }
- power_switch_dis_ok=1;
- eeprom_save=1;
- }
- else if(uart_wifi[uart_disnum-2]==114)//r(color)//灯颜色
- {
- color=uart_wifi[uart_disnum+1]-48;
- color_dis_ok=1;
- eeprom_save=1;
- }
- else if(uart_wifi[uart_disnum-2]==115)//s(brightness)//灯亮度
- {
- brightness=0;
- brightness0=uart_disnum;
- do{
- brightness0++;
- brightness=brightness*10+(uart_wifi[brightness0]-48);
- }
- while(uart_wifi[brightness0+1]!=44&&uart_wifi[brightness0+1]!=125);//下一个是冒号:或闭花括号}时完成
- brightness_dis_ok=1;
- eeprom_save=1;
- }
-
- }
-
- }
- }
-
-
- }
- void timer0() interrupt 1 //定时器0中断
- {
- //ET0 = 0; //关定时器0中断
- TR0 = 0;//定时器0停止计数
- TL0=TH0=0;//用于定时清除串口数据位,从头接收
- xx=0;
- uart_disnum_de=0;
- wifi_num=0;
- pub_ok=0;
- control_ok=0;
- for(uart_disnum=0;uart_disnum<uart_num;uart_disnum++)
- {//将接收到的数据显示并处理想要的数据
-
- wifi_read();//读取控制数据
- }
-
- uart_num=0;
- }
- void stc12c56_save_eeprom()
- {//因为用是的12AD,且没有独立的EEPROM,所以从新下载程序时所有数据会复位到到0xff
-
- ISP_Erase_Sector(0x2e00);//最后的一个扇区,
-
- if(power_switch)
- power_switch_eep=1;
- else
- power_switch_eep=0;
- ISP_Write_Byte(0x2e00,power_switch_eep);
- ISP_Write_Byte(0x2e01,brightness);
- ISP_Write_Byte(0x2e02,color);
- ISP_Write_Byte(0x2e03,color_temp);
- ISP_Write_Byte(0x2e04,clientToken);
- }
- void main()
- {
- unsigned char brightness_on,brightness_off;
- unsigned char brightness1,brightness10,brightness100;
- unsigned char color_temp1,color_temp10,color_temp100;
- unsigned char clientToken1,clientToken10,clientToken100;
- delay_nms(500);
- delay_nms(1000);
- InitUart();//串口初始化
-
- delay_nms(3000);
-
- //SendString("AT+CWMODE=1\r\n"); //设置WIFI模式:0关,1,网卡,2路由,3网上和路由
- //delay_nms(500);
- //SendString("AT+CWJAP="wifi名称","wifi密码"\r\n"); //
- //delay_nms(2000);
- //SendString("AT+TCDEVINFOSET=1\,\设备ID"\,"设备名称"\,"设备密钥"\r\n"); //发送设备信息,首次使用时发一次就可
- //delay_nms(5000);
- //SendString("AT+TCSTARTSMART\r\n"); // 以smart config (网卡)方式进行WiFi 配网;配网中WIFI指示灯会闪,完成后常亮(IO口低电平)
- //delay_nms(20000); //(指示灯接在哪个IO口可用AT+TCMODINFOSET?\r\n命令查看)
- delay_nms(3000);
- SendString("AT+TCMQTTCONN=1,5000,240,0,1\r\n");//连接腾讯云
-
- delay_nms(3000);
-
- SendString("AT+TCMQTTSUB="$thing/down/property/设备ID/设备名称",0\r\n");//订阅主题
- delay_nms(3000);
-
- //SendString("AT+CIPSNTPCFG=1,8\r\n"); // 设置STNP,0为关;(1,8)为开,时区为8
- //delay_nms(2000);
- //SendString("AT+CIPSNTPTIME?\r\n"); // 查询STNP时间
- // delay_nms(2000);
- //SendString("AT+CIPSNTPTIME?\r\n"); // 查询STNP时间 ,只查询一次可能时间不准确
- // esp8266_ok=1;//esp8266初始化完成
-
- /***************发送设备状态(开或关,亮度,颜色)建议上报,这样开机时,手机端知道灯的状态*************************/
- /***************如果整条信息大于256个字符,将会报错********************/
- delay_nms(1000);
- power_switch_eep=ISP_Read_Byte(0x2e00);//读取保存的开关标志
- if(power_switch_eep>1)
- {power_switch=1;power_switch_eep=1;}
- else if(power_switch_eep==1)
- power_switch=1;
- else
- power_switch=0;
- brightness=ISP_Read_Byte(0x2e01);//读取保存的亮度
- if(brightness>100)
- brightness=1;
- brightness100=brightness/100%10;
- brightness10=brightness/10%10;
- brightness1=brightness%10;
-
- color=ISP_Read_Byte(0x2e02);//读取保存的颜色
- if(color>6)
- color=6;
- color_temp=ISP_Read_Byte(0x2e03);//读取保存的色温
- if(color_temp>100)
- color_temp=50;
- color_temp100=color_temp/100%10;
- color_temp10=color_temp/10%10;
- color_temp1=color_temp%10;
-
- clientToken=ISP_Read_Byte(0x2e04);//读取保存的上报计数
- clientToken++;
- clientToken100=clientToken/100%10;
- clientToken10=clientToken/10%10;
- clientToken1=clientToken%10;
- //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");
- SendString("AT+TCMQTTPUB="$thing/up/property/产品ID/设备名称",0,"{\\"method\\":\\"report\\"\\\,\\"clientToken\\":\\"");
- if(clientToken100>0);//百位大于0
- SendData(clientToken100+48);//百位
- if(clientToken100>0||clientToken10>0);//百位大于 0或十位大于0
- SendData(clientToken10+48);//十位
- SendData(clientToken1+48);//个位,发送信息条数,用于上下行消息配对标识,0—255
- SendString("\\"\\\,\\"timestamp\\":1234567890\\\,\\"params\\":{\\"power_switch\\":");//timestamp(属性上报的时间,无实际意义)
- SendData(power_switch_eep+48);//开(1)关(0)标识
- SendString("\\\,\\"brightness\\":");
- if(brightness100>0)
- SendData(brightness100+48);//亮度数据百位
- if(brightness100>0||brightness10>0)
- SendData(brightness10+48);//亮度数据十位
- SendData(brightness1+48);//亮度数据个位
- SendString("\\\,\\"color\\":");
- SendData(color+48);//颜色数据
- SendString("\\\,\\"color_temp\\":");
- if(color_temp100>0)
- SendData(color_temp100+48);//色温数据百位
- if(color_temp100>0||color_temp10>0)
- SendData(color_temp10+48);//色温数据十位
- SendData(color_temp1+48);//色温数据个位
- SendString("} }"\r\n");
- /****************************************************************************/
- delay_nms(1000);
- /*******************发送设备信息(可选,发一次即可),如果整条信息大于256个字符,将会报错**
- clientToken++;//读取保存的上报计数
- clientToken100=clientToken/100%10;
- clientToken10=clientToken/10%10;
- clientToken1=clientToken%10;
- SendString("AT+TCMQTTPUB="$thing/up/property/产品ID/设备名称",0,"{\\"method\\":\\"report_info\\"\\\,\\"clientToken\\":\\"");
- if(clientToken100>0);//百位大于0
- SendData(clientToken100+48);//百位
- if(clientToken100>0||clientToken10>0);//百位大于 0或十位大于0
- SendData(clientToken10+48);//十位
- SendData(clientToken1+48);//个位,发送信息条数,用于上下行消息配对标识,0—255
- //SendData(6+48);//发送信息条数,用于上下行消息配对标识,0—255
- SendString("\\"\\\,\\"params\\":{\\"module_hardinfo\\":\\"ESP-12F\\"\\\,");//模组具体硬件型号(云端和APP端无有效标识),非必填项(moduled_softinfo、module_hardinfo、fw_ver 至少传一个)
- SendString("\\"mac\\":\\"ff:ff:ff:ff:ff:ff\\"");//MAC地址
- SendString("\\\,\\"fw_ver\\":\\"QCloud_AT_ESP8266_v2.0.0\\"");//固件版本
- SendString("}}"\r\n");//扩展信息,\\"device_label\\":{\\"append_info\\":\\"zj97czb\\"}
- /*******************************************************************/
-
- delay_nms(1000);
- stc12c56_save_eeprom();
-
-
- while(1)
- {
- // delay_nms(1);
- if(eeprom_save)
- {
- eeprom_save=0;
- stc12c56_save_eeprom();
-
- }
-
- if(power_switch&&brightness>0)//开灯,亮度大于0
- {
- switch(color)
- {
- /**********常亮灯******************/
- case 0:ledR=0;ledB=ledG=1;break;//红
- case 1:ledG=0;ledB=ledR=1;break;//绿
- case 2:ledB=0;ledG=ledR=1;break;//蓝
- case 3:ledR=ledG=0;ledB=1;break;//黄
- case 4:ledR=ledB=0;ledG=1;break;//紫
- case 5:ledG=ledB=0;ledR=1;break;//青
- case 6:ledB=ledG=ledR=0;break;// 白
-
- break;//
- }
- }
- else
- ledB=ledG=ledR=1;//全灭
-
- brightness_on=brightness;
- brightness_off=100-brightness;
- while(brightness_on--);//亮的时间
- if(brightness_off>0)//亮度为100时
- ledB=ledG=ledR=1;
- while(brightness_off--);//灭的时间
- }
-
- }
复制代码
|
打赏
-
查看全部打赏
|