# HW

A list of used components.

## 3D printed parts
- material: `petg`
- layer height: `0.2mm` (0.25mm is also a option but bolt holes may not print nicely)
- used printers: `Prusa MK4S`, `Prusa MK3s`

## Desktop verison
- 1x RPi zero 2W + SD card - small computer reading, processing and outputing measured data
- 1x Loadcell - the sensor itself converting force to voltage
- 1x off-the-shelf HX711 board - board converting voltage to digital signals

| Data (DT) pin | Clock (SCK) pin |
|---------------|-----------------|
| 23            | 24              |

## Tube version
- 1x RPi zero 2W + SD card - small computer reading, processing and outputing measured data
- 1x custom breaktout board - breakout board exposing easily accesible ports for sensors
- 4x Loadcell - the sensor itself converting force to voltage
- 4x custom HX711 board - board converting voltage to digital signals with a RJ45 connector for easy setup

| Port | Data (DT) pin | Clock (SCK) pin |
|------|---------------|-----------------|
|  P1  | 6             | 5               |
|  P2  | 7             | 8               |
|  P3  | 10            | 22              |
|  P4  | 18            | 27              |

---

## Bluetooth connection - #TODO add images
1. Enable bluetooth
2. Open bluetooth options
3. Click "Add device"
4. Device/s with name `WS-<mac address>` will show up (e.g. `WS-80e82c94b482`)
5. Click on them to connect
6. Connect to PAN network
7. All done, now you are connected to the scale 

---

## Accesing webui
1. Open you web browser of choice (Microsoft Edge, Google chrome...)
2. In the address bar write `http://192.168.50.1:8080` if connecting through bluetooth, or write `http://<hostname>:8080` and replace the hostname with the set hostname (e.g. `cz710scale001`)

---

## Connecting to TE network
1.  Go to the **System** tab
2.  You will see the device mac address, use this mac address to request access to the TE digital factory wifi network.
3.  Go to the **WiFi** tab
4.  Click on **Scan** to show all avaliable networks
5.  Click the digfac network (if multiple are shown, you can choose any of them)
6.  Put in the password for the device (there is a button to show the written password)
7.  When you check that the pasword is correct, click on **Connect**
8.  After a few seconds it should say connected succesfully and disapear
9.  Go to the **System** and check the connection (it will show you the IP, what network its connected to and the signal strenght)
10. Click on **Set hostname** and put in the hostname you put in the access request
11. Now you are able to access the scale on `http://<ip>:8080` and `http://<hostname>:8080` (replace `<ip>` and `<hostname>` with your ip and hostname)
 

---

## Navigation

The application has a sidebar with 6 pages:

| Page | Purpose |
|------|---------|
| Dashboard | Overview with live charts and current readings |
| Sensors | Configure and monitor physical weight sensors |
| Counters | Configure counters that aggregate sensors and count pieces |
| Pieces | Define part types with expected weight and tolerance |
| WiFi | Manage wireless network connections |
| System | Hostname, config backup/restore, system info |

---

## Adding Sensors

1. Go to the **Sensors** tab
2. Click the **Add Sensor** button
3. Fill in the form:
   - **Sensor Name**           - Friendly name (e.g. `Main Scale` or `front left`)
   - **Sensor Type**           - Choose `HX711 Load Cell` (other one is for debugging)
   - **Smoothing Samples**     - Leave on the default 5
      - For current usecases 5 is the optimal value but it may change depending on the usecase
   - **Invert Sensor Reading** - Check if the sensor reads upside-down
   - **Enable Sensor**         - Keep checked to activate
   - **Description**           - Optional notes 

   <br>

   - **Data Pin (DT)** - Look at the top of the document at the HW list and put the corresponding pin here
   - **Clock Pin (SCK)** - --||--
      
   <br>

   - **Gain** - use 128
   - **Value per Gram** - You can use this value to calibrate the scale for weighting in grams, more in **Calibrating grams**
   - **Tare Offset** - Value that will be subtracted from the raw value from sensor used for zeroing
6. Click **Save**

The sensor appears in the table with a live value that updates regularly, time depends on counter settings.

---

## Adding Pieces

Pieces define the parts you want to count. Each piece has an expected weight and tolerance.

1. Click the **Add Piece** button
2. Fill in the form:
   - **Piece Name**           - Friendly name (e.g. "Steel Bracket") or you can use also the SKU here
   - **SKU / Part Number**    - Part number (e.g. "BRK-001")
   - **Expected Weight (g)**  - Target weight in grams (or raw value if not calibrated for weighting in grams -> **Calibrating grams** and **Calibrating counting**)
   - **Tolerance (±g)**       - Acceptable weight deviation in grams (or raw value --||--)
   - **Max added/removed**    - Limits the number of added or removed pieces so reading not caused by weighting pieces dont count - still in develpoment
   - **Description**          - Optional notes
3. Click **Save**

The **Range** column automatically shows `Expected - Tolerance` to `Expected + Tolerance`.

---

## Adding Counters

Counters aggregate one or more sensors into a virtual metric and detect when pieces are added or removed based on weight changes.

### Prerequisites
- At least one sensor must be configured (see **Adding Sensors**)
- At least one pieces must be configured (see **Adding Pieces**)

### Steps

1. Click the **Add Counter** button
2. Fill in the form:
   - **Counter Name**            - Friendly name for this counter (e.g. `Line 1 Total` or `Main`)
   - **Sensors**                 - Check the boxes for sensors to aggregate
   - **Operation**               - How to combine multiple sensors: `Sum`, `Average`, `Maximum`, or `Minimum`, usualy `Sum` is what you want
   - **Check Interval (ms)**     - How often to poll sensors and check for changes (e.g. 1000 = 1 second), for now leave it at 1000ms, more at -> **Calibrating counting**
   - **Detection Threshold**     - Minimum weight change in grams (or raw value) to trigger a count event (e.g. 10)
   - **Piece Selection API URL** - External URL to fetch the active piece SKU (leave blank for manual) -> **API SKU link**
   - **Active Piece SKU**        - Manually set which piece type to track (overridden by API URL if set)
   - **Enable Counter**          - Check to activate the counter
   - **Description**             - Optional notes

3. Click **Save**

---

## Calibrating grams

This is a optional step that provides better readability of the shown data.

You will need a item/s of known weight

1. Go through all of the sensors 
   - set the `Value per gram` to `1` - if you had to change it, save it and open the edit menu again
   - set the `Tare offset` to the live value to get them as close to zero as possible (they are very sensitive and someties drift a bit so it will oscilate around zero)
2. Go to the `Counters` page
3. Write down the value before and after you put the item of known weight on the scale
4. Repeat the measurement multiple times to get better results. If you have multiple of the same items, try multiple items to get better measurements.
5. Calculate the delta by subtracting the **before** value from the **after** value (and if you used multiple items, divide the delta by the number of items)
6. Divide the average of the calculated deltas by the known weight (in grams) to get the **Value per gram**
7. Set this value for each sensor
8. Now it should show you the value in grams instead of the units from before

---

## Calibrating counting

You will need multiple of the same item you want to count

If you done the gram calibration it will be similar

1. Go to the `Counters` page
2. Write down the value before and after you put the item on the scale
3. Repeat the measurement multiple times. Its recomended to try with multiple pieces (of the same type) at the same time to get better results.
4. Calculate the **delta** by subtracting the **before** value from the **after** value (and if you used multiple items, divide the delta by the number of items)
5. Calculate the **average, min and max** of these values
6. Subtract the **min** from **max** to get the **tolerance**
7. Set the `Expected weight` to the **average** and `Tolerance` to the **tolerance** for the piece you are calibrating.