From f7e81b4573547f4f0abcfdf7e06c620909d912a5 Mon Sep 17 00:00:00 2001 From: marcelbuesing Date: Wed, 17 Oct 2018 22:09:15 +0200 Subject: [PATCH] Migrate example to influent v0.5 --- Cargo.toml | 4 ++- examples/influx_client.rs | 55 ++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 13bf53a..53d6c29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,8 @@ log = "0.4" [dev-dependencies] env_logger = "0.5" +futures = "0.1" i2cdev = "0.4" -influent = "0.4" +influent = "0.5" linux-embedded-hal = "0.2" +tokio = "0.1" diff --git a/examples/influx_client.rs b/examples/influx_client.rs index bfac3ab..1587bd9 100644 --- a/examples/influx_client.rs +++ b/examples/influx_client.rs @@ -5,19 +5,19 @@ /// extern crate bme680; -extern crate embedded_hal; extern crate env_logger; +extern crate futures; extern crate linux_embedded_hal; extern crate influent; +extern crate tokio; -use bme680::*; -use embedded_hal::blocking::i2c; +use bme680::{Bme680, OversamplingSetting, IIRFilterSize, I2CAddress, SettingsBuilder, PowerMode, FieldDataCondition}; +use futures::Future; use linux_embedded_hal::*; -use std::thread; use std::time::Duration; use influent::create_client; -use influent::client::{Client, Credentials}; +use influent::client::{Client, Credentials, ClientError}; use influent::measurement::{Measurement, Value}; const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086"; @@ -25,14 +25,12 @@ const INFLUX_USER: &str = "user"; const INFLUX_PASSWORD: &str = "pass"; const INFLUX_DATABASE: &str = "influxdb"; -type I2CResult = std::result::Result<(), Error<::Error, ::Error>>; - -fn main() -> I2CResult +fn main() -> Result<(), ()> { // Init device let i2c = I2cdev::new("/dev/i2c-1").unwrap(); - let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?; + let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary).map_err(|e| eprintln!("Init failed: {:?}", e))?; let settings = SettingsBuilder::new() .with_humidity_oversampling(OversamplingSetting::OS2x) @@ -42,7 +40,7 @@ fn main() -> I2CResult .with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_run_gas(true) .build(); - dev.set_sensor_settings(settings)?; + dev.set_sensor_settings(settings).map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?; // Set up Influx client let credentials = Credentials { @@ -54,34 +52,37 @@ fn main() -> I2CResult let hosts = vec![INFLUX_ADDRESS]; let client = create_client(credentials, hosts); - loop { - dev.set_sensor_mode(PowerMode::ForcedMode)?; - let (data, state) = dev.get_sensor_data()?; + dev.set_sensor_mode(PowerMode::ForcedMode).map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?; + let (data, state) = dev.get_sensor_data().map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?; - println!("State {:?}", state); - println!("Temperature {}°C", data.temperature_celsius()); - println!("Pressure {}hPa", data.pressure_hpa()); - println!("Humidity {}%", data.humidity_percent()); - println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); + println!("State {:?}", state); + println!("Temperature {}°C", data.temperature_celsius()); + println!("Pressure {}hPa", data.pressure_hpa()); + println!("Humidity {}%", data.humidity_percent()); + println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); - if state == FieldDataCondition::NewData { + if state != FieldDataCondition::NewData + { + let temperature_f = send_value(&client, "temperature" ,Value::Float(data.temperature_celsius() as f64)); + let pressure_f = send_value(&client, "pressure" ,Value::Float(data.pressure_hpa() as f64)); + let humidity_f = send_value(&client, "humidity" ,Value::Float(data.humidity_percent() as f64)); + let gas_f = send_value(&client, "gasresistence" , Value::Float(data.gas_resistance_ohm() as f64)); - send_value(&client, "temperature" ,Value::Float(data.temperature_celsius() as f64)); - send_value(&client, "pressure" ,Value::Float(data.pressure_hpa() as f64)); - send_value(&client, "humidity" ,Value::Float(data.humidity_percent() as f64)); - send_value(&client, "gasresistence" , Value::Float(data.gas_resistance_ohm() as f64)); - } - thread::sleep(Duration::from_millis(5000)); + let f = temperature_f.join4(pressure_f, humidity_f, gas_f).map(|_| ()).map_err(|e| eprintln!("Error: {:?}", e)); + + let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); + return rt.block_on(f); } + Ok(()) } /// Sends a measured value to the influx database -fn send_value(client:&Client, type_name: &str, value: Value) { +fn send_value(client:&Client, type_name: &str, value: Value) -> impl Future { let mut measurement = Measurement::new("sensor"); measurement.add_field("value", value); measurement.add_tag("id", "MAC"); measurement.add_tag("name", "bme680"); measurement.add_tag("type", type_name); - client.write_one(measurement, None).unwrap(); + client.write_one(measurement, None) }