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
Probably
B4X:
job.putstring("http://192.168.178.31/api/" & username & "/lights/1/state",$"{"on":true}"$)
 
Upvote 0

DonManfred

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

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

Now let’s do something a bit more fun and start changing some colors. Enter the command below.

Address http://<bridge ip address>/api/1028d66426293e821ecfd9ef1a0731df/lights/1/state
Body {"on":true, "sat":254, "bri":254,"hue":10000}
Method PUT
We’re interacting with the same “state” attributes here but now we’re modifying a couple more attributes. We’re making sure the light is on by setting the “on” resource to true. We’re also making sure the saturation (intensity) of the colors and the brightness is at its maximum by setting the “sat” and “bri” resources to 254. Finally we’re telling the system to set the “hue” (a measure of color) to 10000 points (hue runs from 0 to 65535). Try changing the hue value and keep pressing the PUT button and see the colour of your light changing running through different colors.
 
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 :D

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

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
HUE_onBridgeConnected(com.philips.lighting.hue.sdk.bridge.impl.PHBridgeImpl@2b85daa,ofmxXcuFVe0okMRIxcyaSQRMtqTFBS9ANBjfCboI)
Group = Flur / Type = Room UniqueID =
Group = Küche / Type = Room UniqueID =
Group = Wohnzimmer / Type = Room UniqueID =
Group = Schlafzimmer / Type = Room UniqueID =
Group = Bad / Type = Room UniqueID =
Light = 5 / Manufacturer = Philips Model LWB010 LightType = DIM_LIGHT
Light = 4 / Manufacturer = Philips Model LWB010 LightType = DIM_LIGHT
Light = 1 / Manufacturer = Philips Model LWB010 LightType = DIM_LIGHT
Light = 3 / Manufacturer = Philips Model LWB010 LightType = DIM_LIGHT
Light = 6 / Manufacturer = Philips Model LWB010 LightType = DIM_LIGHT
Light = 2 / Manufacturer = Philips Model LWB004 LightType = DIM_LIGHT

Rules: (ArrayList) [com.philips.lighting.model.rule.PHRule@7d97f7eb, com.philips.lighting.model.rule.PHRule@24eb6ea4, com.philips.lighting.model.rule.PHRule@cc0527d2, com.philips.lighting.model.rule.PHRule@42c49574, com.philips.lighting.model.rule.PHRule@d259d155, com.philips.lighting.model.rule.PHRule@4ba94581, com.philips.lighting.model.rule.PHRule@ead69f0a, com.philips.lighting.model.rule.PHRule@8ed2815b, com.philips.lighting.model.rule.PHRule@516a249e, com.philips.lighting.model.rule.PHRule@48e87c42, com.philips.lighting.model.rule.PHRule@b4131f53, com.philips.lighting.model.rule.PHRule@9687499b, com.philips.lighting.model.rule.PHRule@9e3c11db]

Scenes: (ArrayList) [com.philips.lighting.model.PHScene@ebf18d58, com.philips.lighting.model.PHScene@56a44e81, com.philips.lighting.model.PHScene@a611b1b0, com.philips.lighting.model.PHScene@59ff3da0, com.philips.lighting.model.PHScene@2bfac12, com.philips.lighting.model.PHScene@6e339dff, com.philips.lighting.model.PHScene@aaffd220, com.philips.lighting.model.PHScene@a7e357bd, com.philips.lighting.model.PHScene@e05ad0fb, com.philips.lighting.model.PHScene@2a1c1a1f, com.philips.lighting.model.PHScene@bb81305e, com.philips.lighting.model.PHScene@8e07cba9, com.philips.lighting.model.PHScene@dfe59c59, com.philips.lighting.model.PHScene@a25062f0, com.philips.lighting.model.PHScene@4ef19628]

Sensors: (ArrayList) [com.philips.lighting.model.sensor.PHDaylightSensor@15fd2648, com.philips.lighting.model.sensor.PHSwitch@6c859b2a, com.philips.lighting.model.sensor.PHGenericStatusSensor@884b26b3]
AllShedules: (ArrayList) []
AllTimers: (ArrayList) []
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **

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

ETA: Sensors, Scenes, Sensors wrapped too
 
Last edited:
Upvote 0
Top