Tuya Smart Plug with Wifi. Mua ở đâu, setup Api làm sao?
Mới múc con smart plug để theo dõi tiền điện mỗi tháng là bao nhiêu
Mình mua tại đây (no affiliate): https://go.kyluat.com/43lN4
Còn này xài wifi nên cứ gắm vào ổ điện và cài đặt wifi trên app là đc
Chức năng hửu dụng:
- Tắt mở remote mọi lúc mọi nơi miễn sao có wifi / internet
- Cài đặt thông báo số điện kwh hay số dollar limit theo thời gian quy định( ví dụ: 1 ngày mà xài quá số lượng điện cho phép thì email hay push notification tới điện thoại mình )
- Access vào api cloud platform tuya để code tùy chỉnh control on/off , xem current stats,...
Dưới này là thống kê trên smart life app kết nối với Smart Plug của Tuya.
Về Tuya API platform:
- bạn cần đăng ký account: https://auth.tuya.com/
Lúc đăng ký nhớ lưu ý là phải chọn Western America Data Center chứ ko phải China nha. Và sau đó khi xài SDK Nodejs hay ngôn ngữ khác thì bạn sử dụng link openapi của US:
baseUrl: https://openapi.tuyaus.com
2. Vào cloud platform mà tạo access authorization key và secret
Mở project của bạn lên
Lưu thông tin access id, access secret và điền ip address của nhà bạn vào để bảo mật.
3. Liên kết smart life app tới tài khoản cloud
Sau khi nhấn add app account thì bạn xài app smart life đã cài device của bạn , nhấn vào dấu cộng (+) , chọn Scan để scan qr và cho phép Cloud Platform của Tuya liên kết vào.
Sau khi liên kết hoàn tất, bạn cần lấy device id và test trên cloud explorer iot core của tuya:
4. test với cloud explorer của nó: https://us.platform.tuya.com/cloud/explorer
Sau đây là code mình đã test và sucess:
bạn thay thế accessKey, secretKey và device_id.
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
/**
* api env entrypoint
*
* 'https://openapi.tuyaus.com', // US
*/
const context = new TuyaContext({
baseUrl: 'https://openapi.tuyaus.com',
accessKey: 'yu4wydsasdasdsadjd5a5k',
secretKey: '92c35b701casdasdasd8a58d8e8e',
});
const main = async () => {
// get access token
const token = await context.client.init();
const device_id = "eb5acbfcasdasdasdasdasd61d51az";
const res = await context.request({
path: `/v1.0/devices/${device_id}/status`,
method: 'GET',
query: {
key1: 'value1',
key2: 'value2'
}
});
// convert result to human readable format
const resultStats = res.result.map((item: any) => {
return {
code: item.code,
value: item.value
}
});
// power in kwh, voltage in V, current in mA
// write display in good human readable format from the result with unit name
const humanReadableResult = resultStats.map((item: any) => {
switch (item.code) {
case 'cur_power':
return {
code: 'Power',
value: `${item.value / 10}W`
}
case 'cur_voltage':
return {
code: 'Voltage',
value: `${item.value/10}V`
}
case 'cur_current':
return {
code: 'Current',
value: `${item.value}mA`
}
default:
return {
code: item.code,
value: item.value
}
}
});
const optimizedResult = [];
// return only the 3 values that are needed
humanReadableResult.forEach((item: any) => {
if (item.code === 'Power' || item.code === 'Voltage' || item.code === 'Current') {
optimizedResult.push(item);
}
});
console.log(optimizedResult);
// get logs of device
const lastMonthUnixTimestamp = Math.floor(Date.now() / 1000) - 30 * 24 * 60 * 60;
const currentUnixTimestamp = Math.floor(Date.now() / 1000);
const logs = await context.request({
path: `/v2.0/cloud/thing/${device_id}/logs`,
method: 'GET',
query: {
type: 1,
start_time: lastMonthUnixTimestamp,
end_time: currentUnixTimestamp,
query_type: 1,
size: 20,
}
});
console.log(logs);
};
main().catch(err => {
console.log(err);
});
No Comments