#include"reg51.h"
#include"lcd.h"
#include"ds18b20.h"
#include"string.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1=P1^4;
sbit k2=P1^5;
sbit k3=P1^6;
sbit d1=P2^2;
sbit d2=P2^3;
sbit d3=P2^4;
sbit fire=P3^2;
sbit sound=P2^1;
uchar count,i=0;
uchar tempdata[7]={0,0,0,'.',0,0,'\0'};
uchar recedata[8]=" ";
void delay5ms(void)
{
uchar i,j;
for(i=10;i>0;i--)
for(j=200;j>0;j--)
;
}
void delay1ms(void)
{
uchar i,j;
for(i=2;i>0;i--)
for(j=200;j>0;j--)
;
}
void delay500ms(void)
{
uchar i,j,k;
for(i=10;i>0;i--)
for(j=200;j>0;j--)
for(k=100;k>0;k--)
;
}
void delay(uchar x)
{
uchar i,j;
while(x--)
{
for(j=200;j>0;j--)
for(i=248;i>0;i--)
;
}
}
void delay20ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 1;
j = 216;
k = 35;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void k1scan(void)
{
if(k1==0)
{
delay5ms();
if(k1==0)
{d1=~d1;}
do
{delay5ms();}
while(k1==0);
}
}
void k2scan(void)
{
if(k2==0)
{
delay5ms();
if(k2==0)
{d2=~d2;}
do
{delay5ms();}
while(k2==0);
}
}
void k3scan(void)
{
if(k3==0)
{
delay5ms();
if(k3==0)
{d3=~d3;}
do
{delay5ms();}
while(k3==0);
}
}
void startuptemp(void)
{
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
delay500ms();
}
int readtemp(void)
{
uchar temph,templ;
int temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
templ=Read_DS18B20();
temph=Read_DS18B20();
temp=temph;
temp=temp<<8;
temp=temp|templ;
return temp;
}
void tempdealdisp(int temp)
{
float tp;
if(temp<0)
{
temp=temp-1;
temp=~temp;
tp=temp;
temp=tp*0.0625*100+0.5;
LcdShowStr(0,0,"-");
}
else
{
tp=temp;
temp=tp*0.0625*100+0.5;
LcdShowStr(0,0,"+");
}
tempdata[0]=temp/10000+0x30;
tempdata[1]=temp%10000/1000+0x30;
tempdata[2]=temp%1000/100+0x30;
tempdata[3]='.';
tempdata[4]=temp%100/10+0x30;
tempdata[5]=temp%10+0x30;
tempdata[6]='\0';
LcdShowStr(1,0,tempdata);
LcdShowStr(7,0," ");
}
void init_it0(void)
{
EX0=1;
EA=1;
IT0=0;
}
void it0(void) interrupt 0
{
LcdShowStr(8,0,"fire!!!");
sound=1;
delay1ms();
sound=0;
delay1ms();
}
void lampworking(void)
{
if(d1==0)
{
LcdShowStr(0,1,"1:on ");
}
else
{
LcdShowStr(0,1,"1:off");
}
if(d2==0)
{
LcdShowStr(5,1,"2:on ");
}
else
{
LcdShowStr(5,1,"2:off");
}
if(d3==0)
{
LcdShowStr(10,1,"3:on ");
}
else
{
LcdShowStr(10,1,"3:off");
}
}
void uart_init(void)
{
SCON=0X50;
TMOD=0X20;
PCON=0X00;
IP=0x02;
TH1=0xF3; //波特率2400
TL1=0xF3;
ES=1;
EA=1;
TR1=1;
}
void t0_init(void)
{
TMOD=TMOD&0XF0;
TMOD=TMOD|0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
}
void uart_sendstring(uchar *str)
{
//uchar *p;
// p=str;
TR0=0;
while(*str!='\0')
{
SBUF=*str;
while(TI==0);
TI=0;
str++;
}
TR0=1;
}
/*void uart_sendchar(uchar c)
{
SBUF=c;
while(TI==0);
TI=0;
}*/
void senddata(void)
{
uart_sendstring("POST /devices/34754466/datapoints?type=3 HTTP/1.1\r\n");
delay20ms();
uart_sendstring("api-key: PMS042lY7YNtSC1ilGJUVprdJik=\r\n");
delay20ms();
uart_sendstring("Host: api.heclouds.com\r\n");
delay20ms();
uart_sendstring("Content-Length:24\r\n");
delay20ms();
uart_sendstring("\r\n");
delay20ms();
uart_sendstring("{\"temperature\":"); //11
delay20ms();
uart_sendstring(tempdata);
delay20ms();
uart_sendstring("}\r\n");
delay20ms();
uart_sendstring("\r\n");
delay(50);
}
void t0(void ) interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==40)
{
// uart_sendchar('A');
count=0;
// uart_sendstring(tempdata);
// uart_sendstring("\r\n");
senddata();
//uart_sendstring("\r\n");
count=0;
// if(fire==0)
// {
// uart_sendstring("fire!!!\r\n");
// }
// else
// {
// uart_sendstring("no fire!\r\n");
// }
// if(d1==0)
// {
// uart_sendstring("d1 is on!\r\n");
// }
// else
// {
// uart_sendstring("d1 is off!\r\n");
// }
// if(d2==0)
// {
// uart_sendstring("d2 is on!\r\n");
// }
// else
// {
// uart_sendstring("d2 is off!\r\n");
// }
// if(d3==0)
// {
// uart_sendstring("d3 is on!\r\n");
// }
// else
// {
// uart_sendstring("d3 is off!\r\n");
// }
}
}
void uart_rec(void) interrupt 4
{
if(RI==1)
{
recedata[i]=SBUF;
RI=0;
i++;
if(recedata[i-1]=='9')
{
recedata[i-1]='\0';
i=0;
}
if(strcmp(recedata,"d1on")==0)
{
d1=0;
}
if(strcmp(recedata,"d1off")==0)
{
d1=1;
}
if(strcmp(recedata,"d2on")==0)
{
d2=0;
}
if(strcmp(recedata,"d2off")==0)
{
d2=1;
}
if(strcmp(recedata,"d3on")==0)
{
d3=0;
}
if(strcmp(recedata,"d3off")==0)
{
d3=1;
}
}
}
void tcpserverlogin(void)
{
uart_sendstring("AT+RST\r\n");
delay(50);
uart_sendstring("AT+CWMODE=3\r\n");
delay(20);
uart_sendstring("AT+CWJAP=\"XJ\",\"88888888\"\r\n");
//uart_sendstring("AT+CWJAP=\"XUJIN\",\"12345678\"\r\n");
//uart_sendstring("AT+CWJAP=\"HUAWEI Mate 9\",\"wjtt1111\"\r\n");
delay(100);
//uart_sendstring("AT+CIPSTART=\"TCP\",\"10.36.32.131\",8234\r\n");
uart_sendstring("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80\r\n");
delay(100);
uart_sendstring("AT+CIPMODE=1\r\n");//使能透传模式
delay(20);
uart_sendstring("AT+CIPSEND\r\n");//透传发送数据
delay(20);
}
void main()
{
d1=1;
d2=1;
d3=1;
init_it0();
InitLcd1602();
uart_init();
tcpserverlogin();
t0_init();
while(1)
{
k1scan();
k2scan();
k3scan();
startuptemp();
tempdealdisp(readtemp());
lampworking();
if(fire==1)
{LcdShowStr(8,0,"no fire");}
}
}