原文鏈接
前幾日和朋友說起小米的智能電飯煲宏胯,能在回家前把粥煮好,簡單的理解就是定時接通電源或遠程接通電源開始煮粥本姥,于是本人就想的寫一個遠程控制電源的臺燈肩袍,來一個初步實現(xiàn)。先來上一個圖婚惫。
分為軟件和硬件兩個部分來記錄氛赐。
軟件
控制端和被控制端都是用的Android來開發(fā)的魂爪。兩個終端使用MQTT協(xié)議進行關聯(lián)的。因為關于MQTT的內(nèi)容太多鹰祸,這個就不贅述了甫窟。分享一個不錯的鏈接MQTT
手機控制端
MainActivity.class
用一個Button發(fā)送指令,EventBus通知MQTTService發(fā)出開關的指令
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("info", "content " + editText.getText().toString().trim());
if (key) { EventBus.getDefault().post(OFF);
key = false;
} else { EventBus.getDefault().post(ON);
key = true;
}
}
});
MQTTService.class
public class MQTTService extends Service {
private ServerMQTT serverMQTT;
@Override
public void onCreate() {
super.onCreate();
EventBus.getDefault().register(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
initMqtt();
if (serverMQTT != null) {
serverMQTT.connect();
}
} catch (MqttException e) {
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void getData(Integer s) {
try {
serverMQTT.mqttMessage.setPayload(String.valueOf(s).getBytes());
serverMQTT.publish(serverMQTT.topic, serverMQTT.mqttMessage);
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
private void initMqtt() throws MqttException {
serverMQTT = new ServerMQTT();
serverMQTT.mqttMessage = new MqttMessage();
//保證消息能到達一次 QoS 0(At most once)“至多一次” QoS 1(At least once)“至少一次” QoS 2(Exactly once)“只有一次”
serverMQTT.mqttMessage.setQos(1);
serverMQTT.mqttMessage.setRetained(false);
serverMQTT.mqttMessage.setPayload("hello,topic11".getBytes());
serverMQTT.publish(serverMQTT.topic, serverMQTT.mqttMessage);
Log.e("info", serverMQTT.mqttMessage.isRetained() + "------ratained狀態(tài)");
}
}
ServerMQTT.class
public class ServerMQTT {
//MQTT服務端的地址蛙婴,使用tcp的協(xié)議
protected static final String HOST = "";
//定義一個主題粗井,關于主題請看上文推薦的鏈接
protected static final String TOPIC = "";
//定義MQTT的ID,這個將在MQTT的服務器上顯示
protected static final String CLIENTID = "";
private MqttClient mqttClient;
protected MqttTopic topic;
protected String userName = "";
protected String password = "";
protected MqttMessage mqttMessage;
public ServerMQTT() throws MqttException {
// MemoryPersistence設置clientid的保存形式街图,默認為以內(nèi)存保存
mqttClient = new MqttClient(HOST, CLIENTID, new MemoryPersistence());
connect();
}
/**
* 連接MQTT服務器
*/
public void connect() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName(userName);
options.setPassword(password.toCharArray());
//設置超時時間
options.setConnectionTimeout(10);
//設置會話心跳時間
options.setKeepAliveInterval(20);
try {
mqttClient.setCallback(new PushCallback());
mqttClient.connect(options);
topic = mqttClient.getTopic(TOPIC);
} catch (MqttException e) {
e.printStackTrace();
}
}
public void publish(MqttTopic topic, MqttMessage message) throws MqttException {
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
Log.e("message is published completely! " + token.isComplete());
}
}
樹莓派端
MainActivity.class
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
TextView tvMsg;
// private Gpio gpioButton;
private Gpio gpioLED;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
setContentView(R.layout.activity_main);
tvMsg = findViewById(R.id.tv_msg);
//提前啟動Service浇衬,因為下一個方法要使用EventBus給ClientMQTTService發(fā)送信息
startService(new Intent(this, ClientMQTTService.class));
ledController();
}
private void ledController() {
PeripheralManager manager = PeripheralManager.getInstance();
try {
gpioLED = manager.openGpio("BCM4");
gpioLED.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
//將LED的狀態(tài)(亮,滅)發(fā)送給MQTT服務器
ClientMQTTServiceEventBean eventBean = new ClientMQTTServiceEventBean();
eventBean.setWhat(0x100);
Bundle data = new Bundle();
data.putBoolean("isLight", gpioLED.getValue());
eventBean.setData(data);
EventBus.getDefault().post(eventBean);
} catch (IOException e) {
e.printStackTrace();
}
}
private void setLedValue(boolean value) {
try {
gpioLED.setValue(value);
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
}
private boolean getLedValue() {
try {
return gpioLED.getValue();
} catch (IOException e) {
Log.e(TAG, "Error updating GPIO value", e);
}
return false;
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void getData(String s) {
if (!TextUtils.isEmpty(s) && s.length() == 1) {
int i = Integer.valueOf(s);
if (i == 0) {
setLedValue(false);
} else if (i == 1) {
setLedValue(true);
}
}
}
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
//銷毀LED的引用
if (gpioLED != null) {
try {
gpioLED.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
gpioLED = null;
}
}
super.onDestroy();
}
ClientMQTTService.class
public class ClientMQTTService extends Service {
public static final String HOST = "";
public static final String TOPIC = "";
public static final String TOPIC1 ="";
private static final String clientid = "";
private MqttClient client;
private MqttConnectOptions options;
private String userName = "";
private String passWord = "";
@Override
public void onCreate() {
super.onCreate();
EventBus.getDefault().register(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void start() {
try {
// host為主機名餐济,clientid即連接MQTT的客戶端ID耘擂,一般以唯一標識符表示,MemoryPersistence設置clientid的保存形式絮姆,默認為以內(nèi)存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
// MQTT的連接設置
options = new MqttConnectOptions();
// 設置是否清空session,這里如果設置為false表示服務器會保留客戶端的連接記錄醉冤,這里設置為true表示每次連接到服務器都以新的身份連接
options.setCleanSession(true);
// 設置連接的用戶名
options.setUserName(userName);
// 設置連接的密碼
options.setPassword(passWord.toCharArray());
// 設置超時時間 單位為秒
options.setConnectionTimeout(10);
// 設置會話心跳時間 單位為秒 服務器會每隔1.5*20秒的時間向客戶端發(fā)送個消息判斷客戶端是否在線,但這個方法并沒有重連的機制
options.setKeepAliveInterval(20);
// 設置回調(diào)
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
EventBus.getDefault().post(new String(mqttMessage.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
MqttTopic topic = client.getTopic(TOPIC);
//setWill方法篙悯,如果項目中需要知道客戶端是否掉線可以調(diào)用該方法蚁阳。設置最終端口的通知消息
options.setWill(topic, "close".getBytes(), 2, true);
client.connect(options);
//訂閱消息 qoS 0(At most once)“至多一次” QqoS 1(At least once)“至少一次” QqoS 2(Exactly once)“只有一次”
int[] Qos = {1};
String[] topic1 = {TOPIC, TOPIC1};
client.subscribe(topic1, Qos);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
if (client != null) {
if (client.isConnected()) {
try {
client.disconnect();
} catch (MqttException e) {
e.printStackTrace();
} finally {
client = null;
options = null;
}
}
}
super.onDestroy();
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void getData(ClientMQTTServiceEventBean bean) {
switch (bean.getWhat()) {
case 0x100:
//獲取燈目前的狀態(tài)
boolean isLight = bean.getData().getBoolean("isLight");
MqttMessage mqttMsg = new MqttMessage();
try {
if (isLight) {
mqttMsg.setPayload(String.valueOf(1).getBytes());
client.publish(TOPIC, mqttMsg);
} else {
mqttMsg.setPayload(String.valueOf(0).getBytes());
client.publish(TOPIC, mqttMsg);
}
} catch (MqttException e) {
e.printStackTrace();
}
break;
}
}
}
硬件
使用的是樹莓派3,刷的是Android Things鸽照。關于Raspberry怎么刷Android Things網(wǎng)上有很多教程螺捐,就略過了。硬件嘛矮燎,主要是繼電器的接線定血。整理自己在接線中遇到的問題。按照輸入到輸出的順序記錄诞外。
1. 繼電器澜沟。
1.1 輸入電源
不要使用樹莓派提供的電壓。如果連接Raspberry的電源只有一個還可以峡谊,如果太多倔喂,負載太大,容易把Raspberry燒了靖苇,畢竟一個Raspberry也有500多,能省則省班缰。
我買的繼電器要求輸入電壓是5V的贤壁,我找了半天,發(fā)現(xiàn)手機的充電器提供的就是5V的電壓埠忘,于是找了一個USB的數(shù)據(jù)線脾拆,將連接手機端的接口拆開馒索,發(fā)現(xiàn)有4條線(紅白綠黑)。紅色和黑色是電線名船,白綠是數(shù)據(jù)線绰上。先百度然后用萬能表測一下就知道了。紅色是正極渠驼,黑色是負極蜈块。紅色接DC+,黑色接DC-迷扇。IN的接觸點連接Raspberry百揭。當IN接入高電平的時候,會聽見繼電器有“咔”的一聲蜓席,同時繼電器模塊上的小燈也會亮起器一。這就是初中物理的電磁鐵吸合的聲音。如果你和我一樣厨内,以為這樣就可以咔的一聲的話祈秕,就錯了!3浮请毛!還需要將 DC- 與Raspberry的GND連接起來。
1.2 輸出電源
好了丑掺,繼電器的輸入完了获印,現(xiàn)在開始說繼電器的輸出。
Emmmmmmm街州,不知道怎么說兼丰,來個很傻很直白的圖。然后插座常開接一個臺燈就OK了唆缴。