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