ups-esp32c3-rust/src/main.rs

82 lines
2.2 KiB
Rust

use embedded_svc::mqtt::client::{Publish, QoS};
use esp_idf_sys as _;
use log::*;
use std::{
env,
thread::{self, sleep},
time::Duration,
};
mod blink;
mod message_queue;
mod time;
mod wifi;
mod voltage_detection;
use crate::{
message_queue::MessageQueue, time::Time, wifi::Internet, voltage_detection::VoltageDetection,
};
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;
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();
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();
}
}
}