arduino通过wbr3连接wifi
###1.安装tuya wifi SDK
项目>添加库>库管理器安装Tuya_WiFi_MCU_SDK 的第三方库
![image-20210725221019745](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)
![image-20210725221019745.png](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)
###2.编写控制器程序
涂鸦库中有多种基础示例来帮助开发者更好地理解涂鸦库的使用:
Start:基础示例,包含一个开关 DP 功能,可以通过手机控制灯的开关
简单修改Start例程,增加向ardunio控制led功能,烧写程序
/* * @FileName: start.ino * @Author: Tuya * @Email: * @LastEditors: Tuya * @Date: 2021-04-10 11:24:27 * @LastEditTime: 2021-04-28 19:48:31 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD * @Company: http://www.tuya.com * @Description: This demo is based on the Arduino UNO, and the LEDs on the UNO board are controlled by the Tuya Smart App. * Enter network connection mode when Pin7 to GND. * @Github:https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library */#include <TuyaWifi.h>#include <SoftwareSerial.h>TuyaWifi my_device;/* Current LED status */unsigned char led_state = 0;/* Connect network button pin */int key_pin = 7;int led = 10;/* Data point define */#define DPID_SWITCH 20#define DPID_LIGHT 21 //灯的亮度 DP 6#define DPID_MODE 22 //灯的工作模式 DP /* Stores all DPs and their types. PS: array[][0]:dpid, array[][1]:dp type. * dp type(TuyaDefs.h) : DP_TYPE_RAW, DP_TYPE_BOOL, DP_TYPE_VALUE, DP_TYPE_STRING, DP_TYPE_ENUM, DP_TYPE_BITMAP */unsigned char dp_array[][2] = { {DPID_SWITCH, DP_TYPE_BOOL}, {DPID_LIGHT, DP_TYPE_VALUE}, {DPID_MODE, DP_TYPE_ENUM} }; unsigned char pid[] = {"6nwnhhsm5xxxxx"}; //PIDunsigned char mcu_ver[] = {"1.0.0"};/* last time */unsigned long last_time = 0;void setup() { // Serial.begin(9600); Serial.begin(9600); //Initialize led port, turn off led. pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); pinMode(led, OUTPUT); digitalWrite(led, LOW); //Initialize networking keys. pinMode(key_pin, INPUT_PULLUP); //Enter the PID and MCU software version my_device.init(pid, mcu_ver); //incoming all DPs and their types array, DP numbers my_device.set_dp_cmd_total(dp_array, 3); //register DP download processing callback function my_device.dp_process_func_register(dp_process); //register upload all DP callback function my_device.dp_update_all_func_register(dp_update_all); last_time = millis(); }void loop() { my_device.uart_service(); //Enter the connection network mode when Pin7 is pressed. if (digitalRead(key_pin) == LOW) { delay(80); if (digitalRead(key_pin) == LOW) { my_device.mcu_set_wifi_mode(SMART_CONFIG); } } /* LED blinks when network is being connected */ if ((my_device.mcu_get_wifi_work_state() != WIFI_LOW_POWER) && (my_device.mcu_get_wifi_work_state() != WIFI_CONN_CLOUD) && (my_device.mcu_get_wifi_work_state() != WIFI_SATE_UNKNOW)) { if (millis()- last_time >= 500) { last_time = millis(); if (led_state == LOW) { led_state = HIGH; } else { led_state = LOW; } digitalWrite(LED_BUILTIN, led_state); } } delay(10); }/** * 当 App 控制设备的时候,会从云端下发对应的 DP 命令到设备。设备对数据进行解析后,对下 * 发的命令执行相应的动作。 * @description: DP download callback function. * @param {unsigned char} dpid * @param {const unsigned char} value * @param {unsigned short} length * @return {unsigned char} */unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length){ switch(dpid) { case DPID_SWITCH: led_state = my_device.mcu_get_dp_download_data(dpid, value, length); /* Get the value of the down DP command */ if (led_state) { //Turn on digitalWrite(led, HIGH); } else { //Turn off digitalWrite(led, LOW); } //Status changes should be reported. my_device.mcu_dp_update(dpid, value, length); break; default:break; } return SUCCESS; }/** * @description: Upload all DP status of the current device. * @param {*} * @return {*} */void dp_update_all(void){ my_device.mcu_dp_update(DPID_SWITCH, led_state, 1); }
注意:模组通讯串口使用 Arduino 引脚 0 和引脚 1,因此给 Arduino 开发板下载例程时,
请勿插三明治开发板(或长按三明治开发板板载 reset 按键),否则串口会互相干扰导致无
法正常下载程序。
3.获取产品PID
为了实现ardunio与涂鸦三明治 Wi-Fi 通信板之间的通信,您需要预先通过
涂鸦 IoT 平台创建产品以获取 PID、DP 等信息。
下面以实现产品幻彩灯带为例,获取幻彩灯带产品 PID 的步骤如下:
登录 涂鸦IoT 平台。
创建一款 自定义开发方案 的 Wi-Fi+BLE 协议幻彩灯带(本文以幻彩灯带作为参考案例)。
在 功能定义 页签中,根据功能需求选取对应的 DP 点。
产品功能由标准功能、自定义功能及云功能三部分组成,您可以在产品开发的第一步 功能定义 页面
完成所有功能设定。
标准功能
标准功能是指该类别产品支持的常用功能。
自定义功能
说明:仅选择 自定义开发方案 的产品支持自定义功能。如果您的产品功能不包含在标准功能中,您可以创建自定义功能。 功能点是对产品功能的抽象表
示,每种功能都可以通过不同功能类型进行自定义。
云功能云功能是基于云端的功能,不需要进行硬件嵌入式开发。目前支持的云功能 包括定时 和 跳转网
页。定时:设定开关时间及周循环,⽆需硬件嵌⼊式开发。
跳转⽹⻚:主要⽤于设置购买产品的微商城、产品使⽤说明等各种⻚⾯的跳转。
4.配网
通信板使用了 Wi-Fi 芯片,在 App 上选择手动添加方式,选择一个 Wi-Fi 产品进行配
网。如果通信板使用了 Wi-Fi 芯片,在 App 上选择手动添加方式,选择一个 Wi-Fi 产品进行配
网。
针对上面的代码我选择的是照明>灯丝灯>灯丝灯(wifi)进行配网。
![image-20210728000108130](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)
![image-20210725221146130.png](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)
配网成功生成一个控制面板,可以通过开关远程控制led的亮灭。
![image-20210728000555515](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)
![image-20210728000108130.png](https://lcpsky.top/zb_users/theme/tpure/style/images/lazyload.png)