-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from shiliu-yang/dev
update 0.0.2
- Loading branch information
Showing
19 changed files
with
912 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.