Android Question Philips Hue, Python, B4A

Andie

Member
Licensed User
Longtime User
It's quite simple to turn on lights of the Philips Hue system with Python by using the Hue-Api:
B4X:
url = 'http://192.168.178.31/api/' + username + '/lights/1/state'
status= '{"on":true}'
requests.put(url, data=status)
The string variable username contains my Api-ID.
I was trying to transfer the code from Python to B4A by using the download2 method of the HttpJob object:
B4X:
Dim myJob As HttpJob
myJob.Initialize("", Me)
url = "http://192.168.178.31/api/" & username & "/lights/1/state"
myJob.Download2(url, Array As String("on", "true"))
But this doesn't work.... The question is: What is the B4A equivalent of Python's requests.put?
 

DonManfred

Expert
Licensed User
Longtime User
data must be a json-String

See https://www.developers.meethue.com/documentation/getting-started for more details.

 
Upvote 0

Andie

Member
Licensed User
Longtime User
Manfred's solution works perfectly! Wow - I wouldn't have thought to transfer the data in json-format. Thanks a lot!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Now, as i now know how to "generate" the username needed (even for the Java SDK), i come further with the JavaSDK.
Till yesterday i stuck at a point...

But now

B4X:
Sub HUE_onBridgeConnected(bridge As PHBridge, info As String)
    Log($"HUE_onBridgeConnected(${bridge},${info})"$)
    BridgeSelected = bridge
    sdk.addBridge(bridge)
 
    Dim groups As List
    groups = bridge.AllGroups
    If groups.IsInitialized Then
        If groups.Size > 0 Then
            For i = 0 To groups.Size-1
                Dim group As PHGroup = groups.Get(i)
                Log($"Group = ${group.Name} / Type = ${group.GroupType} UniqueID = ${group.UniqueId}"$)
            Next
        End If
    End If
 
 
    Dim lights As List
    lights = bridge.AllLights
    If lights.IsInitialized Then
        If lights.Size > 0 Then
            For i = 0 To lights.Size-1
                Dim light As PHLight = lights.Get(i)
                Log($"Light = ${light.Identifier} / Manufacturer = ${light.ManufacturerName} Model ${light.ModelNumber} LightType = ${light.LightType}"$)
            Next
        End If
    End If


Rules, Schedules and Timersare not yet wrapped....

ETA: Sensors, Scenes, Sensors wrapped too
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…