MCU是89C52單片機
WiFi模塊是ESP8266
其他 8路繼電器 電源模塊 使用貝殼物聯做服務器 還有客戶端。
也可以用花生殼做內網穿透,8266做服務器,也可以實現物聯以及反饋(8266支持同時5個客戶端)
已經實現8開關+反饋+遠程開門和鎖門(汽車中控鎖)
附C代碼以及手機客戶端代碼(E4A) ID以及APIKEY 需要去貝殼物聯申請貝殼物聯用戶中心
e4a源代碼:
51hei.png (121.79 KB, 下載次數: 105)
下載附件
2020-11-23 21:03 上傳
手機客戶端含源程序:
51hei圖片_20201123204407.jpg (81.64 KB, 下載次數: 129)
下載附件
2020-11-23 20:50 上傳
制作出來的實物圖如下:
51hei圖片_20201123202051.jpg (200.87 KB, 下載次數: 107)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202114.jpg (92.83 KB, 下載次數: 131)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202118.jpg (135.35 KB, 下載次數: 99)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202058.jpg (135.71 KB, 下載次數: 114)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202102.jpg (167.83 KB, 下載次數: 111)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202107.jpg (134.23 KB, 下載次數: 158)
下載附件
2020-11-23 20:50 上傳
51hei圖片_20201123202045.jpg (142.82 KB, 下載次數: 120)
下載附件
2020-11-23 20:50 上傳
單片機源程序如下:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit lcd0=P2^0; //輸出端口
- sbit lcd1=P2^1;
- sbit lcd2=P2^2;
- sbit lcd3=P2^3;
- sbit lcd4=P2^4;
- sbit lcd5=P2^5;
- sbit lcd6=P2^6;
- sbit lcd7=P2^7;
- sbit lcd8=P0^5;
- sbit lcd9=P0^6; //暫定為開鎖按鈕
- sbit state=P0^4; //門鎖開關反饋狀態
- sbit sw0=P1^0;
- sbit sw1=P1^1;
- sbit sw2=P1^2;
- sbit sw3=P1^3;
- sbit sw4=P1^4;
- sbit sw5=P1^5;
- sbit sw6=P1^6;
- sbit sw7=P1^7;
-
- /*****************相關變量**************/
- uchar Receive;
- uchar Recive_table[150]; //用于接收wifi模塊反饋到MCU上的數據
- uchar ac; //登錄狀態標識
- /*******************************************************************
- 名稱:延時函數 作用:毫秒級延時,微妙級延時函數,為數據收發完成作等待.......
- ********************************************************************/
- 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--);
- }
-
- void Uart_Init() //使用定時器1作為波特率發生器(STC89C52、STC89C51、AT89C51等均可)
- {
- TMOD = 0x20;
- SCON = 0x50; //設置串行方式
- TH1 = 0xFD; //波特率9600
- TL1 = TH1;
- PCON = 0x00;
- EA = 1; //總中斷打開
- ES = 1; //開串口中斷
- TR1 = 1;
- }
-
- void InitTimer0(void)
- {
- TMOD |= 0x01;
- TH0 = 0x0DC;
- TL0 = 0x00;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- /********************************************************************
- 名稱:串口發送函數 功能:MCU向無線WIFI模塊ESP8266發送數據
- ********************************************************************/
- void Send_Uart(uchar value)
- {
- ES=0; //關閉串口中斷
- TI=0; //清發送完畢中斷請求標志位
- SBUF=value; //發送
- while(TI==0); //等待發送完畢
- TI=0; //清發送完畢中斷請求標志位
- ES=1; //允許串口中斷
- }
-
- /********************************************************************
- 名稱:WIFI模塊設置函數 作用: 啟動模塊,以便可以實現無線接入和控制
- ********************************************************************/
-
- void ESP8266_Set(uchar *puf,uchar a) // 數組指針*puf指向字符串數組
- {
-
-
- while(*puf!='\0') //遇到空格跳出循環
- {
- Send_Uart(*puf); //向WIFI模塊發送控制指令。
- us_delay(5);
- puf++;
- }
-
- if(a==1)
- {
- us_delay(5);
- Send_Uart('\r'); //回車
- us_delay(5);
- Send_Uart('\n');
- } //換行
- }
- unsigned char *Itoa(unsigned int ni,int dd)
- {char i=0,j=0,temp[16],outstr[16];
- unsigned int n,num=ni;
- while(num>=dd)
- {n=num%dd;
- if(n>9)temp[i]=n+0x37;else temp[i]=n+0x30;
- num=num/dd;
- i++;
- }
- n=num;if(n>9)temp[i]=n+0x37;else temp[i]=n+0x30;
- j=0;
- for(;i>=0;i--){outstr[j]=temp[i];j++;}
- outstr[j]=0;
- return outstr;
- }
- //------------------------------------------------------
- void ML_fasong()
- {
- ESP8266_Set("{\"M\":\"say\",\"ID\":\"ALL\",\"C\":\"",0);
- ESP8266_Set(Itoa(P2,2),0);
- ESP8266_Set(Itoa(state,2),0);
- ESP8266_Set("\",\"SIGN\":\"00\"}\n",1);
- }
- void ledcl(uchar a) //led處理
- {
-
- if(a==0)
- { lcd0=~lcd0;
- ML_fasong();
- }
- if(a==1)
- {
- lcd1=~lcd1;
- ML_fasong(); }
- if(a==2)
- {
- lcd2=~lcd2;
- ML_fasong();
- }
- if(a==3)
- {
- lcd3=~lcd3;
- ML_fasong();
- }
- if(a==4)
- {
- lcd4=~lcd4;
- ML_fasong();
- }
- if(a==5)
- {
- lcd5=~lcd5;
- ML_fasong();
- }
- if(a==6)
- {
- lcd6=~lcd6;
- ML_fasong();
- }
- if(a==7)
- {
- lcd7=~lcd7;
- ML_fasong();
- }
- if(a==8)
- {
- lcd8=0;
- ms_delay(500);
- lcd8=1;
- ML_fasong();
- }
- if(a==9)
- {
- lcd9=0;
- ms_delay(500);
- lcd9=1;
- ML_fasong();
- }
- }
- void key() //獨立按鍵處理
- {
- if(sw0==0)
- {
- ms_delay(10);
- if(sw0==0)
- { ledcl(0); }
- while(!sw0);
- }
- if(sw1==0)
- {
- ms_delay(10);
- if(sw1==0)
- { ledcl(1); }
- while(!sw1);
- }
- if(sw2==0)
- {
- ms_delay(10);
- if(sw2==0)
- { ledcl(2); }
- while(!sw2);
- }
- if(sw3==0)
- {
- ms_delay(10);
- if(sw3==0)
- { ledcl(3); }
- while(!sw3);
- }
- if(sw4==0)
- {
- ms_delay(10);
- if(sw4==0)
- { ledcl(4); }
- while(!sw4);
- }
- if(sw5==0)
- {
- ms_delay(10);
- if(sw5==0)
- { ledcl(5); }
- while(!sw5);
- }
- if(sw6==0)
- {
- ms_delay(10);
- if(sw6==0)
- { ledcl(6); }
- while(!sw6);
- }
- if(sw7==0)
- {
- ms_delay(10);
- if(sw7==0)
- { ledcl(7); }
- while(!sw7);
- }
- }
-
- void ML_panduan()
- {
- uchar x,i,y,z;
- x=30;
- y=50;
- i=0;
- if(Recive_table[6]=='b')
- {
- ms_delay(1000);
- ESP8266_Set("{\"M\":\"beat\"}\n",1);
- Recive_table[6]='0';
- if(ac>2)
- {
- ac--;
- }
- else
- {
- ac=0;
- }
-
-
- }
- while(x--)
- {
- if (Recive_table[i]=='s')
- {
- if(Recive_table[i+1]=='a')
- {
- z=i+25;
- x=0;
- while(y--)
- {
- if(Recive_table[z]=='C')
- {
- if(Recive_table[z+2]==':')
- {
- switch(Recive_table[z+4])
- {
- case('a'): y=0; ledcl(0); Recive_table[z+4]='1'; break;
- case('b'): y=0; ledcl(1); Recive_table[z+4]='1'; break;
- case('c'): y=0; ledcl(2); Recive_table[z+4]='1'; break;
- case('d'): y=0; ledcl(3); Recive_table[z+4]='1'; break;
- case('e'): y=0; ledcl(4); Recive_table[z+4]='1'; break;
- case('f'): y=0; ledcl(5); Recive_table[z+4]='1'; break;
- case('g'): y=0; ledcl(6); Recive_table[z+4]='1'; break;
- case('h'): y=0; ledcl(7); Recive_table[z+4]='1'; break;
- case('i'): y=0; ledcl(8); Recive_table[z+4]='1'; break;
- case('j'): y=0; ledcl(9); Recive_table[z+4]='1'; break;
- case('o'): y=0; lcd5=0; Recive_table[z+4]='1'; break;
- case('n'): y=0; lcd5=1; Recive_table[z+4]='1'; break;
- case('0'): y=0; ML_fasong(); Recive_table[z+4]='1'; break;
-
- }
- }
- }
- else
- z++;
- }
- }
- }
- else
- i++;
- }
- }
- //-------------------------------------------------------------------------
- void ML_chushihua()
- {
- uint p;
- p=2000;
- // ESP8266_Set("AT+RST",1);
- // ms_delay(p);
- ESP8266_Set("AT+CIPMUX=0",1);
- ms_delay(p);
- ESP8266_Set("AT+CIPSTART=\"TCP\",\"121.42.180.30\",8282",1);
- ms_delay(p);
- ESP8266_Set("AT+CIPMODE=1",1);
- ms_delay(p);
- ESP8266_Set("AT+CIPSEND",1);
- ms_delay(p);
- ESP8266_Set("{\"M\":\"checkin\",\"ID\":\"此處為ID\",\"K\":\"此處為APIKEY\"}\n",1);
- ac=0;
- }
- void main()
- { bit bdat;
- bdat=state;
- Uart_Init();
- InitTimer0();
- ML_chushihua();
-
- while(1)
- {
- ML_panduan();
- key();
- if(state!=bdat)
- {
- bdat=state;
- ML_fasong();
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
51hei.png (3.02 KB, 下載次數: 120)
下載附件
2020-11-23 21:01 上傳
所有資料51hei提供下載:
89C52+ESP8266簡易物聯網+手機客戶端.zip
(638.02 KB, 下載次數: 666)
2020-11-23 20:51 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|