找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1317|回复: 3
收起左侧

51单片机onenet协议 总是数据上不去 用的是mqtt协议,一直搞不懂

[复制链接]
ID:700054 发表于 2022-4-2 21:27 | 显示全部楼层 |阅读模式
50黑币
为什么这里程序没问题,总是数据上不去 用的是mqtt协议,一直搞不懂
  1. #include <REG52.H>
  2. #include "LCD1602.h"
  3. #include "intrins.h"
  4. #include "esp8266.h"
  5. #include "timer0.h"
  6. #include "usart.h"

  7. //typedef unsigned char uchar;
  8. //typedef unsigned int uint;
  9.        
  10. //定义变量

  11. sbit Data=P3^6;
  12. uchar sdata;        //存放整数温度
  13. uchar sdatahum;        //存放整数湿度
  14. unsigned char rec_dat[13];//用于保存接收到的数据组

  15. void DHT11_delay_us(unsigned char n)
  16. {
  17.     while(--n);
  18. }

  19. void DHT11_delay_ms(unsigned int z)
  20. {
  21.    unsigned int i,j;
  22.    for(i=z;i>0;i--)
  23.       for(j=110;j>0;j--);
  24. }

  25. void DHT11_start()
  26. {
  27.    Data=1;
  28.    DHT11_delay_us(2);
  29.    Data=0;
  30.    DHT11_delay_ms(20);   //延时18ms以上
  31.    Data=1;
  32.    DHT11_delay_us(30);
  33. }

  34. unsigned char DHT11_rec_byte()      //接收一个字节
  35. {
  36.    unsigned char i,dat=0;
  37.   for(i=0;i<8;i++)    //从高到低依次接收8位数据
  38.    {         
  39.       while(!Data);   //等待50us低电平过去
  40.       DHT11_delay_us(8);     //延时60us,如果还为高则数据为1,否则为0
  41.       dat<<=1;           //移位使正确接收8位数据,数据为0时直接移位
  42.       if(Data==1)    //数据为1时,使dat加1来接收数据1
  43.          dat+=1;
  44.       while(Data);  //等待数据线拉低   
  45.     }  
  46.     return dat;
  47. }

  48. void DHT11_receive()      //接收40位的数据
  49. {
  50.     unsigned char R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
  51.     DHT11_start();
  52.     if(Data==0)
  53.     {
  54.         while(Data==0);   //等待拉高     
  55.         DHT11_delay_us(40);  //拉高后延时80us
  56.         R_H=DHT11_rec_byte();    //接收湿度高八位  
  57.         R_L=DHT11_rec_byte();    //接收湿度低八位  
  58.         T_H=DHT11_rec_byte();    //接收温度高八位  
  59.         T_L=DHT11_rec_byte();    //接收温度低八位
  60.         revise=DHT11_rec_byte(); //接收校正位

  61.         DHT11_delay_ms(25);    //结束

  62.         if((R_H+R_L+T_H+T_L)==revise)      //校正
  63.         {
  64.             RH=R_H;
  65.             RL=R_L;
  66.             TH=T_H;
  67.             TL=T_L;
  68.         }
  69.                
  70.        
  71.         /*数据处理,方便显示*/
  72.         rec_dat[0]=RH/10+'0';
  73.         rec_dat[1]=(RH%10)+'0';
  74.         rec_dat[7]=(TH/10)+'0';
  75.         rec_dat[8]=(TH%10)+'0';
  76.                                 sdata=rec_dat[7]*10+rec_dat[8]*1;
  77.                                 sdatahum=rec_dat[0]*10+rec_dat[1]*1;
  78.     }
  79. }
  80. //void Timer0(void) //interrupt 1
  81. //{
  82. //        static uint count = 0;
  83. //    TL0 = (65536 - 20000)%256;    //装初值,低8位初值是1000即1ms
  84. //    TH0 = (65536 - 20000)/256;    //高8位

  85. //        count++;
  86. //        if(count >= 75) //20*50
  87. //        {
  88. //                count = 0;
  89. //                ESP_Send_Data("tem",sdata);
  90. //                ESP_Send_Data("hum",sdatahum);
  91. //        }
  92. //       
  93. //}


  94. void main()
  95. {
  96.          LCD_Init();
  97.         Timer0_Init();
  98.         UART_init();
  99.         ESP_Init();
  100.         while(1)
  101.         {
  102. //                Timer0();
  103.                 DHT11_receive();
  104.                 LCD_ShowChar(1,2,rec_dat[0]);
  105.                 LCD_ShowChar(1,3,rec_dat[1]);
  106.                 LCD_ShowString(1,4,"%RH");
  107.                 LCD_ShowChar(1,9,rec_dat[7]);
  108.                 LCD_ShowChar(1,10,rec_dat[8]);
  109.                 LCD_ShowString(1,11,"^CLH");
  110.                 ESP_Send_Data("temp",sdata);
  111.                 ESP_Send_Data("hum",sdatahum);
  112.                 DHT11_delay_ms(1000);
  113.                
  114.         }
  115. }
  116. #include <REG52.H>
  117. #include "LCD1602.h"
  118. #include "intrins.h"
  119. #include "esp8266.h"
  120. #include "timer0.h"
  121. #include "usart.h"

  122. //typedef unsigned char uchar;
  123. //typedef unsigned int uint;
  124.        
  125. //定义变量

  126. sbit Data=P3^6;
  127. uchar sdata;        //存放整数温度
  128. uchar sdatahum;        //存放整数湿度
  129. unsigned char rec_dat[13];//用于保存接收到的数据组

  130. void DHT11_delay_us(unsigned char n)
  131. {
  132.     while(--n);
  133. }

  134. void DHT11_delay_ms(unsigned int z)
  135. {
  136.    unsigned int i,j;
  137.    for(i=z;i>0;i--)
  138.       for(j=110;j>0;j--);
  139. }

  140. void DHT11_start()
  141. {
  142.    Data=1;
  143.    DHT11_delay_us(2);
  144.    Data=0;
  145.    DHT11_delay_ms(20);   //延时18ms以上
  146.    Data=1;
  147.    DHT11_delay_us(30);
  148. }

  149. unsigned char DHT11_rec_byte()      //接收一个字节
  150. {
  151.    unsigned char i,dat=0;
  152.   for(i=0;i<8;i++)    //从高到低依次接收8位数据
  153.    {         
  154.       while(!Data);   //等待50us低电平过去
  155.       DHT11_delay_us(8);     //延时60us,如果还为高则数据为1,否则为0
  156.       dat<<=1;           //移位使正确接收8位数据,数据为0时直接移位
  157.       if(Data==1)    //数据为1时,使dat加1来接收数据1
  158.          dat+=1;
  159.       while(Data);  //等待数据线拉低   
  160.     }  
  161.     return dat;
  162. }

  163. void DHT11_receive()      //接收40位的数据
  164. {
  165.     unsigned char R_H,R_L,T_H,T_L,RH,RL,TH,TL,revise;
  166.     DHT11_start();
  167.     if(Data==0)
  168.     {
  169.         while(Data==0);   //等待拉高     
  170.         DHT11_delay_us(40);  //拉高后延时80us
  171.         R_H=DHT11_rec_byte();    //接收湿度高八位  
  172.         R_L=DHT11_rec_byte();    //接收湿度低八位  
  173.         T_H=DHT11_rec_byte();    //接收温度高八位  
  174.         T_L=DHT11_rec_byte();    //接收温度低八位
  175.         revise=DHT11_rec_byte(); //接收校正位

  176.         DHT11_delay_ms(25);    //结束

  177.         if((R_H+R_L+T_H+T_L)==revise)      //校正
  178.         {
  179.             RH=R_H;
  180.             RL=R_L;
  181.             TH=T_H;
  182.             TL=T_L;
  183.         }
  184.                
  185.        
  186.         /*数据处理,方便显示*/
  187.         rec_dat[0]=RH/10+'0';
  188.         rec_dat[1]=(RH%10)+'0';
  189.         rec_dat[7]=(TH/10)+'0';
  190.         rec_dat[8]=(TH%10)+'0';
  191.                                 sdata=rec_dat[7]*10+rec_dat[8]*1;
  192.                                 sdatahum=rec_dat[0]*10+rec_dat[1]*1;
  193.     }
  194. }
  195. //void Timer0(void) //interrupt 1
  196. //{
  197. //        static uint count = 0;
  198. //    TL0 = (65536 - 20000)%256;    //装初值,低8位初值是1000即1ms
  199. //    TH0 = (65536 - 20000)/256;    //高8位

  200. //        count++;
  201. //        if(count >= 75) //20*50
  202. //        {
  203. //                count = 0;
  204. //                ESP_Send_Data("tem",sdata);
  205. //                ESP_Send_Data("hum",sdatahum);
  206. //        }
  207. //       
  208. //}


  209. void main()
  210. {
  211.          LCD_Init();
  212.         Timer0_Init();
  213.         UART_init();
  214.         ESP_Init();
  215.         while(1)
  216.         {
  217. //                Timer0();
  218.                 DHT11_receive();
  219.                 LCD_ShowChar(1,2,rec_dat[0]);
  220.                 LCD_ShowChar(1,3,rec_dat[1]);
  221.                 LCD_ShowString(1,4,"%RH");
  222.                 LCD_ShowChar(1,9,rec_dat[7]);
  223.                 LCD_ShowChar(1,10,rec_dat[8]);
  224.                 LCD_ShowString(1,11,"^CLH");
  225.                 ESP_Send_Data("temp",sdata);
  226.                 ESP_Send_Data("hum",sdatahum);
  227.                 DHT11_delay_ms(1000);
  228.                
  229.         }
  230. }
复制代码


比赛 - 副本.rar

61.96 KB, 下载次数: 8

回复

使用道具 举报

ID:157274 发表于 2024-4-7 01:27 来自手机 | 显示全部楼层
不懂就问 51单片机能用8266.h吗
回复

使用道具 举报

ID:879348 发表于 2024-4-7 08:04 | 显示全部楼层
数据上传是esp8266的事,你的固件得对,串口数据要确定符合要求
回复

使用道具 举报

ID:453974 发表于 2024-4-7 08:56 | 显示全部楼层
风雪残留 发表于 2024-4-7 01:27
不懂就问 51单片机能用8266.h吗

.h是头文件,里面包含了一些其他的函数体,一般封装一下硬件(ESP8266)固定驱动程序,所以和51自身的函数不冲突
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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