TP-Link HS110

For issues with communication ports, protocols, etc.
Post Reply
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

I just bought a HS110 myself. Downloaded the almost 80 MB Kasa app for android. But I cannot use the app because my phone is the internet access point, so I cannot have wifi on and have internet connection at the same time, which seems needed. I'll test with my old phone...

But when the HS110 is in configuration mode (access point itself, Link led blinking amber and green) I can connect my laptop to the HS110 and ping the device (192.168.0.1) and run the python code. Formatted result of python hs110.py -t 192.168.0.1 -c info :

Code: Select all

Sent:{
	"system":{
		"get_sysinfo":{}
	}
}

Received:{
	"system":{
		"get_sysinfo":{
			"sw_ver":"1.5.4 Build 180815 Rel.121440",
			"hw_ver":"2.0","type":"IOT.SMARTPLUGSWITCH",
			"model":"HS110(EU)",
			"mac":"68:FF:7B:80:40:DE",
			"dev_name":"Smart Wi-Fi Plug With Energy Monitoring",
			"alias":"TP-LINK_Smart Plug_40DE",
			"relay_state":1,
			"on_time":312,
			"active_mode":"none",
			"feature":"TIM:ENE",
			"updating":0,
			"icon_hash":"",
			"rssi":33,
			"led_off":0,
			"longitude_i":0,
			"latitude_i":0,
			"hwId":"044A516EE63C875F9458DA25C2CCC5A0",
			"fwId":"00000000000000000000000000000000",
			"deviceId":"80061065B3D2BD6C2803CB3B3C8DB09C1B62B107",
			"oemId":"1998A14DAA86E4E001FD7CAF42868B5E",
			"next_action":{
				"type":-1
			},
			"err_code":0
		}
	}
}
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

@badidea
Glad you got that working, and with so little effort :(

I have managed to get decrypt working with the HS110 responding with 49 bytes when I turn it Off/On
However, I have done it rather long winded using file write / read.
Previously in another program using SNC , I have been able to use the buffer like this.

Code: Select all

				If Connection->CanGet() > 0 Then
					var status = Connection->GetData(.RxPtr,80)				'Expecting 80 Bytes
					.RxStatus = Status										'record Bytes received
					If .RxStatus then										'if > 0
						.RxData  = *.RxPtr									'get Data
						If Left(.RxData,1) <> "9" then						'If not 9 (Reply)
							.RxStatus = 0									'all feedback.
						Endif
					End if
				End If
For some reason every which way I try to that with this buffer, I end up with zeroes or &H1A
In the code below I haven't converted back to encrypt yet, but I know I will have to fill the result with String len before adding the encrypted string.
Also worth noting that the first char received is 45 which I record before decrypting.

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"

function encrypt(byref cmd as string) as string
   if len(cmd) > 0 then
      dim as ubyte key = 171 'same as -85
      for i as integer = 0 to len(cmd) - 1
         dim as ubyte a = key xor cmd[i]
         key = a
         cmd[i] = a
      next
   end if
   return cmd
end function

function decrypt(byref cmd as string) as string
   if len(cmd) > 0 then
      dim as ubyte key = 171 'same as -85
      for i as integer = 0 to len(cmd) - 1
         dim as ubyte a = key xor cmd[i]
         key = cmd[i]
         cmd[i] = a
      next
   end if
   return cmd
end function
'==========================================================================
dim as string cmdStr = Chr(0)+Chr(0)+Chr(0)+Chr(42)+Chr(208)+Chr(242)+Chr(129)+Chr(248)+Chr(139)+Chr(255)+Chr(154)+Chr(247)+Chr(213)+Chr(239)_
+Chr(148)+Chr(182)+Chr(197)+Chr(160)+Chr(212)+Chr(139)+Chr(249)+Chr(156)+Chr(240)+Chr(145)+Chr(232)+Chr(183)_
+Chr(196)+Chr(176)+Chr(209)+Chr(165)+Chr(192)+Chr(226)+Chr(216)+Chr(163)+Chr(129)+Chr(242)+Chr(134)+Chr(231)_
+Chr(147)+Chr(246)+Chr(212)+Chr(238)+Chr(222)+Chr(163)+Chr(222)+Chr(163)

const as string ServerIP   = "192.168.1.145"
const as ushort ServerPort = 9999
var client = NetworkClient(ServerIP,ServerPort)
var connection = client.GetConnection()
Print "Connection   = ";Connection

while connection->CanPut()<>1: sleep 100 : wend
Print "Connection Opened"
connection->PutData(StrPtr(cmdStr),Len(cmdStr))
Print "Wait for CanGet"
while connection->CanGet()<>1
  sleep 100
wend
print "receive data ..."
dim as zstring ptr buffer
var nBytes = connection->GetData(buffer)
print "number of received bytes " & nBytes
' get last char position of the HTTP asci header
var LastChar=instr(*buffer,HeaderEnd)-1
var Header  =left(*buffer,LastChar)
' get first byte behind the HTTP asci header
var DataStart=LastChar+4
' From here I need to be able to put it into a string that I can send to decrypt.
open "tplink.dat" for binary access write as #1
    dim as ubyte ptr FileBuffer=@buffer[DataStart]
    nBytes-=DataStart
    put #1,,*FileBuffer,nBytes
close #1
Dim RxData as String * 45        'Here I should be able to specify nBytes ???
Open "tplink.dat" for input as #1 len = nBytes
    Get #1,,RxData
Close #1
dim as uByte StrLen = asc(left(Rxdata,1))
Print StrLen
decrypt(RxData)
Print Right(RxData,StrLen - 1)

print "file saved ..."
' free the buffer (allocate by snc.bi)
deallocate buffer
sleep
end
Hopefully you can give me some hints as to how to avoid opening & closing files

Regards
EDIT: Once you have the HS110 setup , you dont need the Internet any more.
In fact you can use a dedicated Wifi modem on which there is no Internet connection.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

I got it working with the HS110 as a 'normal' WiFi device. Although bit complicated setup now. One phone as access point (hotspot) and internet router with 3 devices connected: The HS110, my laptop, my second phone with the Kasa app. Also when I switch off and back on the hotspot the IP-address of the HS110 can be different. I could not find the device IP-address in the Kasa app. Need to improve my IT infrastructure here.

The Kasa app even wanted me to enable location. So I assume TP-link now knows where this specific device is.

Next step: freeBASIC code. The energy/power readout could be a fun project. Maybe a time-graph of the power consumption of my fridge. Only problem is, I then have to leave my phone and laptop near my fridge :-)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

I think that stream lining the core code is my main priority, which I will then put into a separate routine.
Then all the other options can be experimented with.
So far the code below is acceptable, except the file read write and the print statements.

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"

    function encrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = a
             cmd[i] = a
          next
       end if
       return cmd
    end function
    function decrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = cmd[i]
             cmd[i] = a
          next
       end if
       return cmd
    end function
    '=====================================================================
    Dim as integer OnOff
    Input "0 for Off, 1 for On;";OnOff
    dim as string cmdStr ,TxStr
    If OnOff = 0 Then
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":0}}}"
            encrypt(cmdStr)
            TxStr = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
    EndIf        
    If OnOff = 1 Then
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":1}}}"        
            encrypt(cmdStr)
            TxStr = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
    Endif
    '=====================================================================
    const as string ServerIP   = "192.168.1.145"
    const as ushort ServerPort = 9999
    var client = NetworkClient(ServerIP,ServerPort)
    var connection = client.GetConnection()
    Print "Got Connection = ";Connection
    
    while connection->CanPut()<>1: sleep 100 : wend
    Print "Connection Opened"
    connection->PutData(StrPtr(TxStr),Len(TxStr))
    Print "Wait for CanGet"
    while connection->CanGet()<>1
      sleep 100
    wend
    print "receive data ..."
    dim as zstring ptr buffer
    var nBytes = connection->GetData(buffer)
    print "number of received bytes " & nBytes
    ' get last char position of the HTTP asci header
    var LastChar=instr(*buffer,HeaderEnd)-1
    var Header  =left(*buffer,LastChar)
    ' get first byte behind the HTTP asci header
    var DataStart=LastChar+4
    ' save it
    open "tplink.dat" for binary access write as #1
        dim as ubyte ptr FileBuffer=@buffer[DataStart]
        nBytes-=DataStart
        put #1,,*FileBuffer,nBytes
    close #1
    print "file saved ..."

    Dim RxData as String * 45   'Here I should be able to specify nBytes
    Open "tplink.dat" for input as #1 len = nBytes
        Get #1,,RxData
    Close #1
    decrypt(RxData)
    If Instr(RxData, "err_code") > 0 Then
        If Instr(RxData,":0}}") > 0 Then
                Print "No Error"
            Else
                Print RxData
        EndIf
    EndIf

    ' free the buffer (allocate by snc.bi)
    deallocate buffer
    sleep
    end
It's Beer O'Clock here.

Regards
EDIT: PS, if you want to monitor power consumption, then again your HS110 has to have Internet access.
Havent looked amongst the commands to see if time can be supplied from a PC on the wifi.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

Dinosaur wrote:EDIT: PS, if you want to monitor power consumption, then again your HS110 has to have Internet access.
Why? with:

Code: Select all

python hs110.py -t 192.168.0.1 -c energy
I get:

Code: Select all

Sent:      {"emeter":{"get_realtime":{}}}
Received:  {"emeter":{"get_realtime":{"voltage_mv":240393,"current_ma":96,"power_mw":8279,"total_wh":0,"err_code":0}}}
Which is the power consumption of my laptop with full battery. 240 V, 96 mA, 8.2 W. Only problem: 240 V * 96 mA is 23 W, unless the load is highly inductive.

I did a factory reset. Back to HS110 as access point. Easier for me to test (and it won't connect to internet).

With {"netif":{"set_stainfo":{"ssid":"WiFi","password":"123","key_type":3}}} it should be possible to change it back to a normal WiFi device again, without the Kasa app.

Now I look at your code...

Edit: With an old 100 W lightbulb I get: "voltage_mv":237890,"current_ma":446,"power_mw":106165
That looks much better. Bulb design voltage is 230V. A bit higher power is expected thus.
So it gives us the power factor as well.

I find this info quite useful (did not ship with it in paper):
Image
The difference between red and amber is small (hard to see).

BTW. Instead of chr(0)+chr(0)+chr(0)+chr(len("blabla")) you can also do chr(0, 0, 0, len("blabla"))
Last edited by badidea on Sep 23, 2019 20:37, edited 1 time in total.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

I will test that as wel, but read on the sites that this was an issue.
Glad you proved me wrong.

badidea, before you look at my code, this is the latest sofar.

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"
    '=====================================================================
    function encrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = a
             cmd[i] = a
          next
       end if
       return cmd
    end function
    function decrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = cmd[i]
             cmd[i] = a
          next
       end if
       return cmd
    end function
    '=====================================================================
    Dim as integer Cmnd,Cnt
    Dim as String RxData, TxData,cmdStr, Letter
    '=====================================================================
    Input "0 for Off, 1 for On;";Cmnd
    Select Case Cmnd
        Case 0  'Turn Device Off
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":0}}}"
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 1  'Turn Device On
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":1}}}"        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 2  'Turn Device Led Off
            cmdStr = !"{\"system\":{\"set_led_off\":{\"off\":1}}}"
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 3  'Get Device Info
            cmdStr = !"{\"system\":{\"get_sysinfo\":null}}"
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 4
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 5
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 6
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 7
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 8
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 9
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
        Case 10
        
            encrypt(cmdStr)
            TxData = Chr(0)+Chr(0)+Chr(0)+Chr(Len(cmdStr)) + cmdStr
    End Select
    '=====================================================================
    const as string ServerIP   = "192.168.1.145"
    const as ushort ServerPort = 9999
    var client = NetworkClient(ServerIP,ServerPort)
    var connection = client.GetConnection()
    Print "Got Connection = ";Connection
    
    while connection->CanPut()<>1: sleep 100 : wend
    Print "Connection Opened"
    connection->PutData(StrPtr(TxData),Len(TxData))
    Print "Wait for CanGet"
    while connection->CanGet()<>1
      sleep 100
    wend
    
    print "Get Reply"
    dim as zstring ptr buffer
    var nBytes = connection->GetData(buffer)
    print "Received bytes =" & nBytes
    ' get last char position of the HTTP asci header
    var LastChar=instr(*buffer,HeaderEnd)-1
    var Header  =left(*buffer,LastChar)
    ' get first byte behind the HTTP asci header
    var DataStart=LastChar+4
    Print "LastChar=";LastChar,"Header =";Header

    ' save it
    dim as ubyte ptr FileBuffer=@buffer[DataStart]
    print "file saved ..."
    'nBytes = *FileBuffer
    For Cnt = 1 to nBytes
        RxData += Chr(*(FileBuffer + Cnt))
    Next
    decrypt(RxData)
    Do
        Cnt = Instr(RxData,",") 
        If Cnt Then
                Mid(RxData,Cnt) = Chr(&H0A)
            Else
                Exit Do
        EndIf
    Loop
    Print RxData
    If Instr(RxData, "err_code") > 0 Then
        If Instr(RxData,":0}}") > 0 Then
                Print "No Error"
            Else
                Print RxData
        EndIf
    EndIf

    ' free the buffer (allocate by snc.bi)
    deallocate buffer
    sleep
    end
Currently implementing all the commands

Regards
EDIT:
Within FreeBasic I got this. I also have only my laptop on it.(So I can confirm the commands)
{"emeter":{"get_realtime":{"voltage_mv":124334
"current_ma":235
"power_mw":13826
"total_wh":42
"err_code":0}}}�
EDIT: Having difficulty translating command with commas in it.
ie:cmdStr = !"{\"count_down\":{\"add_rule\":{\"enable\":1,"delay\":1800,"act":1,"name":"turn on"}}}"
compile error with delay ?
Last edited by Dinosaur on Sep 23, 2019 20:53, edited 1 time in total.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

May I suggest the following function:

Code: Select all

function TxLenStr(cmdStr as string ) as string
	return chr(0, 0, 0, len(cmdStr)) + cmdStr
end function

dim as string cmdStr = "123456789"
dim as string TxStr = TxLenStr(cmdStr)
Then you can simplify to:

Code: Select all

TxData = TxLenStr(encrypt(cmdStr))
Which you can place after the select case / end select once.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

I usually leave things like that for the fine tuning, BUT good suggestion.
Any suggestions on the conversion of commands with comma's in them ?
ie:cmdStr = !"{\"count_down\":{\"add_rule\":{\"enable\":1,"delay\":1800,"act":1,"name":"turn on"}}}"
compile error with delay ?

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

I think I broke my HS110. The LEDs (WiFi and Power) remain off. Or it switched to 'night-mode'. All else is still working.

More "\":

Code: Select all

!"{\"count_down\":{\"add_rule\":{\"enable\":1,\"delay\":1800,\"act\":1,\"name\":\"turn on\"}}}"
I know, annoying. Also because my IDE is not helping here (all kinds of colors).
A simple JSON parser / generator is needed.

Anyway, I can confirm that your code is working. Timeouts are not included yet I assume?
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

If you have tested my code then the command to turn the led off will do that,
haven't found a command to turn it on again.
Just unplugging I guess.

Thanks for the command, will keep at it.

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

Right, I must have accidentally pressed 2.

!"{\"system\":{\"set_led_off\":{\"off\":0}}}"

Enough for today.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

badidea , when you are ready to test your HS110 a bit further, try the program below.
Tidied up by putting things into subs and implemented 11 commands sofar.
It is in preparation for putting it into a GUI that will allow multiple units with schedules.
Allowing the "Folding" of the subs makes it a bit more readable.
Feel free to improve.

The only surprise is the amount of time it takes to TxRx a command.
Command 4 (Get info) takes 2456 mSec on my laptop.
I suspect it may be trying to communicate with the cloud.
At the moment I haven't got a modem that is not connected to the net, so I cant verify that.
If you do, then try the set Date Time command 11 then we can verify if the HS110 will keep time
without the net.
Purposely not implementing the commands that can do the most damage, but will implement any others outstanding.

Code: Select all

#include once "snc.bi"
#include once "snc_utility.bi"
Dim Shared as integer Cmnd,Cnt,ErrFlag,PrintFlag
Dim Shared as String RxData, TxData,cmdStr
Dim Shared as zstring ptr buffer
const as string ServerIP   = "192.168.1.145"
const as ushort ServerPort = 9999
Type Timing
    CalTime		As  Double              ''Timer is Double, read once at Start of program
    mSec		AS	Ulong               ''These are (Timer - CalTime) * 1000 
    OldmSec		As  Ulong               ''so NOT floating point
    StartTime   As  Ulong
End TYPE
Dim Shared Times As Timing

'=====================================================================
function encrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = a
             cmd[i] = a
          next
       end if
       return cmd
end function
function decrypt(byref cmd as string) as string
       if len(cmd) > 0 then
          dim as ubyte key = 171 'same as -85
          for i as integer = 0 to len(cmd) - 1
             dim as ubyte a = key xor cmd[i]
             key = cmd[i]
             cmd[i] = a
          next
       end if
       return cmd
    end function
function TxLenStr(cmdStr as string )  as string
        return chr(0, 0, 0, len(cmdStr)) + cmdStr
    end function
Sub GetCmnd
    ErrFlag = 0:PrintFlag = 1
    Select Case Cmnd
        Case 0  'Turn Device Off
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":0}}}"
        Case 1  'Turn Device On
            cmdStr = !"{\"system\":{\"set_relay_state\":{\"state\":1}}}"        
        Case 2  'Turn Device Led Off
            cmdStr = !"{\"system\":{\"set_led_off\":{\"off\":1}}}"
        Case 3  'Turn Device Led On
            cmdStr = !"{\"system\":{\"set_led_off\":{\"off\":0}}}"
        Case 4  'Get Device Info
            cmdStr = !"{\"system\":{\"get_sysinfo\":null}}"
        Case 5  'Get Power consumption
            cmdStr = !"{\"emeter\":{\"get_realtime\":{}}}"
        Case 6  'Get next scheduled Action
            cmdStr = !"{\"schedule\":{\"get_next_action\":null}}"
        Case 7  'Get Rule
            cmdStr = !"{\"count_down\":{\"get_rules\":null}}"
        Case 8  'Set Rule
            cmdStr = !"{\"count_down\":{\"add_rule\":{\"enable\":1,\"delay\":18,\"act\":1,\"name\":\"turn on\"}}}"
        Case 9  'Get Time
            cmdStr = !"{\"time\":{\"get_time\":null}}"
        Case 10 'Get Timezone
            cmdStr = !"{\"time\":{\"get_timezone\":null}}"  ' 
        Case 11 'Set Time zone
            Print Date, Time
            Dim as String Year  = Right(Date,4)
            Dim as String Month = Left(Date,2)
            Dim as String Day   = Mid(Date,4,2)
            Dim as String Hour  = Left(Time,2)
            Dim as String Min   = Mid(Time,4,2)
            Dim as String Sec   = Right(Time,2)
            cmdStr = !"{\"time\":  {\"set_timezone\":  {\"year\":Jaar,\"month\":MM,\"mday\":DD,\"hour\":HH,\"min\":mm,\"sec\":ss,\"index\":42}}}"
            Cnt = Instr(cmdStr,"Jaar")
            Mid(cmdStr,Cnt,4) = Year
            Cnt = Instr(cmdStr,"MM")
            Mid(cmdStr,Cnt,2) = Month
            Cnt = Instr(cmdStr,"DD")
            Mid(cmdStr,Cnt,2) = Day
            Cnt = Instr(cmdStr,"HH")
            Mid(cmdStr,Cnt,2) = Hour
            Cnt = Instr(cmdStr,"mm")
            Mid(cmdStr,Cnt,2) = Min
            Cnt = Instr(cmdStr,"ss")
            Mid(cmdStr,Cnt,2) = Sec
        Case Else
            cmdStr = ""
            ErrFlag = 1
    End Select
    If ErrFlag < 1 Then
            If PrintFlag > 0 Then Print cmdStr
            TxData = TxLenStr(encrypt(cmdStr))
            ErrFlag = 0
        Else
            ErrFlag = 1
    EndIf
End Sub 
Sub DataTxRx
    var client = NetworkClient(ServerIP,ServerPort)
    var connection = client.GetConnection()
    while connection->CanPut() <> 1
        sleep 100
    wend
    connection->PutData(StrPtr(TxData),Len(TxData))
    while connection->CanGet() <> 1
      sleep 100
    wend
    var nBytes = connection->GetData(buffer)
    var LastChar=instr(*buffer,HeaderEnd)-1
    var Header  =left(*buffer,LastChar)
    var DataStart=LastChar+4
    dim as ubyte ptr FileBuffer=@buffer[DataStart]
    For Cnt = 1 to nBytes
        RxData += Chr(*(FileBuffer + Cnt))
    Next
    decrypt(RxData)
        Do
        Cnt = Instr(RxData,",") 
        If Cnt Then
                Mid(RxData,Cnt) = Chr(&H0A)
            Else
                Exit Do
        EndIf
    Loop
    Print RxData
    If Instr(RxData, "err_code") > 0 Then
        If Instr(RxData,":0}}") > 0 Then
                Print "No Error"
            Else
                Print RxData
        EndIf
    EndIf
End Sub

    Times.CalTime = Timer           ''Double Integer x 1000 = mSec as Start of Program Time
    Print "Menu"
    Print "0 = Device O/P Off"
    Print "1 = DEvice O/P On"
    Print "2 = Led Off"
    Print "3 = Led On"
    Print "4 = Get Device information"
    Print "5 = Get Power consumption"
    Print "6 = Get next scheduled action"
    Print "7 = Get Rule"
    Print "8 = Set Rule"
    Print "9 = Get Time"
    Print "10= Get Time zone"
    Print "11= Set Date, Time from PC"
    Input "Selection + Enter;";Cmnd
    GetCmnd
    If ErrFlag < 1 Then
        With Times
            .StartTime  = (Timer - .CalTime) * 1000
            DataTxRx
            .mSec = (Timer - .CalTime) * 1000
            Print "Elapsed Time ="; .mSec - .StartTime
        End With
    EndIf

    deallocate buffer
    end
Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

Dinosaur wrote: Command 4 (Get info) takes 2456 mSec on my laptop.
I suspect it may be trying to communicate with the cloud.
About the same timing here. My suspect is SNC. Python code is instant reply.

In snc.bi, NetworkConnection.GetData(), change
dim as integer _timeout = 2000
to e.g:
dim as integer _timeout = 200
Because the HS110 data easily fits in the CHUNK_SIZE (1024 * 8), this waiting time is not relevant anyway I think.

BTW: https://freebasic.net/forum/viewtopic.p ... 30#p213332 (last few lines of your post and the posts that follow :-)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: TP-Link HS110

Post by Dinosaur »

Hi All

badidea, it looks my past posts came back to haunt me.
Reading it reminds me of a saying:
"My Memory bucket is Full and to remember more
something has to give"

That's my excuse anyway.

Will do some more reading and testing.
Thanks for finding that.

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: TP-Link HS110

Post by badidea »

I think imple [N]etwork [C]onnection isn't that simple. For this purpose, the network code can be less complex.
Post Reply