Sub SetRelay(RelayNumber As Int, RelayState As Int)
Dim NumRelays As Int = 8
If RelayNumber < 1 or RelayNumber > NumRelays Then
Log("Relay " & RelayNumber & "? Really?") 'help me help you
Return
End If
'protocol per "documentation" found at: https://www.aliexpress.com/item/32892267055.html
'Control instruction(Hexadecimal format):
'Channel 1 ON: A00101A2
'Channel 1 OFF: A00100A1
'Channel 2 ON: A00201A3
'Channel 2 OFF: A00200A2
Dim Cmd(4) As Byte
'Sync/Header = A0 (hexadecimal) = 160 (decimal)
Cmd(0) = 0xA0
'Relay Number : 01 = first relay, 02 = second relay (and etc for > 2 relays, presumably)
Cmd(1) = relayNumber
'Relay State : 00 = off, 01 = on
Cmd(2) = Min(Abs(RelayState), 1) '= 0 if RelayState 0, otherwise = 1
'Checksum = sum of preceding bytes (eg, Channel 2 OFF = A0 + 02 + 00 = A2)
Cmd(3) = Cmd(0) + Cmd(1) + Cmd(2)
manager.WriteData(BLEService, BLECharacteristic, Cmd)
End Sub
'and perhaps these two helper functions (not necessary, just nice):
Sub SetRelayOff(RelayNumber As Int)
SetRelay(RelayNumber, 0)
End If
Sub SetRelayOn(RelayNumber As Int)
SetRelay(RelayNumber, 1)
End If