MCU 51 used to achieve the temperature and humidity ESP8266 DHT11 and wireless transmission of data

MCU 51 used to achieve the temperature and humidity ESP8266 DHT11 and wireless transmission of data

The first article

This is what I try to write the first article in CSDN, the project source is Daiso project University, in the process of making, because he also got a lot of great God from CSDN platform support, but also summed up a similar article the problem is not mentioned, have written bad and unprofessional place also we want to correct me a lot, ado begin.
Here Insert Picture Description

DHT11 module

The basic module is able to know how to use the terminal to buy (directly corresponding to the three ports can be connected to the microcontroller development board, when only the back of the need to expand a plurality of data port need to be redefined, if the power line can not use breadboard Note that specific functions other port on the definition of the microcontroller ).

DHT11 part of the program

This sensor program is that I spent the longest part of the entire production stage, because when this thing is new to a lot of things not learned, rarely write code, to fully grasp the code of this module is the completion of the single-chip computer and two after the course. In fact this system now looks like this is the easiest one code.

DHT11 data collection

Should first read timing diagrams, timing diagrams about this on the Internet, including instructions are presented here not long-winded, then grasp the number of electric and SCM, fully capable to master. Here Insert Picture Description
This is the start signal, and DHT11 most of the code is the same, mainly delay-1ms to write four 1us behind the delay may or may not.

void start()//开始信号
{
  i0=1;
  delay_1us();
  i0=0;
  delay_1ms(20);  //拉低至少 18ms  保证能检测到起始信号
  i0=1;	  //检测到开始信号后,将电平拉高20-40us
  delay_1us();	//可替换成20-40us程序
  delay_1us();	
  delay_1us();
  delay_1us();
}

This is the code after the end of the start signal, the main function is to read recive the fact that the white shining write timing, get it behind the 40-bit output separated by temperature and humidity and the parity bit, we mainly used in high temperature and humidity of eight position, temperature and humidity low eight I also tried the results are displayed through 0, so if it is possible to write your own discard low eight bits of data. It is worth noting that the parity bit is not discarded, discard the check digit data for the last influential.

uchar receive_byte()//接收一个字节
{
 uchar i,temp;
 for(i=0;i<8;i++)
 {
   while(!i0);//等待50us的低电平开始信号结束    
   delay_1us();    //开始信号结束后,延时26us-28us
   delay_1us();
   delay_1us();
   temp=0;   //如果时间为26us-28us 则表示接收的数据为‘0’
   if(i0==1)
    temp=1;//如果时间超过26us-28us 则表示收到的数据为‘1’
	while(i0);//等待数据信号高电平, ‘0’为26us-28us;‘1’为70us
	data_byte<<=1;//将接收到的数据向高位左移
	data_byte|=temp;	
 }
 return data_byte;
}

void receive() //接收数据
{
 uchar T_H,T_L,R_H,R_L,check,num_check;
 start();//开始信号
 i0=1;   //主机设为输入  判断从机DHT11的响应信号
 if(!i0) //判断是否有响应信号
 {
  while(!i0);//判断从机发出80us低电平信号是否结束
  while(i0);
  R_H= receive_byte();//湿度高位
  R_L= receive_byte();//湿度低位
  T_H= receive_byte();//温度高位
  T_L= receive_byte();//温度低位
  check= receive_byte();//检验位
  i0=0;//当所有的数据接收完成后,将从机电平拉低50us
  delay_1us();	//拉低50us
  delay_1us();
  delay_1us();
  delay_1us();
  delay_1us();
  i0=1;//将从机电平拉低,进入空闲状态
  num_check=R_H+R_L+T_H+T_L;
  if(num_check=check)//判断读取的数据与校验位是否相同
  {
	RH=R_H;
	RL=R_L;
	TH=T_H;
	TL=T_L;
	check=num_check;
  } 
 }
}

ESP8266 module

About this module There are many things that need attention, I am using this module through a lot of detours, now for everyone to talk about the experience of this module, for everyone to mine. Specific basic education can recommend to you a few articles https://blog.csdn.net/lzw508170827/article/details/89671350
http://www.matools.com/blog/190486480

About baud rate

This is what I stepped on a mine this module the biggest, but also let me learn a lot of things.
About baud rate esp8266 module, which is 115200 baud factory, crystal 51 microcontroller development board our common use is 11.0592hz, its maximum baud rate is 57600 So at that time that we need to replace the crystal, we designed a minimal system, prepare all components are integrated together, here coupled with schematics and we may need. Pictures from friends Kellogg
But here I want to tell you is that in fact the baud rate ESP8266 can be changed, we change the baud rate on the instructions of the code may not work, here to provide a code AT + UART_CUR = 9600,8, 1,0,0
If that does not work you can look for other online, but make sure the baud rate of the module can be changed! ! ! ! !

Wiring problem

We are directly on top of the development board to do, so the wiring should be noted that most microcontroller and ESP8266 to common ground , I also suffered losses in it, and check the code without any problems but the transmission over the data is garbled, waste a lot of time, eat a hardware basis for bad loss.
The rest of the wiring is 3v3 and EN termination 3.3v power supply, RX and TX and SCM RI and TI can reverse only need to take this five lines of our ESP8266 can work properly.

Initialization ESP8266

The biggest problem is the whole ESP8266 initialization problems that may check the code many times, no problems, but the configuration is unsuccessful, explain here my problems when writing code.

while(1)
  {
   sendstr(esp_cipmux);
   if(data_compare("OK"))
   break;
   delay(3000);
  }

This section is to configure multiple connections, because after ESP8266 model name and password set up power-down is not erased, so we can simply look at the configuration of multiple connections and server. To note here is not to direct our instruction on the array which is then sent to the ESP8266
like this

for(a=0;a<50000;a++);
  sendstring("AT+CWMODE=2\n");
  for(a=0;a<20000;a++);
  sendstring("AT+RST");
  for(a=0;a<20000;a++);

The reason for this string may be too late to send ESP8266 identified, you will be in serial debugging assistant there to see these things in an infinite loop, but you can not find the wifi hotspot, though delayed, but still can not play a role , may sometimes also occasionally configuration but is very unstable. Correct or write some comparison function to see whether there is ESP8266 return "ok"
return function is also very simple

uchar data_compare(uchar *p)
{
 	if(strstr(esp8266_return,p)!=NULL)
	return 1;
	else
	{
	return 0;
	}
}

Also there is the serial interrupt remember to shut the initialization time! ! ! , Or when performing sendstr will be caught in an endless loop, It must be noted that, at the time of writing the whole code, we must remember the serial port interrupt status, once misplaced enter the serial interrupt logic leads to an endless loop dislocation, their own hard to check it out. **

data transmission

This part is actually very simple code directly on

	ES=0;
	for(i=0;i<10;i++)
   {
	sendbyte(com[i]);
	}
	sendstr("\r\n");
	sendstr(esp_cipsend); 

Noteworthy point is first transmission data to be transmitted to a serial array, and then enter the command at + cipsend otherwise transmits the result of this isHere Insert Picture Description

Kind show

Here Insert Picture Description

to sum up

CSDN first time to write the article, can not say too many things professional, but also a lot of places I saw this thing on CSDN article, here to thank God for helping me big, but I also like to thank the production stage give me help small partner, after the creation of a new mine will give everyone on CSDN, last thank you to join inHere Insert Picture Description

Published an original article · won praise 0 · Views 111

Guess you like

Origin blog.csdn.net/CHENNOTWAIT/article/details/105350944