Giter Site home page Giter Site logo

nicholascioli / rbroadlink Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 5.0 77 KB

A broadlink library written in Rust. Based on https://github.com/mjg59/python-broadlink

Home Page: https://crates.io/crates/rbroadlink

License: MIT License

Rust 96.45% Nix 3.55%

rbroadlink's Introduction

rbroadlink

Crates.io Crates.io

A rust port of the python-broadlink library.

Devices Tested

The following devices have been tested and found to work with this library:

Model Code Device Name Manufacturer Type
0x649B RM4 Pro Broadlink Remote
0x4E2A Ande Jupiter+ ANG Klimatyzacja Sp. z o.o. Hvac

Setup

Before a device can be used, it must be connected to a network. Refer to this link on how to get the device into AP mode, connect to its network (e.g. Broadlink_Device_Wifi), and then run the following code:

use rbroadlink::Device;
use rbroadlink::network::WirelessConnection;

// Construct the network information
let network_info = WirelessConnection::WPA2(
    "SSID Here",
    "Password here",
);

// Connect the device to the specified network
Device::connect_to_network(&network_info)
    .expect("Could not connect the device to the network!");

You can also use the included cli to do so:

# Pass the password directly
cargo run --example rbroadlink-cli -- connect wpa2 "SSID Here" "Password here"

# Prompt for the password safely
cargo run --example rbroadlink-cli -- connect -p wpa2 "SSID Here"

Usage

Devices can either be constructed from a known IP or by local discovery:

use std::net::Ipv4Addr;
use rbroadlink::Device;

// Create a device by IP
// Note: Devices only support Ipv4 addresses
let known_ip = Ipv4Addr::new(1, 2, 3, 4);
let device = Device::from_ip(known_ip, None)
    .expect("Could not connect to device!");

// You can also specify the local IP of the machine in the case of the device being
// on a different subnet.
let local_ip = Ipv4::new(9, 8, 7, 6);
let device_with_local_ip = Device::from_ip(known_ip, Some(local_ip))
    .expect("Could not connect to device!");

// You can also just enumerate all of the discovered devices, with an optional
// local ip as well.
let devices = Device::list(Some(local_ip))
    .expect("Could not enumerate devices!");

Once you have a valid device, you probably want to differentiate the kind of device that you have. Device is a structured enum that contains different types of devices with more specialized methods.

use rbroadlink::Device;

// Assuming that you have a valid device in `device`...
let remote_device = match device {
    Device::Remote { remote } => remote,
    _ => return Err("Not a remote!"),
};

// Use a remote-specific method to echo a learned IR code.
let code = remote_device.learn_ir()
    .expect("Could not learn IR code!");
remote_device.send_code(&code)
    .expect("Could not send code!");

HVAC

Starting from version 0.4.0 of this library the HVAC/Air Conditioners support was added. Supported devices are broadlink device type 0x4E2A.

Although it was tested on one specific unit, it is very likely that it should work with more similar devices. Those air conditioners are usually controlled with AC Freedom application.

If you have such device connected to the AC Freedom application, then it is surely in a "locked" state (cannot be controlled using this library).

You can control it either from AC Freedom or this library (not both). If you decide to use rbroadlink, then you need to delete the device from AC Freedom cloud, then reset the WiFi dongle and re-configure WiFi parameters again.

You can also head to this post for details: liaan/broadlink_ac_mqtt#76 (comment)

Probably configuring the WiFi parameters using this library/rbroadlink-cli should also work (refer to the Setup section above).

A sample snippet for setting target temperature setpoint:

use rbroadlink::Device;

// Assuming that you have a valid device in `device`...
let hvac_device = match device {
    Device::Hvac { hvac } => hvac,
    _ => return Err("Not a HVAC device!"),
};

// First obtain current state/parameters of the device:
let mut state = hvac_device.get_state().expect("Cannot obtain current state");
println!("Current state: {:?}", state);

// Print current temperature and try to set a new setpoint (degree Celsius)
println!("Target temp: {:.1}", state.get_target_temp());
if let Err(e) = state.set_target_temp(22.0) {
    println!("Error setting temperature: {}", e);
}

// Request to set a new state (with new temperature)
hvac_device.set_state(&mut state);

Examples

There are a few examples of this library present in the examples folder. Refer to the examples folder README for more info.

rbroadlink's People

Contributors

nicholascioli avatar manio avatar wazner avatar

Stargazers

Laco Skokan avatar Tas avatar Sebastian Mandrean avatar

Watchers

 avatar James Cloos avatar  avatar

rbroadlink's Issues

Not enough data with RM4 pro

Hello,

I am testing with RM4pro and I am getting this error:

cargo run --example rbroadlink-cli --features="rbroadlink-cli" info 10.0.10.32
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/examples/rbroadlink-cli info 10.0.10.32`
Getting information for device at 10.0.10.32
thread 'main' panicked at examples/rbroadlink-cli.rs:229:55:
Could not connect to device!: "Could not communicate with specified device! Received invalid response! Not enough data."
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Looking inside the lib, my broadlink is sending 146 bytes instead of 128. When changing the condition here https://github.com/nicholascioli/rbroadlink/blob/main/src/device.rs#L202 to be if bytes_received < 128 { and getting just first 128 bytes it seems to all work. But I don't know how to fix properly. Thanks

Error with locked broadlink RM4 pro

I have a Broadlink RM4 Pro and I am getting this error with example:

cargo run --features="rbroadlink-cli" --example rbroadlink-cli info 10.0.10.32
warning: unused manifest key: target.cfg(target_os = "macos").rustflags
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/examples/rbroadlink-cli info 10.0.10.32`
Getting information for device at 10.0.10.32
thread 'main' panicked at /Users/lskokan/.cargo/registry/src/index.crates.io-6f17d22bba15001f/block-padding-0.2.1/src/lib.rs:101:21:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.