通过串口助手可以上传数据但是单片机中就不行
#include <reg52.h>
#include <string.h>
#include <intrins.h>
#include <stdio.h>
typedef unsigned char u8;
typedef unsigned int u16;
#define uchar unsigned char //?????????????????????
#define uint unsigned int
sbit bemp=P1^5;
sbit Data=P3^7;
sbit fire=P3^2;
sbit LED=P2^0;
sbit fog=P3^3;
u8 m=0;
u16 i;
u16 j=0;
unsigned int rec_fire[1]=0;
unsigned int rec_fog[1]=0;
unsigned int rec_dat[4]; //????????????
char Recive_table[20]=""; //???????,???20?????
char Recive_state = 0; //?????????
void delay(u16 i)
{
while(i--);
}
void sound(){
bemp=~bemp;
delay(25);
}
void delay1(uint z) //???1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void ms_delay(int t)
{
int i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void DHT11_delay_us(unsigned char n)
{
while(--n);
}
void DHT11_delay_ms(unsigned int z)
{
unsigned int i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void DHT11_start()
{
Data=1;
DHT11_delay_us(2);
Data=0;
DHT11_delay_ms(20);
Data=1;
DHT11_delay_us(13);
}
unsigned char DHT11_rec_byte()
{
unsigned char i,dat;
for(i=0;i<8;i++)
{
while(!Data);
DHT11_delay_us(8);
dat<<=1;
if(Data==1)
{
dat+=1;
}
while(Data);
}
return dat;
}
//?????????????
void DHT11_receive()
{
unsigned int R_H,R_L,T_H,T_L;
unsigned char RH,RL,TH,TL,revise;
DHT11_start();
Data=1;
if(Data==0)
{
while(Data==0); //???????
DHT11_delay_us(40); //????????80us
R_H=DHT11_rec_byte(); //?????????λ
R_L=DHT11_rec_byte(); //?????????λ
T_H=DHT11_rec_byte(); //?????????λ
T_L=DHT11_rec_byte(); //?????????λ
revise=DHT11_rec_byte(); //????У??λ
DHT11_delay_us(25); //????
if((R_H+R_L+T_H+T_L)==revise) //У??
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
}
/*???????????????*/
rec_dat[0]=RH;
rec_dat[1]=RL;
if((RH/10)>=9&&(RL%10)>=0){
for(j=0;j<1000;j++){
sound();
}
}
rec_dat[2]=TH;
rec_dat[3]=TL;
if((TH/10)>=3&&(TH%10)>=0){
for(j=0;j<1000;j++){
sound();
}
}
}
}
void wenshidu()
{
ES = 0;
TI = 1;
ms_delay(1000);
DHT11_delay_ms(150);
DHT11_receive();
printf("POST /devices/735378509/datapoints?type=5 HTTP/1.1\r\napi-key:jG7K=iMXXnxtfuKyUnT43Q81to8=\r\nHost:api.heclouds.com\r\nContent-Length:10\r\n\r\n,;temp,%d\r\n",rec_dat[2]);
/*printf("POST /devices/734866622/datapoints?type=5 HTTP/1.1 \r\n");
printf("api-key:68tqRjnm4nzBMu0C6KMQ2zx18gk= \r\n");
printf("Host:api.heclouds.com \r\n");
printf("Content-Length:10 \r\n") ;
printf("\r\n");
printf(",;temp,%d",rec_dat[2]);*/
// printf("AT+CIPSEND=0,32\r\n");
// printf("Humi:%d.%d Temp:%d.%d Fire:%d fog:%d \r\n",rec_dat[0],rec_dat[1],rec_dat[2],rec_dat[3],rec_fire[0],rec_fog[0]);
while(!TI);
TI = 0;
ES = 1;
}
void Uart_Init()
{
TMOD=0x20;
TH1=0xf3;
TL1=0xf3;
PCON=0x80; //???????????????
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
}
void WIFI_Init()
{
ES = 0;
TI = 1;
printf("AT+CWJAP=\"TP-LINK_767A\",\"123321985211\"\r\n");
delay1(5000);
printf("AT+CIFSR\r\n");
delay1(2000);
printf("AT+CIPMUX=0\r\n");
delay1(2000);
printf("AT+CIPSTART=\"TCP\",\"183.230.40.33\",80 \r\n");
delay1(2000);
printf("AT+CIPMODE=1\r\n");
delay1(2000);
printf("AT+CIPSEND\r\n");
delay1(3000);
while(!TI);
TI = 0;
ES = 1;
}
void main(){
Uart_Init();//?????????????????9600
ms_delay(1000) ;
WIFI_Init(); //wifi?????
while(1){
if(fire==0){
delay(100);
if(fire==0){
rec_fire[0]=1;
for(j=0;j<1000;j++){
sound();
}
}
}else{
rec_fire[0]=0;
}
if(fog==0){
delay(100);
if(fog==0){
rec_fog[0]=1;
for(j=0;j<1000;j++){
sound();
}
}
}else{
rec_fog[0]=0;
}
wenshidu();
if(Recive_state == 1)
{
ES=0; //????????λ
/* if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P'))//????????????????+IPD,x,x:y
{
if((Recive_table[3]=='D')&&(Recive_table[6]==','))
{
if(Recive_table[9]=='1')
{
wenshidu();
}
if(Recive_table[9]=='2')
{
LED=0; //??????????????????
}
if(Recive_table[9]=='3')
{
LED=1; //??????????????????
}
}
} */
memset(Recive_table,'\0',20);
Recive_state = 0;
ES=1; //????????λ
}
}
}
void Uart_Interrupt() interrupt 4
{
static char i=0; //??????λ?λ???????????static
if(RI==1)
{
ES = 0;
RI=0;
Recive_table[i]=SBUF;
i++;
if((Recive_table[i-1] == '\n'))
{
Recive_table[i]='\0';
i=0;
Recive_state = 1;
}
ES = 1;
}
else
TI = 0;
}
|