AdyRock / SwitchBotBLEHub

Code for a ESP32, Arduino compatible board to make into to a network local hub for SwichBot BLE devices.
GNU General Public License v3.0
10 stars 0 forks source link

Sending HTTP requests for controlling Switchbot Bot #7

Open ix300 opened 2 months ago

ix300 commented 2 months ago

Hello!

Could it be possible to send HTTP requests to the SwitchBot BLE / Network Hub (using Bitfocus Companion) to control Switchbot Bot? Could the following example for turning a Bot on get modified for the SwitchBot BLE / Network Hub?

Request POST https://api.switch-bot.com/v1.1/devices/210/commands { "command": "turnOn", "parameter": "default", "commandType": "command" } Response { "statusCode": 100, "body": {}, "message": "success" }

I have already learned from another post, that it's possible to get feedback from Switchbot using HTTP GET, which is working great with Bitfocus Companion generic HTTP module: http://my.IP.address/api/v1/device?address=Switchbot:BLE:MAC:address

Thank you in advance!

AdyRock commented 2 months ago

The format for controlling device is written to: [http://my.IP.address/api/v1/device](http://my.ip.address/api/v1/device/write It uses a POST with a a JSON body that contains:

{
  address: MAC_Address_of_device,
  data: array_of_bytes_to_write,
}

So you would need to encode the required parameters into the array_of_bytes_to_write. For a bot this would be: [0x57, 0x01, 0x01] to turn it on and [0x57, 0x01, 0x02] to turn it off

JochenKr commented 2 months ago

I would also like to control a Switchbot curtain by http. The example above "http://my.ip.address/api/v1/device?address=Switchbot:BLE:MAC:address", works fine for me and I get a nice feedback. Unluckily I do not exactly understand the post above concerning the write. Could you kindly give a noob example?

AdyRock commented 2 months ago

I can understand the confusion as the data is in the format required by the BLE device. If it helps the BLE is defined here https://github.com/OpenWonderLabs/SwitchBotAPI-BLE

So the data required for the curtain is [0x57, 0x0f, 0x45, 0x01, 0x05, mode, percent] Where: mode is 0x00 for fast, 0x01 for slow or 0xFF for last used speed. percent is the percentage opening between 0 and 100 converted to hex (0x00 to 0x64)

Hope that helps.

JochenKr commented 2 months ago

Thanks for sharing the details. actually I'm struggling on a more noobie point already. how to get that JSON body into a simple url I can call. Or isn't that possible at all, as I was also not able to find a quick answer on google. e.g. (of course not working) http://my.IP.address/api/v1/device/write/address=Switchbot:BLE:MAC:address?data=[0x57,0x0f,0x45,0x01,0x05,0x00,0x7F]

AdyRock commented 2 months ago

You have to create a POST call to the address and pass the JSON data via the body. What are you using to write to the hub?

For testing, I use a program call ARC, but I believe Postman is very similar. In ARC I would set it up like this: image

As you can see, I have had to convert the HEX to decimal for the array.

As far as the JSON object is concerned, it is converted to text for the body in most library's and the header is set to JSON: image

So, if you are using js http then you need something like:

const httpOptions = {
    host: HubAddress,
    path: `/api/v1/${url}`,
    method: 'POST',
    headers:
    {
        contentType: 'application/json; charset=utf-8',
        'Content-Length': bodyText.length,
        connection: 'Close',
    },
};
const req = http.request(httpOptions, (res);
req.write('{"address": "Switchbot:BLE:MAC:address","data": [87,15,69,1,5,0,127]}');
req.end();
JochenKr commented 2 months ago

Thanks for your very detailed guidance. Working fine now

ix300 commented 1 month ago

Thank you very much for your kind help! And actually the comments about the Switchbot curtain were very useful for me too!