1
0
Fork 0

Compare commits

...

6 Commits

Author SHA1 Message Date
Alexander Böhm 0751b9ec5e Removed dotenv dependency 2024-01-05 17:11:57 +01:00
Alexander Böhm 07a1928d7d Removed expandable close call 2024-01-05 17:03:29 +01:00
Alexander Böhm 5545372ca8 Updated README 2024-01-05 17:02:03 +01:00
Alexander Böhm 63044009c9 Fix debug wait log message 2024-01-05 16:56:18 +01:00
Alexander Böhm 703bb1b48c Refactoring 2024-01-05 16:53:27 +01:00
Alexander Böhm ca70d8b39c Add install instruction 2024-01-05 16:30:28 +01:00
4 changed files with 107 additions and 67 deletions

7
Cargo.lock generated
View File

@ -302,12 +302,6 @@ dependencies = [
"syn",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "either"
version = "1.9.0"
@ -863,7 +857,6 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
name = "pi"
version = "0.1.1"
dependencies = [
"dotenv",
"env_logger",
"log",
"rppal",

View File

@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
env_logger = "0.10.1"
log = "0.4.20"
rppal = "0.14.1"

View File

@ -1,15 +1,12 @@
# Spaceapi Tuerpi
Feeding our SpaceAPI with Data from our entrance door.
Feeding our SpaceAPI with data from our entrance door.
## Setup
## Usage
Please change `DOOR_PIN` accordingly.
To run the client, please change `DOOR_PIN` accordingly to your setup. `DOOR_PIN` defaults to a closed door, when `HIGH`.
`DOOR_PIN` defaults to a closed door, when HIGH.
You need to provide the environment variables
`SPACEAPI_URL` and `API_KEY`.
You need to provide the environment variables `SPACEAPI_URL` and `API_KEY` to send the status to the SpaceAPI server. Details of the server can be found on the [project page](https://github.com/dezentrale/spaceapi-rs).
## Build
@ -32,3 +29,53 @@ Build the application for raspberry pi:
```sh
cargo build --target=armv7-unknown-linux-musleabihf
```
## Install
Build the project as release and place `pi` binary under `/usr/local/bin/spaceapi-tuerpi`
```sh
sudo install -m 755 -o root -g root \
target/release/pi \
/usr/local/bin/spaceapi-tuerpi
```
Configure the spaceapi systemd service by placing the service description under `/etc/systemd/system/spaceapi.service`:
```
[Unit]
Description=SpaceAPI tuerpi client
After=network.target
[Service]
Type=simple
Restart=always
EnvironmentFile=/etc/spaceapi-tuerpi.env
ExecStart=/usr/local/bin/spaceapi-tuerpi
[Install]
WantedBy=multi-user.target
```
Place the environment configuration with URL and API-Key under `/etc/spaceapi-tuerpi.env`:
```
SPACEAPI_URL=<your SpaceAPI server endpoint>
API_KEY=<your secret API key>
#DOOR_PIN=27 # Set the door pin
#RUST_LOG=ERROR # Set debug level
```
Ensure the `/etc/spaceapi-tuerpi.env` can be read only by root by executing:
```sh
sudo chmod 600 /etc/spaceapi-tuerpi.env
sudo chown root:root /etc/spaceapi-tuerpi.env
```
Apply changes to systemd to enable and start the service
```sh
sudo systemctl daemon-reload
sudo systemctl enable --now spaceapi
```

View File

@ -1,76 +1,77 @@
use std::env;
use std::thread::sleep;
use std::time::Duration;
use rppal::gpio::{Gpio, InputPin, Level};
use spaceapi_dezentrale_client::Client;
use spaceapi_dezentrale_client::ClientBuilder;
use std::{env, time::Duration};
use tokio::time::sleep;
// set your scripts for opening and closing the space here
static DOOR_PIN: u8 = 27;
// delays in seconds
static RECHECK_DELAY: u64 = 5;
static ANTI_BOUNCE_DELAY: u64 = 1;
// Static delays
static RECHECK_DELAY: Duration = Duration::from_secs(5);
static ANTI_BOUNCE_DELAY: Duration = Duration::from_secs(1);
#[derive(Debug)]
enum DoorStatus {
Open,
Closed,
}
#[tokio::main]
async fn main() {
env_logger::init();
let door_pin = env::var("DOOR_PIN").unwrap_or("27".to_string());
let door_pin = door_pin.parse().expect("A number for the DOOR_PIN");
// setup
log::debug!("Setup GPIO");
let gpio = Gpio::new().unwrap();
let pin = gpio.get(DOOR_PIN).unwrap().into_input();
let pin = gpio.get(door_pin).unwrap().into_input();
let spaceapi_client = spaceapi_dezentrale_client::ClientBuilder::new()
.api_key(&env::var("API_KEY").unwrap())
.base_url(&env::var("SPACEAPI_URL").unwrap())
log::debug!("Build client");
let spaceapi_client = ClientBuilder::new()
.api_key(&env::var("API_KEY").expect("Set API_KEY in environment"))
.base_url(&env::var("SPACEAPI_URL").expect("Set SPACEAPI_URL in environment"))
.build()
.unwrap();
// get initial door status
let mut door_status_old = check_door(&pin);
let _ = push_door_status(&spaceapi_client, door_status_old).await;
loop {
// maybe not the best solution but the pi isn't doing anything else
log::debug!("Waiting {} secs to read the door status", RECHECK_DELAY);
sleep(Duration::from_secs(RECHECK_DELAY));
// read new status
let door_status_new = check_door(&pin);
log::debug!("Read {}", door_status_new);
// if the new status isn't the old one
if door_status_old != door_status_new {
// wait for the switch to stop bouncing around
log::debug!("Waiting {} secs for recheck", ANTI_BOUNCE_DELAY);
sleep(Duration::from_secs(ANTI_BOUNCE_DELAY));
// the new read status is still the same after a minute then push it to the api
if door_status_new == check_door(&pin) {
log::debug!("Check passed, applying new status");
log::debug!("Pushing space status: Open = {}", door_status_new);
let _ = push_door_status(&spaceapi_client, door_status_new)
log::debug!("Checking door status");
match check_door(&pin).await {
DoorStatus::Open => {
log::debug!("Door is open, sending keep open to server");
let _ = spaceapi_client
.keep_open()
.await
.map(|_| ())
.map_err(|err| format!("Problem while pushing door status: {err}"));
log::debug!("Saving space status: {}", door_status_new);
door_status_old = door_status_new;
} else {
log::debug!("Check wasn't successful")
}
DoorStatus::Closed => {
log::debug!("Door is closed, doing nothing");
}
};
log::debug!("Waiting {RECHECK_DELAY:?} secs to read the door status");
sleep(RECHECK_DELAY).await;
}
}
async fn check_door(pin: &InputPin) -> DoorStatus {
// reading space status from door
let mut pin_state = pin.read();
// Anti bounce verification
loop {
sleep(ANTI_BOUNCE_DELAY).await;
let pin_state_check = pin.read();
if pin_state_check == pin_state {
break;
} else {
pin_state = pin_state_check
}
}
}
fn check_door(pin: &InputPin) -> bool {
// reading space status from door
pin.read() == Level::Low
}
async fn push_door_status(spaceapi: &Client, open: bool) -> Result<(), String> {
if open {
log::debug!("Sending keep open to server");
spaceapi.keep_open().await.map(|_| ())
log::debug!("Pin state is {pin_state}");
if pin_state == Level::Low {
DoorStatus::Open
} else {
log::debug!("Sending explicit close to server");
spaceapi.close().await
DoorStatus::Closed
}
}