基于MQTT协议实现微信小程序控制树莓派

基于MQTT协议实现微信小程序控制树莓派

在我的github上有源码,大家可以直接下载来用
https://github.com/yjc-123/-MQTT- ,这里给大家说一下实现的过程。
小程序端:
需要将我的github文件引入这三个文件
在这里插入图片描述
这是github上的源码,具体网站https://github.com/mqttjs/MQTT.js,我们需要讲上述文件引入才可已使用mqtt。

  • 源码
    你只需要修改opt的信息就行了。test是你在EMQ创建的用户和密码。另外就是你服务器的域名,这里需要ssl,所以你在这个过程中需要讲域名安装证书。
import mqtt from '../../utils/mqtt.js';
//连接的服务器域名,注意格式!!!
const host = 'wxs://www.iot-yjc.cn/mqtt';
Page({

  data: {
    client: null,
    //记录重连的次数
    reconnectCounts: 0,
    //MQTT连接的配置
    options: {
      protocolVersion: 4, //MQTT连接协议版本c
      clientId: 'wx_' + parseInt(Math.random() * 100 + 800, 10),
      clean: false,
      password: 'test',
      username: 'test',
      reconnectPeriod: 1000, //1000毫秒,两次重新连接之间的间隔
      connectTimeout: 30 * 1000, //1000毫秒,两次重新连接之间的间隔
      resubscribe: true //如果连接断开并重新连接,则会再次自动订阅已订阅的主题(默认true)
    }
  },
  onClick_connect: function() {

    var that = this;
    //开始连接
    this.data.client = mqtt.connect(host, this.data.options);
    this.data.client.on('connect', function(connack) {
      wx.showToast({
        title: '连接成功'
      })
    })


    //服务器下发消息的回调
    that.data.client.on("message", function(topic, payload) {
      console.log(" 收到 topic:" + topic + " , payload :" + payload)
      wx.showModal({
        content: " 收到topic:[" + topic + "], payload :[" + payload + "]",
        showCancel: false,
      });
    })


    //服务器连接异常的回调
    that.data.client.on("error", function(error) {
      console.log(" 服务器 error 的回调" + error)

    })

    //服务器重连连接异常的回调
    that.data.client.on("reconnect", function() {
      console.log(" 服务器 reconnect的回调")

    })


    //服务器连接异常的回调
    that.data.client.on("offline", function(errr) {
      console.log(" 服务器offline的回调")

    })


  },
  onClick_SubOne: function() {
    if (this.data.client && this.data.client.connected) {
      //仅订阅单个主题
      this.data.client.subscribe('Topic0', function(err, granted) {
        if (!err) {
          wx.showToast({
            title: '订阅主题成功'
          })
        } else {
          wx.showToast({
            title: '订阅主题失败',
            icon: 'fail',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },
  onClick_SubMany: function() {

    if (this.data.client && this.data.client.connected) {
      //仅订阅多个主题
      this.data.client.subscribe({
        'Topic1': {
          qos: 0
        },
        'Topic2': {
          qos: 1
        }
      }, function(err, granted) {
        if (!err) {
          wx.showToast({
            title: '订阅多主题成功'
          })
        } else {
          wx.showToast({
            title: '订阅多主题失败',
            icon: 'fail',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },

  //发布消息
  listenerSwitch: function(e) {
    if (this.data.client && this.data.client.connected){
      if( e.detail.value == true){
        this.data.client.publish('test', '打开');
        wx.showToast({
          title: '发布成功'
        })
      }
      else if(e.detail.value == false){
        this.data.client.publish('test', '关闭');
        wx.showToast({
          title: '发布成功'
        })
      }
      else{}
    }
    console.log('switch类型开关当前状态-----', e.detail.value);},
  onClick_PubMsg: function() {
    if (this.data.client && this.data.client.connected) {
      this.data.client.publish('test', '打开');
      wx.showToast({
        title: '发布成功'
      })
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },



  onClick_unSubOne: function() {
    if (this.data.client && this.data.client.connected) {
      this.data.client.unsubscribe('Topic1');
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },
  onClick_unSubMany: function() {
    if (this.data.client && this.data.client.connected) {
      this.data.client.unsubscribe(['Topic1', 'Topic2']);
    } else {
      wx.showToast({
        title: '请先连接服务器',
        icon: 'none',
        duration: 2000
      })
    }
  },
  onLoad: function() {
    wx.setNavigationBarTitle({
      title: '简单服务器Mqtt连接'
    })
  }
})
  • 域名安装证书
    1、首先在阿里云下载ssl证书,然后再服务器下载宝塔面板
    2、在宝塔中下载nignx,然后添加站点
    在这里插入图片描述
    3、点击设置–>ssl,输入密钥和证书,点击保存。
    在这里插入图片描述
    4、添加nignx反向代理
    在这里插入图片描述
    在这里插入图片描述
location = /mqtt {
        # 8083就是我们的emq的websocket的端口号
        proxy_pass http://www.xxxxxxx.com:8083;
        proxy_redirect off;
        proxy_set_header Host www.xxxxxxx.com:8083;

        proxy_set_header Sec-WebSocket-Protocol mqtt;
        
        # 这个是与你的 js客户端的库有关系,本博文的不需要,为了兼顾以后小伙伴,我这里注释了下! 
        #more_clear_headers Sec-WebSocket-Protocol;

        # 这些都是 websocket必须要配置的
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        }
}

树莓派:
这边只需要实现订阅跟发布就行了。

  • 订阅
#coding=utf-8
import paho.mqtt.client as mqtt
import time
import RPi.GPIO as GPIO
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
HOST = "59.110.42.24"
PORT = 1883
GPIO_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIO_PIN, GPIO.OUT)
def client_loop():
    client_id = "0bd981c5-a055-4196-8b7f-efb9f7a4d6ac"
    client = mqtt.Client(client_id)    # ClientId不能重复,所以使用当前时间
    client.username_pw_set("test", "test")  # 必须设置,否则会返回「Connected with result code 4」
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(HOST, PORT, 60)
    client.loop_forever()

def on_connect(client, userdata, flags, rc):
    client.subscribe("test")

def trun(a):
    
    if(a == "打开"):
        GPIO.output(GPIO_PIN,GPIO.HIGH)
    else:
        GPIO.output(GPIO_PIN,GPIO.LOW)

def on_message(client, userdata, msg):

    print(msg.topic+" "+msg.payload.decode("utf-8"))
    trun(msg.payload.decode("utf-8"))

if __name__ == '__main__':
    global a
    a = -1
    client_loop()
  • 发布
#!/usr/bin/env python  
# encoding: utf-8 

import json
import sys
import os
import paho.mqtt.client as mqtt
import time
 
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
sys.path.append("..")
 
TASK_TOPIC = 'test'  # 客户端发布消息主题

client_id = time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))

client = mqtt.Client(client_id, transport='tcp')
 
client.connect("59.110.42.24", 1883, 60)  # 此处端口默认为1883,通信端口期keepalive默认60
client.loop_start()
 
 
def clicent_main(message: str):
    
    time_now = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(time.time()))
    payload = {"msg": "%s" % message, "data": "%s" % time_now}
    # publish(主题:Topic; 消息内容)
    client.publish(TASK_TOPIC, json.dumps(payload, ensure_ascii=False))
    client.publish(TASK_TOPIC,message)
    print("Successful send message!")
    return True
if __name__ == '__main__':
    msg = "我是一条测试数据!"
    clicent_main(msg)

emq服务器
在我之前的博客中有emq服务器怎么安装。这是最终效果,我小程序只要点击打开就会发送打开,点击关闭就会关闭
在这里插入图片描述

  • 6
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值