wbr3模组通讯串口使用 Arduino 引脚 0 和引脚 1,因此给 Arduino 开发板下载例程时, 请勿插三明治开发板(或长按三明治开发板板载 reset 按键),否则串口会互相干扰导致无 法正常下载程序。
以下是完整代码,实现涂鸦智能远程控制呼吸灯启停。
呼吸灯逻辑代码是在loop函数中监测arduino 10#引脚状态,如果为HIGH则启动,否则停止 通过3#引脚输出PWM来实现LED的明暗变化。
#include <TuyaWifi.h>
#include <SoftwareSerial.h>
TuyaWifi my_device;
unsigned char led_state = 0;
int key_pin = 7;
int led = 3;
int pinBtn = 10;
#define DPID_SWITCH 20
#define DPID_LIGHT 21
#define DPID_MODE 22
unsigned char dp_array[][2] = {
{DPID_SWITCH, DP_TYPE_BOOL},
{DPID_LIGHT, DP_TYPE_VALUE},
{DPID_MODE, DP_TYPE_ENUM}
};
unsigned char pid[] = {"6nwnhhsm5qiscvj6"};
unsigned char mcu_ver[] = {"1.0.0"};
unsigned long last_time = 0;
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(led, OUTPUT);
pinMode(pinBtn,OUTPUT);
pinMode(key_pin, INPUT_PULLUP);
my_device.init(pid, mcu_ver);
my_device.set_dp_cmd_total(dp_array, 3);
my_device.dp_process_func_register(dp_process);
my_device.dp_update_all_func_register(dp_update_all);
last_time = millis();
}
void loop()
{
my_device.uart_service();
if (digitalRead(key_pin) == LOW) {
delay(80);
if (digitalRead(key_pin) == LOW) {
my_device.mcu_set_wifi_mode(SMART_CONFIG);
}
}
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);
if(digitalRead(pinBtn)==HIGH){
breathingLed();}
}
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);
if (led_state) {
digitalWrite(pinBtn, 1);
} else {
digitalWrite(pinBtn, 0);
}
my_device.mcu_dp_update(dpid, value, length);
break;
default:break;
}
return SUCCESS;
}
void dp_update_all(void)
{
my_device.mcu_dp_update(DPID_SWITCH, led_state, 1);
}
void breathingLed(){
for (int a=0; a<=255;a++)
{
analogWrite(led,a);
delay(8);
}
for (int a=255; a>=0;a--)
{
analogWrite(led,a);
delay(8);
}
}
|