(一)基于阿里云的MQTT遠程控制(Android 連接MQTT服務器,ESP8266連接MQTT服務器實現(xiàn)遠程通信控制----簡單的連接通信)

如果不了解MQTT的可以看這篇文章http://www.cnblogs.com/yangfengwu/p/7764667.html

http://www.cnblogs.com/yangfengwu/p/8026014.html

關于錢的問題,其實自己是花錢買的云服務,雖然自己現(xiàn)在能支付的起,但是呢為了盡量減少支出,自已還有好多好多文章要寫,好多好多元器件要買,所以哈會在自己的淘寶上賣源碼2元價格,一元捐出,一元自己留著當親們支付云服務的費用了

如果看不懂也沒關系,跟著做就可以了,做完以后您會發(fā)現(xiàn)原來MQTT這么好用,也如此簡單.

對了我要盡量把程序寫的爛一些,界面做的爛一些,因為既然是學習用的應該越直觀越好.......說一下,自己的服務器因為公開了穩(wěn)定性上肯定不好,

數(shù)據沖突也是可能的,這是第一篇,下面幾篇慢慢的來,咱一塊慢慢完善哈

實現(xiàn)的功能--手機和WIFI模塊都連接MQTT服務器,手機用按鈕實現(xiàn)遠程控制一個繼電器,然后WIFI模塊采集的DHT11的溫濕度,遠程發(fā)給手機

不過自己這批貼片的板子要等到后天才到..........................




先看一下Android 程序怎么寫,首先就是下載個MQTT的jar包

鏈接:https://pan.baidu.com/s/1bpjRzyB?密碼:90vv

新建一個Android 工程就不說了吧...............

將下載的jar包放在一個地方



我放在了我的Android的源碼的根目錄

現(xiàn)在在Android 工程導入下載的那個jar包







現(xiàn)在把可能用到的一些權限加上?


? ? ? ? ? "? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?


現(xiàn)在呢先寫個程序獲取手機的IMEI號,因為連接的時候每一個客戶端的ClientID要求不能一樣,咱就用IMEI號代表ClientID

其實就這兩句

TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);?

TelephonyIMEI = mTm.getDeviceId();


現(xiàn)在配置咱的MQTT


publicclass MainActivity extends Activity {


? ? String TelephonyIMEI="";


? ? privateMqttClient client;//clientprivateMqttConnectOptions options;//配置? ?

? ? @Override

? ? protectedvoid onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);


? ? ? ? TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);?

? ? ? ? TelephonyIMEI = mTm.getDeviceId();

? ? ? ? //Toast.makeText(getApplicationContext(), TelephonyIMEI, 500).show();? ? ? ? MyMqttInit();

? ? }


? ? /*? 初始化配置Mqtt? */privatevoid MyMqttInit()

? ? {


? ? ? ? try? ? ? ? {

? ? ? ? ? ? //(1)主機地址(2)客戶端ID,一般以客戶端唯一標識符(不能夠和其它客戶端重名)(3)最后一個參數(shù)是指數(shù)據保存在內存(具體保存什么數(shù)據,以后再說,其實現(xiàn)在我也不是很確定)client =newMqttClient("tcp://47.93.19.134:1883",TelephonyIMEI,new MemoryPersistence());

? ? ? ? } catch (MqttException e) {

? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


? ? ? ? options =newMqttConnectOptions();//MQTT的連接設置? ? ? ?

? ? ? ? options.setCleanSession(true);//設置是否清空session,這里如果設置為false表示服務器會保留客戶端的連接記錄,這里設置為true表示每次連接到服務器都以新的身份連接? ? ? ?

? ? ? ? options.setUserName("username");//設置連接的用戶名(自己的服務器沒有設置用戶名)? ? ? ?

? ? ? ? options.setPassword("password".toCharArray());//設置連接的密碼(自己的服務器沒有設置密碼)? ? ? ?

? ? ? ? options.setConnectionTimeout(10);// 設置連接超時時間 單位為秒? ? ? ?

? ? ? ? options.setKeepAliveInterval(20);// 設置會話心跳時間 單位為秒 服務器會每隔1.5*20秒的時間向客戶端發(fā)送個消息判斷客戶端是否在線,但這個方法并沒有重連的機制? ? ? ?

? ? ? ? client.setCallback(new MqttCallback() {

? ? ? ? ? ? @Override//獲取的消息會執(zhí)行這里--arg0是主題,arg1是消息publicvoid messageArrived(String arg0, MqttMessage arg1) throws Exception {

? ? ? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? ? ?

? ? ? ? ? ? }


? ? ? ? ? ? @Override//訂閱主題后會執(zhí)行到這里publicvoid deliveryComplete(IMqttDeliveryToken arg0) {

? ? ? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? ? ?

? ? ? ? ? ? }


? ? ? ? ? ? @Override//連接丟失后,會執(zhí)行這里publicvoid connectionLost(Throwable arg0) {

? ? ? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? ? ?

? ? ? ? ? ? }

? ? ? ? });

? ? }


?現(xiàn)在連接咱的服務器,連接成功后打印一下連接成功,連接是阻塞的,所以放在一個任務里面執(zhí)行連接


publicclass MainActivity extends Activity {


? ? String TelephonyIMEI="";


? ? privateMqttClient client;//clientprivateMqttConnectOptions options;//配置MqttConnectThread mqttConnectThread =newMqttConnectThread();//連接服務器任務? ? @Override

? ? protectedvoid onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);


? ? ? ? TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);?

? ? ? ? TelephonyIMEI = mTm.getDeviceId();

? ? ? ? //Toast.makeText(getApplicationContext(), TelephonyIMEI, 500).show();MyMqttInit();//初始化配置MQTT客戶端mqttConnectThread.start();//執(zhí)行連接服務器任務? ? }


? ? /*? 初始化配置Mqtt? */privatevoid MyMqttInit()

? ? {

? ? ? ? .........? ? }



? ? /*連接服務器任務*/class MqttConnectThread extends Thread

? ? {

? ? ? ? publicvoid run()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? client.connect(options);//連接服務器,連接不上會阻塞在這runOnUiThread(newRunnable() {//publicvoid run() {

? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "連接成功",500).show();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }

? ? ? ? ? ? catch (MqttSecurityException e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //安全問題連接失敗? ? ? ? ? ? }

? ? ? ? ? ? catch (MqttException e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //連接失敗原因? ? ? ? ? ? }

? ? ? ? }

? ? }


?現(xiàn)在下載到手機試一試


現(xiàn)在呢測試一下通信,測試接收消息,用調試助手發(fā)信息,然后手機端接收,然后顯示出來

?調試助手鏈接

鏈接:https://pan.baidu.com/s/1qYxEeLI?密碼:exfj


?現(xiàn)在先設置一下APP的訂閱的主題,和接收到消息之后就顯示出來


publicclass MainActivity extends Activity {


? ? String TelephonyIMEI="";


? ? privateMqttClient client;//clientprivateMqttConnectOptions options;//配置MqttConnectThread mqttConnectThread =newMqttConnectThread();//連接服務器任務? ? @Override

? ? protectedvoid onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);


? ? ? ? TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);?

? ? ? ? TelephonyIMEI = mTm.getDeviceId();

? ? ? ? //Toast.makeText(getApplicationContext(), TelephonyIMEI, 500).show();MyMqttInit();//初始化配置MQTT客戶端mqttConnectThread.start();//執(zhí)行連接服務器任務? ? }


? ? /*? 初始化配置Mqtt? */privatevoid MyMqttInit()

? ? {


? ? ? ? try? ? ? ? {

? ? ? ? ? ? //(1)主機地址(2)客戶端ID,一般以客戶端唯一標識符(不能夠和其它客戶端重名)(3)最后一個參數(shù)是指數(shù)據保存在內存(具體保存什么數(shù)據,以后再說,其實現(xiàn)在我也不是很確定)client =newMqttClient("tcp://47.93.19.134:1883",TelephonyIMEI,new MemoryPersistence());

? ? ? ? } catch (MqttException e) {

? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


? ? ? ? options =newMqttConnectOptions();//MQTT的連接設置? ? ? ?

? ? ? ? options.setCleanSession(true);//設置是否清空session,這里如果設置為false表示服務器會保留客戶端的連接記錄,這里設置為true表示每次連接到服務器都以新的身份連接? ? ? ?

? ? ? ? options.setUserName("username");//設置連接的用戶名(自己的服務器沒有設置用戶名)? ? ? ?

? ? ? ? options.setPassword("password".toCharArray());//設置連接的密碼(自己的服務器沒有設置密碼)? ? ? ?

? ? ? ? options.setConnectionTimeout(10);// 設置連接超時時間 單位為秒? ? ? ?

? ? ? ? options.setKeepAliveInterval(20);// 設置會話心跳時間 單位為秒 服務器會每隔1.5*20秒的時間向客戶端發(fā)送個消息判斷客戶端是否在線,但這個方法并沒有重連的機制? ? ? ?

? ? ? ? client.setCallback(new MqttCallback() {

? ? ? ? ? ? @Override//獲取消息會執(zhí)行這里--arg0是主題,arg1是消息publicvoid messageArrived(String arg0, MqttMessage arg1) throws Exception {

? ? ? ? ? ? ? ? // TODO Auto-generated method stubfinal String topic = arg0;//主題final String msgString = arg1.toString();//消息? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? runOnUiThread(newRunnable() {//publicvoid run() {

? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(),"主題:"+topic+"消息:"+msgString,500).show();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }


? ? ? ? ? ? @Override//訂閱主題后會執(zhí)行到這里publicvoid deliveryComplete(IMqttDeliveryToken arg0) {

? ? ? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? ? ?

? ? ? ? ? ? }


? ? ? ? ? ? @Override//連接丟失后,會執(zhí)行這里publicvoid connectionLost(Throwable arg0) {

? ? ? ? ? ? ? ? // TODO Auto-generated method stub? ? ? ? ? ? ? ?

? ? ? ? ? ? }

? ? ? ? });

? ? }



? ? /*連接服務器任務*/class MqttConnectThread extends Thread

? ? {

? ? ? ? publicvoid run()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? client.connect(options);//連接服務器,連接不上會阻塞在這? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? client.subscribe("test",0);//設置(訂閱)接收的主題,主題的級別是0? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? runOnUiThread(newRunnable() {//publicvoid run() {

? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "連接成功",500).show();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? });

? ? ? ? ? ? }

? ? ? ? ? ? catch (MqttSecurityException e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //安全問題連接失敗? ? ? ? ? ? }

? ? ? ? ? ? catch (MqttException e)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? //連接失敗原因? ? ? ? ? ? }

? ? ? ? }

? ? }

下載到手機?


?現(xiàn)在配置一下軟件,對了有些參數(shù)現(xiàn)在不明白沒關系,后面會介紹一下相關的知識,

軟件的主題名稱要和APP中訂閱的主題一樣 都是 test

現(xiàn)在連接


?現(xiàn)在點擊發(fā)布消息


看手機端


說明已經能通信了

現(xiàn)在說一下關于主題哈,關于/

現(xiàn)在把手機端的訂閱的主題改為"/#"


然后下載到手機

你會發(fā)現(xiàn)手機也能接收消息

手機都能接收到消息

#是一個匹配主題中任意層次數(shù)的通配符。比如說,如果你訂閱了test/device/#,你就可以接收到以下這些主題的消息。

test/device

test/device/后面隨便是什么

咱們的設備可以用"/"來進行分類,咱們的APP呢可以指定接收哪一類的產品的數(shù)據"XXXX/#"....是不是很方便

對了如果現(xiàn)在接收兩個已知主題的設備

假如說是

第一種方式



第二種方式

結果和上面一樣


?現(xiàn)在呢在界面加一個按鈕,按下發(fā)送消息"1",松開發(fā)送消息"0"

然后設置發(fā)布的主題是"/test/button"




publicclass MainActivity extends Activity {


? ? String TelephonyIMEI="";


? ? privateMqttClient client;//clientprivateMqttConnectOptions options;//配置MqttConnectThread mqttConnectThread =newMqttConnectThread();//連接服務器任務? ?

? ? Button button;//發(fā)送消息按鈕? ? @Override

? ? protectedvoid onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);


? ? ? ? button = (Button) findViewById(R.id.button1);//獲取發(fā)送消息按鈕button.setOnTouchListener(buttonTouch);//設置按鈕的觸摸事件? ? ? ?

? ? ? ? TelephonyManager mTm = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);?

? ? ? ? TelephonyIMEI = mTm.getDeviceId();

? ? ? ? //Toast.makeText(getApplicationContext(), TelephonyIMEI, 500).show();MyMqttInit();//初始化配置MQTT客戶端mqttConnectThread.start();//執(zhí)行連接服務器任務? ? }


? ? /*按鈕觸摸事件*/privateOnTouchListener buttonTouch =new OnTouchListener() {


? ? ? ? @Override

? ? ? ? publicboolean onTouch(View v, MotionEventevent)

? ? ? ? {

? ? ? ? ? ? MqttMessage msgMessage =null;//Mqtt消息變量if(event.getAction() == MotionEvent.ACTION_DOWN)//按下? ? ? ? ? ? {

? ? ? ? ? ? ? ? msgMessage =newMqttMessage("1".getBytes());

? ? ? ? ? ? }

? ? ? ? ? ? elseif(event.getAction() == MotionEvent.ACTION_UP)//松開? ? ? ? ? ? {

? ? ? ? ? ? ? ? msgMessage =newMqttMessage("0".getBytes());

? ? ? ? ? ? }


? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? client.publish("/test/button",msgMessage);//發(fā)送主題為"/test/button"的消息}catch (MqttPersistenceException e) {

? ? ? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? } catch (MqttException e) {

? ? ? ? ? ? ? ? // TODO Auto-generated catch block? ? ? ? ? ? ? ? e.printStackTrace();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception e) {

? ? ? ? ? ? ? ? //其余的狀態(tài)msgMessage = null;所以加了這個catch (Exception e)? ? ? ? ? ? }



? ? ? ? ? ? returnfalse;

? ? ? ? }

? ? };


現(xiàn)在下載到手機,

調試助手訂閱一下主題?"/test/button"


?動作一下按鈕



現(xiàn)在把發(fā)過來的數(shù)據用文本框顯示,不讓他提示了

? ? ?接收顯示的換一下



runOnUiThread(newRunnable() {//因為操作的是主界面的控件所以用刷新UI的線程,最好用handle哈,我這里怎么簡單怎么寫publicvoid run() {

? ? ? ? ? ? ? ? ? ? ? ? //Toast.makeText(getApplicationContext(),"主題:"+topic+"消息:"+msgString, 500).show();textView.setText("主題:"+topic+"\n消息:"+msgString);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? });


現(xiàn)在試一下




好了,現(xiàn)在咱開始控制咱的WIFI模塊了....用咱的手機控制WIFI板子上的繼電器,WIFI模塊呢采集溫濕度,然后顯示在手機的文本框中

自己更傾向于用lua開發(fā),所以要刷入lua的固件哈

關于刷固件可以參考

http://www.cnblogs.com/yangfengwu/p/7514336.html

自己已經下載好的固件

鏈接:https://pan.baidu.com/s/1o8pAISy?密碼:9zns


如果親們自己下載的話別忘了,把mqtt和dht選擇上哈



程序--init.lua

wifi.setmode(wifi.STATION)

RelayPin =2;--RelayPin

gpio.mode(RelayPin,gpio.OUTPUT)--RelayPin

gpio.write(RelayPin,0)--RelayPin

LedPin =4;--LedPin

gpio.mode(LedPin,gpio.OUTPUT)--LedPin

gpio.write(LedPin,0)--LedPin

DHT11pin =5--DHT11 GPIO

Temperature ="0";--Storage temperature

Humidity ="0";--Store humidity

apcfg={}

apcfg.ssid="qqqqq"apcfg.pwd="11223344"wifi.sta.config(apcfg)--wifi.sta.connect()

wifi.sta.autoconnect(1)

clientid = wifi.sta.getmac()

mqttClient=nil

mqttConnectedFlage =0;

Mymqtt = mqtt.Client(clientid,120,"user","password");--[[The connection serve]]

tmr.alarm(0,1000,1, function()

? ? Mymqtt:connect("47.93.19.134",1883,0,ConnectSuccess,ConnectFailed)

end)--[[The connection Success]]

function ConnectSuccess(client)

? ? client:subscribe("/test/button",0, subscribeSuccess)


? ? print("connected")

? ? mqttClient = client;

? ? tmr.stop(0);

? ? mqttConnectedFlage =1;

end--[[The connection fails]]

function mqttConnectFailed(client,reason)

? mqttConnectedFlage =0;

? print("failed reason: " .. reason)

? tmr.start(0);

end--[[The subscribe Success]]

function subscribeSuccess(client)

? ? print("subscribe success")

end--[[The Receive Msg]]

Mymqtt:on("message", function(client, topic, data)

? ? ifstring.find(data,"1") ~= nil then

? ? ? ? gpio.write(RelayPin,1)

? ? end

? ? ifstring.find(data,"0") ~= nil then

? ? ? ? gpio.write(RelayPin,0)

? ? end


end)--[[The Send Msg]]

tmr.alarm(1,1000,1, function()

? ? ifmqttClient ~= nil and mqttConnectedFlage ==1 then

? ? ? ? mqttClient:publish("/test/yang","Temperature="..Temperature..";".."Humidity="..Humidity,0,0,

? ? ? ? ? function(client)

? ? ? ? ? ? gpio.write(4,1-gpio.read(4))

? ? ? ? ? end)

? ? end? ? ?

end)--[[The gather humiture data]]

tmr.alarm(5,2000,1, function()--Every other 1S

? ? local status, temp, humi, temp_dec, humi_dec = dht.read11(DHT11pin)--Gathering temperature and humidity? ? ? ? ? ? ? ?

? ? ifstatus == dht.OK or status == dht.ERROR_CHECKSUM then

? ? ? ? Temperature = temp;

? ? ? ? Humidity = humi;

? ? ? ? --print("DHT Temperature:"..temp..";".."Humidity:"..humi)

? ? end

end)

printip =0wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)

? ? printip =0end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)

? ifprintip ==0 then

? ? ? ip,netmask,gateway = wifi.sta.getip()

? ? ? print(gateway)

? end

? printip =1end)


現(xiàn)在說一下個個部分的功能,對了關于語法問題和其余的問題就請大家參考我的,其實上面的代碼就是參考的官方給的API函數(shù),

我希望親們最重要的是有自學的能力,而不是需要別人灌輸東西的機器.




?好了親們可以自己去測試了

源碼鏈接

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.7a8f8764Ey3xGc&id=563674399702

wifi的就是上面的,直接復制粘貼過去就好啦

APP源碼鏈接:https://pan.baidu.com/s/1pLKGLrt

自己淘寶店的鏈接呢在最上面

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容