説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.rs 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. use std::env;
  2. use std::io;
  3. use std::thread::sleep;
  4. use std::time::Duration;
  5. use log::error;
  6. use ::mqtt::control::variable_header::ConnectReturnCode;
  7. use crate::mqtt::MQTT;
  8. mod mqtt;
  9. mod radio;
  10. mod spi;
  11. fn main() {
  12. env::set_var(
  13. "RUST_LOG",
  14. env::var_os("RUST_LOG").unwrap_or_else(|| "info".into()),
  15. );
  16. env_logger::init();
  17. radio::start();
  18. // Whenever the MQTT connection returns an error, we wait for a couple of seconds and then try
  19. // to reconnect.
  20. loop {
  21. // TODO: Pass events from ratio to mqtt connection.
  22. if let Err(e) = run_mqtt_connection() {
  23. error!("MQTT error: {:?}", e);
  24. }
  25. sleep(Duration::from_secs(3));
  26. }
  27. }
  28. fn run_mqtt_connection() -> Result<(), Error> {
  29. let mut m = MQTT::connect("127.0.0.1:1883")?;
  30. let mut counter = 0;
  31. loop {
  32. // TODO: Real values
  33. m.publish("gottschlag/livingroom/temperature", &format!("{}", counter))?;
  34. sleep(Duration::from_secs(3));
  35. counter += 1;
  36. }
  37. }
  38. #[derive(thiserror::Error, Debug)]
  39. pub enum Error {
  40. #[error("I/O error")]
  41. Io(#[from] io::Error),
  42. #[error("connection refused, return code {0:?}")]
  43. ConnectionRefused(ConnectReturnCode),
  44. #[error("radio error: {0:?}")]
  45. Radio(embedded_nrf24l01::Error<std::io::Error>)
  46. }
  47. impl From<embedded_nrf24l01::Error<std::io::Error>> for Error {
  48. fn from(e: embedded_nrf24l01::Error<std::io::Error>) -> Self {
  49. Error::Radio(e)
  50. }
  51. }