ESP8266 wireless 18B20 temperature mobile phone receiver with source code and open source

Publisher:MoonlightStarLatest update time:2019-11-25 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

WIFI wireless communication combined with mobile phone E4A programming, a good learning routine, open source all the codes, such as the ESP8266 wireless network address is directly built into E4A, one-click access each time, the initial value is 10.10.10.208
port 5000
, those who don't know E4A can only burn the ESP8266 module according to this value. If you know E4A, just change it to your own IP

 

MCU source program:

/************************************************************************/ 

/* WIFI wireless transmission mobile phone display */

/************************************************************************/

#include

#include

#include

#define uint unsigned int 

#define uchar unsigned char 


/*********************Related variables**************/ 

uint Receive,i,xm1,xm2,xm3,xm0;                              

long s;  


[size=36.9444px]uchar Receive_table[40]; //Used to receive data fed back to MCU by the WiFi module[/size]

uchar code ta[]="0123456789- "; //Test it 

uchar table[]="Temperature: 00.0 C"; //Positioning template for transmission


sbit dq=P3^7; //18B20 bus interface. DQ


bit tflag; //Define a temperature flag. +, -

uint tvalue; //define a global variable to combine high and low 8-bit temperature //display temperature




void delay (int a) // millisecond delay

{

int x,y;

for(x=a;x>0;x--)

        for(y=110;y>0;y--);

}

void delayus(int t) // microsecond delay

{

while(t--);

}

/*********************************************************************** 

Name: Delay function Function: millisecond delay, microsecond delay function, waiting for data transmission and reception to complete.......  

************************************************************************/ 

void ms_delay(uint t) 

{  

        uint i,j;  

        for(i=t;i>0;i--)   

        for(j=110;j>0;j--); 

}   


void us_delay(uchar t) 

{  

        while(t--); 

}         

/*------------------------------------------------

uS delay function, with input parameter unsigned char t, no return value

unsigned char is used to define an unsigned character variable, whose value range is

0~255 Here we use a 12M crystal oscillator. Please use assembly for accurate delay. Approximate delay

The length is as follows: T=tx2+5 uS 

------------------------------------------------*/

void DelayUs2x(unsigned char t);

/*------------------------------------------------

mS delay function, with input parameter unsigned char t, no return value

unsigned char is used to define an unsigned character variable, whose value range is

0~255 Here we use a 12M crystal oscillator. Please use assembly code for accurate delay.

------------------------------------------------*/

void DelayMs(unsigned char t);

/*------------------------------------------------

uS delay function, with input parameter unsigned char t, no return value

unsigned char is used to define an unsigned character variable, whose value range is

0~255 Here we use a 12M crystal oscillator. Please use assembly for accurate delay. Approximate delay

The length is as follows: T=tx2+5 uS 

------------------------------------------------*/

void DelayUs2x(unsigned char t)

{   

[size=36.9444px] while(--t);[/size]

}

/*------------------------------------------------

mS delay function, with input parameter unsigned char t, no return value

unsigned char is used to define an unsigned character variable, whose value range is

0~255 Here we use a 12M crystal oscillator. Please use assembly code for accurate delay.

------------------------------------------------*/

void DelayMs(unsigned char t)

{

[size=36.9444px] while(t--) //roughly delay 1mS[/size]

[size=36.9444px] {[/size]

DelayUs2x(245);

                 DelayUs2x(245);

[size=36.9444px] }[/size]

}


[size=36.9444px]void Uart_Init() //Use timer 1 as the baud rate generator (STC89C52, STC89C51, AT89C51, etc.) [/size]

{  

    TMOD = 0x21;

SCON = 0x50; //Set the serial mode

TH1 = 0xFD; //Baud rate 9600

    TL1 = TH1;

    PCON = 0x00;

EA = 1; //General interruption enabled

ES = 1; //Open serial port interrupt

TR1 = 1; //Start timer 1


/******************************************************************** 

Name: Serial port sending function Function: MCU sends data to the wireless WIFI module ESP8266  

************************************************************************/ 

void Send_Uart(uchar value) 

{  

        ES=0; //Disable serial port interrupt  

        TI=0; // Clear the interrupt request flag when sending is completed   

        SBUF=value; //send  

        while(TI==0); //Wait for sending to complete   

        TI=0; // Clear the interrupt request flag when sending is completed   

        ES=1; //Enable serial port interrupt

        TH0=0;

        TL0=0;  

}  


/******************************************************************** 

Name: WIFI module setting function Purpose: Start the module so that wireless access and control can be achieved  

************************************************************************/ 


[size=36.9444px]void ESP8266_Set(uchar *puf) //Array pointer *puf points to string array [/size]

{    

        while(*puf!='') //jump out of loop when encountering a space  

        {   

                Send_Uart(*puf); //Send control instructions to the WIFI module.   

                us_delay(5);   

                puf++;    

        }  

        us_delay(5);  

        Send_Uart('r'); //Enter  

        us_delay(5);  

        Send_Uart('n'); //line break  

void ds18b20rst() // reset

{

dq=1;

delay(5);

dq=0;

delayus(400);

dq=1;

delayus(50);

}

uchar ds18b20rd() // read data

{

uchar i=0;

uchar dat=0; //Use DAT to store the read data

for(i=0;i<8;i++)

{

dq=0;

dat>>=1; //DAT not only shifts right by one bit, but also delays by one microsecond

dq=1;

if(dq==1)//You can also write (if(dq))

dat|=0x80; //DAT or 0X80

delayus(15);

}

return(dat); //Call DAT 

}

uchar ds18b20wr (uchar com) // write data/command 

{

                uchar i=0;

                for (i=0; i<8; i++)

  {

        dq=0;

        dq=com&0x01; //Compare the written number with 0x01

        delayus(15);

        dq=1;

        com>>=1;//data

        }

}

uint read_temp() //Read temperature, if it is greater than 256, use uint

{

uchar a,b; //Define two numbers as binary temperature.

ds18b20rst(); // reset

ds18b20wr(0xcc); //skip reading ROM

ds18b20wr(0x44); //Send temperature conversion command

ds18b20rst(); // reset

ds18b20wr(0xcc); //skip reading ROM

ds18b20wr(0xbe); //Read register

a=ds18b20rd(); //A can only read the lower 8 bits.

b=ds18b20rd(); //B can only read the upper 8 bits.

tvalue=b;

tvalue=tvalue<<8;//Shift left by 8 bits//Remove the upper 8 bits of the data in B. It can also be written as (tvalue=tvalue<<8;)//tvalue=tvalue*256;//tvalue<<=8;

tvalue|=a; // OR with A, the complete tvalue value is read away

if(tvalue<0xffff)

                tflag=0;

  else 

  {

  tvalue=~tvalue+1; //get the original data

                tflag=1;

        }

                tvalue=tvalue*0.625; //Multiply the data by precision=temperature. To display, it needs to be enlarged 10 times. (0.0625)

                return(tvalue); //Return tvalue value

}


/******************************************************************** 

Name: Main function Function: The execution entry of the program  

************************************************************************/ 


void main() 

[size=36.9444px]{ [/size]

        Uart_Init(); //Baud rate generator 

        ms_delay(10);

        ESP8266_Set("AT+CWMODE=2"); //Set router mode 1 to station, mode 2 to AP, mode 3 to station+AP mixed mode   

        ms_delay(500);

[size=36.9444px]// ESP8266_Set("AT+RST"); //Restart the wifi module [/size]

// ms_delay(1000);

        ESP8266_Set("AT+CWSAP="wifi_yuan","123456789",11,4"); //AT+CWSAP="wifi_yuan","123456789",11,4 Set module SSID:WIFI, PWD: password and security type encryption mode (WPA2-PSK) 

        ms_delay(500);

        ESP8266_Set("AT+CIPMUX=1"); // Enable multi-connection mode to allow multiple clients to access 

        ms_delay(500);

        ESP8266_Set("AT+CIPSERVER=1,5000"); //Start TCP/IP to realize network-based //control ESP8266_Set("AT+CIPSERVER=1,5000");  

        ms_delay(500);

        ESP8266_Set("AT+CIPSTO=0"); //Never time out

        ms_delay(500);         

        ES=1; //Enable serial port interrupt                 


        while(1)   

        {                

            read_temp(); //Read temperature

                  ms_delay(500);

                  s=tvalue;

                

                  xm0=(s/100)%10; //Store the hundreds digit

[size=36.9444px] xm1=(s/10)%10; //Store ten-digit data[/size]

[size=36.9444px] xm2=s%10; //Store single digit data[/size]

                

                  table[6]=ta[xm0];

[size=36.9444px] table[7]=ta[xm1];[/size]

[size=36.9444px] table[9]=ta[xm2]; [/size]

           

                  ESP8266_Set(table); //Send the distance data

            ms_delay(500);

                

        }    

}    


/************************************************************************  

Name: Serial communication interrupt Function: Enter this function after sending or receiving is completed, clear the corresponding flag bit by software to 0, and realize the normal sending and receiving of data by the module.  

************************************************************************/ 


void Uart_Interrupt() interrupt 4         

{    

        static uchar i=0;  

        if(RI==1)  

        {   

                RI=0;   

                Receive=SBUF; //MCU receives the data fed back by the wifi module       

                Receive_table[i]=Receive;   

                if((recive_table[i]=='n'))

                {                     

                        i=0;

                }    

                else i++; //Reload value when encountering line break  

        }   

        else TI=0;   

}  



Reference address:ESP8266 wireless 18B20 temperature mobile phone receiver with source code and open source

Previous article:NRF24L01 wireless module multi-machine communication MCU program host computer + slave computer
Next article:51 single chip temperature control alarm and time display program + Proteus simulation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号