Migrate influx example to influx_db_client crate and tokio 0.2
This commit is contained in:
parent
97d9f5ea81
commit
9df17c592d
3 changed files with 49 additions and 78 deletions
|
@ -19,9 +19,9 @@ log = "0.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
futures = { version = "0.3", features = ["compat"] }
|
futures = { version = "0.3" }
|
||||||
futures-timer = "3.0"
|
|
||||||
i2cdev = "0.4"
|
i2cdev = "0.4"
|
||||||
influent = "0.5"
|
influx_db_client = { version = "0.4", default-features= false, features = ["rustls-tls"] }
|
||||||
linux-embedded-hal = "0.3"
|
linux-embedded-hal = "0.3"
|
||||||
tokio = {version = "0.2", features = ["full"] }
|
tokio = {version = "0.2", features = ["full"] }
|
||||||
|
url = "2.1"
|
||||||
|
|
|
@ -3,24 +3,15 @@
|
||||||
/// continously send them to an influx database.
|
/// continously send them to an influx database.
|
||||||
/// Make sure you adapt the influx constants and likely also the i2c device id and I2CAddress.
|
/// Make sure you adapt the influx constants and likely also the i2c device id and I2CAddress.
|
||||||
///
|
///
|
||||||
extern crate bme680;
|
|
||||||
extern crate env_logger;
|
|
||||||
extern crate futures;
|
|
||||||
extern crate influent;
|
|
||||||
extern crate linux_embedded_hal;
|
|
||||||
extern crate tokio;
|
|
||||||
|
|
||||||
use crate::futures::compat::Future01CompatExt;
|
|
||||||
use bme680::{
|
use bme680::{
|
||||||
Bme680, FieldDataCondition, I2CAddress, IIRFilterSize, OversamplingSetting, PowerMode,
|
Bme680, FieldDataCondition, I2CAddress, IIRFilterSize, OversamplingSetting, PowerMode,
|
||||||
SettingsBuilder,
|
SettingsBuilder,
|
||||||
};
|
};
|
||||||
use futures::prelude::*;
|
use influx_db_client::{points, Client, Point, Points, Precision, Value};
|
||||||
use influent::client::{Client, ClientError, Credentials};
|
|
||||||
use influent::create_client;
|
|
||||||
use influent::measurement::{Measurement, Value};
|
|
||||||
use linux_embedded_hal::*;
|
use linux_embedded_hal::*;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use tokio::time::delay_for;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086";
|
const INFLUX_ADDRESS: &str = "http://127.0.0.1:8086";
|
||||||
const INFLUX_USER: &str = "user";
|
const INFLUX_USER: &str = "user";
|
||||||
|
@ -45,69 +36,54 @@ async fn main() -> Result<(), ()> {
|
||||||
dev.set_sensor_settings(settings)
|
dev.set_sensor_settings(settings)
|
||||||
.map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
|
.map_err(|e| eprintln!("Setting sensor settings failed: {:?}", e))?;
|
||||||
|
|
||||||
// Set up Influx client
|
let client = Client::new(Url::parse(INFLUX_ADDRESS).unwrap(), INFLUX_DATABASE)
|
||||||
let credentials = Credentials {
|
.set_authentication(INFLUX_USER, INFLUX_PASSWORD);
|
||||||
username: INFLUX_USER,
|
|
||||||
password: INFLUX_PASSWORD,
|
|
||||||
database: INFLUX_DATABASE,
|
|
||||||
};
|
|
||||||
|
|
||||||
let hosts = vec![INFLUX_ADDRESS];
|
loop {
|
||||||
let client = create_client(credentials, hosts);
|
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))?;
|
||||||
|
|
||||||
dev.set_sensor_mode(PowerMode::ForcedMode)
|
println!("State {:?}", state);
|
||||||
.map_err(|e| eprintln!("Setting sensor mode failed: {:?}", e))?;
|
println!("Temperature {}°C", data.temperature_celsius());
|
||||||
let (data, state) = dev
|
println!("Pressure {}hPa", data.pressure_hpa());
|
||||||
.get_sensor_data()
|
println!("Humidity {}%", data.humidity_percent());
|
||||||
.map_err(|e| eprintln!("Retrieving sensor data failed: {:?}", e))?;
|
println!("Gas Resistence {}Ω", data.gas_resistance_ohm());
|
||||||
|
|
||||||
println!("State {:?}", state);
|
if state == FieldDataCondition::NewData {
|
||||||
println!("Temperature {}°C", data.temperature_celsius());
|
let temperature_f = ipoint(
|
||||||
println!("Pressure {}hPa", data.pressure_hpa());
|
"temperature",
|
||||||
println!("Humidity {}%", data.humidity_percent());
|
Value::Float(data.temperature_celsius() as f64),
|
||||||
println!("Gas Resistence {}Ω", data.gas_resistance_ohm());
|
);
|
||||||
|
let pressure_f = ipoint("pressure", Value::Float(data.pressure_hpa() as f64));
|
||||||
|
let humidity_f = ipoint("humidity", Value::Float(data.humidity_percent() as f64));
|
||||||
|
let gas_f = ipoint(
|
||||||
|
"gasresistence",
|
||||||
|
Value::Float(data.gas_resistance_ohm() as f64),
|
||||||
|
);
|
||||||
|
|
||||||
if state != FieldDataCondition::NewData {
|
let points = points!(temperature_f, pressure_f, humidity_f, gas_f);
|
||||||
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),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Err(e) = future::try_join4(temperature_f, pressure_f, humidity_f, gas_f).await {
|
if let Err(e) = client
|
||||||
eprintln!("Error: {:?}", e);
|
.write_points(points, Some(Precision::Seconds), None)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
eprintln!("Error: {:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
delay_for(Duration::from_secs(10)).await;
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a measured value to the influx database
|
/// Sends a measured value to the influx database
|
||||||
async fn send_value<'a>(
|
fn ipoint(type_name: &str, value: Value) -> Point {
|
||||||
client: &dyn Client,
|
let point = Point::new("sensor")
|
||||||
type_name: &str,
|
.add_field("value", value)
|
||||||
value: Value<'a>,
|
.add_tag("id", Value::String("VMAC".to_string()))
|
||||||
) -> Result<(), ClientError> {
|
.add_tag("name", Value::String("bme680".to_string()))
|
||||||
let mut measurement = Measurement::new("sensor");
|
.add_tag("type", Value::String(type_name.to_string()));
|
||||||
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).compat().await?;
|
point
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,20 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
extern crate bme680;
|
|
||||||
extern crate embedded_hal;
|
|
||||||
extern crate env_logger;
|
|
||||||
extern crate linux_embedded_hal as hal;
|
|
||||||
#[macro_use]
|
|
||||||
extern crate log;
|
|
||||||
|
|
||||||
use crate::hal::*;
|
|
||||||
use bme680::*;
|
use bme680::*;
|
||||||
use core::result;
|
use core::result;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use embedded_hal::blocking::delay::DelayMs;
|
use embedded_hal::blocking::delay::DelayMs;
|
||||||
use embedded_hal::blocking::i2c;
|
use embedded_hal::blocking::i2c;
|
||||||
|
use linux_embedded_hal as hal;
|
||||||
|
use linux_embedded_hal::Delay;
|
||||||
|
use log::info;
|
||||||
|
|
||||||
fn main(
|
fn main(
|
||||||
) -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
|
) -> result::Result<(), Error<<hal::I2cdev as i2c::Read>::Error, <hal::I2cdev as i2c::Write>::Error>>
|
||||||
{
|
{
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let i2c = I2cdev::new("/dev/i2c-1").unwrap();
|
let i2c = hal::I2cdev::new("/dev/i2c-1").unwrap();
|
||||||
|
|
||||||
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
|
let mut dev = Bme680::init(i2c, Delay {}, I2CAddress::Primary)?;
|
||||||
let mut delay = Delay {};
|
let mut delay = Delay {};
|
||||||
|
|
Loading…
Reference in a new issue