查看: 2168|回复: 2

[原创] 【 钛极小龟】远程控制电动卷闸

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2019-1-9 14:49:46 | 显示全部楼层 |阅读模式
    分享到:

    相关连接
    【 钛极小龟】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进行远程控制发码
    1. package tikit.t600.esp8266b;


    2. import java.io.IOException;

    3. import tijos.framework.devicecenter.TiGPIO;
    4. import tijos.framework.networkcenter.dns.TiDNS;
    5. import tijos.framework.networkcenter.mqtt.MqttClient;
    6. import tijos.framework.networkcenter.mqtt.MqttClientListener;
    7. import tijos.framework.networkcenter.mqtt.MqttConnectOptions;
    8. import tijos.framework.platform.peripheral.ITiKeyboardListener;
    9. import tijos.framework.platform.peripheral.TiKeyboard;
    10. import tijos.framework.platform.wlan.TiWiFi;
    11. import tijos.framework.sensor.button.ITiButtonEventListener;
    12. import tijos.framework.sensor.button.TiButton;
    13. import tijos.framework.util.Delay;
    14. import tijos.framework.util.json.JSONObject;

    15. public class RCSwitchSample {
    16.        
    17.        
    18.         public static void main(String[] args) {
    19.                 // TODO Auto-generated method stub
    20.                 final Protocol protoclPT2262 = new Protocol((short)305, (byte)1, (byte)31, (byte)1, (byte)3, (byte)3, (byte)1, false);
    21.                 try {
    22.                        
    23.                         try {
    24.                                 // 启动WLAN及DNS
    25.                                 TiWiFi.getInstance().startup(10);
    26.                                 TiDNS.getInstance().startup();
    27.                                
    28.                                 //配置成功
    29.                                 System.out.println("RollerGate wifi OK");
    30.                         } catch (IOException e) {
    31.                                 // TODO: handle exception
    32.                                 e.printStackTrace();
    33.                         }
    34.                        
    35.                         /*
    36.                          * 定义使用的TiGPIO port
    37.                          */
    38.                         int gpioPort0 = 0;
    39.                         /*
    40.                          * 定义使用的TiGPIO Pin
    41.                          */
    42.                         int gpioPinRCSwitchSend = 2;
    43.                         int gpioPinRCSwitchRecv = 5;
    44.                         int gpioPinButtonTouch = 4;
    45.                         int gpioPinKeyLc = 6;
    46.                         /*
    47.                          * 资源分配, 将gpioPort与gpioPin0分配给TiGPIO对象gpio2
    48.                          */
    49.                         TiGPIO gpio0 = TiGPIO.open(gpioPort0,gpioPinRCSwitchSend,gpioPinButtonTouch,gpioPinRCSwitchRecv,gpioPinKeyLc);
    50.                        
    51.                         //资源申请
    52.                         RCSwitchSend rcSwitchSend= new RCSwitchSend(gpio0, gpioPinRCSwitchSend,protoclPT2262,4);
    53.                         RCSwitchRecv rcSwitchRecv = new RCSwitchRecv(gpio0, gpioPinRCSwitchRecv, protoclPT2262);
    54.                         RCSwitchRecvRollerGateListener rcSwitchRecvLc = new RCSwitchRecvRollerGateListener();
    55.                         rcSwitchRecv.setEventListener(rcSwitchRecvLc);
    56.                         //触摸按键资源申请 与 绑定
    57.                         TiButton buttonTouch = new TiButton(gpio0, gpioPinButtonTouch,true);//触发电平:高电平
    58.                         RCSwitchButtonEventListener lc = new RCSwitchButtonEventListener(rcSwitchSend);
    59.                         buttonTouch.setEventListener(lc);
    60.                         //键盘资源申请 与 绑定
    61.                         RCSwitchKeyEventListener keyLc = new RCSwitchKeyEventListener(rcSwitchSend);
    62.                         TiKeyboard keyBoard = TiKeyboard.getInstance();
    63.                         keyBoard.setEventListener(keyLc);// 设置键盘事件监听对象
    64.                        
    65.                         // MQTT Server 地址,用户名, 密码
    66.                         final String broker = "tcp://162.168.1.103:1883";
    67.                         final String username = "demo";
    68.                         final String password = "tijos";
    69.                        
    70.                         // ClientID
    71.                         final String clientId = "mqtt_test_java_tijos";
    72.                        
    73.                         // MQTT连接设置
    74.                         MqttConnectOptions connectOptions = new MqttConnectOptions();
    75.                         connectOptions.setUserName(username);
    76.                         connectOptions.setPassword(password);
    77.                         // 允许自动重新连接
    78.                         connectOptions.setAutomaticReconnect(true);
    79.                        
    80.                         MqttClient mqttClient = new MqttClient(broker, clientId);
    81.                        
    82.                         int qos = 0;
    83.                        
    84.                         try {
    85.                                 RCSwitchMqttEventLister mqttClientLc = new RCSwitchMqttEventLister();
    86.                                 //订阅监听
    87.                                 mqttClient.SetMqttClientListener(mqttClientLc);
    88.                                 // 连接MQTT服务器
    89.                                 mqttClient.connect(connectOptions,rcSwitchSend);
    90.                                 // 主题topic
    91.                                 String subtopic = "cmd";
    92.                                 String pubtopic = "data";
    93.                                
    94.                                 // 订阅topic
    95.                                 mqttClient.subscribe(subtopic, qos);
    96.                                
    97.                                 while (true) {
    98.                                         switch (mqttClientLc.recvMqtt) {
    99.                                         case 1:
    100.                                                 //发布
    101.                                                 mqttClient.publish(pubtopic, "{switch:1}".getBytes(), qos, false);
    102.                                                 break;
    103.                                         case 2:
    104.                                                 //发布
    105.                                                 mqttClient.publish(pubtopic, "{switch:2}".getBytes(), qos, false);
    106.                                                 break;
    107.                                         case 3:
    108.                                                 //发布
    109.                                                 mqttClient.publish(pubtopic, "{switch:3}".getBytes(), qos, false);
    110.                                                 break;
    111.                                         case 4:
    112.                                                 //发布
    113.                                                 mqttClient.publish(pubtopic, "{switch:4}".getBytes(), qos, false);
    114.                                                 break;
    115.                                         default:
    116.                                                 break;
    117.                                         }
    118.                                        
    119.                                         Thread.sleep(2000);
    120.                                 }
    121.                                
    122.                                
    123.                         } catch (Exception e) {
    124.                                 // TODO: handle exception
    125.                                 e.printStackTrace();
    126.                         }finally {
    127.                                 try {
    128.                                         //关闭MQTT
    129.                                         mqttClient.close();
    130.                                 } catch (Exception e2) {
    131.                                         // TODO: handle exception
    132.                                         e2.printStackTrace();
    133.                                 }
    134.                         }
    135.                        
    136.                        
    137.                        
    138.                        
    139.                         //while (true) {
    140.                         //        Delay.msDelay(1000);
    141.                         //}
    142.                        
    143.                 } catch (IOException ie) {
    144.                         // TODO: handle exception
    145.                         ie.printStackTrace();
    146.                 }
    147.         }

    148. }



    149. /**
    150. * MQTT 事件监听
    151. *
    152. */
    153. class RCSwitchMqttEventLister implements MqttClientListener{

    154.         int recvMqtt = 0;
    155.        
    156.         public int getRecvMqtt() {
    157.                 return recvMqtt;
    158.         }

    159.         public void setRecvMqtt(int recvMqtt) {
    160.                 this.recvMqtt = recvMqtt;
    161.         }

    162.         @Override
    163.         public void connectComplete(Object userContext, boolean reconnect) {
    164.                 // TODO Auto-generated method stub
    165.                
    166.         }

    167.         @Override
    168.         public void connectionLost(Object userContext) {
    169.                 // TODO Auto-generated method stub
    170.                
    171.         }

    172.         @Override
    173.         public void messageArrived(Object userContext, String topic, byte[] payload) {
    174.                 // TODO Auto-generated method stub
    175.                 System.out.println("MQTT RECV:"+new String(payload));
    176.                 try {
    177.                         RCSwitchSend _rcSwitchSend = (RCSwitchSend)userContext;
    178.                         //JSON格式获取开关值
    179.                         JSONObject newObj = new JSONObject(new String(payload));
    180.                         recvMqtt = newObj.getInt("switch");
    181.                         //如果订阅
    182.                         switch (recvMqtt) {
    183.                         case 1:
    184.                                 //开卷闸
    185.                                 _rcSwitchSend.Send("111111111111111111000100");
    186.                                 break;
    187.                         case 2:
    188.                                 //关卷闸
    189.                                 _rcSwitchSend.Send("111111111111111111000001");
    190.                                 break;       
    191.                         case 4:
    192.                                 //锁卷闸
    193.                                 _rcSwitchSend.Send("111111111111111111000010");
    194.                                 break;
    195.                         case 3:
    196.                         default:
    197.                                 //停止
    198.                                 _rcSwitchSend.Send("111111111111111111001000");
    199.                                 break;
    200.                         }
    201.                 } catch (IOException e) {
    202.                         // TODO: handle exception
    203.                         e.printStackTrace();
    204.                 }
    205.         }

    206.         @Override
    207.         public void onMqttConnectFailure(Object userContext, int cause) {
    208.                 // TODO Auto-generated method stub
    209.                
    210.         }

    211.         @Override
    212.         public void onMqttConnectSuccess(Object userContext) {
    213.                 // TODO Auto-generated method stub
    214.                
    215.         }

    216.         @Override
    217.         public void publishCompleted(Object userContext, int msgId, String topic, int result) {
    218.                 // TODO Auto-generated method stub
    219.                
    220.         }

    221.         @Override
    222.         public void subscribeCompleted(Object userContext, int msgId, String topic, int result) {
    223.                 // TODO Auto-generated method stub
    224.                
    225.         }

    226.         @Override
    227.         public void unsubscribeCompleted(Object userContext, int msgId, String topic, int result) {
    228.                 // TODO Auto-generated method stub
    229.                
    230.         }
    231.        
    232.        
    233.        
    234. }







    235. class RCSwitchRecvRollerGateListener implements  RCSwitchRecvEventListener{

    236.         @Override
    237.         public void cmdReceived(RCSwitchRecv rcSwithcRecv) {
    238.                 // TODO Auto-generated method stub
    239.                 System.out.println("Received:" + rcSwithcRecv.getReceivedValue());
    240.         }
    241.        
    242.        
    243. }

    244. /**
    245. * 1.此类实现了ITiButtonEventListener四按键事件监听接口
    246. * 在onPressed与onReleased方法中不要阻塞处理事件太久,
    247. * 因为TiJOS系统所有事件分发在同一个事件监听线程中,若阻塞
    248. * 太久会影响其他事件的分发,导致事件响应不及时甚至丢失。
    249. *
    250. * 2.如果实际应用中,需要较长时间来处理某事件,建议在新的线程中
    251. * 处理。
    252. */
    253. class RCSwitchButtonEventListener implements ITiButtonEventListener{

    254.         RCSwitchSend rcSwitchSend;
    255.        
    256.         public RCSwitchButtonEventListener(RCSwitchSend rcSwitchSend) {
    257.                 this.rcSwitchSend = rcSwitchSend;
    258.         }
    259.        
    260.         @Override
    261.         /**
    262.          * 按键按下事件处理
    263.          */
    264.         public void onPressed(TiButton button) {
    265.                 // TODO Auto-generated method stub
    266.                 System.out.println("onPressed" + "  time(us):" + button.getEventTime() + "  buttonPinID:" + button.getSignalPinID());
    267.                 try {
    268.                         rcSwitchSend.Send("111111111111111111000100");//开卷闸
    269.                         System.out.println("set");
    270.                 } catch (IOException ie) {
    271.                         // TODO: handle exception
    272.                         ie.printStackTrace();
    273.                 }
    274.         }

    275.         @Override
    276.         /**
    277.          * 按键释放事件处理
    278.          */
    279.         public void onReleased(TiButton button) {
    280.                 // TODO Auto-generated method stub
    281.                 System.out.println("onReleased" + "  time(us):" + button.getEventTime() + "  buttonPinID:" + button.getSignalPinID());
    282.         }
    283. }

    284. class RCSwitchKeyEventListener implements ITiKeyboardListener{

    285.         RCSwitchSend rcSwitchSend;
    286.        
    287.         public RCSwitchKeyEventListener(RCSwitchSend rcSwitchSend) {
    288.                 this.rcSwitchSend = rcSwitchSend;
    289.         }
    290.        
    291.         @Override
    292.         public void onPressed(int id, long time) {
    293.                 // TODO Auto-generated method stub
    294.                 System.out.println("onPressed" + "  time(us):" + time + "  buttonPinID:" + id);
    295.                 try {
    296.                         rcSwitchSend.Send("111111111111111111000001");//关卷闸
    297.                         System.out.println("cancel");
    298.                 } catch (IOException ie) {
    299.                         // TODO: handle exception
    300.                         ie.printStackTrace();
    301.                 }
    302.         }

    303.         @Override
    304.         public void onReleased(int id, long time) {
    305.                 // TODO Auto-generated method stub
    306.                 System.out.println("onReleased" + "  time(us):" + time + "  buttonPinID:" + id);
    307.         }
    308.        
    309. }
    复制代码
    MQTT服务器反馈的数据
    2.png
    进行电脑PC远程控制开关卷闸。

    相关代码如下
    RCSwitch.rar (5.3 KB, 下载次数: 0, 售价: 1 与非币)
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    3 天前
  • 签到天数: 43 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2019-1-9 15:30:05 | 显示全部楼层
    还有APP,厉害了。。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 10:09 , Processed in 0.154557 second(s), 20 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.