Migrate example to influent v0.5

This commit is contained in:
marcelbuesing 2018-10-17 22:09:15 +02:00
parent 30d6588a19
commit f7e81b4573
No known key found for this signature in database
GPG key ID: 03F06F8179D5549D
2 changed files with 31 additions and 28 deletions

View file

@ -14,6 +14,8 @@ log = "0.4"
[dev-dependencies] [dev-dependencies]
env_logger = "0.5" env_logger = "0.5"
futures = "0.1"
i2cdev = "0.4" i2cdev = "0.4"
influent = "0.4" influent = "0.5"
linux-embedded-hal = "0.2" linux-embedded-hal = "0.2"
tokio = "0.1"

View file

@ -5,19 +5,19 @@
/// ///
extern crate bme680; extern crate bme680;
extern crate embedded_hal;
extern crate env_logger; extern crate env_logger;
extern crate futures;
extern crate linux_embedded_hal; extern crate linux_embedded_hal;
extern crate influent; extern crate influent;
extern crate tokio;
use bme680::*; use bme680::{Bme680, OversamplingSetting, IIRFilterSize, I2CAddress, SettingsBuilder, PowerMode, FieldDataCondition};
use embedded_hal::blocking::i2c; use futures::Future;
use linux_embedded_hal::*; use linux_embedded_hal::*;
use std::thread;
use std::time::Duration; use std::time::Duration;
use influent::create_client; use influent::create_client;
use influent::client::{Client, Credentials}; use influent::client::{Client, Credentials, ClientError};
use influent::measurement::{Measurement, Value}; use influent::measurement::{Measurement, Value};
const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086"; 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_PASSWORD: &str = "pass";
const INFLUX_DATABASE: &str = "influxdb"; const INFLUX_DATABASE: &str = "influxdb";
type I2CResult = std::result::Result<(), Error<<I2cdev as i2c::Read>::Error, <I2cdev as i2c::Write>::Error>>; fn main() -> Result<(), ()>
fn main() -> I2CResult
{ {
// Init device // Init device
let i2c = I2cdev::new("/dev/i2c-1").unwrap(); 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() let settings = SettingsBuilder::new()
.with_humidity_oversampling(OversamplingSetting::OS2x) .with_humidity_oversampling(OversamplingSetting::OS2x)
@ -42,7 +40,7 @@ fn main() -> I2CResult
.with_gas_measurement(Duration::from_millis(1500), 320, 25) .with_gas_measurement(Duration::from_millis(1500), 320, 25)
.with_run_gas(true) .with_run_gas(true)
.build(); .build();
dev.set_sensor_settings(settings)?; dev.set_sensor_settings(settings).map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
// Set up Influx client // Set up Influx client
let credentials = Credentials { let credentials = Credentials {
@ -54,9 +52,8 @@ fn main() -> I2CResult
let hosts = vec![INFLUX_ADDRESS]; let hosts = vec![INFLUX_ADDRESS];
let client = create_client(credentials, hosts); let client = create_client(credentials, hosts);
loop { dev.set_sensor_mode(PowerMode::ForcedMode).map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?;
dev.set_sensor_mode(PowerMode::ForcedMode)?; let (data, state) = dev.get_sensor_data().map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?;
let (data, state) = dev.get_sensor_data()?;
println!("State {:?}", state); println!("State {:?}", state);
println!("Temperature {}°C", data.temperature_celsius()); println!("Temperature {}°C", data.temperature_celsius());
@ -64,24 +61,28 @@ fn main() -> I2CResult
println!("Humidity {}%", data.humidity_percent()); println!("Humidity {}%", data.humidity_percent());
println!("Gas Resistence {}Ω", data.gas_resistance_ohm()); 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)); let f = temperature_f.join4(pressure_f, humidity_f, gas_f).map(|_| ()).map_err(|e| eprintln!("Error: {:?}", e));
send_value(&client, "pressure" ,Value::Float(data.pressure_hpa() as f64));
send_value(&client, "humidity" ,Value::Float(data.humidity_percent() as f64)); let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
send_value(&client, "gasresistence" , Value::Float(data.gas_resistance_ohm() as f64)); return rt.block_on(f);
}
thread::sleep(Duration::from_millis(5000));
} }
Ok(())
} }
/// Sends a measured value to the influx database /// 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<Item=(), Error=ClientError> {
let mut measurement = Measurement::new("sensor"); let mut measurement = Measurement::new("sensor");
measurement.add_field("value", value); measurement.add_field("value", value);
measurement.add_tag("id", "MAC"); measurement.add_tag("id", "MAC");
measurement.add_tag("name", "bme680"); measurement.add_tag("name", "bme680");
measurement.add_tag("type", type_name); measurement.add_tag("type", type_name);
client.write_one(measurement, None).unwrap(); client.write_one(measurement, None)
} }