摘要:涂鴉模組中的固件應為通用固件,通用固件支持,兩種波特率自適應。涂鴉智能將每個功能點抽象成數據點,數據點定義成不同的類型,例如布爾枚舉數值等。指定通信協議下事件的。上報設備狀態函數注冊獲取連接狀態輪詢串口數據在中調用。
Tuya MCU SDK Arduino Library 基于涂鴉Wi-Fi 通用對接方案進行開發的,設備 MCU 通過串口與 Wi-Fi 模組連接實現設備聯網。涂鴉模組中的固件應為通用固件,通用固件支持9600,115200
兩種波特率自適應。
注意點:
Arduino板子 + 涂鴉模塊
的開發方式,據說后期可以多帶帶模塊開發。- 串口通信
https://gitee.com/ant_onio/tuya-wifi-mcu-sdk-arduino-library 點擊下載
解壓到自己的電腦路徑(此電腦->文檔->Arduino->libraries)
Arduino IDE -> 工具 -> 管理庫 -> 搜索 tuya-> 安裝最新版本
記住一點,這種方式是mcu + module
串口通信開發方式
簡單來說,肯定數據是
不斷處理串口通信
,那么從另外一個點來說就是模組已經內置了串口協議數據處理邏輯。
博哥大概代碼看了一下:
class TuyaUart{public: // 主要是定義串口通信buffer緩存大小 volatile unsigned char wifi_uart_rx_buf[PROTOCOL_HEAD + WIFI_UART_RECV_BUF_LMT]; //Serial data processing buffer volatile unsigned char wifi_uart_tx_buf[PROTOCOL_HEAD + WIFIR_UART_SEND_BUF_LMT]; //Serial receive buffer volatile unsigned char wifi_data_process_buf[PROTOCOL_HEAD + WIFI_DATA_PROCESS_LMT]; //Serial port send buffer TuyaUart(void); ~TuyaUart(void); void wifi_protocol_init(void); // 接收數據 unsigned char uart_receive_input(unsigned char data); void uart_receive_buff_input(unsigned char value[], unsigned short data_len); unsigned char take_byte_rxbuff(void); unsigned char with_data_rxbuff(void); // 傳輸發送數據 void uart_transmit_output(unsigned char value); void wifi_uart_write_data(unsigned char *in, unsigned short len); void wifi_uart_write_frame(unsigned char fr_type, unsigned char fr_ver, unsigned short len); unsigned short set_wifi_uart_byte(unsigned short dest, unsigned char byte); unsigned short set_wifi_uart_buffer(unsigned short dest, const unsigned char *src, unsigned short len); /* serial set */ bool _isHWSerial; // 串口設置 void set_serial(HardwareSerial *serial); void set_serial(SoftwareSerial *serial); void begin(long baud_rate); char read(void); size_t write(char value); int available(void);private: volatile unsigned char *rx_buf_in; volatile unsigned char *rx_buf_out; Stream *_port;};
class TuyaTools{public: TuyaTools(void); ~TuyaTools(void); unsigned char hex_to_bcd(unsigned char Value_H, unsigned char Value_L); // 計算字符串長度 unsigned long my_strlen(unsigned char *str); // 設置字符串 void *my_memset(void *src, unsigned char ch, unsigned short count); // 內存拷貝 void *my_memcpy(void *dest, const void *src, unsigned short count); // 字符串拷貝 char *my_strcpy(char *dest, const char *src); // 字符串對比 int my_strcmp(char *s1, char *s2); // int 轉字節數組 void int_to_byte(unsigned long number, unsigned char value[4]); // 字節數組 轉int unsigned long byte_to_int(const unsigned char value[4]); unsigned char get_check_sum(unsigned char *pack, unsigned short pack_len);private:};
class TuyaDataPoint{public: // TuyaDataPoint(void); // ~TuyaDataPoint(void); // 下發節點數據 unsigned char mcu_get_dp_download_bool(const unsigned char value[], unsigned short len); unsigned char mcu_get_dp_download_enum(const unsigned char value[], unsigned short len); unsigned long mcu_get_dp_download_value(const unsigned char value[], unsigned short len); // 上報節點數據 unsigned char mcu_dp_raw_update(unsigned char dpid, const unsigned char value[], unsigned short len); unsigned char mcu_dp_bool_update(unsigned char dpid, unsigned char value); unsigned char mcu_dp_value_update(unsigned char dpid, unsigned long value); unsigned char mcu_dp_string_update(unsigned char dpid, const unsigned char value[], unsigned short len); unsigned char mcu_dp_enum_update(unsigned char dpid, unsigned char value); unsigned char mcu_dp_fault_update(unsigned char dpid, unsigned long value);private:};
注意區分一下數據類型。
DP為 Data Point 的縮寫,即數據點,偶爾被稱為 DP 點,表示智能設備所具備的功能點。
True
或 False
。DPID:指定通信協議下 DP 事件的 ID。
節點類型:
#define DP_TYPE_RAW 0x00 //RAW type#define DP_TYPE_BOOL 0x01 //bool type#define DP_TYPE_VALUE 0x02 //value type#define DP_TYPE_STRING 0x03 //string type#define DP_TYPE_ENUM 0x04 //enum type#define DP_TYPE_BITMAP 0x05 //fault type
在Arduino中使用該庫進行編程開發時,必須在你的Arduino工程文件中包含 TuyaWifi.h 頭文件。
一般很多sdk的開發方式都是:
- 1、創建對象 構造方法
- 2、初始化 init
- 3、回調配置、信息注冊 register、callbackHandler
- 4、不斷輪詢處理響應
PID通過在 Tuya IoT平臺 上創建產品獲取,wifi類的長度一般為16,mcu_ver 這個參數是軟件的版本號。
在Tuya IoT平臺創建產品,獲取產品DP點信息。
DP為 Data Point 的縮寫,即數據點,偶爾被稱為 DP 點,表示智能設備所具備的功能點。
True
或 False
。DPID:指定通信協議下 DP 事件的 ID。
MCU SDK需要知道你創建了哪些DP點,他們是什么類型的。
/** * @description: MCU set wifi working mode * @param {unsigned char} mode : enter mode * 0(SMART_CONFIG):enter smartconfig mode * 1(AP_CONFIG):enter AP mode * @return {*} */void TuyaWifi::mcu_set_wifi_mode(unsigned char mode);
當APP控制設備的時候,會從云端下發對應的DP命令到設備,設備對數據進行解析后,對下發的命令執行相對于的動作。
對下發命令的處理,通過回調函數來調用,所以我們需要把你的處理函數注冊一下。
偽代碼:
//register DP download processing callback function my_device.dp_process_func_register(dp_process);/** * @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_BOOL: DebugSerial.println("Bool type:"); dp_bool_value = my_device.mcu_get_dp_download_data(dpid, value, length); DebugSerial.println(dp_bool_value); /* After processing the download DP command, the current status should be reported. */ my_device.mcu_dp_update(DPID_BOOL, dp_bool_value, 1); break; case DPID_VALUE: DebugSerial.println("Value type:"); dp_value_value = my_device.mcu_get_dp_download_data(DPID_VALUE, value, length); DebugSerial.println(dp_value_value); /* After processing the download DP command, the current status should be reported. */ my_device.mcu_dp_update(DPID_VALUE, dp_value_value, 1); break; case DPID_ENUM: DebugSerial.println("Enum type:"); dp_enum_value = my_device.mcu_get_dp_download_data(dpid, value, length); DebugSerial.println(dp_enum_value); /* After processing the download DP command, the current status should be reported. */ my_device.mcu_dp_update(DPID_ENUM, dp_enum_value, 1); break; case DPID_STRING: DebugSerial.println("String type:"); /* */ for (unsigned int i=0; i<length; i++) { dp_string_value[i] = value[i]; DebugSerial.write(dp_string_value[i]); } DebugSerial.println(""); /* After processing the download DP command, the current status should be reported. */ my_device.mcu_dp_update(DPID_STRING, dp_string_value, length); break; case DPID_RAW: DebugSerial.println("Raw type:"); /* */ for (unsigned int i=0; i<length; i++) { dp_raw_value[i] = value[i]; DebugSerial.write(dp_raw_value[i]); } DebugSerial.println(""); /* After processing the download DP command, the current status should be reported. */ my_device.mcu_dp_update(DPID_RAW, dp_raw_value, length); break; default: break; } return SUCCESS;}
這里得注意一個方法:
mcu_get_dp_download_data (`下發DP數據處理`)
對字節數組進行數據解析。
/* char * */ unsigned char mcu_dp_update(unsigned char dpid, const unsigned char value[], unsigned short len); /* char raw */ /* unsigned long / long */ unsigned char mcu_dp_update(unsigned char dpid, unsigned long value, unsigned short len); unsigned char mcu_dp_update(unsigned char dpid, long value, unsigned short len); /* unsigned char / char */ unsigned char mcu_dp_update(unsigned char dpid, unsigned char value, unsigned short len); unsigned char mcu_dp_update(unsigned char dpid, char value, unsigned short len); /* unsigned int / int */ unsigned char mcu_dp_update(unsigned char dpid, unsigned int value, unsigned short len); unsigned char mcu_dp_update(unsigned char dpid, int value, unsigned short len);
基本上大同小異。注意一下dpid和數據類型即可。
#include TuyaWifi my_device;#define DPID_SWITCH 20//Record the current status of the ledunsigned char switch_value = 0;...void setup() { ... //register DP download processing callback function my_device.dp_update_all_func_register(dp_update_all); ...}/** * @description: Upload all DP status of the current device. * @param {*} * @return {*} */void dp_update_all(void){ my_device.mcu_dp_update(DPID_SWITCH, switch_v
/** * @description: Wifi serial port processing service * @param {*} * @return {*} */void TuyaWifi::uart_service(void)
在arduino loop中調用。不斷檢測串口數據。
/* * @FileName: DataPointType.ino * @Author: Tuya * @Email: * @LastEditors: Tuya * @Date: 2021-04-19 14:31:52 * @LastEditTime: 2021-04-28 19:47:36 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD * @Company: http://www.tuya.com * @Description: * @Github:https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library */#include #include TuyaWifi my_device;SoftwareSerial DebugSerial(8,9);/* Current LED status */unsigned char led_state = 0;/* Connect network button pin */int wifi_key_pin = 7;/* Data point define */#define DPID_BOOL 101#define DPID_VALUE 102#define DPID_ENUM 103#define DPID_STRING 104#define DPID_RAW 105#define DPID_FAULT 106/* Current device DP values */unsigned char dp_bool_value = 0;long dp_value_value = 0;unsigned char dp_enum_value = 0;unsigned char dp_string_value[8] = {"Hi,Tuya"};unsigned char dp_raw_value[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};int dp_fault_value = 0x01;/* 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_BOOL, DP_TYPE_BOOL}, {DPID_VALUE, DP_TYPE_VALUE}, {DPID_ENUM, DP_TYPE_ENUM}, {DPID_STRING, DP_TYPE_STRING}, {DPID_RAW, DP_TYPE_RAW}, {DPID_FAULT, DP_TYPE_BITMAP},};unsigned char pid[] = {"xxxxxxxxxxx"}; // 填上自己的產品IDunsigned 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(wifi_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, 6); //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(wifi_key_pin) == LOW) { delay(80); if (digitalRead(wifi_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(); /* Toggle current LED status */ if (led_state == LOW) { led
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/119500.html
摘要:使用庫讀寫環境光照度傳感器本文將教大家如何快速使用庫讀取光照度數據。五實驗樣機測試展示通過之前配置好的面板,通過涂鴉智能進行配網實時采集光照度傳感器的數據。 使用STM32 HAL庫讀寫環境光照度傳感器(BH1750) 本文將教大家如何快速使用STM32HAL庫讀取光照度數據。 實現功能:通...
摘要:開啟掃描時需要設備處于配網狀態一分類配網子設備可以通過使用手機藍牙直接掃描獲取設備到設備基礎信息,再使用配網接口實現設備的本地配網。 ? (一)分類 ? (二)設備配置 ? (三)設備管理 ? ? 設備管理,大體分為兩類,mesh 和 其他 ? ? 獲取設備列表,給涂鴉sdk發送當前房間id...
摘要:研究者創建了一個名叫文森特的系統是的,就是梵高那個文森特使用深度學習,將簡筆畫轉變為藝術品。研究人員認為,除了在藝術繪畫方面大放異彩,類似文森特的技術還有一系列潛在的應用。 如果你喜歡藝術但下筆皆為靈魂畫作,那么今天要介紹的這個項目肯定合你心意。AI研究者創建了一個名叫文森特(Vincet)的系統——是的,就是梵高那個文森特——使用深度學習,將簡筆畫轉變為藝術品。用戶在平板上的涂鴉經過文森特...
摘要:我們進行了一個交互式網絡實驗,讓你能與一個名為的循環神經網絡模型一起繪制作品。我們利用來自于游戲的數百萬涂鴉訓練該神經網絡。一旦你停止涂鴉,神經網絡將接管并試圖猜測涂鴉的其余部分。在變分自動編碼器演示中,你將繪制指定對象的完整圖形。 對于通過蜂窩數據連接網絡的移動用戶:第一個演示大小約為 5 MB 數據。每次在演示中更改模型時,您將使用另外 5 MB 的數據。我們進行了一個交互式網絡實驗,讓...
閱讀 2883·2021-09-22 15:20
閱讀 2958·2021-09-22 15:19
閱讀 3448·2021-09-22 15:15
閱讀 2382·2021-09-08 09:35
閱讀 2372·2019-08-30 15:44
閱讀 3003·2019-08-30 10:50
閱讀 3707·2019-08-29 16:25
閱讀 1585·2019-08-26 13:55