巴法云物联网平台的MQTT接入只有说明文档,没有移动端实例。经过多次连接测试,使用uniapp开发的移动端终于成功连接服务器。
uniapp 代码:
<template>
<view class="content">
<!-- <image class="logo" src="/static/logo.png" ></image> -->
<image class="logo" :src="state=='on' ? imgurlon : imgurloff"></image>
<view class="text-area">
<view class="title">{{title}}</view>
</view>
<view class="btn">
<button type="default" @click="btnon()">开</button>
<button type="default" @click="btnoff()">关</button>
</view>
</view>
</template>
<script>
var mqtt = require('../../mqtt.min.js')
export default {
data() {
return {
title: '网络连接中...',
state:'off',
imgurloff:'/static/off.png',
imgurlon:'/static/on.png',
client: null,
url: 'wxs://bemfa.com:9504/wss',
options: {
clientId: '***************',
keepalive: 60,
}
}
},
onLoad() {
this.mqttconnect()
},
methods: {
mqttconnect() {
this.client = mqtt.connect(this.url,this.options)
this.client.on('connect', () => {
this.title = '服务器连接成功!'
this.client.subscribe('light002',{qos: 1}, (err) => {
console.log(err || '订阅主题成功')
})
})
this.client.on('message', (topic, message) => {
let msg = message.toString()
this.title = '设备在线'
this.state = msg
console.log('收到来自'+ topic + '的消息:' + message.toString())
})
},
btnon() {
this.client.publish('light002', 'on',{
qos: 1,
retain: true
},(err) => {
console.log(err || '发送信息成功')
}
)
},
btnoff() {
this.client.publish('light002', 'off',{
qos: 1,
retain: true
},(err) => {
console.log(err || '发送信息成功')
}
)
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
.btn{
display: flex;
flex-direction: row;
margin: 20rpx;
}
button{
margin: 20rpx;
border-radius: 5rpx;
color: #0055ff;
background-color: #909090;
}
</style>
ESP8266 模块代码 在官方代码的基础上加入了自动配网,当wifi环境改变后,可以使用手机连接esp8266热点进行网络配置。 详见官方文档:https://cloud.bemfa.com/docs/#/?id=_151-%e6%8e%a7%e5%88%b6led%e7%81%af
#include <ESP8266WiFi.h>
#include "PubSubClient.h"
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#define ID_MQTT "6888888888888888888887"
const char* topic = "light002";
const int B_led = D1;
const char* mqtt_server = "bemfa.com";
const int mqtt_server_port = 9501;
WiFiClient espClient;
PubSubClient client(espClient);
void turnOnLed();
void turnOffLed();
void setup() {
pinMode(B_led, OUTPUT);
digitalWrite(B_led, LOW);
Serial.begin(9600);
WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
Serial.println("");
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address:\t");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, mqtt_server_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Topic:");
Serial.println(topic);
String msg = "";
for (int i = 0; i < length; i++) {
msg += (char)payload[i];
}
Serial.print("Msg:");
Serial.println(msg);
if (msg == "on") {
turnOnLed();
} else if (msg == "off") {
turnOffLed();
}
msg = "";
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(ID_MQTT)) {
Serial.println("connected");
Serial.print("subscribe:");
Serial.println(topic);
client.subscribe(topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void turnOnLed() {
Serial.println("turn on light");
digitalWrite(B_led, HIGH);
}
void turnOffLed() {
Serial.println("turn off light");
digitalWrite(B_led, LOW);
}
|