Metadata-Version: 2.4
Name: coffee_tag
Version: 0.44.2
Author-email: Toralba Thibault <thibault.toralba@ensta.fr>, Clément Laroudie <clement.laroudie@ensta.fr>, Adrien Poire <adrien.poire@ensta.fr>, Sébastien Kerbourc'h <sebastien.kerbourch@ensta.fr>
Maintainer-email: Adrien Poire <adrien.poire@ensta.fr>, Sébastien Kerbourc'h <sebastien.kerbourch@ensta.fr>
License-Expression: GPL-3.0
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: tk
Requires-Dist: Pillow
Requires-Dist: RPi.GPIO
Requires-Dist: quart
Requires-Dist: quart-auth
Requires-Dist: bcrypt
Requires-Dist: spidev
Requires-Dist: pyserial
Requires-Dist: juracoffeemachine>=1.5.7
Requires-Dist: jinja2
Requires-Dist: markupsafe
Dynamic: license-file

# coffee_tag

[![Publish](https://github.com/Seb-sti1/coffee_tag/actions/workflows/publish.yml/badge.svg)](https://github.com/Seb-sti1/coffee_tag/actions/workflows/publish.yml)
[![PyPI](https://img.shields.io/pypi/v/coffee_tag)](https://pypi.org/project/coffee_tag/)
[![Python](https://img.shields.io/pypi/pyversions/coffee_tag)](https://pypi.org/project/coffee_tag/)
[![License](https://img.shields.io/pypi/l/coffee_tag)](LICENSE)

A coffee machine kiosk running on a Raspberry Pi 4: badge in, brew a coffee on a Jura E6
(through [juracoffeemachine](https://github.com/Seb-sti1/juracoffeemachine)), and pay from a prepaid balance.

## Features

- Login with an RFID/NFC badge or by name, and self-registration.
- Brewing with presets, and a debt ceiling per account.
- Admin screens on the kiosk, with three roles: user, maintainer and owner.
- Email notifications (registration, low balance, date of departure).
- The Jura counters are logged periodically (every weekday during the day, plus once at night).

### Admin web app

Open `http://<pi-address>:8080/login`. It lets you:

- view a dashboard (balances, stock, errors),
- manage users,
- record payments and transfers,
- browse purchases and stock,
- send and resend emails,
- export the database as SQL or CSV.

## Hardware

> TODO: parts list (cables, level shifter/relay if any)

> TODO: schematic

> TODO: Jura connector pinout and voltage levels

> TODO: what GPIO23 switches on the Jura side

> TODO: screen model and how it is connected

> TODO: PN532 jumper settings (I2C mode, address 0x24)

Only the used pins are labelled:

| Usage              | Pin | Left Signal     | Right Signal      | Pin | Usage              |
|--------------------|-----|-----------------|-------------------|-----|--------------------|
|                    | 1   | 3V3 Power       | 5V Power          | 2   | PN532 (red)        |
| PN532 SDA (yellow) | 3   | GPIO2 (SDA1)    | 5V Power          | 4   | Screen (red)       |
| PN532 SCL (green)  | 5   | GPIO3 (SCL1)    | GND               | 6   | Screen (brown)     |
|                    | 7   | GPIO4           | GPIO14 (TXD0)     | 8   | Jura RX (blue)     |
| Jura (black)       | 9   | GND             | GPIO15 (RXD0)     | 10  | Jura TX (brown)    |
|                    | 11  | GPIO17          | GPIO18 (PWM0)     | 12  |                    |
|                    | 13  | GPIO27          | GND               | 14  | PN532 (brown)      |
|                    | 15  | GPIO22          | GPIO23            | 16  | Jura power (white) |
|                    | 17  | 3V3 Power       | GPIO24            | 18  |                    |
|                    | 19  | GPIO10 (MOSI)   | GND               | 20  |                    |
|                    | 21  | GPIO9 (MISO)    | GPIO25            | 22  |                    |
|                    | 23  | GPIO11 (SCLK)   | GPIO8 (CE0)       | 24  |                    |
|                    | 25  | GND             | GPIO7 (CE1)       | 26  |                    |
|                    | 27  | GPIO0 (ID_SD)   | GPIO1 (ID_SC)     | 28  |                    |
|                    | 29  | GPIO5           | GND               | 30  |                    |
|                    | 31  | GPIO6           | GPIO12 (PWM0)     | 32  |                    |
|                    | 33  | GPIO13 (PWM1)   | GND               | 34  |                    |
|                    | 35  | GPIO19 (PCM_FS) | GPIO16            | 36  |                    |
|                    | 37  | GPIO26          | GPIO20 (PCM_DIN)  | 38  |                    |
|                    | 39  | GND             | GPIO21 (PCM_DOUT) | 40  |                    |

The Jura's TX/RX must be connected to the Pi's RX/TX (crossed).
The NFC reader is a 13.56 MHz [Waveshare PN532 NFC HAT](https://www.waveshare.com/wiki/PN532_NFC_HAT#Features).

## Install

The Pi must run Raspberry Pi OS 64-bit (desktop, Trixie) with a user named `coffee`.

1. In `/boot/firmware/config.txt`, set the following lines and reboot:

   ```
   dtparam=i2c_arm=off
   dtoverlay=i2c1,pins_2_3
   init_uart_clock=16000000
   dtoverlay=miniuart-bt
   dtoverlay=uart0
   gpio=23=ip
   enable_uart=1
   ```

2. Install the dependencies and [pipx](https://github.com/pypa/pipx):

   ```shell
   sudo apt install -y python3-tk python3-pillow libjpeg-dev sqlite3
   ```

3. Install the app with `pipx install coffee_tag`.
4. Copy [config.template.json](https://github.com/Seb-sti1/coffee_tag/blob/main/config.template.json) to
   `/home/coffee/coffee/config.json` and edit it (every key is documented in the file).
5. Start the app (see [Usage](#usage)), register yourself on the kiosk, then make yourself an owner:

   ```shell
   sqlite3 /home/coffee/coffee/coffee.db "UPDATE users SET permissions='owner' WHERE mail='you@example.com';"
   ```

To upgrade: close the app, copy the database, run `pipx upgrade coffee_tag` and restart the app.

## Usage

Start the app with the command below. After this first run, it starts automatically at boot.

```shell
coffee_tag /home/coffee/coffee/config.json
```

## Where to look when something breaks

- Screen and buttons: [gui.py](https://github.com/Seb-sti1/coffee_tag/blob/main/coffee_tag/gui.py)
- Badge not read: [rfid.py](https://github.com/Seb-sti1/coffee_tag/blob/main/coffee_tag/rfid.py) and
  [pn532/](https://github.com/Seb-sti1/coffee_tag/tree/main/coffee_tag/pn532)
- Coffee flow and notifications
  logic: [coffee.py](https://github.com/Seb-sti1/coffee_tag/blob/main/coffee_tag/coffee.py)
  (then [juracoffeemachine](https://github.com/Seb-sti1/juracoffeemachine) for the brewing itself)
- Accounts, balances and stock: [database.py](https://github.com/Seb-sti1/coffee_tag/blob/main/coffee_tag/database.py)
- Admin web app: [website/](https://github.com/Seb-sti1/coffee_tag/tree/main/coffee_tag/website)
- Emails: [mail/](https://github.com/Seb-sti1/coffee_tag/tree/main/coffee_tag/mail)

## Development

```shell
sudo apt install python3-tk python3-pillow libjpeg-dev
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp config.template.json config.json  # then edit it
python -m coffee_tag config.json --dev --read-only --verbose
```

`--dev` disables the RFID reader and the Jura (badges are typed in the console), `--read-only` protects the database.
`python -m coffee_tag -h` lists the other flags, and `python -m test.mock_app` runs the app with a fake Jura.

Tests run on GitLab. Every commit on the default branch is published to PyPI by GitHub Actions, so remember to bump
the version in `pyproject.toml`.

## Contributing & license

Issues and pull requests are welcome on [GitHub](https://github.com/Seb-sti1/coffee_tag).
See [LICENSE](LICENSE).
