仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
注意仿真里面用的是at89c51单片机
接上一个贴,连接ONENET MQTT服务器
unsigned char code CIPSTART[]="AT+CIPSTART=\"TCP\",\"x.x.x.39\",6002\r\n";
unsigned char code CIPMODE[]="AT+CIPMODE=1\r\n";
unsigned char code CIPSEND[]="AT+CIPSEND\r\n";//先连接上服务器
unsigned int code mqdata[]={0x32,0x0D,0x00,0x06,0x54,
0x6F,0x70,0x69,0x63,0x32,0x00,0x01,0x31,0x32,0x33}; //发送数据Topic2 123
unsigned int code mqtt[]={0x10,0x27,0x00,0x04,0x4D,0x51,0x54,0x54,0x04,0xC0,0x00,0x78,
0x00,0x09,0x39,0x35,0x34,0x30,0x32,0x35,0x39,0x36,0x30,0x00,0x06,
0x35,0x32,0x34,0x34,0x38,0x35,0x00,0x08,0x31,0x31,0x31,0x31,0x31,0x31,0x31,0x31}; //连接自己创建的设备后设备会显示在线,这里的代码可以自己去复制。(这是我的设备,不要去连,改成自己的)
1.
if(res=='O'&&CP==1){
CP=0;
sengstring(CWSTOPSMART);
delay(500);
sengstring(CIPMODE);
delay(500);
sengstring(CIPSEND);
delay(500);
memset(GOTIP,'\0',sizeof(GOTIP));
for(i=0;i<41;i++){
sengchar(mqtt[ i]);//这里发送连接服务器
delay(1);
}
showchina_char(15,18,0,0);
ET0=1;
wififalg=1;
}
2.
void lcd1602T0() interrupt 1 /
{
unsigned char i;
nu++;
if(bis==0){ ET0=0;homepage();bis=1;}
if(nu>=150){
nu=0;
nud++;
DTH11_START();
humi =((DTH11data[0]>>4)*16)+(DTH11data[0]&0x0f);
temp =((DTH11data[2]>>4)*16)+(DTH11data[2]&0x0f); //温度
si[2]=((humi/10));
si[3]=((humi%10));
si[0]=((temp/10));
si[1]=((temp%10));
humiture();
if(nud>=5){
ET0=0;
nud=0;
if(wififalg==1){
// sbuftem[0]=((temp/10)+'0');
// sbuftem[1]=((temp%10)+'0');
// sengstring(sbuftem);
for(i=0;i<15;i++){
sengchar(mqdata[ i]);//这里定时发送消息到服务器,数据写死的,可以自行更改
}
ET0=1;
}
}
}
Keil5代码下载:
Keil代码.7z
(91.19 KB, 下载次数: 16)
|