nohous / tenda-beli

Tenda Beli SP3 Smart Plug Python Control Library
7 stars 1 forks source link

SP9 power monitoring API? #2

Open CZDanol opened 2 years ago

CZDanol commented 2 years ago

Hello, how did you figure out the "getSta" and "setSta" parameters? I have SP9 which also has power monitoring functionality and I'm trying to figure out what rest endpoint is used to get that data.

I tried doing a tcpdump in my router, but I couldn't get to make the app communicate with the plug directly and the cloud server seems to be using a slightly different protocol. I've found this response: image but I wasn't able to find out how to ask for the data locally.

tvaira commented 2 years ago

Only :

CZDanol commented 2 years ago

How did you figure the list out? Is that definitely the complete list for SP9? Previous versions of the socket don't have the power monitoring functionality afaik.

tvaira commented 2 years ago

In Android app code

with : http://<ip>:5000/XXX

I have an SP9 and I'm also looking

CZDanol commented 2 years ago

I see. Thank you for your response. Please let me know if you find it out :) I'm trying to make the plug working with Home Assistant. I've made it turn on and off already, it's a pity I can't access the power monitoring data.

tvaira commented 2 years ago

See also : https://goughlui.com/2020/02/22/reverse-engineer-tenda-beli-sp3-smart-wi-fi-plug-protocol/

tvaira commented 2 years ago

There are two operating modes:

Can you post the full exchange with tcpdump?

my dump :

Received: 2400060001670053033f0000000000007b2273657269616c4e756d223a224541...34222c226d61726b223a22222c2274696d655f7a6f6e65223a302c226c6f636174696f6e223a224672616e63652f5061726973227d

{"serialNum":"EA...4","mark":"","time_zone":0,"location":"France/Paris"}

Received: 240006000166002c033d0000000000007b2273657269616c4e756d223a224541...34222c22737461747573223a307d

{"serialNum":"EA...94","status":0}

Received: 24000600018100000344000000000000240006000189001503450000000000007b22766572223a332c22656e65726779223a5b5d7d

{"ver":3,"energy":[]}

...
rohanabraham commented 1 year ago

Did you ever figure out how to get the energy usage on API?

CZDanol commented 1 year ago

Nope. Even tried contacting the support, they weren't of much help either.

JakDoh commented 1 year ago

I am also looking for a solution to get data from the plug. I tried to decompile the official application and look for a way to get this kind of data. If there is a possibility to get data using GET or POST request, then it is not used in the official application. Instead, the power data are collected by the provisioning server where the plug sends data in regular periods. How to setup the plug to own provisioning server is very nicely described in the article metioned by tvaira.

The plug connects to the ntp server and synchronizes the time. In an hour interval - every hour on the hour - plug sends the overall consumption data to the provisioning server. Actual power consumption and partial consumption (last hour) is provided on request. Actual power consumption is provided on every single request, partial consuption data are availible each circa 5 minutes and more frequent request are rejected.

response for Actual powerconsuption {"resp_code":0,"data":{"power":84.481}}' response for partial consuption {"energy":["1666809547,2370,0.060,2315,2","1666809547,2370,0.060,2315,3"]}' response for partial consuption on the request in the first minutes on the hour (eq 8:00, 9:00) - also contains data from the previous hour {"energy":["1666735585,362,0.000,0,2","1666735585,4048,0.010,1109,3"]}' overall consuption data - no request {"ver":3,"energy":["1666810806,203902,1.920,129354,0"]}

I haven't completely decoded these energy data frames yet because they are slightly different from time to time but i believe:

1666809547 epoch time 2370 time in seconds for which energy consuption is counted 0.060 energy consumed in kwh 2315 time in seconds for which power was actualy drowned 2 I don't understand the meaning yet

I extended the code from lui_gough by adding reading and requesting of energy data ability (POC only), to capture data for afterward "decoding". Download from here

I am familiarizing myself with the development of integration for Home Assistant which is my main aim.