108 lines
3.3 KiB
Rust
108 lines
3.3 KiB
Rust
use embedded_svc::event_bus::EventBus;
|
|
use esp_idf_svc::eventloop::{Background, EspBackgroundEventLoop, EspEventLoop};
|
|
use esp_idf_sys::{self as _, EspError};
|
|
use log::*;
|
|
use std::{
|
|
env,
|
|
thread::{self, sleep},
|
|
time::Duration,
|
|
};
|
|
|
|
mod blink;
|
|
mod message_queue;
|
|
mod time;
|
|
mod voltage_detection;
|
|
mod wifi;
|
|
|
|
use crate::voltage_detection::VoltageDetectionWorker;
|
|
use crate::{
|
|
message_queue::MessageQueue, time::Time, voltage_detection::VoltageDetection, wifi::Internet,
|
|
};
|
|
|
|
static mut EVENTLOOP: Option<EspEventLoop<esp_idf_svc::eventloop::User<Background>>> = None;
|
|
|
|
fn main() {
|
|
env::set_var("DEFMT_LOG", "trace");
|
|
env::set_var("RUST_BACKTRACE", "1");
|
|
env::set_var("RUST_LOG", "trace");
|
|
env_logger::init();
|
|
|
|
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
|
|
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
|
|
esp_idf_sys::link_patches();
|
|
|
|
info!("Hello, world!");
|
|
|
|
let peripherals = esp_idf_hal::peripherals::Peripherals::take().unwrap();
|
|
|
|
let blink_pin = peripherals.pins.gpio5;
|
|
let beep_pin = peripherals.pins.gpio6;
|
|
let ledc_timer0 = peripherals.ledc.timer0;
|
|
let ledc_channel0 = peripherals.ledc.channel0;
|
|
let dc_out_ctl_pin = peripherals.pins.gpio3;
|
|
let i2c0 = peripherals.i2c0;
|
|
let sda_pin = peripherals.pins.gpio4;
|
|
let scl_pin = peripherals.pins.gpio10;
|
|
|
|
info!("About to start a background event loop");
|
|
match EspBackgroundEventLoop::new(&Default::default()) {
|
|
Ok(eventloop) => unsafe { EVENTLOOP = Some(eventloop) },
|
|
Err(err) => error!("Init Event Loop failed. {:?}", err),
|
|
};
|
|
|
|
thread::spawn(move || {
|
|
let mut blink = blink::Blink::new(
|
|
blink_pin
|
|
.into_output()
|
|
.expect("Failed to set GPIO5 as output"),
|
|
);
|
|
blink.play();
|
|
});
|
|
|
|
let mut voltage_detection = VoltageDetection::new();
|
|
|
|
voltage_detection
|
|
.watching()
|
|
.expect("Can not watch voltages.");
|
|
|
|
let _wifi = Internet::new().unwrap();
|
|
|
|
let mut mq = MessageQueue::new();
|
|
mq.init().unwrap();
|
|
|
|
let mut time = Time::new();
|
|
time.sync().unwrap();
|
|
|
|
let _subscription;
|
|
if let Some(eventloop) = unsafe { EVENTLOOP.as_mut() } {
|
|
_subscription = eventloop
|
|
.subscribe(move |message: &VoltageDetectionWorker| {
|
|
info!("Event Loop Value");
|
|
if let Ok(json_str) = serde_json::to_string(&message) {
|
|
if let Err(err) = mq.publish("voltage", json_str.as_bytes()) {
|
|
warn!("Can not publish message to MQTT. {}", err);
|
|
}
|
|
}
|
|
})
|
|
.expect(" Listening Event Loop Failed");
|
|
}
|
|
|
|
loop {
|
|
sleep(Duration::from_millis(1000));
|
|
// if let Some(ref mut mq_client) = mq.client {
|
|
// let timestamps = time.get_time().as_millis();
|
|
|
|
// info!("timestamps {}", timestamps);
|
|
// mq_client
|
|
// .publish(
|
|
// "ups-0.2/heartbeat",
|
|
// QoS::AtMostOnce,
|
|
// false,
|
|
// timestamps.to_string().as_bytes(),
|
|
// )
|
|
// .map_err(|err| warn!("publish heartbeat failed {}", err))
|
|
// .unwrap();
|
|
// }
|
|
}
|
|
}
|