日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

APP同过ESP8266与51单片机通信

發(fā)布時(shí)間:2023/12/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 APP同过ESP8266与51单片机通信 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

APP通過ESP8266與單片機(jī)通信

簡述

本項(xiàng)目中是用ESP8266作為熱點(diǎn),工作在MODE2模式,手機(jī)作為station接入ESP8266的網(wǎng)絡(luò)進(jìn)行數(shù)據(jù)傳輸,同時(shí),ESP8266與52單片機(jī)之間通過串口進(jìn)行數(shù)據(jù)傳輸。
本項(xiàng)目中只需要APP向WIFI發(fā)送數(shù)據(jù),所以在APP的程序中沒有接收WIFI數(shù)據(jù)的程序。在文章最后,則是筆者在寫代碼的過程中遇到的一個(gè)問題,51單片機(jī)不能接收WIFI數(shù)據(jù),但能向WIFI發(fā)送AT指令的問題。

手機(jī)端實(shí)現(xiàn)

建立連接線程

以下為APP與ESP8266建立連接的線程,建立連接時(shí)只要獲取其對(duì)象即可。其中的IP和port為ESP8266的IP地址和端口號(hào),IP一般默認(rèn)為192.168.0.1,端口號(hào)默認(rèn)為8080,端口號(hào)可以自己設(shè)置。

private class ConnectThred extends Thread{private String ip;private int port;public ConnectThred(String ip,int port){this.ip = ip;this.port = port;}public void run(){//接收數(shù)據(jù)可用子線程也可直接在此線程操作char[] buffer=new char[256];//定義數(shù)組接收輸入流數(shù)據(jù)String bufferString="";//定義一個(gè)字符接收數(shù)組數(shù)據(jù)int conut =0;//初始化buffer數(shù)組長度為0int tag=0;//初時(shí)寫入數(shù)組的位置try {//建立連接socket = new Socket(ip,port);runOnUiThread(new Runnable() {@Overridepublic void run() {Log.d("Controler","runOnUI");bt_link.setText("斷開連接");bt_link.setBackgroundColor(Color.GREEN);Toast.makeText(Controler.this,"連接成功",Toast.LENGTH_LONG).show();}});//讀取輸入流bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));//獲取輸出流OutputStream=socket.getOutputStream();//獲取socket的輸出流,接收數(shù)據(jù)InputStream = socket.getInputStream();while (true) {//當(dāng)輸入流寫入buffer數(shù)組的長度大于0時(shí)即接收到數(shù)據(jù)時(shí)while ((conut=bufferedReader.read(buffer))>0) {//將buffer數(shù)組的數(shù)據(jù)全部寫入bufferString字符類型while ( tag<buffer.length) {bufferString=bufferString+buffer[tag];tag++;}//將數(shù)據(jù)給messageHandler刷新UI界面Message msgMessage =new Message();msgMessage.obj=bufferString;msgMessage.what=1;messageHandler.sendMessage(msgMessage);//初始化數(shù)據(jù),以便處理下一條輸入流信息tag=0;bufferString="";}}} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();runOnUiThread(new Runnable() {@Overridepublic void run() {Toast.makeText(Controler.this,"連接錯(cuò)誤",Toast.LENGTH_LONG).show();}});}}}

發(fā)送數(shù)據(jù)線程

以下是APP向ESP8266發(fā)送數(shù)據(jù)的代碼,同樣在一個(gè)子線程中執(zhí)行。需要發(fā)送數(shù)據(jù)時(shí)通過對(duì)象調(diào)用此方法即可。

//向服務(wù)器發(fā)送數(shù)據(jù)子程序public class Send extends Thread{public void run(){if(socket != null){try{if (msg_send != null && msg_send.length()>0){OutputStream.write(msg_send.getBytes("utf-8"));OutputStream.flush();msg_send="";}else{Looper.prepare();Toast.makeText(Controler.this,"發(fā)送的數(shù)據(jù)不能為空",Toast.LENGTH_LONG).show();Looper.loop();}}catch (IOException e){e.printStackTrace();}}}}

通過按鈕建立或斷開連接

定義相關(guān)變量

private static final String IP = "192.168.4.1";//服務(wù)端IPprivate static final int PORT = 8080; //端口號(hào)Socket socket; //創(chuàng)建一個(gè)socketBoolean IsConnected = false; //連接標(biāo)志ConnectThred mConnectThread; //TCP連接線程//輸入輸出流OutputStream OutputStream = null;//數(shù)據(jù)輸出流,發(fā)送數(shù)據(jù)BufferedReader bufferedReader;//聲明輸入流對(duì)象InputStream InputStream = null;//定義數(shù)據(jù)輸入流,用于接收數(shù)據(jù)

通過按鈕實(shí)現(xiàn)連接:

bt_link.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { //連接按鈕,點(diǎn)擊與ESP8266芯片連接或斷開if (socket ==null || !socket.isConnected()){mConnectThread = new ConnectThred(IP,PORT);mConnectThread.start();}if (socket!=null && socket.isConnected()){try {socket.close();socket = null;bt_link.setText("點(diǎn)擊連接");bt_link.setBackgroundColor(Color.GRAY);Toast.makeText(Controler.this,"連接已斷開",Toast.LENGTH_LONG).show();}catch (IOException e){e.printStackTrace();}}}});

通過按鈕發(fā)送數(shù)據(jù):
通過new一個(gè)發(fā)送數(shù)據(jù)線程的對(duì)象,調(diào)用start方法啟動(dòng)發(fā)送數(shù)據(jù)線程即可發(fā)送數(shù)據(jù)。

select_mode.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {mode_count++;if (mode_count > 4)mode_count=0;mode = mode_arr[mode_count];msg_send = "mod"+Integer.toString(mode_count)+"/n";new Send().start();tempmode.setText(mode); //設(shè)置模式}});

下位機(jī)端接收WIFI數(shù)據(jù)

下位機(jī)端使用51單片機(jī)實(shí)現(xiàn),下位機(jī)端與ESP8266之間通過串口進(jìn)行通信,傳輸數(shù)據(jù)。所以下位機(jī)接收WIFI數(shù)據(jù)時(shí)使用的是串口接收程序。

void UartConfiguration() {TMOD=0x20; // 設(shè)置計(jì)數(shù)器1工作模式2TH1=0xfd; //計(jì)數(shù)器1初值TL1=0xfd; //計(jì)數(shù)器1初值 兩個(gè)初值決定波特率,此處設(shè)置波特率9600PCON=0x00; //波特率倍增0x00不加倍,0x80加倍SCON=0x50; // EA=1; //ES=1; //TR1=1; //TI=1; }

將接收到的數(shù)據(jù)存入一個(gè)數(shù)組中然后進(jìn)行處理

void ESP_UART() interrupt 4 {static uchar i=0; // led1=~led1;if(RI==1){RI=0; //?óê?±ê??Recive_table[i]=SBUF;sendByte(Recive_table[i]); i++; if((Recive_table[i-1]=='\n')){Recive_table[i]='\0';i=0; }}elseTI=0; }

處理接收到的數(shù)據(jù)

void getData() {if(Recive_table[0]=="+")sendString(Recive_table);if((Recive_table[0]=='+')&&(Recive_table[1]=='I')&&(Recive_table[2]=='P')){if((Recive_table[3]=='D')&&(Recive_table[6]==',')){ if(Recive_table[9]=='0')//&& Recive_table[10]=='0' //{Shutdown();}else if(Recive_table[9]=='1')//&& Recive_table[10]=='1' //{MakeNum(Recive_table[10],Recive_table[11]);Startup();}else if(Recive_table[9]=='d')//&& Recive_table[10]=='1' //{MakeNum(Recive_table[10],Recive_table[11]);}else if(Recive_table[9]=='e')//&& Recive_table[10]=='1' //{MakeNum(Recive_table[10],Recive_table[11]);}else if(Recive_table[9]=='o'&& Recive_table[10]=='d') //{m_index=Recive_table[11]-'0';SetMode(m_index);}else if(Recive_table[9]=='f')//&& Recive_table[10]=='1' //{SetSweep(Recive_table[10]-'0');}else if(Recive_table[9]=='p')//&& Recive_table[10]=='1' //{SetSpeed(Recive_table[10]-'0');}else if(Recive_table[9]=='i')//&& Recive_table[10]=='1' //{SetDirect(Recive_table[10]-'0');}memset(Recive_table,0,50);} } }

注意

當(dāng)ESP8266與51單片機(jī)通信時(shí),51單片機(jī)不能使用CH340芯片進(jìn)行串口數(shù)據(jù)的傳輸,不然會(huì)出現(xiàn)ESP8266發(fā)送的數(shù)據(jù)51單片機(jī)接收不到的情況。

總結(jié)

以上是生活随笔為你收集整理的APP同过ESP8266与51单片机通信的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。