uniapp 小程序AP配网(硬件配网)物联网

一、连接硬件开启的热点

 uni.connectWifi({
        forceNewApi: true,
        SSID: this.SSID, // 设备热点名称
        password: this.password, // 设备热点密码
        success: (res) => {
          console.log("连接硬件热点成功,可进行tcp连接配网");
        },
        fail: (err) => {
          console.log(err);
        },

二、建立TCP通讯,把要配置的WiFi名密码发给硬件设备,让硬件设备完成配网

1.TCPSocket.js 封装TCP协议
class socket {
  constructor() {
    this.connection = {};
  }
  // 创建一个TCP实例
  establish(monitor) {
    this.connection = wx.createTCPSocket();
    this.connection.connect({ address: "000.000.0.0", port: 6800 });
  }
  // 发送消息
  connect(message) {
    this.connection.onConnect(() => {
      this.connection.write(message);
    });
  }
  // 监听端口函数
  onMessage(success, failure) {
    this.connection.onMessage((res) => {
      success(res)
    });
    this.connection.onError((err) => {
      failure(err);
    });
  }

  // 关闭搜索事件
  TCPclose() {
    setTimeout(() => {
      // 3S后,关闭socket
      this.connection.close(() => {
        console.log("TCP关闭");
      });
    }, 3000);
  }
}
const TCPSocket = new socket();
export default TCPSocket;
 2.vue文件中使用TCP
<template>
  <view class="zai-box">
        <view class="container">
          <text class="textName">{{ wifiSSID }}</text>
          <input type="text" placeholder="请输入WLAN密码" v-model="password" />
        </view>
        <button
          class="loginButton"
          :loading="loadingWIfi"
          :disabled="JSON.stringify(password.length) < 8 || loadingWIfi"
          @click="settiing"
        >
          {{ loadingWIfi ? "连接中" : "连接" }}
        </button>
  </view>
</template>

<script>
import TCP from "@/common/util/TCPSocket.js";
export default {
  data() {
    return {
      wifiSSID: "WIFI name",
      password: "",
      isFirst: true,
      wifiCountInterval: null,
      wifiCountDown: 0,
      loadingWIfi: false,
    };
  },
  onHide() {
    this.clearInterval();
  },
  methods: {
    settiing() {
      this.loadingWIfi = true;
      this.onConfirm();
    },
    onConfirm() {
      this.wifiCountDown = 60;
      this.startSMSTimer();
      if (this.isFirst) {
        // TCP实例创建
        // 连接端口
        TCP.establish();
        this.isFirst = false;
      }

      // 请求设备上报状态
      this.udpsend();
      // 开始监听
      TCP.onMessage(
        (res) => {
          if (!res) return;
          // 关闭TCP连接
          TCP.TCPclose();
          console.log(res) // 查看返回数据
          
        },
        (err) => {
          // debug
          console.log(err, "err");
        }
      );
    },
    udpsend() {
      const msgData = {
        event: "network",
        data: {
          ssid: this.wifiSSID,
          password: this.password,
        },
      };
      TCP.connect(JSON.stringify(msgData));
    },
    startSMSTimer() {
      this.wifiCountInterval = setInterval(() => {
        this.wifiCountDown--;
        if (this.wifiCountDown <= 0) {
          clearInterval(this.wifiCountInterval);
          this.wifiCountInterval = null;
          // 响应超过60秒后的操作
          // ........
        }
      }, 1000);
    },
    clearInterval() {
      if (this.wifiCountInterval) {
        clearInterval(this.wifiCountInterval);
        this.wifiCountInterval = null;
      }
    },
  },
};
</script>

<style scoped>
.zai-box {
  padding: 0;
  margin: 0;
}

.container {
  padding: 30rpx;
  margin: 0 0 20rpx;
  font-size: 28rpx;
  color: #20212b;
  background-color: #fff;
  text-align: left;
  font-weight: 400;
}

.loginButton {
  font-size: 36rpx;
  width: 690rpx;
  color: #fff;
  background-color: #00a0e9;
  border-radius: 44rpx;
  margin: 60rpx auto 0;
  border: none;
}

.loginButton[disabled] {
  color: #fff;
  background-color: #dfdfdf;
}

button::after {
  border: none;
}

.textName {
  display: block;
  font-size: 28rpx;
  font-weight: 500;
  color: #20212b;
  margin-bottom: 30rpx;
}
</style>

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值