OxidESPark is a Rust library for the Rust ESP Board embedding an ESP32-C3 microcontroller (RISC-V). It uses the ESP-IDF framework and provides tools to easily build applications that interact with the physical world.
Its two main goals are:
Setting the configuration is the very first step that anyone whishing to use OxidESPark must do.
As no file system is available on a microcontroller, OxidESPark requires the use of the include_str
macro to embed the TOML configuration file:
```Rust use oxidesparkutils::config::Config;
Config::set(include_str!("OxideSparkConfig.toml")); ```
```Rust use espidfhal::peripherals::Peripherals; use oxide_spark::{network::i2c::I2c,sensors::Sensors};
let peripherals = Peripherals::take().unwrap();
let i2c = I2c::init( peripherals.i2c0, peripherals.pins.gpio8, peripherals.pins.gpio10, ).unwrap();
Sensors::init(i2c, &datasender, &rgbled).unwrap(); ```
After the init()
call, the sensors automatically start sending data to the given data_sender
(see MQTT). If a measure error occurs, the provided RGB LED (see supported devices) turns black for a few miliseconds.
toml
[[sensors]]
uuid = "sensor_uuid"
name = "temp_and_humidity_sensor"
kind = "Shtc3"
metrics = { "Temperature" = "t","Humidity" = "h" } # Each metric has its own identifier
sample_div = 1 # Optional, a value of n means that only 1 out of n samples will be taken
Coming soon:
Each external sensor must be connected to the board's I2C bus (SCL and SDA pins are respectively GPIO 8 and 10).
toml
[[sensors]]
uuid = "sensor_uuid"
name = "light_sensor"
kind = "Tsl2561"
metrics = { "InfraredLight" = "il", "VisibleInfraredLight" = "vil" }
sample_div = 1
Coming soon:
```Rust use oxidespark::{color::Color::{Green, Red}, models::ledstrip::LedStripBuilder}; use oxidesparkutils::ledstripmode::{LedStripMode};
let rgb_led = LedStripBuilder::new() .channel(0) .gpio(2) .length(1) .mode(LedStripMode::Full(Red)) .build() .unwrap() .init() .unwrap();
rgb_led.send(LedStripMode::Full(Green)).unwrap(); ```
See LedStripMode for a list of available modes.
Wifi connection is available (though optional) through the TOML configuration file :
toml
[wifi]
ssid = "wifi_ssid"
pwd = "wifi_password"
auth_method = "wifi_authentication_method" # Optional, defaults to wpa2personal
timeout = 8 # Optional, defaults to 10 seconds
See AuthMethod for a list of available authentication methods.
```Rust use espidfhal::peripherals::Peripherals; use oxide_spark::network::wifi::Wifi;
let peripherals = Peripherals::take().unwrap();
Wifi::init(peripherals.modem).unwrap(); ```
After the init()
call, the Wifi connection is established. If a timeout occurs, the device automatically reboots.
MQTT connection is also available (and optional too). It requires a Wifi connection to be established first and is also configured through the TOML configuration file:
toml
[mqtt]
ip = "1.2.3.4"
port = 1883
cmd_topic = "command"
data_topic = "esps/sensors/metrics"
A broker must of course be running on the given IP and port. The cmd_topic
is used to receive commands from the broker while the data_topic
is used to send data to the broker.
```Rust use oxide_spark::network::mqtt::Mqtt;
let (datasender, cmdreceiver) = Mqtt::init().unwrap(); ```
data_sender
is the sending half of a sync channel used to send data (namely, sensor measures) while cmd_receiver
is the receiving half of a sync channel used to receive remote commands.
Enable the use of ESP logging facilities through the log crate. See LogLevel for a list of available levels.
toml
[esp]
uuid = "esp_uuid"
name = "esp_name"
log_level = "Error"
```Rust use log::{error, info}; use oxide_spark::utils::log::Log;
Log::init()?;
info!("Hello, world!"); info!("Configuration:\n{:#?}", *CONFIG); ```