Skip to content

Commit

Permalink
Merge pull request #6 from shiliu-yang/dev
Browse files Browse the repository at this point in the history
update 0.0.2
  • Loading branch information
flyingcys authored Nov 4, 2021
2 parents 0e17ca4 + 521ef45 commit 7e0e738
Show file tree
Hide file tree
Showing 19 changed files with 912 additions and 140 deletions.
4 changes: 2 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ MCU SDK需要知道你创建了哪些DP点,他们是什么类型的,通过 `
这里我们假设有一个灯,它主要有3个功能也就是3个DP点,分别是:
+ 开关(DP ID : 20, DP 类型:布尔类型)
+ 灯的亮度(DP ID : 20, DP 类型:数值类型)
+ 灯光模式(DP ID : 20, DP 类型:枚举类型)
+ 灯的亮度(DP ID : 21, DP 类型:数值类型)
+ 灯光模式(DP ID : 22, DP 类型:枚举类型)
```c
#include <TuyaWifi.h>
Expand Down
33 changes: 19 additions & 14 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @FileName: config.h
* @Author: Tuya
* @Email:
* @LastEditors: Tuya
* @LastEditors: shiliu
* @Date: 2021-04-10 11:24:27
* @LastEditTime: 2021-04-28 19:50:37
* @LastEditTime: 2021-11-04 14:00:42
* @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
* @Company: http://www.tuya.com
* @Description: Tuya mcu sdk Arduino library config file.
Expand All @@ -15,24 +15,29 @@
#include "TuyaDefs.h"

/* Distribution mode selection, only three choices, Anti-touch mode is recommende */
//#define CONFIG_MODE CONFIG_MODE_DEFAULT //Default working mode
//#define CONFIG_MODE CONFIG_MODE_LOWPOWER //Safe mode (low power working mode)
#define CONFIG_MODE CONFIG_MODE_SPECIAL // Anti-touch mode (special working mode)
// #define CONFIG_MODE CONFIG_MODE_DEFAULT //Default working mode
// #define CONFIG_MODE CONFIG_MODE_LOWPOWER //Safe mode (low power working mode)
#define CONFIG_MODE CONFIG_MODE_SPECIAL // Anti-touch mode (special working mode)

/* some extra features defined begin */
#define WIFI_CONTROL_SELF_MODE 0
#define SUPPORT_GREEN_TIME 1
#define SUPPORT_RTC_TIME 1
/* some extra features defined end */

#ifndef SUPPORT_MCU_FIRM_UPDATE
#define WIFI_UART_RECV_BUF_LMT 16 //UART data receiving buffer size, can be reduced if the MCU has insufficient RAM
#define WIFI_DATA_PROCESS_LMT 24 //UART data processing buffer size, according to the user DP data size, must be greater than 24
#define WIFI_UART_RECV_BUF_LMT 32 //UART data receiving buffer size, can be reduced if the MCU has insufficient RAM
#define WIFI_DATA_PROCESS_LMT 64 //UART data processing buffer size, according to the user DP data size, must be greater than 24
#else
#define WIFI_UART_RECV_BUF_LMT 128 //UART data receiving buffer size, can be reduced if the MCU has insufficient RAM

#define WIFI_UART_RECV_BUF_LMT 128 //UART data receiving buffer size, can be reduced if the MCU has insufficient RAM
/* Select the appropriate UART data processing buffer size here
(select the buffer size based on the size selected by the above MCU firmware upgrade package and whether to turn on the weather service) */
#define WIFI_DATA_PROCESS_LMT 1000 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 256, the buffer must be greater than 260, or larger if the weather service is enabled
//#define WIFI_DATA_PROCESS_LMT 600 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 512, the buffer must be greater than 520, or larger if the weather service is enabled
//#define WIFI_DATA_PROCESS_LMT 1200 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 1024, the buffer must be greater than 1030, or larger if the weather service is enabled

#define WIFI_DATA_PROCESS_LMT 1000 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 256, the buffer must be greater than 260, or larger if the weather service is enabled
// #define WIFI_DATA_PROCESS_LMT 600 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 512, the buffer must be greater than 520, or larger if the weather service is enabled
// #define WIFI_DATA_PROCESS_LMT 1200 //UART data processing buffer size. If the MCU firmware upgrade is required, the single-packet size is 1024, the buffer must be greater than 1030, or larger if the weather service is enabled
#endif

#define WIFIR_UART_SEND_BUF_LMT 48 //According to the user's DP data size, it must be greater than 48
#define WIFIR_UART_SEND_BUF_LMT 64 //According to the user's DP data size, it must be greater than 48


#endif /* __CONFIG_H__ */
6 changes: 3 additions & 3 deletions examples/DataPointType/DataPointType.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @FileName: DataPointType.ino
* @Author: Tuya
* @Email:
* @LastEditors: Tuya
* @LastEditors: shiliu
* @Date: 2021-04-19 14:31:52
* @LastEditTime: 2021-04-28 19:47:36
* @LastEditTime: 2021-11-01 10:02:07
* @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
* @Company: http://www.tuya.com
* @Description:
Expand Down Expand Up @@ -172,7 +172,7 @@ unsigned char dp_process(unsigned char dpid, const unsigned char value[], unsign
break;
}

return SUCCESS;
return TY_SUCCESS;
}

/**
Expand Down
153 changes: 153 additions & 0 deletions examples/GetGreenTime/GetGreenTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* @FileName: GetGreenTime.ino
* @Author: Tuya
* @Email:
* @LastEditors: shiliu
* @Date: 2021-11-04 14:44:25
* @LastEditTime: 2021-11-04 14:50:21
* @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
* @Company: http://www.tuya.com
* @Description: get Greenwich Mean Time
* @Github:https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library
*/
#include <TuyaWifi.h>
#include <SoftwareSerial.h>

TuyaWifi my_device;
SoftwareSerial DebugSerial(8,9);

/* Current LED status */
unsigned char led_state = 0;
/* Connect network button pin */
int key_pin = 7;

/* Data point define */
#define DPID_SWITCH 20

TUYA_WIFI_TIME green_time;
unsigned long last_get_green_time;

/* 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},
};

unsigned char pid[] = {"ma67l9sgmdyg3d2k"};
unsigned char mcu_ver[] = {"1.0.0"};

/* last time */
unsigned long last_time = 0;

void setup()
{
Serial.begin(9600);
DebugSerial.begin(9600);

//Initialize led port, turn off led.
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 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, 1);
//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);
}
}

/* 5s get Greenwich Mean Time */
if (millis() - last_get_green_time >= 3000) {
last_get_green_time = millis();
if (TY_SUCCESS == my_device.get_green_time(&green_time, 100)) { /* if network lag, you can increase the timeout */
DebugSerial.print(green_time.year);
DebugSerial.print("-");
DebugSerial.print(green_time.month);
DebugSerial.print("-");
DebugSerial.println(green_time.day);

DebugSerial.print(green_time.hour);
DebugSerial.print(":");
DebugSerial.print(green_time.minute);
DebugSerial.print(":");
DebugSerial.println(green_time.second);
} else {
DebugSerial.println("get green time failed");
}
}

delay(10);
}

/**
* @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_BUILTIN, HIGH);
} else {
//Turn off
digitalWrite(LED_BUILTIN, LOW);
}
//Status changes should be reported.
my_device.mcu_dp_update(dpid, value, length);
break;

default:break;
}
return TY_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);
}

88 changes: 88 additions & 0 deletions examples/GetGreenTime/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# get green time
[English](./README.md) | [中文](./README_zh.md)

This library only supports the Tuya WiFi+BLE, WiFi module with the generic firmware burned in and verified on the Arduino UNO board.

**Note: The default Serial serial port in Arduino has been taken over by the Tuya mcu sdk, please do not do anything with the default Serial (pins 0 , 1).**



## I. Introduction to the demo

The connection between the Tuya module and the Arduino board will interfere with the burning of the Arduino board. The Arduino board should be disconnected from the Tuya communication board or the Tuya communication board when burning.

This example uses Arduino board + Tuya wifi communication board. This demo is mainly used to show how to get the network time, using Arduino pin 8 of as RX and pin 9 as TX to print the time information.

The function of getting time can only be used after the module is successfully networked.


### 1. Get Green time

**Function**
```c
char TuyaWifi::get_green_time(TUYA_WIFI_TIME *time, const unsigned int timeout);
```
**Parameters:**
+ <font color="#dd0000">`time`: </font> Store the data of the time from the module.
```c
typedef struct
{
unsigned short year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char weekday;
char update_flag;
}TUYA_WIFI_TIME;
```
+ <font color="#dd0000">`timeout`: </font> Timeout time, unit ms. The timeout time can be extended if the network condition is bad.
**Return:**
+ <font color="#dd0000">`1`: </font> Get success.
+ <font color="#dd0000">`-1`: </font> The input parameter is `NULL`, the input parameter is wrong.
+ <font color="#dd0000">`-2`: </font> Timeout.
</br>
### 1. Get RTC time
**Function**
```c
char TuyaWifi::get_rtc_time(TUYA_WIFI_TIME *time, const unsigned int timeout);
```
**Parameters:**
+ <font color="#dd0000">`time`: </font> Store the data of the time from the module.
```c
typedef struct
{
unsigned short year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;
unsigned char weekday;

char update_flag;
}TUYA_WIFI_TIME;
```
+ <font color="#dd0000">`timeout`: </font> Timeout time, unit ms. The timeout time can be extended if the network condition is bad.

**Return:**
+ <font color="#dd0000">`1`: </font> Get success.
+ <font color="#dd0000">`-1`: </font> The input parameter is `NULL`, the input parameter is wrong.
+ <font color="#dd0000">`-2`: </font> Timeout.

</br>


## III. Technical Support

You can get support for Tuya by using the following methods:

- Developer Centre: https://developer.tuya.com
- Help Centre: https://support.tuya.com/help
- Technical Support Work Order Centre: https://service.console.tuya.com
Loading

0 comments on commit 7e0e738

Please sign in to comment.