TA的每日心情 | 奋斗 2023-7-8 16:17 |
---|
签到天数: 971 天 连续签到: 1 天 [LV.10]以坛为家III
|
相关连接
【 钛极小龟】315MHZ RF发送模块
https://www.cirmall.com/bbs/foru ... 3&fromuid=23447
(出处: https://www.cirmall.com/bbs/)
【 钛极小龟】315MHZ RF接收模块
https://www.cirmall.com/bbs/foru ... 9&fromuid=23447
(出处: https://www.cirmall.com/bbs/)
结合前面两个驱动的移动,通过RF接收模块,学习到电动卷闸的遥控器码
//开卷闸 Send("111111111111111111000100");
//关卷闸 Send("111111111111111111000001");
//锁卷闸 Send("111111111111111111000010");
//停止 Send("111111111111111111001000");
然后通过RF发送对应的码值 ,先通过触摸按键 实验发码,最后通过MQTT进行远程控制发码
- package tikit.t600.esp8266b;
- import java.io.IOException;
- import tijos.framework.devicecenter.TiGPIO;
- import tijos.framework.networkcenter.dns.TiDNS;
- import tijos.framework.networkcenter.mqtt.MqttClient;
- import tijos.framework.networkcenter.mqtt.MqttClientListener;
- import tijos.framework.networkcenter.mqtt.MqttConnectOptions;
- import tijos.framework.platform.peripheral.ITiKeyboardListener;
- import tijos.framework.platform.peripheral.TiKeyboard;
- import tijos.framework.platform.wlan.TiWiFi;
- import tijos.framework.sensor.button.ITiButtonEventListener;
- import tijos.framework.sensor.button.TiButton;
- import tijos.framework.util.Delay;
- import tijos.framework.util.json.JSONObject;
- public class RCSwitchSample {
-
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- final Protocol protoclPT2262 = new Protocol((short)305, (byte)1, (byte)31, (byte)1, (byte)3, (byte)3, (byte)1, false);
- try {
-
- try {
- // 启动WLAN及DNS
- TiWiFi.getInstance().startup(10);
- TiDNS.getInstance().startup();
-
- //配置成功
- System.out.println("RollerGate wifi OK");
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
-
- /*
- * 定义使用的TiGPIO port
- */
- int gpioPort0 = 0;
- /*
- * 定义使用的TiGPIO Pin
- */
- int gpioPinRCSwitchSend = 2;
- int gpioPinRCSwitchRecv = 5;
- int gpioPinButtonTouch = 4;
- int gpioPinKeyLc = 6;
- /*
- * 资源分配, 将gpioPort与gpioPin0分配给TiGPIO对象gpio2
- */
- TiGPIO gpio0 = TiGPIO.open(gpioPort0,gpioPinRCSwitchSend,gpioPinButtonTouch,gpioPinRCSwitchRecv,gpioPinKeyLc);
-
- //资源申请
- RCSwitchSend rcSwitchSend= new RCSwitchSend(gpio0, gpioPinRCSwitchSend,protoclPT2262,4);
- RCSwitchRecv rcSwitchRecv = new RCSwitchRecv(gpio0, gpioPinRCSwitchRecv, protoclPT2262);
- RCSwitchRecvRollerGateListener rcSwitchRecvLc = new RCSwitchRecvRollerGateListener();
- rcSwitchRecv.setEventListener(rcSwitchRecvLc);
- //触摸按键资源申请 与 绑定
- TiButton buttonTouch = new TiButton(gpio0, gpioPinButtonTouch,true);//触发电平:高电平
- RCSwitchButtonEventListener lc = new RCSwitchButtonEventListener(rcSwitchSend);
- buttonTouch.setEventListener(lc);
- //键盘资源申请 与 绑定
- RCSwitchKeyEventListener keyLc = new RCSwitchKeyEventListener(rcSwitchSend);
- TiKeyboard keyBoard = TiKeyboard.getInstance();
- keyBoard.setEventListener(keyLc);// 设置键盘事件监听对象
-
- // MQTT Server 地址,用户名, 密码
- final String broker = "tcp://162.168.1.103:1883";
- final String username = "demo";
- final String password = "tijos";
-
- // ClientID
- final String clientId = "mqtt_test_java_tijos";
-
- // MQTT连接设置
- MqttConnectOptions connectOptions = new MqttConnectOptions();
- connectOptions.setUserName(username);
- connectOptions.setPassword(password);
- // 允许自动重新连接
- connectOptions.setAutomaticReconnect(true);
-
- MqttClient mqttClient = new MqttClient(broker, clientId);
-
- int qos = 0;
-
- try {
- RCSwitchMqttEventLister mqttClientLc = new RCSwitchMqttEventLister();
- //订阅监听
- mqttClient.SetMqttClientListener(mqttClientLc);
- // 连接MQTT服务器
- mqttClient.connect(connectOptions,rcSwitchSend);
- // 主题topic
- String subtopic = "cmd";
- String pubtopic = "data";
-
- // 订阅topic
- mqttClient.subscribe(subtopic, qos);
-
- while (true) {
- switch (mqttClientLc.recvMqtt) {
- case 1:
- //发布
- mqttClient.publish(pubtopic, "{switch:1}".getBytes(), qos, false);
- break;
- case 2:
- //发布
- mqttClient.publish(pubtopic, "{switch:2}".getBytes(), qos, false);
- break;
- case 3:
- //发布
- mqttClient.publish(pubtopic, "{switch:3}".getBytes(), qos, false);
- break;
- case 4:
- //发布
- mqttClient.publish(pubtopic, "{switch:4}".getBytes(), qos, false);
- break;
- default:
- break;
- }
-
- Thread.sleep(2000);
- }
-
-
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }finally {
- try {
- //关闭MQTT
- mqttClient.close();
- } catch (Exception e2) {
- // TODO: handle exception
- e2.printStackTrace();
- }
- }
-
-
-
-
- //while (true) {
- // Delay.msDelay(1000);
- //}
-
- } catch (IOException ie) {
- // TODO: handle exception
- ie.printStackTrace();
- }
- }
- }
- /**
- * MQTT 事件监听
- *
- */
- class RCSwitchMqttEventLister implements MqttClientListener{
- int recvMqtt = 0;
-
- public int getRecvMqtt() {
- return recvMqtt;
- }
- public void setRecvMqtt(int recvMqtt) {
- this.recvMqtt = recvMqtt;
- }
- @Override
- public void connectComplete(Object userContext, boolean reconnect) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void connectionLost(Object userContext) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void messageArrived(Object userContext, String topic, byte[] payload) {
- // TODO Auto-generated method stub
- System.out.println("MQTT RECV:"+new String(payload));
- try {
- RCSwitchSend _rcSwitchSend = (RCSwitchSend)userContext;
- //JSON格式获取开关值
- JSONObject newObj = new JSONObject(new String(payload));
- recvMqtt = newObj.getInt("switch");
- //如果订阅
- switch (recvMqtt) {
- case 1:
- //开卷闸
- _rcSwitchSend.Send("111111111111111111000100");
- break;
- case 2:
- //关卷闸
- _rcSwitchSend.Send("111111111111111111000001");
- break;
- case 4:
- //锁卷闸
- _rcSwitchSend.Send("111111111111111111000010");
- break;
- case 3:
- default:
- //停止
- _rcSwitchSend.Send("111111111111111111001000");
- break;
- }
- } catch (IOException e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- @Override
- public void onMqttConnectFailure(Object userContext, int cause) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void onMqttConnectSuccess(Object userContext) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void publishCompleted(Object userContext, int msgId, String topic, int result) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void subscribeCompleted(Object userContext, int msgId, String topic, int result) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void unsubscribeCompleted(Object userContext, int msgId, String topic, int result) {
- // TODO Auto-generated method stub
-
- }
-
-
-
- }
- class RCSwitchRecvRollerGateListener implements RCSwitchRecvEventListener{
- @Override
- public void cmdReceived(RCSwitchRecv rcSwithcRecv) {
- // TODO Auto-generated method stub
- System.out.println("Received:" + rcSwithcRecv.getReceivedValue());
- }
-
-
- }
- /**
- * 1.此类实现了ITiButtonEventListener四按键事件监听接口
- * 在onPressed与onReleased方法中不要阻塞处理事件太久,
- * 因为TiJOS系统所有事件分发在同一个事件监听线程中,若阻塞
- * 太久会影响其他事件的分发,导致事件响应不及时甚至丢失。
- *
- * 2.如果实际应用中,需要较长时间来处理某事件,建议在新的线程中
- * 处理。
- */
- class RCSwitchButtonEventListener implements ITiButtonEventListener{
- RCSwitchSend rcSwitchSend;
-
- public RCSwitchButtonEventListener(RCSwitchSend rcSwitchSend) {
- this.rcSwitchSend = rcSwitchSend;
- }
-
- @Override
- /**
- * 按键按下事件处理
- */
- public void onPressed(TiButton button) {
- // TODO Auto-generated method stub
- System.out.println("onPressed" + " time(us):" + button.getEventTime() + " buttonPinID:" + button.getSignalPinID());
- try {
- rcSwitchSend.Send("111111111111111111000100");//开卷闸
- System.out.println("set");
- } catch (IOException ie) {
- // TODO: handle exception
- ie.printStackTrace();
- }
- }
- @Override
- /**
- * 按键释放事件处理
- */
- public void onReleased(TiButton button) {
- // TODO Auto-generated method stub
- System.out.println("onReleased" + " time(us):" + button.getEventTime() + " buttonPinID:" + button.getSignalPinID());
- }
- }
- class RCSwitchKeyEventListener implements ITiKeyboardListener{
- RCSwitchSend rcSwitchSend;
-
- public RCSwitchKeyEventListener(RCSwitchSend rcSwitchSend) {
- this.rcSwitchSend = rcSwitchSend;
- }
-
- @Override
- public void onPressed(int id, long time) {
- // TODO Auto-generated method stub
- System.out.println("onPressed" + " time(us):" + time + " buttonPinID:" + id);
- try {
- rcSwitchSend.Send("111111111111111111000001");//关卷闸
- System.out.println("cancel");
- } catch (IOException ie) {
- // TODO: handle exception
- ie.printStackTrace();
- }
- }
- @Override
- public void onReleased(int id, long time) {
- // TODO Auto-generated method stub
- System.out.println("onReleased" + " time(us):" + time + " buttonPinID:" + id);
- }
-
- }
复制代码 MQTT服务器反馈的数据
进行电脑PC远程控制开关卷闸。
相关代码如下
RCSwitch.rar
(5.3 KB, 下载次数: 0, 售价: 1 与非币)
|
|