收藏官网首页
查看: 5534|回复: 0

IoT小能手:SmartConfig 配置入网

6

主题

21

帖子

202

积分

中级会员

Rank: 3Rank: 3

积分
202
发表于 2017-6-27 17:33:26 | 显示全部楼层 |阅读模式
免费使用STM32、APP自动代码生成工具
本帖最后由 loulan 于 2017-6-27 18:06 编辑

原文链接

随着物联网技术的发展,越来越多的智能设备接入互联网。在连接网络的过程中,通常依赖家庭的 Wi-Fi 或者是物联网 SIM 卡。在连接 Wi-Fi 上网的时候,智能设备没有配备通常没有用于输入 WiFi 网络名称和密码键盘。所以产生了很多的配置设备入网的技术。
  • 利用 SmartConfig 等技术配置入网。
  • 设备变为热点,手机连接进入网页设置。
  • 设备拥有摄像头等其他特殊方式的配置模式。

SmartConfig 原理
SmartConifg 技术让你的手机直接通过 UDP 以特定的协议广播发送 WiFi 信息密码和手机 IP 数据包给智能硬件,硬件通过监听信道的信息,就可以解析出来无线网络和密码连接到路由器。
市面上有很多的智能设备配网技术,比如 AirKiss、SmartConfig、ESPTouch 各种名称,智能配置上网技术的原理基本上都是一致的,其开山鼻祖应该是 TI SmartConfig。

硬件代码
使用 SmartConfig 硬件 WiFi 要处于 Station 模式。下面列举不同的硬件平台的配网代码示例。
1.arduino IDE 使用 SmartConfig
这个稍后再补上
2.esp8266-RTOS-SDK 使用 SmartConfig
要注意修改 Makefile 文件,和引入必要的 .h 文件,然后调用代码就是 user_main.c,其他代码太多复制即可,具体可以看这个 Demo
  1. user_init(void)  
  2. {
  3.      printf("SDK version:%s\n", system_get_sdk_version());

  4.      /* need to set opmode before you set config */
  5.     wifi_set_opmode(STATION_MODE);

  6.     xTaskCreate(smartconfig_task, "smartconfig_task", 256, NULL, 2, NULL);
  7. }
复制代码
修改完成进行构建,生成 bin 文件烧写到 ESP8266 即可。
3.NodeMCU 使用 SmartConfig
NodeMCU 使用 SmartConfig 也要注意官方的库默认是关闭对它的支持,你需要在 user_config.h 中取消注释,然后重新编译固件,拷贝下面的代码到你的 init.lua 文件即可实现智能配网 Demo。
  1. wifi.setmode(wifi.STATION)  
  2. -- 0 is esptouch;
  3. -- 1 is airkiss;
  4. wifi.startsmart(0,  
  5.     function(ssid, password)

  6.     -- print log
  7.     print(string.format("Success. SSID:%s ; PASSWORD:%s", ssid, password))

  8.    -- write wifi ssid and pass to txt
  9.     file.open("wifi.txt", "w+")
  10.     file.write(ssid)
  11.    file.close()

  12.     file.open("pass.txt", "w+")
  13.     file.write(password)
  14.     file.close()

  15.     end
  16. )
复制代码
软件代码
苹果平台代码-Swift语言
Step1   
下载工程EspressifApp封装库代码,拖到工程里。
Step2
然后实例化一个 esptouchTask,将信息传入 ESPTouchTask,设置代理,然后回调结果即可。
  1. ESPTouchTask(apSsid: SSID, andApBssid: BSSID, andApPwd: PASS)  
复制代码
代理部分
  1. class EspTouchDelegateImpl: NSObject, ESPTouchDelegate {

  2.     @objc func dismissAlert(_ alertView: UIAlertView) {
  3.      alertView.dismiss(withClickedButtonIndex: alertView.cancelButtonIndex, animated: true)
  4.     }

  5.     func showAlert(with result: ESPTouchResult) {
  6.      let text = NSLocalizedString("WIFI_CONNECTION", comment: "")
  7.      let message: String = result.bssid + text
  8.      let dismissSeconds: TimeInterval = 3.5
  9.      let alertView = UIAlertView(title: "", message: message, delegate: nil, cancelButtonTitle: nil)
  10.      alertView.show()
  11.      perform(#selector(self.dismissAlert), with: alertView, afterDelay: dismissSeconds)
  12.     }

  13.     func onEsptouchResultAdded(with result: ESPTouchResult) {
  14.      print("EspTouchDelegateImpl onEsptouchResultAddedWithResult bssid: \(result.bssid)")
  15.     // 放到主线程显示
  16.      DispatchQueue.main.async(execute: {() -> Void in
  17.          self.showAlert(with: result)
  18.      })
  19. }
  20. }
复制代码
配置函数
  1. <blockquote>func tapConfirmForResult() {  
复制代码
配置结果
  1. <blockquote><blockquote>func executeForResult() -> ESPTouchResult {
复制代码
我使用乐鑫官方的 ESPTouch SDK 开发了一个 SmartConfig APP 现在已经在 APP Store 上架。

安卓平台代码
稍后补充

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表