Compare commits
43 Commits
79daecf97d
...
refactor/m
| Author | SHA1 | Date | |
|---|---|---|---|
|
2cfb2607a9
|
|||
|
271c1a1383
|
|||
|
a02b84d732
|
|||
| b0f8bcc9da | |||
| 103859120c | |||
| 403517fdb4 | |||
| 11eb8713bf | |||
| d903c2bf52 | |||
| f8f76674ce | |||
| 3cc5a0d2bd | |||
| 3be585ecbf | |||
| 5b1a945ac3 | |||
| f4e050d413 | |||
| 776db785c4 | |||
|
|
ef0ec47d92 | ||
| 0ed9d6bb57 | |||
| 4771a77686 | |||
| eef165b6de | |||
| 1ace878488 | |||
| a30d59605d | |||
| 2ee3615dcd | |||
| db0f7daa4c | |||
| 6809a37d9d | |||
| 0ca09ed498 | |||
| 542ff578bc | |||
| 2e16163b0e | |||
| 9280bbb244 | |||
|
|
e0b8acd55c | ||
|
|
c04109a76c | ||
|
|
f0c9ed4e7f | ||
|
|
3fa8077b81 | ||
|
|
7f0714914f | ||
| 61806a5fa2 | |||
| 016047ab23 | |||
| eb276cfa68 | |||
| f1c85d1d74 | |||
| 097aff5360 | |||
| fc0e18da56 | |||
| 2e4eb283b5 | |||
| cc92c82ac9 | |||
| b8f01f0de9 | |||
| 07aed02fe7 | |||
| aef0ffd5a1 |
62
AGENTS.md
Normal file
62
AGENTS.md
Normal file
@@ -0,0 +1,62 @@
|
||||
# AGENTS.md
|
||||
|
||||
## Scope
|
||||
These instructions apply to the entire repository unless a deeper `AGENTS.md` overrides them.
|
||||
|
||||
## Repository Overview
|
||||
`PlantCtrl` is a mixed-discipline repository with embedded firmware, shared Rust crates, hardware design files, and a Hugo-based website.
|
||||
|
||||
Top-level layout:
|
||||
- `Software/MainBoard/rust`: main embedded Rust firmware for the controller board (`plant-ctrl2`).
|
||||
- `Software/CAN_Sensor`: embedded Rust firmware for the CAN sensor / BMS board.
|
||||
- `Software/Shared/canapi`: shared Rust crate used by firmware projects.
|
||||
- `Hardware`: PCB, case, and related hardware design assets.
|
||||
- `DataSheets`: reference material; treat as source data, not generated output.
|
||||
- `website`: Hugo site based on the Blowfish theme.
|
||||
- `bin`: helper scripts and local tooling, if present.
|
||||
|
||||
## Working Rules
|
||||
- Keep changes tightly scoped to the user request; this repo spans hardware, firmware, and website code.
|
||||
- Prefer fixing the underlying cause instead of applying cosmetic workarounds.
|
||||
- Preserve existing file structure and naming unless the user explicitly asks for restructuring.
|
||||
- Avoid mass formatting or opportunistic cleanup in KiCad files, lockfiles, generated assets, or vendored dependencies.
|
||||
- Do not edit dependency directories such as `website/themes` or `Software/MainBoard/rust/src_webpack/node_modules` unless the user explicitly asks for vendor changes.
|
||||
- When touching firmware code, keep resource usage and target constraints in mind; avoid unnecessary allocations or feature creep.
|
||||
|
||||
## Firmware Guidance
|
||||
- Shared protocol or serialization changes must be checked for impact across both `Software/MainBoard/rust` and `Software/CAN_Sensor`.
|
||||
- Prefer small, explicit changes in embedded code paths; do not introduce heavyweight abstractions without a clear payoff.
|
||||
- Keep `no_std`/embedded assumptions intact unless the code clearly opts into something else.
|
||||
- Be careful with feature flags, target-specific dependencies, and boot/runtime configuration in Cargo manifests.
|
||||
|
||||
## Hardware Guidance
|
||||
- Treat hardware directories as design artifacts, not generic text files.
|
||||
- Do not reorder, normalize, or bulk-edit PCB / CAD files unless the user specifically requests those changes.
|
||||
- If a software change depends on hardware assumptions, call that out clearly in the final handoff.
|
||||
|
||||
## Website Guidance
|
||||
- The site in `website` uses Hugo with the Blowfish theme.
|
||||
- Prefer editing site content, config, or custom assets over modifying vendored theme internals.
|
||||
- Keep frontend changes consistent with the existing site structure unless the user asks for a redesign.
|
||||
|
||||
## Validation
|
||||
Use the narrowest relevant check first.
|
||||
|
||||
Useful commands:
|
||||
- `cargo check --manifest-path Software/Shared/canapi/Cargo.toml`
|
||||
- `cargo check --manifest-path Software/CAN_Sensor/Cargo.toml`
|
||||
- `cargo check --manifest-path Software/MainBoard/rust/Cargo.toml`
|
||||
- `npm run dev` from `website` for local Hugo development if the environment has the required tools installed.
|
||||
|
||||
Validation notes:
|
||||
- Embedded firmware may require target-specific toolchains or hardware-adjacent tooling that is not always available.
|
||||
- If you cannot run a meaningful validation step, say so explicitly and describe the likely prerequisite.
|
||||
|
||||
## File Hygiene
|
||||
- Read large files in chunks.
|
||||
- Prefer targeted searches (`rg`, or `find` if unavailable) over broad scans.
|
||||
- Do not commit build outputs, generated binaries, or local IDE metadata unless the user explicitly requests it.
|
||||
|
||||
## Handoff Expectations
|
||||
- Summarize what changed, where it changed, and any validation performed.
|
||||
- Call out follow-up work when a change likely affects both firmware targets, hardware assumptions, or the website.
|
||||
@@ -15,6 +15,7 @@
|
||||
"vias": 1.0,
|
||||
"zones": 0.6
|
||||
},
|
||||
"prototype_zone_fills": false,
|
||||
"selection_filter": {
|
||||
"dimensions": true,
|
||||
"footprints": true,
|
||||
@@ -53,6 +54,7 @@
|
||||
"zone_display_mode": 1
|
||||
},
|
||||
"git": {
|
||||
"integration_disabled": false,
|
||||
"repo_type": "",
|
||||
"repo_username": "",
|
||||
"ssh_key": ""
|
||||
@@ -105,6 +107,7 @@
|
||||
"filter_text": "",
|
||||
"group_by_constraint": false,
|
||||
"group_by_netclass": false,
|
||||
"show_time_domain_details": false,
|
||||
"show_unconnected_nets": false,
|
||||
"show_zero_pad_nets": false,
|
||||
"sort_ascending": true,
|
||||
@@ -115,6 +118,7 @@
|
||||
"files": []
|
||||
},
|
||||
"schematic": {
|
||||
"hierarchy_collapsed": [],
|
||||
"selection_filter": {
|
||||
"graphics": true,
|
||||
"images": true,
|
||||
@@ -122,6 +126,7 @@
|
||||
"lockedItems": false,
|
||||
"otherItems": true,
|
||||
"pins": true,
|
||||
"ruleAreas": true,
|
||||
"symbols": true,
|
||||
"text": true,
|
||||
"wires": true
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
"3dviewports": [],
|
||||
"design_settings": {
|
||||
"defaults": {
|
||||
"apply_defaults_to_fp_barcodes": false,
|
||||
"apply_defaults_to_fp_dimensions": false,
|
||||
"apply_defaults_to_fp_fields": false,
|
||||
"apply_defaults_to_fp_shapes": false,
|
||||
"apply_defaults_to_fp_text": false,
|
||||
@@ -82,6 +84,7 @@
|
||||
"extra_footprint": "warning",
|
||||
"footprint": "error",
|
||||
"footprint_filters_mismatch": "ignore",
|
||||
"footprint_symbol_field_mismatch": "warning",
|
||||
"footprint_symbol_mismatch": "warning",
|
||||
"footprint_type_mismatch": "ignore",
|
||||
"hole_clearance": "error",
|
||||
@@ -99,6 +102,7 @@
|
||||
"mirrored_text_on_front_layer": "warning",
|
||||
"missing_courtyard": "ignore",
|
||||
"missing_footprint": "warning",
|
||||
"missing_tuning_profile": "warning",
|
||||
"net_conflict": "warning",
|
||||
"nonmirrored_text_on_back_layer": "warning",
|
||||
"npth_inside_courtyard": "ignore",
|
||||
@@ -118,9 +122,12 @@
|
||||
"too_many_vias": "error",
|
||||
"track_angle": "error",
|
||||
"track_dangling": "warning",
|
||||
"track_not_centered_on_via": "ignore",
|
||||
"track_on_post_machined_layer": "error",
|
||||
"track_segment_length": "error",
|
||||
"track_width": "error",
|
||||
"tracks_crossing": "error",
|
||||
"tuning_profile_track_geometries": "ignore",
|
||||
"unconnected_items": "error",
|
||||
"unresolved_variable": "error",
|
||||
"via_dangling": "warning",
|
||||
@@ -235,17 +242,28 @@
|
||||
"zones_allow_external_fillets": false
|
||||
},
|
||||
"ipc2581": {
|
||||
"bom_rev": "",
|
||||
"dist": "",
|
||||
"distpn": "",
|
||||
"internal_id": "",
|
||||
"mfg": "",
|
||||
"mpn": ""
|
||||
"mpn": "",
|
||||
"sch_revision": ""
|
||||
},
|
||||
"layer_pairs": [],
|
||||
"layer_presets": [],
|
||||
"viewports": []
|
||||
},
|
||||
"boards": [],
|
||||
"component_class_settings": {
|
||||
"assignments": [],
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"sheet_component_classes": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"cvpcb": {
|
||||
"equivalence_files": []
|
||||
},
|
||||
@@ -494,13 +512,14 @@
|
||||
"priority": 2147483647,
|
||||
"schematic_color": "rgba(0, 0, 0, 0.000)",
|
||||
"track_width": 0.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.6,
|
||||
"via_drill": 0.3,
|
||||
"wire_width": 6
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"version": 4
|
||||
"version": 5
|
||||
},
|
||||
"net_colors": null,
|
||||
"netclass_assignments": null,
|
||||
@@ -683,6 +702,7 @@
|
||||
"sort_asc": true,
|
||||
"sort_field": "Reference"
|
||||
},
|
||||
"bus_aliases": {},
|
||||
"connection_grid_size": 50.0,
|
||||
"drawing": {
|
||||
"dashed_lines_dash_length_ratio": 12.0,
|
||||
@@ -721,7 +741,14 @@
|
||||
"spice_save_all_dissipations": false,
|
||||
"spice_save_all_voltages": false,
|
||||
"subpart_first_id": 65,
|
||||
"subpart_id_separator": 0
|
||||
"subpart_id_separator": 0,
|
||||
"top_level_sheets": [
|
||||
{
|
||||
"filename": "MPPT.kicad_sch",
|
||||
"name": "MPPT",
|
||||
"uuid": "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sheets": [
|
||||
[
|
||||
@@ -729,5 +756,11 @@
|
||||
"Root"
|
||||
]
|
||||
],
|
||||
"text_variables": {}
|
||||
"text_variables": {},
|
||||
"tuning_profiles": {
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"tuning_profiles_impedance_geometric": []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"vias": 1.0,
|
||||
"zones": 0.6
|
||||
},
|
||||
"prototype_zone_fills": false,
|
||||
"ratsnest_display_mode": 0,
|
||||
"selection_filter": {
|
||||
"dimensions": true,
|
||||
@@ -54,6 +55,7 @@
|
||||
"zone_display_mode": 1
|
||||
},
|
||||
"git": {
|
||||
"integration_disabled": false,
|
||||
"repo_password": "",
|
||||
"repo_type": "",
|
||||
"repo_username": "",
|
||||
@@ -113,6 +115,7 @@
|
||||
"filter_text": "",
|
||||
"group_by_constraint": false,
|
||||
"group_by_netclass": false,
|
||||
"show_time_domain_details": false,
|
||||
"show_unconnected_nets": false,
|
||||
"show_zero_pad_nets": false,
|
||||
"sort_ascending": true,
|
||||
@@ -123,6 +126,7 @@
|
||||
"files": []
|
||||
},
|
||||
"schematic": {
|
||||
"hierarchy_collapsed": [],
|
||||
"selection_filter": {
|
||||
"graphics": true,
|
||||
"images": true,
|
||||
@@ -130,6 +134,7 @@
|
||||
"lockedItems": false,
|
||||
"otherItems": true,
|
||||
"pins": true,
|
||||
"ruleAreas": true,
|
||||
"symbols": true,
|
||||
"text": true,
|
||||
"wires": true
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
"3dviewports": [],
|
||||
"design_settings": {
|
||||
"defaults": {
|
||||
"apply_defaults_to_fp_barcodes": false,
|
||||
"apply_defaults_to_fp_dimensions": false,
|
||||
"apply_defaults_to_fp_fields": false,
|
||||
"apply_defaults_to_fp_shapes": false,
|
||||
"apply_defaults_to_fp_text": false,
|
||||
@@ -78,6 +80,7 @@
|
||||
"extra_footprint": "warning",
|
||||
"footprint": "error",
|
||||
"footprint_filters_mismatch": "ignore",
|
||||
"footprint_symbol_field_mismatch": "warning",
|
||||
"footprint_symbol_mismatch": "warning",
|
||||
"footprint_type_mismatch": "warning",
|
||||
"hole_clearance": "error",
|
||||
@@ -96,6 +99,7 @@
|
||||
"mirrored_text_on_front_layer": "warning",
|
||||
"missing_courtyard": "ignore",
|
||||
"missing_footprint": "warning",
|
||||
"missing_tuning_profile": "warning",
|
||||
"net_conflict": "warning",
|
||||
"nonmirrored_text_on_back_layer": "warning",
|
||||
"npth_inside_courtyard": "ignore",
|
||||
@@ -115,9 +119,12 @@
|
||||
"too_many_vias": "error",
|
||||
"track_angle": "error",
|
||||
"track_dangling": "warning",
|
||||
"track_not_centered_on_via": "ignore",
|
||||
"track_on_post_machined_layer": "error",
|
||||
"track_segment_length": "error",
|
||||
"track_width": "error",
|
||||
"tracks_crossing": "error",
|
||||
"tuning_profile_track_geometries": "ignore",
|
||||
"unconnected_items": "error",
|
||||
"unresolved_variable": "error",
|
||||
"via_dangling": "warning",
|
||||
@@ -242,17 +249,28 @@
|
||||
"zones_use_no_outline": true
|
||||
},
|
||||
"ipc2581": {
|
||||
"bom_rev": "",
|
||||
"dist": "",
|
||||
"distpn": "",
|
||||
"internal_id": "",
|
||||
"mfg": "",
|
||||
"mpn": ""
|
||||
"mpn": "",
|
||||
"sch_revision": ""
|
||||
},
|
||||
"layer_pairs": [],
|
||||
"layer_presets": [],
|
||||
"viewports": []
|
||||
},
|
||||
"boards": [],
|
||||
"component_class_settings": {
|
||||
"assignments": [],
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"sheet_component_classes": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"cvpcb": {
|
||||
"equivalence_files": []
|
||||
},
|
||||
@@ -443,11 +461,14 @@
|
||||
"duplicate_sheet_names": "error",
|
||||
"endpoint_off_grid": "ignore",
|
||||
"extra_units": "error",
|
||||
"field_name_whitespace": "warning",
|
||||
"footprint_filter": "ignore",
|
||||
"footprint_link_issues": "warning",
|
||||
"four_way_junction": "ignore",
|
||||
"global_label_dangling": "warning",
|
||||
"ground_pin_not_ground": "warning",
|
||||
"hier_label_mismatch": "error",
|
||||
"isolated_pin_label": "warning",
|
||||
"label_dangling": "error",
|
||||
"label_multiple_wires": "warning",
|
||||
"lib_symbol_issues": "warning",
|
||||
@@ -470,6 +491,7 @@
|
||||
"similar_power": "warning",
|
||||
"simulation_model_issue": "ignore",
|
||||
"single_global_label": "warning",
|
||||
"stacked_pin_name": "warning",
|
||||
"unannotated": "error",
|
||||
"unconnected_wire_endpoint": "warning",
|
||||
"undefined_netclass": "error",
|
||||
@@ -502,6 +524,7 @@
|
||||
"priority": 2147483647,
|
||||
"schematic_color": "rgba(0, 0, 0, 0.000)",
|
||||
"track_width": 1.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 6
|
||||
@@ -520,6 +543,7 @@
|
||||
"priority": 0,
|
||||
"schematic_color": "rgb(255, 4, 6)",
|
||||
"track_width": 1.0,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 12
|
||||
@@ -538,6 +562,7 @@
|
||||
"priority": 1,
|
||||
"schematic_color": "rgb(255, 153, 0)",
|
||||
"track_width": 0.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 12
|
||||
@@ -556,6 +581,7 @@
|
||||
"priority": 2,
|
||||
"schematic_color": "rgb(81, 255, 3)",
|
||||
"track_width": 1.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 12
|
||||
@@ -574,6 +600,7 @@
|
||||
"priority": 3,
|
||||
"schematic_color": "rgb(130, 130, 130)",
|
||||
"track_width": 1.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 12
|
||||
@@ -592,13 +619,14 @@
|
||||
"priority": 4,
|
||||
"schematic_color": "rgb(0, 0, 0)",
|
||||
"track_width": 0.5,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.8,
|
||||
"via_drill": 0.4,
|
||||
"wire_width": 12
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"version": 4
|
||||
"version": 5
|
||||
},
|
||||
"net_colors": null,
|
||||
"netclass_assignments": null,
|
||||
@@ -1077,6 +1105,10 @@
|
||||
},
|
||||
"schematic": {
|
||||
"annotate_start_num": 0,
|
||||
"annotation": {
|
||||
"method": 0,
|
||||
"sort_order": 0
|
||||
},
|
||||
"bom_export_filename": "PlantCtrlESP32.csv",
|
||||
"bom_fmt_presets": [],
|
||||
"bom_fmt_settings": {
|
||||
@@ -1256,6 +1288,7 @@
|
||||
"sort_asc": true,
|
||||
"sort_field": "LCSC_PART_NUMBER"
|
||||
},
|
||||
"bus_aliases": {},
|
||||
"connection_grid_size": 50.0,
|
||||
"drawing": {
|
||||
"dashed_lines_dash_length_ratio": 12.0,
|
||||
@@ -1263,6 +1296,7 @@
|
||||
"default_line_thickness": 6.0,
|
||||
"default_text_size": 50.0,
|
||||
"field_names": [],
|
||||
"hop_over_size_choice": 0,
|
||||
"intersheets_ref_own_page": false,
|
||||
"intersheets_ref_prefix": "",
|
||||
"intersheets_ref_short": false,
|
||||
@@ -1295,6 +1329,7 @@
|
||||
},
|
||||
"page_layout_descr_file": "",
|
||||
"plot_directory": "/tmp/",
|
||||
"reuse_designators": true,
|
||||
"space_save_all_events": true,
|
||||
"spice_adjust_passive_values": false,
|
||||
"spice_current_sheet_as_root": false,
|
||||
@@ -1304,7 +1339,16 @@
|
||||
"spice_save_all_dissipations": false,
|
||||
"spice_save_all_voltages": false,
|
||||
"subpart_first_id": 65,
|
||||
"subpart_id_separator": 0
|
||||
"subpart_id_separator": 0,
|
||||
"top_level_sheets": [
|
||||
{
|
||||
"filename": "PlantCtrlESP32.kicad_sch",
|
||||
"name": "PlantCtrlESP32",
|
||||
"uuid": "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
],
|
||||
"used_designators": "",
|
||||
"variants": []
|
||||
},
|
||||
"sheets": [
|
||||
[
|
||||
@@ -1312,5 +1356,11 @@
|
||||
"Root"
|
||||
]
|
||||
],
|
||||
"text_variables": {}
|
||||
"text_variables": {},
|
||||
"tuning_profiles": {
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"tuning_profiles_impedance_geometric": []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"vias": 1.0,
|
||||
"zones": 0.6
|
||||
},
|
||||
"prototype_zone_fills": false,
|
||||
"selection_filter": {
|
||||
"dimensions": true,
|
||||
"footprints": true,
|
||||
@@ -53,6 +54,7 @@
|
||||
"zone_display_mode": 0
|
||||
},
|
||||
"git": {
|
||||
"integration_disabled": false,
|
||||
"repo_type": "",
|
||||
"repo_username": "",
|
||||
"ssh_key": ""
|
||||
@@ -105,6 +107,7 @@
|
||||
"filter_text": "",
|
||||
"group_by_constraint": false,
|
||||
"group_by_netclass": false,
|
||||
"show_time_domain_details": false,
|
||||
"show_unconnected_nets": false,
|
||||
"show_zero_pad_nets": false,
|
||||
"sort_ascending": true,
|
||||
@@ -115,6 +118,7 @@
|
||||
"files": []
|
||||
},
|
||||
"schematic": {
|
||||
"hierarchy_collapsed": [],
|
||||
"selection_filter": {
|
||||
"graphics": true,
|
||||
"images": true,
|
||||
@@ -122,6 +126,7 @@
|
||||
"lockedItems": false,
|
||||
"otherItems": true,
|
||||
"pins": true,
|
||||
"ruleAreas": true,
|
||||
"symbols": true,
|
||||
"text": true,
|
||||
"wires": true
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
"3dviewports": [],
|
||||
"design_settings": {
|
||||
"defaults": {
|
||||
"apply_defaults_to_fp_barcodes": false,
|
||||
"apply_defaults_to_fp_dimensions": false,
|
||||
"apply_defaults_to_fp_fields": false,
|
||||
"apply_defaults_to_fp_shapes": false,
|
||||
"apply_defaults_to_fp_text": false,
|
||||
@@ -82,6 +84,7 @@
|
||||
"extra_footprint": "warning",
|
||||
"footprint": "error",
|
||||
"footprint_filters_mismatch": "ignore",
|
||||
"footprint_symbol_field_mismatch": "warning",
|
||||
"footprint_symbol_mismatch": "warning",
|
||||
"footprint_type_mismatch": "warning",
|
||||
"hole_clearance": "error",
|
||||
@@ -99,6 +102,7 @@
|
||||
"mirrored_text_on_front_layer": "warning",
|
||||
"missing_courtyard": "ignore",
|
||||
"missing_footprint": "warning",
|
||||
"missing_tuning_profile": "warning",
|
||||
"net_conflict": "warning",
|
||||
"nonmirrored_text_on_back_layer": "warning",
|
||||
"npth_inside_courtyard": "ignore",
|
||||
@@ -118,9 +122,12 @@
|
||||
"too_many_vias": "error",
|
||||
"track_angle": "error",
|
||||
"track_dangling": "warning",
|
||||
"track_not_centered_on_via": "ignore",
|
||||
"track_on_post_machined_layer": "error",
|
||||
"track_segment_length": "error",
|
||||
"track_width": "error",
|
||||
"tracks_crossing": "error",
|
||||
"tuning_profile_track_geometries": "ignore",
|
||||
"unconnected_items": "error",
|
||||
"unresolved_variable": "error",
|
||||
"via_dangling": "warning",
|
||||
@@ -236,17 +243,28 @@
|
||||
"zones_allow_external_fillets": false
|
||||
},
|
||||
"ipc2581": {
|
||||
"bom_rev": "",
|
||||
"dist": "",
|
||||
"distpn": "",
|
||||
"internal_id": "",
|
||||
"mfg": "",
|
||||
"mpn": ""
|
||||
"mpn": "",
|
||||
"sch_revision": ""
|
||||
},
|
||||
"layer_pairs": [],
|
||||
"layer_presets": [],
|
||||
"viewports": []
|
||||
},
|
||||
"boards": [],
|
||||
"component_class_settings": {
|
||||
"assignments": [],
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"sheet_component_classes": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"cvpcb": {
|
||||
"equivalence_files": []
|
||||
},
|
||||
@@ -495,13 +513,14 @@
|
||||
"priority": 2147483647,
|
||||
"schematic_color": "rgba(0, 0, 0, 0.000)",
|
||||
"track_width": 0.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.6,
|
||||
"via_drill": 0.3,
|
||||
"wire_width": 6
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"version": 4
|
||||
"version": 5
|
||||
},
|
||||
"net_colors": null,
|
||||
"netclass_assignments": null,
|
||||
@@ -629,6 +648,7 @@
|
||||
"sort_asc": true,
|
||||
"sort_field": "Reference"
|
||||
},
|
||||
"bus_aliases": {},
|
||||
"connection_grid_size": 50.0,
|
||||
"drawing": {
|
||||
"dashed_lines_dash_length_ratio": 12.0,
|
||||
@@ -667,7 +687,14 @@
|
||||
"spice_save_all_dissipations": false,
|
||||
"spice_save_all_voltages": false,
|
||||
"subpart_first_id": 65,
|
||||
"subpart_id_separator": 0
|
||||
"subpart_id_separator": 0,
|
||||
"top_level_sheets": [
|
||||
{
|
||||
"filename": "PumpOutput.kicad_sch",
|
||||
"name": "PumpOutput",
|
||||
"uuid": "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sheets": [
|
||||
[
|
||||
@@ -675,5 +702,11 @@
|
||||
"Root"
|
||||
]
|
||||
],
|
||||
"text_variables": {}
|
||||
"text_variables": {},
|
||||
"tuning_profiles": {
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"tuning_profiles_impedance_geometric": []
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Hardware/Controller_Case/flap_v2.3mf
Normal file
BIN
Hardware/Controller_Case/flap_v2.3mf
Normal file
Binary file not shown.
@@ -15,6 +15,7 @@
|
||||
"vias": 1.0,
|
||||
"zones": 0.6
|
||||
},
|
||||
"prototype_zone_fills": false,
|
||||
"selection_filter": {
|
||||
"dimensions": true,
|
||||
"footprints": true,
|
||||
@@ -54,6 +55,7 @@
|
||||
"zone_display_mode": 0
|
||||
},
|
||||
"git": {
|
||||
"integration_disabled": false,
|
||||
"repo_type": "",
|
||||
"repo_username": "",
|
||||
"ssh_key": ""
|
||||
@@ -106,6 +108,7 @@
|
||||
"filter_text": "",
|
||||
"group_by_constraint": false,
|
||||
"group_by_netclass": false,
|
||||
"show_time_domain_details": false,
|
||||
"show_unconnected_nets": false,
|
||||
"show_zero_pad_nets": false,
|
||||
"sort_ascending": true,
|
||||
@@ -116,6 +119,7 @@
|
||||
"files": []
|
||||
},
|
||||
"schematic": {
|
||||
"hierarchy_collapsed": [],
|
||||
"selection_filter": {
|
||||
"graphics": true,
|
||||
"images": true,
|
||||
@@ -123,6 +127,7 @@
|
||||
"lockedItems": false,
|
||||
"otherItems": true,
|
||||
"pins": true,
|
||||
"ruleAreas": true,
|
||||
"symbols": true,
|
||||
"text": true,
|
||||
"wires": true
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
"3dviewports": [],
|
||||
"design_settings": {
|
||||
"defaults": {
|
||||
"apply_defaults_to_fp_barcodes": false,
|
||||
"apply_defaults_to_fp_dimensions": false,
|
||||
"apply_defaults_to_fp_fields": false,
|
||||
"apply_defaults_to_fp_shapes": false,
|
||||
"apply_defaults_to_fp_text": false,
|
||||
@@ -77,6 +79,7 @@
|
||||
"extra_footprint": "warning",
|
||||
"footprint": "error",
|
||||
"footprint_filters_mismatch": "warning",
|
||||
"footprint_symbol_field_mismatch": "warning",
|
||||
"footprint_symbol_mismatch": "warning",
|
||||
"footprint_type_mismatch": "warning",
|
||||
"hole_clearance": "error",
|
||||
@@ -94,6 +97,7 @@
|
||||
"mirrored_text_on_front_layer": "warning",
|
||||
"missing_courtyard": "warning",
|
||||
"missing_footprint": "warning",
|
||||
"missing_tuning_profile": "warning",
|
||||
"net_conflict": "warning",
|
||||
"nonmirrored_text_on_back_layer": "warning",
|
||||
"npth_inside_courtyard": "warning",
|
||||
@@ -113,9 +117,12 @@
|
||||
"too_many_vias": "error",
|
||||
"track_angle": "error",
|
||||
"track_dangling": "warning",
|
||||
"track_not_centered_on_via": "ignore",
|
||||
"track_on_post_machined_layer": "error",
|
||||
"track_segment_length": "error",
|
||||
"track_width": "error",
|
||||
"tracks_crossing": "error",
|
||||
"tuning_profile_track_geometries": "ignore",
|
||||
"unconnected_items": "error",
|
||||
"unresolved_variable": "error",
|
||||
"via_dangling": "warning",
|
||||
@@ -227,17 +234,28 @@
|
||||
"zones_allow_external_fillets": false
|
||||
},
|
||||
"ipc2581": {
|
||||
"bom_rev": "",
|
||||
"dist": "",
|
||||
"distpn": "",
|
||||
"internal_id": "",
|
||||
"mfg": "",
|
||||
"mpn": ""
|
||||
"mpn": "",
|
||||
"sch_revision": ""
|
||||
},
|
||||
"layer_pairs": [],
|
||||
"layer_presets": [],
|
||||
"viewports": []
|
||||
},
|
||||
"boards": [],
|
||||
"component_class_settings": {
|
||||
"assignments": [],
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"sheet_component_classes": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"cvpcb": {
|
||||
"equivalence_files": []
|
||||
},
|
||||
@@ -486,13 +504,14 @@
|
||||
"priority": 2147483647,
|
||||
"schematic_color": "rgba(0, 0, 0, 0.000)",
|
||||
"track_width": 0.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.6,
|
||||
"via_drill": 0.3,
|
||||
"wire_width": 6
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"version": 4
|
||||
"version": 5
|
||||
},
|
||||
"net_colors": null,
|
||||
"netclass_assignments": null,
|
||||
@@ -861,6 +880,7 @@
|
||||
"sort_asc": true,
|
||||
"sort_field": "Reference"
|
||||
},
|
||||
"bus_aliases": {},
|
||||
"connection_grid_size": 50.0,
|
||||
"drawing": {
|
||||
"dashed_lines_dash_length_ratio": 12.0,
|
||||
@@ -899,7 +919,14 @@
|
||||
"spice_save_all_dissipations": false,
|
||||
"spice_save_all_voltages": false,
|
||||
"subpart_first_id": 65,
|
||||
"subpart_id_separator": 0
|
||||
"subpart_id_separator": 0,
|
||||
"top_level_sheets": [
|
||||
{
|
||||
"filename": "sensor.kicad_sch",
|
||||
"name": "sensor",
|
||||
"uuid": "00000000-0000-0000-0000-000000000000"
|
||||
}
|
||||
]
|
||||
},
|
||||
"sheets": [
|
||||
[
|
||||
@@ -907,5 +934,11 @@
|
||||
"Root"
|
||||
]
|
||||
],
|
||||
"text_variables": {}
|
||||
"text_variables": {},
|
||||
"tuning_profiles": {
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"tuning_profiles_impedance_geometric": []
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
"board": {
|
||||
"active_layer": 0,
|
||||
"active_layer_preset": "",
|
||||
"auto_track_width": true,
|
||||
"auto_track_width": false,
|
||||
"hidden_netclasses": [],
|
||||
"hidden_nets": [],
|
||||
"high_contrast_mode": 0,
|
||||
@@ -15,6 +15,7 @@
|
||||
"vias": 1.0,
|
||||
"zones": 0.6
|
||||
},
|
||||
"prototype_zone_fills": false,
|
||||
"selection_filter": {
|
||||
"dimensions": true,
|
||||
"footprints": true,
|
||||
@@ -54,6 +55,7 @@
|
||||
"zone_display_mode": 0
|
||||
},
|
||||
"git": {
|
||||
"integration_disabled": false,
|
||||
"repo_type": "",
|
||||
"repo_username": "",
|
||||
"ssh_key": ""
|
||||
@@ -63,8 +65,30 @@
|
||||
"version": 5
|
||||
},
|
||||
"net_inspector_panel": {
|
||||
"col_hidden": [],
|
||||
"col_order": [],
|
||||
"col_hidden": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
],
|
||||
"col_order": [
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9
|
||||
],
|
||||
"col_widths": [],
|
||||
"custom_group_rules": [],
|
||||
"expanded_rows": [],
|
||||
@@ -73,6 +97,7 @@
|
||||
"filter_text": "",
|
||||
"group_by_constraint": false,
|
||||
"group_by_netclass": false,
|
||||
"show_time_domain_details": false,
|
||||
"show_unconnected_nets": false,
|
||||
"show_zero_pad_nets": false,
|
||||
"sort_ascending": true,
|
||||
@@ -83,6 +108,7 @@
|
||||
"files": []
|
||||
},
|
||||
"schematic": {
|
||||
"hierarchy_collapsed": [],
|
||||
"selection_filter": {
|
||||
"graphics": true,
|
||||
"images": true,
|
||||
@@ -90,6 +116,7 @@
|
||||
"lockedItems": false,
|
||||
"otherItems": true,
|
||||
"pins": true,
|
||||
"ruleAreas": true,
|
||||
"symbols": true,
|
||||
"text": true,
|
||||
"wires": true
|
||||
|
||||
@@ -2,25 +2,262 @@
|
||||
"board": {
|
||||
"3dviewports": [],
|
||||
"design_settings": {
|
||||
"defaults": {},
|
||||
"diff_pair_dimensions": [],
|
||||
"defaults": {
|
||||
"apply_defaults_to_fp_barcodes": false,
|
||||
"apply_defaults_to_fp_dimensions": false,
|
||||
"apply_defaults_to_fp_fields": false,
|
||||
"apply_defaults_to_fp_shapes": false,
|
||||
"apply_defaults_to_fp_text": false,
|
||||
"board_outline_line_width": 0.05,
|
||||
"copper_line_width": 0.2,
|
||||
"copper_text_italic": false,
|
||||
"copper_text_size_h": 1.5,
|
||||
"copper_text_size_v": 1.5,
|
||||
"copper_text_thickness": 0.3,
|
||||
"copper_text_upright": false,
|
||||
"courtyard_line_width": 0.05,
|
||||
"dimension_precision": 4,
|
||||
"dimension_units": 3,
|
||||
"dimensions": {
|
||||
"arrow_length": 1270000,
|
||||
"extension_offset": 500000,
|
||||
"keep_text_aligned": true,
|
||||
"suppress_zeroes": true,
|
||||
"text_position": 0,
|
||||
"units_format": 0
|
||||
},
|
||||
"fab_line_width": 0.1,
|
||||
"fab_text_italic": false,
|
||||
"fab_text_size_h": 1.0,
|
||||
"fab_text_size_v": 1.0,
|
||||
"fab_text_thickness": 0.15,
|
||||
"fab_text_upright": false,
|
||||
"other_line_width": 0.1,
|
||||
"other_text_italic": false,
|
||||
"other_text_size_h": 1.0,
|
||||
"other_text_size_v": 1.0,
|
||||
"other_text_thickness": 0.15,
|
||||
"other_text_upright": false,
|
||||
"pads": {
|
||||
"drill": 0.8,
|
||||
"height": 1.27,
|
||||
"width": 2.54
|
||||
},
|
||||
"silk_line_width": 0.1,
|
||||
"silk_text_italic": false,
|
||||
"silk_text_size_h": 1.0,
|
||||
"silk_text_size_v": 1.0,
|
||||
"silk_text_thickness": 0.1,
|
||||
"silk_text_upright": false,
|
||||
"zones": {
|
||||
"min_clearance": 0.5
|
||||
}
|
||||
},
|
||||
"diff_pair_dimensions": [
|
||||
{
|
||||
"gap": 0.0,
|
||||
"via_gap": 0.0,
|
||||
"width": 0.0
|
||||
}
|
||||
],
|
||||
"drc_exclusions": [],
|
||||
"rules": {},
|
||||
"track_widths": [],
|
||||
"via_dimensions": []
|
||||
"meta": {
|
||||
"version": 2
|
||||
},
|
||||
"rule_severities": {
|
||||
"annular_width": "error",
|
||||
"clearance": "error",
|
||||
"connection_width": "warning",
|
||||
"copper_edge_clearance": "error",
|
||||
"copper_sliver": "warning",
|
||||
"courtyards_overlap": "error",
|
||||
"creepage": "error",
|
||||
"diff_pair_gap_out_of_range": "error",
|
||||
"diff_pair_uncoupled_length_too_long": "error",
|
||||
"drill_out_of_range": "error",
|
||||
"duplicate_footprints": "warning",
|
||||
"extra_footprint": "warning",
|
||||
"footprint": "error",
|
||||
"footprint_filters_mismatch": "ignore",
|
||||
"footprint_symbol_field_mismatch": "warning",
|
||||
"footprint_symbol_mismatch": "warning",
|
||||
"footprint_type_mismatch": "ignore",
|
||||
"hole_clearance": "error",
|
||||
"hole_to_hole": "warning",
|
||||
"holes_co_located": "warning",
|
||||
"invalid_outline": "error",
|
||||
"isolated_copper": "warning",
|
||||
"item_on_disabled_layer": "error",
|
||||
"items_not_allowed": "error",
|
||||
"length_out_of_range": "error",
|
||||
"lib_footprint_issues": "warning",
|
||||
"lib_footprint_mismatch": "warning",
|
||||
"malformed_courtyard": "error",
|
||||
"microvia_drill_out_of_range": "error",
|
||||
"mirrored_text_on_front_layer": "warning",
|
||||
"missing_courtyard": "ignore",
|
||||
"missing_footprint": "warning",
|
||||
"missing_tuning_profile": "warning",
|
||||
"net_conflict": "warning",
|
||||
"nonmirrored_text_on_back_layer": "warning",
|
||||
"npth_inside_courtyard": "error",
|
||||
"padstack": "warning",
|
||||
"pth_inside_courtyard": "error",
|
||||
"shorting_items": "error",
|
||||
"silk_edge_clearance": "ignore",
|
||||
"silk_over_copper": "warning",
|
||||
"silk_overlap": "warning",
|
||||
"skew_out_of_range": "error",
|
||||
"solder_mask_bridge": "error",
|
||||
"starved_thermal": "error",
|
||||
"text_height": "warning",
|
||||
"text_on_edge_cuts": "error",
|
||||
"text_thickness": "warning",
|
||||
"through_hole_pad_without_hole": "error",
|
||||
"too_many_vias": "error",
|
||||
"track_angle": "error",
|
||||
"track_dangling": "warning",
|
||||
"track_not_centered_on_via": "ignore",
|
||||
"track_on_post_machined_layer": "error",
|
||||
"track_segment_length": "error",
|
||||
"track_width": "error",
|
||||
"tracks_crossing": "error",
|
||||
"tuning_profile_track_geometries": "ignore",
|
||||
"unconnected_items": "error",
|
||||
"unresolved_variable": "error",
|
||||
"via_dangling": "warning",
|
||||
"zones_intersect": "error"
|
||||
},
|
||||
"rules": {
|
||||
"max_error": 0.005,
|
||||
"min_clearance": 0.0,
|
||||
"min_connection": 0.0,
|
||||
"min_copper_edge_clearance": 0.5,
|
||||
"min_groove_width": 0.0,
|
||||
"min_hole_clearance": 0.25,
|
||||
"min_hole_to_hole": 0.25,
|
||||
"min_microvia_diameter": 0.2,
|
||||
"min_microvia_drill": 0.1,
|
||||
"min_resolved_spokes": 2,
|
||||
"min_silk_clearance": 0.0,
|
||||
"min_text_height": 0.8,
|
||||
"min_text_thickness": 0.08,
|
||||
"min_through_hole_diameter": 0.3,
|
||||
"min_track_width": 0.2,
|
||||
"min_via_annular_width": 0.1,
|
||||
"min_via_diameter": 0.5,
|
||||
"solder_mask_to_copper_clearance": 0.005,
|
||||
"use_height_for_length_calcs": true
|
||||
},
|
||||
"teardrop_options": [
|
||||
{
|
||||
"td_onpthpad": true,
|
||||
"td_onroundshapesonly": false,
|
||||
"td_onsmdpad": true,
|
||||
"td_ontrackend": false,
|
||||
"td_onvia": true
|
||||
}
|
||||
],
|
||||
"teardrop_parameters": [
|
||||
{
|
||||
"td_allow_use_two_tracks": true,
|
||||
"td_curve_segcount": 0,
|
||||
"td_height_ratio": 1.0,
|
||||
"td_length_ratio": 0.5,
|
||||
"td_maxheight": 2.0,
|
||||
"td_maxlen": 1.0,
|
||||
"td_on_pad_in_zone": false,
|
||||
"td_target_name": "td_round_shape",
|
||||
"td_width_to_size_filter_ratio": 0.9
|
||||
},
|
||||
{
|
||||
"td_allow_use_two_tracks": true,
|
||||
"td_curve_segcount": 0,
|
||||
"td_height_ratio": 1.0,
|
||||
"td_length_ratio": 0.5,
|
||||
"td_maxheight": 2.0,
|
||||
"td_maxlen": 1.0,
|
||||
"td_on_pad_in_zone": false,
|
||||
"td_target_name": "td_rect_shape",
|
||||
"td_width_to_size_filter_ratio": 0.9
|
||||
},
|
||||
{
|
||||
"td_allow_use_two_tracks": true,
|
||||
"td_curve_segcount": 0,
|
||||
"td_height_ratio": 1.0,
|
||||
"td_length_ratio": 0.5,
|
||||
"td_maxheight": 2.0,
|
||||
"td_maxlen": 1.0,
|
||||
"td_on_pad_in_zone": false,
|
||||
"td_target_name": "td_track_end",
|
||||
"td_width_to_size_filter_ratio": 0.9
|
||||
}
|
||||
],
|
||||
"track_widths": [
|
||||
0.0,
|
||||
0.2,
|
||||
0.5,
|
||||
1.0,
|
||||
2.0,
|
||||
5.0
|
||||
],
|
||||
"tuning_pattern_settings": {
|
||||
"diff_pair_defaults": {
|
||||
"corner_radius_percentage": 80,
|
||||
"corner_style": 1,
|
||||
"max_amplitude": 1.0,
|
||||
"min_amplitude": 0.2,
|
||||
"single_sided": false,
|
||||
"spacing": 1.0
|
||||
},
|
||||
"diff_pair_skew_defaults": {
|
||||
"corner_radius_percentage": 80,
|
||||
"corner_style": 1,
|
||||
"max_amplitude": 1.0,
|
||||
"min_amplitude": 0.2,
|
||||
"single_sided": false,
|
||||
"spacing": 0.6
|
||||
},
|
||||
"single_track_defaults": {
|
||||
"corner_radius_percentage": 80,
|
||||
"corner_style": 1,
|
||||
"max_amplitude": 1.0,
|
||||
"min_amplitude": 0.2,
|
||||
"single_sided": false,
|
||||
"spacing": 0.6
|
||||
}
|
||||
},
|
||||
"via_dimensions": [
|
||||
{
|
||||
"diameter": 0.0,
|
||||
"drill": 0.0
|
||||
}
|
||||
],
|
||||
"zones_allow_external_fillets": false
|
||||
},
|
||||
"ipc2581": {
|
||||
"bom_rev": "",
|
||||
"dist": "",
|
||||
"distpn": "",
|
||||
"internal_id": "",
|
||||
"mfg": "",
|
||||
"mpn": ""
|
||||
"mpn": "",
|
||||
"sch_revision": ""
|
||||
},
|
||||
"layer_pairs": [],
|
||||
"layer_presets": [],
|
||||
"viewports": []
|
||||
},
|
||||
"boards": [],
|
||||
"component_class_settings": {
|
||||
"assignments": [],
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"sheet_component_classes": {
|
||||
"enabled": false
|
||||
}
|
||||
},
|
||||
"cvpcb": {
|
||||
"equivalence_files": []
|
||||
},
|
||||
@@ -210,11 +447,14 @@
|
||||
"duplicate_sheet_names": "error",
|
||||
"endpoint_off_grid": "warning",
|
||||
"extra_units": "error",
|
||||
"field_name_whitespace": "warning",
|
||||
"footprint_filter": "ignore",
|
||||
"footprint_link_issues": "warning",
|
||||
"four_way_junction": "ignore",
|
||||
"global_label_dangling": "warning",
|
||||
"ground_pin_not_ground": "warning",
|
||||
"hier_label_mismatch": "error",
|
||||
"isolated_pin_label": "warning",
|
||||
"label_dangling": "error",
|
||||
"label_multiple_wires": "warning",
|
||||
"lib_symbol_issues": "warning",
|
||||
@@ -237,6 +477,7 @@
|
||||
"similar_power": "warning",
|
||||
"simulation_model_issue": "ignore",
|
||||
"single_global_label": "ignore",
|
||||
"stacked_pin_name": "warning",
|
||||
"unannotated": "error",
|
||||
"unconnected_wire_endpoint": "warning",
|
||||
"undefined_netclass": "error",
|
||||
@@ -269,13 +510,14 @@
|
||||
"priority": 2147483647,
|
||||
"schematic_color": "rgba(0, 0, 0, 0.000)",
|
||||
"track_width": 0.2,
|
||||
"tuning_profile": "",
|
||||
"via_diameter": 0.6,
|
||||
"via_drill": 0.3,
|
||||
"wire_width": 6
|
||||
}
|
||||
],
|
||||
"meta": {
|
||||
"version": 4
|
||||
"version": 5
|
||||
},
|
||||
"net_colors": null,
|
||||
"netclass_assignments": null,
|
||||
@@ -297,6 +539,10 @@
|
||||
},
|
||||
"schematic": {
|
||||
"annotate_start_num": 0,
|
||||
"annotation": {
|
||||
"method": 0,
|
||||
"sort_order": 0
|
||||
},
|
||||
"bom_export_filename": "${PROJECTNAME}.csv",
|
||||
"bom_fmt_presets": [],
|
||||
"bom_fmt_settings": {
|
||||
@@ -359,15 +605,298 @@
|
||||
"label": "Datasheet",
|
||||
"name": "Datasheet",
|
||||
"show": true
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Actuator/Cap Color",
|
||||
"name": "Actuator/Cap Color",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Attrition Qty",
|
||||
"name": "Attrition Qty",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Capacitance",
|
||||
"name": "Capacitance",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Category",
|
||||
"name": "Category",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Circuit",
|
||||
"name": "Circuit",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Class",
|
||||
"name": "Class",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Contact Current",
|
||||
"name": "Contact Current",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Diode Configuration",
|
||||
"name": "Diode Configuration",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Field5",
|
||||
"name": "Field5",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Forward Voltage (Vf@If)",
|
||||
"name": "Forward Voltage (Vf@If)",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Insulation Resistance",
|
||||
"name": "Insulation Resistance",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "LCSC",
|
||||
"name": "LCSC",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "LCSC_PART_NUMBER",
|
||||
"name": "LCSC_PART_NUMBER",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Manufacturer",
|
||||
"name": "Manufacturer",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Mechanical Life",
|
||||
"name": "Mechanical Life",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Minimum Qty",
|
||||
"name": "Minimum Qty",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Mounting Style",
|
||||
"name": "Mounting Style",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Operating Force",
|
||||
"name": "Operating Force",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Operating Temperature",
|
||||
"name": "Operating Temperature",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Operating Temperature Range",
|
||||
"name": "Operating Temperature Range",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Overload Voltage (Max)",
|
||||
"name": "Overload Voltage (Max)",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Part",
|
||||
"name": "Part",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Pin Style",
|
||||
"name": "Pin Style",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Power(Watts)",
|
||||
"name": "Power(Watts)",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Price",
|
||||
"name": "Price",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Process",
|
||||
"name": "Process",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Rectified Current",
|
||||
"name": "Rectified Current",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Resistance",
|
||||
"name": "Resistance",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Reverse Leakage Current",
|
||||
"name": "Reverse Leakage Current",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Reverse Voltage (Vr)",
|
||||
"name": "Reverse Voltage (Vr)",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Sim.Device",
|
||||
"name": "Sim.Device",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Sim.Pins",
|
||||
"name": "Sim.Pins",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Sim.Type",
|
||||
"name": "Sim.Type",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Stock",
|
||||
"name": "Stock",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Strike Gundam",
|
||||
"name": "Strike Gundam",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Switch Height",
|
||||
"name": "Switch Height",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Switch Length",
|
||||
"name": "Switch Length",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Switch Width",
|
||||
"name": "Switch Width",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Temperature Coefficient",
|
||||
"name": "Temperature Coefficient",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Tolerance",
|
||||
"name": "Tolerance",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Type",
|
||||
"name": "Type",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Voltage Rated",
|
||||
"name": "Voltage Rated",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Voltage Rating (Dc)",
|
||||
"name": "Voltage Rating (Dc)",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "With Lamp",
|
||||
"name": "With Lamp",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Actuator Style",
|
||||
"name": "Actuator Style",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "Description",
|
||||
"name": "Description",
|
||||
"show": false
|
||||
},
|
||||
{
|
||||
"group_by": false,
|
||||
"label": "#",
|
||||
"name": "${ITEM_NUMBER}",
|
||||
"show": false
|
||||
}
|
||||
],
|
||||
"filter_string": "",
|
||||
"group_symbols": true,
|
||||
"include_excluded_from_bom": true,
|
||||
"name": "Default Editing",
|
||||
"name": "",
|
||||
"sort_asc": true,
|
||||
"sort_field": "Reference"
|
||||
},
|
||||
"bus_aliases": {},
|
||||
"connection_grid_size": 50.0,
|
||||
"drawing": {
|
||||
"dashed_lines_dash_length_ratio": 12.0,
|
||||
@@ -375,6 +904,7 @@
|
||||
"default_line_thickness": 6.0,
|
||||
"default_text_size": 50.0,
|
||||
"field_names": [],
|
||||
"hop_over_size_choice": 0,
|
||||
"intersheets_ref_own_page": false,
|
||||
"intersheets_ref_prefix": "",
|
||||
"intersheets_ref_short": false,
|
||||
@@ -398,6 +928,7 @@
|
||||
"net_format_name": "",
|
||||
"page_layout_descr_file": "",
|
||||
"plot_directory": "",
|
||||
"reuse_designators": true,
|
||||
"space_save_all_events": true,
|
||||
"spice_current_sheet_as_root": false,
|
||||
"spice_external_command": "spice \"%I\"",
|
||||
@@ -406,13 +937,28 @@
|
||||
"spice_save_all_dissipations": false,
|
||||
"spice_save_all_voltages": false,
|
||||
"subpart_first_id": 65,
|
||||
"subpart_id_separator": 0
|
||||
"subpart_id_separator": 0,
|
||||
"top_level_sheets": [
|
||||
{
|
||||
"filename": "bms.kicad_sch",
|
||||
"name": "bms",
|
||||
"uuid": "7972d0e7-2611-420d-b298-ef8307db6186"
|
||||
}
|
||||
],
|
||||
"used_designators": "",
|
||||
"variants": []
|
||||
},
|
||||
"sheets": [
|
||||
[
|
||||
"7972d0e7-2611-420d-b298-ef8307db6186",
|
||||
"Root"
|
||||
"bms"
|
||||
]
|
||||
],
|
||||
"text_variables": {}
|
||||
"text_variables": {},
|
||||
"tuning_profiles": {
|
||||
"meta": {
|
||||
"version": 0
|
||||
},
|
||||
"tuning_profiles_impedance_geometric": []
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
1
Hardware/open-bms/bms/fabrication-toolkit-options.json
Normal file
1
Hardware/open-bms/bms/fabrication-toolkit-options.json
Normal file
@@ -0,0 +1 @@
|
||||
{"ARCHIVE_NAME": "", "EXTRA_LAYERS": "", "ALL_ACTIVE_LAYERS": false, "EXTEND_EDGE_CUT": false, "ALTERNATIVE_EDGE_CUT": false, "AUTO TRANSLATE": true, "AUTO FILL": true, "EXCLUDE DNP": false, "OPEN BROWSER": true, "NO_BACKUP_OPT": false}
|
||||
@@ -0,0 +1,337 @@
|
||||
(footprint "AMASS_XT30UPB+DATA-M_1x02_P5.0mm_Vertical"
|
||||
(version 20240108)
|
||||
(generator "pcbnew")
|
||||
(generator_version "8.0")
|
||||
(layer "F.Cu")
|
||||
(descr "Connector XT30 Vertical PCB Male, https://www.tme.eu/en/Document/4acc913878197f8c2e30d4b8cdc47230/XT30UPB%20SPEC.pdf")
|
||||
(tags "RC Connector XT30")
|
||||
(property "Reference" "REF**"
|
||||
(at 2.5 -4 0)
|
||||
(layer "F.SilkS")
|
||||
(uuid "f7510d54-dcb1-4c3b-b842-cd250a98370c")
|
||||
(effects
|
||||
(font
|
||||
(size 1 1)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Value" "AMASS_XT30UPB+DATA-M_1x02_P5.0mm_Vertical"
|
||||
(at 2.5 4 0)
|
||||
(layer "F.Fab")
|
||||
(uuid "c5a8a60c-4ea1-4401-a30c-34d36be61c07")
|
||||
(effects
|
||||
(font
|
||||
(size 1 1)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Footprint" ""
|
||||
(at 0 0 0)
|
||||
(unlocked yes)
|
||||
(layer "F.Fab")
|
||||
(hide yes)
|
||||
(uuid "8fb27306-c085-4316-b554-4ba9be794054")
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Datasheet" ""
|
||||
(at 0 0 0)
|
||||
(unlocked yes)
|
||||
(layer "F.Fab")
|
||||
(hide yes)
|
||||
(uuid "74b71861-05d2-4229-8e81-25952aaaef7e")
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(property "Description" ""
|
||||
(at 0 0 0)
|
||||
(unlocked yes)
|
||||
(layer "F.Fab")
|
||||
(hide yes)
|
||||
(uuid "a1d16ecc-7e64-48c4-b772-a9255380960d")
|
||||
(effects
|
||||
(font
|
||||
(size 1.27 1.27)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(attr through_hole)
|
||||
(fp_line
|
||||
(start -2.71 -1.41)
|
||||
(end -2.71 1.41)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "e96c6ad2-9ca6-4df1-b35b-76e090d7ff4e")
|
||||
)
|
||||
(fp_line
|
||||
(start -2.71 -1.41)
|
||||
(end -1.01 -2.71)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "0784d204-0a48-4a2b-8085-50e1ff7a1493")
|
||||
)
|
||||
(fp_line
|
||||
(start -2.71 1.41)
|
||||
(end -1.01 2.71)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "db750970-e424-4a8e-a882-20a90baabffc")
|
||||
)
|
||||
(fp_line
|
||||
(start -1.01 -2.71)
|
||||
(end 7.71 -2.71)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "9f23420e-db87-438c-a708-676a0616966e")
|
||||
)
|
||||
(fp_line
|
||||
(start -1.01 2.71)
|
||||
(end 7.71 2.71)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "2ba574e3-9de5-4d7d-9777-e19e6fa702e7")
|
||||
)
|
||||
(fp_line
|
||||
(start 7.71 -2.71)
|
||||
(end 7.71 2.71)
|
||||
(stroke
|
||||
(width 0.12)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.SilkS")
|
||||
(uuid "2018cc6b-6115-4763-8b7c-54b60affbda7")
|
||||
)
|
||||
(fp_rect
|
||||
(start -6.3 -2.71)
|
||||
(end 7.71 2.7)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type default)
|
||||
)
|
||||
(fill none)
|
||||
(layer "F.SilkS")
|
||||
(uuid "11aac399-c862-4b67-9828-087abeea5b1b")
|
||||
)
|
||||
(fp_line
|
||||
(start -3.1 -1.8)
|
||||
(end -3.1 1.8)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "06ae69d8-1372-4524-8b1a-27a2f062f1c5")
|
||||
)
|
||||
(fp_line
|
||||
(start -3.1 -1.8)
|
||||
(end -1.4 -3.1)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "ae869a92-c688-4f2b-82ca-0578106a035a")
|
||||
)
|
||||
(fp_line
|
||||
(start -3.1 1.8)
|
||||
(end -1.4 3.1)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "cac6d927-6ba1-4095-825b-f94ee0d7abe9")
|
||||
)
|
||||
(fp_line
|
||||
(start -1.4 -3.1)
|
||||
(end 8.1 -3.1)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "fb4fa373-5492-4717-a9fe-7b69f4c53ba0")
|
||||
)
|
||||
(fp_line
|
||||
(start -1.4 3.1)
|
||||
(end 8.1 3.1)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "70296c77-546d-44a2-b5d3-e6dc58cf713b")
|
||||
)
|
||||
(fp_line
|
||||
(start 8.1 -3.1)
|
||||
(end 8.1 3.1)
|
||||
(stroke
|
||||
(width 0.05)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.CrtYd")
|
||||
(uuid "64764a09-de32-4f35-b54a-17e44810370f")
|
||||
)
|
||||
(fp_line
|
||||
(start -2.6 -1.3)
|
||||
(end -2.6 1.3)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "8d7ee7cb-5dda-453f-aa9a-6420c87f1b8e")
|
||||
)
|
||||
(fp_line
|
||||
(start -2.6 -1.3)
|
||||
(end -0.9 -2.6)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "2fa3ad90-36bb-4374-95ed-e44e50c7e385")
|
||||
)
|
||||
(fp_line
|
||||
(start -2.6 1.3)
|
||||
(end -0.9 2.6)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "2e4f8556-ffc2-4791-91da-e68c3513337e")
|
||||
)
|
||||
(fp_line
|
||||
(start -0.9 -2.6)
|
||||
(end 7.6 -2.6)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "cb9cd8af-1997-41db-b9fe-8982960ac6db")
|
||||
)
|
||||
(fp_line
|
||||
(start -0.9 2.6)
|
||||
(end 7.6 2.6)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "ea8a6c02-e974-4677-a854-a0891c323245")
|
||||
)
|
||||
(fp_line
|
||||
(start 7.6 -2.6)
|
||||
(end 7.6 2.6)
|
||||
(stroke
|
||||
(width 0.1)
|
||||
(type solid)
|
||||
)
|
||||
(layer "F.Fab")
|
||||
(uuid "c66f305c-0d56-4591-bc02-23252ad20321")
|
||||
)
|
||||
(fp_text user "-"
|
||||
(at -4 0 0)
|
||||
(layer "F.SilkS")
|
||||
(uuid "c119570a-6846-48dc-9422-0b5665ab2df6")
|
||||
(effects
|
||||
(font
|
||||
(size 1.5 1.5)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(fp_text user "+"
|
||||
(at 9 0 0)
|
||||
(layer "F.SilkS")
|
||||
(uuid "d6ab678c-47f7-47e0-869b-ef3b9dbd1ba9")
|
||||
(effects
|
||||
(font
|
||||
(size 1.5 1.5)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(fp_text user "${REFERENCE}"
|
||||
(at 2.5 0 0)
|
||||
(layer "F.Fab")
|
||||
(uuid "a70efd12-1491-4664-ae98-5b2b7f52a502")
|
||||
(effects
|
||||
(font
|
||||
(size 1 1)
|
||||
(thickness 0.15)
|
||||
)
|
||||
)
|
||||
)
|
||||
(pad "1" thru_hole rect
|
||||
(at 0 0)
|
||||
(size 3 3)
|
||||
(drill 1.8)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(uuid "3a0f3b23-814b-4df9-a02c-3d9fed9e23c9")
|
||||
)
|
||||
(pad "2" thru_hole circle
|
||||
(at 5 0)
|
||||
(size 3 3)
|
||||
(drill 1.8)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(uuid "d897d74a-a13b-47cf-9806-eb8a75fe8d08")
|
||||
)
|
||||
(pad "3" thru_hole circle
|
||||
(at -3.9 -1)
|
||||
(size 1.524 1.524)
|
||||
(drill 1)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(uuid "02a8c3fc-d75c-47a4-a907-f9191ff19e2c")
|
||||
)
|
||||
(pad "4" thru_hole circle
|
||||
(at -3.9 1)
|
||||
(size 1.524 1.524)
|
||||
(drill 1)
|
||||
(layers "*.Cu" "*.Mask")
|
||||
(remove_unused_layers no)
|
||||
(uuid "2000b5b8-f7c9-40a8-9010-65c16d2aefce")
|
||||
)
|
||||
(model "${KICAD8_3DMODEL_DIR}/Connector_AMASS.3dshapes/AMASS_XT30UPB-M_1x02_P5.0mm_Vertical.wrl"
|
||||
(offset
|
||||
(xyz 0 0 0)
|
||||
)
|
||||
(scale
|
||||
(xyz 1 1 1)
|
||||
)
|
||||
(rotate
|
||||
(xyz 0 0 0)
|
||||
)
|
||||
)
|
||||
)
|
||||
4
Hardware/open-bms/bms/fp-lib-table
Normal file
4
Hardware/open-bms/bms/fp-lib-table
Normal file
@@ -0,0 +1,4 @@
|
||||
(fp_lib_table
|
||||
(version 7)
|
||||
(lib (name "amass") (type "KiCad") (uri "${KIPRJMOD}/footprints/amass") (options "") (descr ""))
|
||||
)
|
||||
@@ -8,4 +8,15 @@ fn main() {
|
||||
std::fs::write(out_dir.join("memory.x"), include_bytes!("memory.x")).unwrap();
|
||||
println!("cargo:rustc-link-search={}", out_dir.display());
|
||||
println!("cargo:rerun-if-changed=memory.x");
|
||||
|
||||
// Embed firmware build timestamp as minutes since Unix epoch (4 bytes, big-endian).
|
||||
// Dropping sub-minute precision keeps it in 4 bytes for many years.
|
||||
let build_seconds = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.expect("System time before UNIX_EPOCH")
|
||||
.as_secs();
|
||||
let build_minutes = (build_seconds / 60) as u32;
|
||||
let bytes = build_minutes.to_be_bytes();
|
||||
std::fs::write(out_dir.join("build_minutes.bin"), bytes).unwrap();
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
extern crate alloc;
|
||||
|
||||
use crate::hal::peripherals::CAN1;
|
||||
use canapi::id::{plant_id, IDENTIFY_CMD_OFFSET, MOISTURE_DATA_OFFSET};
|
||||
use canapi::id::{plant_id, FIRMWARE_BUILD_OFFSET, IDENTIFY_CMD_OFFSET, MOISTURE_DATA_OFFSET};
|
||||
use canapi::SensorSlot;
|
||||
use ch32_hal::adc::{Adc, SampleTime, ADC_MAX};
|
||||
use ch32_hal::{pac};
|
||||
@@ -47,6 +47,10 @@ static CAN_TX_CH: Channel<CriticalSectionRawMutex, CanFrame, 4> = Channel::new()
|
||||
|
||||
static BEACON: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Firmware build timestamp in minutes since Unix epoch, embedded at compile time.
|
||||
const FIRMWARE_BUILD_MINUTES: u32 =
|
||||
u32::from_be_bytes(*include_bytes!(concat!(env!("OUT_DIR"), "/build_minutes.bin")));
|
||||
|
||||
#[embassy_executor::main(entry = "qingke_rt::entry")]
|
||||
async fn main(spawner: Spawner) {
|
||||
ch32_hal::pac::AFIO.pcfr1().write(|w| w.set_can1_rm(2));
|
||||
@@ -111,6 +115,7 @@ async fn main(spawner: Spawner) {
|
||||
}
|
||||
let moisture_id = plant_id(MOISTURE_DATA_OFFSET, slot, addr as u16);
|
||||
let identify_id = plant_id(IDENTIFY_CMD_OFFSET, slot, addr as u16);
|
||||
let firmware_build_id = plant_id(FIRMWARE_BUILD_OFFSET, slot, addr as u16);
|
||||
let standard_identify_id = StandardId::new(identify_id).unwrap();
|
||||
|
||||
//is any floating, or invalid addr (only 1-8 are valid)
|
||||
@@ -251,12 +256,12 @@ async fn main(spawner: Spawner) {
|
||||
|
||||
// Improve CAN robustness for longer cables:
|
||||
// 1. Enable Automatic Bus-Off Management (ABOM)
|
||||
// 2. Disable Automatic Retransmission (NART) as we send regular measurements anyway
|
||||
// 2. Enable Automatic Retransmission (NART) to recover from transient errors
|
||||
// 3. Enable Receive FIFO Overwrite Mode (RFLM = 0, default)
|
||||
// 4. Increase Resync Jump Width (SJW) if possible by patching BTIMR
|
||||
hal::pac::CAN1.ctlr().modify(|w| {
|
||||
w.set_abom(false);
|
||||
w.set_nart(true);
|
||||
w.set_nart(false);
|
||||
});
|
||||
|
||||
// SJW is bits 24-25 of BTIMR. HAL sets it to 0 (SJW=1).
|
||||
@@ -269,8 +274,9 @@ async fn main(spawner: Spawner) {
|
||||
// filter.get(0).unwrap().set(Id::Standard(standard_identify_id), Default::default());
|
||||
// can.add_filter(filter);
|
||||
let standard_moisture_id = StandardId::new(moisture_id).unwrap();
|
||||
let standard_firmware_build_id = StandardId::new(firmware_build_id).unwrap();
|
||||
spawner
|
||||
.spawn(can_task(can,info, warn, standard_identify_id, standard_moisture_id))
|
||||
.spawn(can_task(can, info, warn, standard_identify_id, standard_moisture_id, standard_firmware_build_id))
|
||||
.unwrap();
|
||||
|
||||
// move Q output, LED, ADC and analog input into worker task
|
||||
@@ -282,6 +288,8 @@ async fn main(spawner: Spawner) {
|
||||
ain,
|
||||
standard_moisture_id,
|
||||
standard_identify_id,
|
||||
standard_firmware_build_id,
|
||||
slot,
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
@@ -362,6 +370,7 @@ async fn can_task(
|
||||
warn: &'static mut Output<'static>,
|
||||
identify_id: StandardId,
|
||||
moisture_id: StandardId,
|
||||
_firmware_build_id: StandardId,
|
||||
) {
|
||||
// Non-blocking beacon blink timing.
|
||||
// We keep this inside the CAN task so it can't stall other tasks (like `worker`) with `await`s.
|
||||
@@ -406,7 +415,7 @@ async fn can_task(
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
let mut msg: heapless::String<128> = heapless::String::new();
|
||||
let _ = write!(&mut msg, "rx err {:?}", err);
|
||||
let _ = write!(&mut msg, "rx err {:?} \r\n", err);
|
||||
log(msg);
|
||||
}
|
||||
}
|
||||
@@ -420,9 +429,25 @@ async fn can_task(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
while let Ok(mut frame) = CAN_TX_CH.try_receive() {
|
||||
match can.transmit(&mut frame) {
|
||||
Ok(..) => {
|
||||
Ok(_ok) => {
|
||||
let status = hal::pac::CAN1.errsr().read();
|
||||
|
||||
// Check CAN error status register for bus-off condition
|
||||
if status.boff() || status.ewgf() || status.epvf() {
|
||||
let mut msg: heapless::String<128> = heapless::String::new();
|
||||
let _ = write!(&mut msg, "canbus status {} {} {} \r\n", status.boff(), status.ewgf(), status.epvf());
|
||||
log(msg);
|
||||
for _ in 0..2 {
|
||||
warn.set_high();
|
||||
Timer::after_millis(100).await;
|
||||
warn.set_low();
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(nb::Error::WouldBlock) => {
|
||||
for _ in 0..2 {
|
||||
@@ -432,7 +457,7 @@ async fn can_task(
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
let mut msg: heapless::String<128> = heapless::String::new();
|
||||
let _ = write!(&mut msg, "canbus out buffer full");
|
||||
let _ = write!(&mut msg, "canbus out buffer full \r\n");
|
||||
log(msg);
|
||||
}
|
||||
Err(nb::Error::Other(err)) => {
|
||||
@@ -443,7 +468,7 @@ async fn can_task(
|
||||
Timer::after_millis(100).await;
|
||||
}
|
||||
let mut msg: heapless::String<128> = heapless::String::new();
|
||||
let _ = write!(&mut msg, "tx err {:?}", err);
|
||||
let _ = write!(&mut msg, "tx err {:?} \r\n", err);
|
||||
log(msg);
|
||||
}
|
||||
}
|
||||
@@ -460,72 +485,130 @@ async fn worker(
|
||||
mut ain: hal::peripherals::PA1,
|
||||
moisture_id: StandardId,
|
||||
identify_id: StandardId,
|
||||
firmware_build_id: StandardId,
|
||||
slot: SensorSlot,
|
||||
) {
|
||||
// 555 emulation state: Q initially Low
|
||||
let mut q_high = false;
|
||||
let low_th: u16 = (ADC_MAX as u16) / 3; // ~1/3 Vref
|
||||
let high_th: u16 = ((ADC_MAX as u32 * 2) / 3) as u16; // ~2/3 Vref
|
||||
|
||||
loop {
|
||||
// Count rising edges of Q in a 100 ms window
|
||||
let start = Instant::now();
|
||||
let mut pulses: u32 = 0;
|
||||
let mut last_q = q_high;
|
||||
const AVG_WINDOWS: u32 = 4;
|
||||
const YIELD_EVERY: u32 = 64;
|
||||
let probe_duration = Duration::from_millis(100);
|
||||
let measurement_time = probe_duration.as_millis() * AVG_WINDOWS as u64; // 400ms
|
||||
let interleaving_gap = Duration::from_millis(50);
|
||||
|
||||
probe_gnd.set_as_output(Speed::Low);
|
||||
probe_gnd.set_low();
|
||||
let probe_duration = Duration::from_millis(100);
|
||||
while Instant::now()
|
||||
.checked_duration_since(start)
|
||||
.unwrap_or(Duration::from_millis(0))
|
||||
< probe_duration
|
||||
{
|
||||
// Sample the analog input (Threshold/Trigger on A1)
|
||||
let val: u16 = adc.convert(&mut ain, SampleTime::CYCLES28_5);
|
||||
// Interleaving timing to ensure A and B never overlap:
|
||||
// - Sensor A: measures for 400ms
|
||||
// - Gap: 50ms
|
||||
// - Sensor B: measures for 400ms
|
||||
// - Gap: 50ms
|
||||
// Total cycle: 900ms, so each sensor measures every 900ms (~1.1 measurements/second)
|
||||
//
|
||||
// Timeline:
|
||||
// 0-400ms: A measures, B idle
|
||||
// 400-450ms: both idle (gap)
|
||||
// 450-850ms: B measures, A idle
|
||||
// 850-900ms: both idle (gap)
|
||||
// Then repeat from 0ms
|
||||
|
||||
// 555 core behavior:
|
||||
// - If input <= 1/3 Vref => set Q high (trigger)
|
||||
// - If input >= 2/3 Vref => set Q low (threshold)
|
||||
// - Otherwise keep previous Q state (hysteresis)
|
||||
if val <= low_th {
|
||||
q_high = true;
|
||||
} else if val >= high_th {
|
||||
q_high = false;
|
||||
}
|
||||
|
||||
// Drive output pin accordingly
|
||||
if q_high {
|
||||
q.set_high();
|
||||
} else {
|
||||
q.set_low();
|
||||
}
|
||||
|
||||
// Count rising edges
|
||||
if !last_q && q_high {
|
||||
pulses = pulses.saturating_add(1);
|
||||
}
|
||||
last_q = q_high;
|
||||
|
||||
// Yield to allow USB and other tasks to run
|
||||
yield_now().await;
|
||||
// Initial offset: B waits for A's measurement time + one gap
|
||||
match slot {
|
||||
SensorSlot::A => {
|
||||
// A sensors start measuring immediately
|
||||
}
|
||||
probe_gnd.set_as_input(Pull::None);
|
||||
SensorSlot::B => {
|
||||
// B sensors wait for A to finish measuring + gap
|
||||
Timer::after(Duration::from_millis(measurement_time + interleaving_gap.as_millis())).await;
|
||||
}
|
||||
}
|
||||
|
||||
let freq_hz: u32 = pulses * (1000 / probe_duration.as_millis()) as u32; // pulses per 0.1s => Hz
|
||||
loop {
|
||||
let mut total_pulses: u32 = 0;
|
||||
|
||||
for _window in 0..AVG_WINDOWS {
|
||||
// Count rising edges of Q in a 100 ms window
|
||||
let start = Instant::now();
|
||||
let mut pulses: u32 = 0;
|
||||
let mut last_q = q_high;
|
||||
let mut iter_count: u32 = 0;
|
||||
|
||||
probe_gnd.set_as_output(Speed::Low);
|
||||
probe_gnd.set_low();
|
||||
while Instant::now()
|
||||
.checked_duration_since(start)
|
||||
.unwrap_or(Duration::from_millis(0))
|
||||
< probe_duration
|
||||
{
|
||||
// Sample the analog input (Threshold/Trigger on A1)
|
||||
let val: u16 = adc.convert(&mut ain, SampleTime::CYCLES28_5);
|
||||
|
||||
// 555 core behavior:
|
||||
// - If input <= 1/3 Vref => set Q high (trigger)
|
||||
// - If input >= 2/3 Vref => set Q low (threshold)
|
||||
// - Otherwise keep previous Q state (hysteresis)
|
||||
if val <= low_th {
|
||||
q_high = true;
|
||||
} else if val >= high_th {
|
||||
q_high = false;
|
||||
}
|
||||
|
||||
// Drive output pin accordingly
|
||||
if q_high {
|
||||
q.set_high();
|
||||
} else {
|
||||
q.set_low();
|
||||
}
|
||||
|
||||
// Count rising edges
|
||||
if !last_q && q_high {
|
||||
pulses = pulses.saturating_add(1);
|
||||
}
|
||||
last_q = q_high;
|
||||
|
||||
// Yield every YIELD_EVERY samples to keep USB alive without
|
||||
// disrupting per-sample timing
|
||||
iter_count += 1;
|
||||
if iter_count % YIELD_EVERY == 0 {
|
||||
yield_now().await;
|
||||
}
|
||||
}
|
||||
probe_gnd.set_as_input(Pull::None);
|
||||
total_pulses = total_pulses.saturating_add(pulses);
|
||||
}
|
||||
|
||||
let avg_pulses = total_pulses / AVG_WINDOWS;
|
||||
let freq_hz: u32 = avg_pulses * (1000 / probe_duration.as_millis()) as u32;
|
||||
|
||||
let mut msg: heapless::String<128> = heapless::String::new();
|
||||
let _ = write!(
|
||||
&mut msg,
|
||||
"555 window={}ms pulses={} freq={} Hz (A1->Q on PB0) id={:?}\r\n",
|
||||
probe_duration.as_millis(),
|
||||
pulses,
|
||||
"555 window={}ms avg_pulses={} freq={} Hz (A1->Q on PB0) id={:?}\r\n",
|
||||
probe_duration.as_millis() * AVG_WINDOWS as u64,
|
||||
avg_pulses,
|
||||
freq_hz,
|
||||
identify_id.as_raw()
|
||||
);
|
||||
log(msg);
|
||||
|
||||
let moisture = CanFrame::new(moisture_id, &(freq_hz as u32).to_be_bytes()).unwrap();
|
||||
let delay_ms = moisture_id.as_raw() as u64 % 50;
|
||||
Timer::after(Duration::from_millis(delay_ms)).await;
|
||||
CAN_TX_CH.send(moisture).await;
|
||||
|
||||
// Send firmware build timestamp after each measurement so the controller
|
||||
// always has up-to-date build info without requiring an identify request.
|
||||
let firmware = CanFrame::new(firmware_build_id, &FIRMWARE_BUILD_MINUTES.to_be_bytes()).unwrap();
|
||||
let delay_ms = firmware_build_id.as_raw() as u64 % 50;
|
||||
Timer::after(Duration::from_millis(delay_ms)).await;
|
||||
CAN_TX_CH.send(firmware).await;
|
||||
|
||||
// Wait for the other slot to measure, plus gaps to ensure no overlap
|
||||
// After A finishes measuring: wait 50ms (gap) + 400ms (B measures) + 50ms (gap) = 500ms
|
||||
// After B finishes measuring: wait 50ms (gap) + 400ms (A measures) + 50ms (gap) = 500ms
|
||||
// This ensures the full 900ms cycle is maintained and A/B never overlap
|
||||
Timer::after(Duration::from_millis(interleaving_gap.as_millis() + measurement_time + interleaving_gap.as_millis())).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1
Software/MainBoard/rust/.idea/vcs.xml
generated
1
Software/MainBoard/rust/.idea/vcs.xml
generated
@@ -2,6 +2,5 @@
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/../../../website/themes/blowfish" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
1103
Software/MainBoard/rust/Cargo.lock
generated
1103
Software/MainBoard/rust/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -46,57 +46,34 @@ canapi = { path = "../../Shared/canapi" }
|
||||
|
||||
# Platform and ESP-specific runtime/boot/runtime utilities
|
||||
log = "0.4.28"
|
||||
esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32c6", "log-04"] }
|
||||
esp-hal = { version = "1.0.0", features = ["esp32c6", "log-04"] }
|
||||
esp-rtos = { version = "0.2.0", features = ["esp32c6", "embassy", "esp-radio"] }
|
||||
esp-backtrace = { version = "0.18.1", features = ["esp32c6", "panic-handler", "println", "colors", "custom-halt"] }
|
||||
esp-println = { version = "0.16.1", features = ["esp32c6", "log-04", "auto"] }
|
||||
esp-storage = { version = "0.8.1", features = ["esp32c6"] }
|
||||
esp-radio = { version = "0.17.0", features = ["esp32c6", "log-04", "smoltcp", "wifi", "unstable"] }
|
||||
esp-alloc = { version = "0.9.0", features = ["esp32c6", "internal-heap-stats"] }
|
||||
esp-bootloader-esp-idf = { version = "0.5.0", features = ["esp32c6", "log-04"] }
|
||||
esp-hal = { version = "1.1.0", features = ["esp32c6", "log-04"] }
|
||||
esp-rtos = { version = "0.3.0", features = ["esp32c6", "embassy", "esp-radio"] }
|
||||
esp-backtrace = { version = "0.19.0", features = ["esp32c6", "panic-handler", "println", "colors", "custom-halt"] }
|
||||
esp-println = { version = "0.17.0", features = ["esp32c6", "log-04", "auto"] }
|
||||
esp-storage = { version = "0.9.0", features = ["esp32c6"] }
|
||||
esp-radio = { version = "0.18.0", features = ["esp32c6", "log-04", "wifi", "unstable"] }
|
||||
esp-alloc = { version = "0.10.0", features = ["esp32c6", "internal-heap-stats"] }
|
||||
|
||||
# Async runtime (Embassy core)
|
||||
embassy-executor = { version = "0.9.1", features = ["log", "nightly"] }
|
||||
embassy-time = { version = "0.5.0", features = ["log"], default-features = false }
|
||||
embassy-sync = { version = "0.7.2", features = ["log"] }
|
||||
embassy-executor = { version = "0.10.0", features = ["log", "nightly"] }
|
||||
embassy-time = { version = "0.5.1", features = ["log"], default-features = false }
|
||||
embassy-sync = { version = "0.8.0", features = ["log"] }
|
||||
|
||||
# Networking and protocol stacks
|
||||
embassy-net = { version = "0.7.1", features = [
|
||||
"dhcpv4",
|
||||
"log",
|
||||
"medium-ethernet",
|
||||
"tcp",
|
||||
"udp",
|
||||
"proto-ipv4",
|
||||
"dns"
|
||||
] }
|
||||
smoltcp = { version = "0.12.0", default-features = false, features = [
|
||||
"alloc",
|
||||
"log",
|
||||
"medium-ethernet",
|
||||
"multicast",
|
||||
"proto-dhcpv4",
|
||||
"proto-ipv6",
|
||||
"proto-dns",
|
||||
"proto-ipv4",
|
||||
"socket-dns",
|
||||
"socket-icmp",
|
||||
"socket-raw",
|
||||
"socket-tcp",
|
||||
"socket-udp",
|
||||
] }
|
||||
embassy-net = { version = "0.8.0", features = ["dhcpv4", "log", "medium-ethernet", "tcp", "udp", "proto-ipv4", "dns", "proto-ipv6"] }
|
||||
sntpc = { version = "0.6.1", default-features = false, features = ["log", "embassy-socket", "embassy-socket-ipv6"] }
|
||||
edge-dhcp = "0.6.0"
|
||||
edge-nal = "0.5.0"
|
||||
edge-nal-embassy = "0.6.0"
|
||||
edge-http = { version = "0.6.1", features = ["log"] }
|
||||
edge-dhcp = "0.7.0"
|
||||
edge-nal = "0.6.0"
|
||||
edge-nal-embassy = "0.8.1"
|
||||
edge-http = { version = "0.7.0", features = ["log"] }
|
||||
|
||||
esp32c6 = { version = "0.22.0" }
|
||||
esp32c6 = { version = "0.23.2" }
|
||||
|
||||
# Hardware abstraction traits and HAL adapters
|
||||
embedded-hal = "1.0.0"
|
||||
embedded-storage = "0.3.1"
|
||||
embassy-embedded-hal = "0.5.0"
|
||||
embassy-embedded-hal = "0.6.0"
|
||||
embedded-can = "0.4.1"
|
||||
nb = "1.1.0"
|
||||
|
||||
@@ -114,7 +91,7 @@ embedded-savegame = { version = "0.3.0" }
|
||||
# Serialization / codecs
|
||||
serde = { version = "1.0.228", features = ["derive", "alloc"], default-features = false }
|
||||
serde_json = { version = "1.0.145", default-features = false, features = ["alloc"] }
|
||||
bincode = { version = "2.0.1", default-features = false, features = ["derive", "alloc"] }
|
||||
postcard = { version = "1.1.3", default-features = false, features = ["alloc"] }
|
||||
|
||||
# Time and time zones
|
||||
chrono = { version = "0.4.42", default-features = false, features = ["iana-time-zone", "alloc", "serde"] }
|
||||
@@ -124,6 +101,7 @@ chrono-tz = { version = "0.10.4", default-features = false, features = ["filter-
|
||||
heapless = { version = "0.7.17", features = ["serde"] } # stay in sync with mcutie version
|
||||
static_cell = "2.1.1"
|
||||
portable-atomic = "1.11.1"
|
||||
critical-section = "1"
|
||||
crc = "3.3.0"
|
||||
bytemuck = { version = "1.24.0", features = ["derive", "min_const_generics", "pod_saturating", "extern_crate_alloc"] }
|
||||
deranged = "0.5.5"
|
||||
@@ -132,13 +110,8 @@ unit-enum = "1.4.3"
|
||||
async-trait = "0.1.89"
|
||||
option-lock = { version = "0.3.1", default-features = false }
|
||||
measurements = "0.11.1"
|
||||
mcutie = { path = "src/mcutie_3_0_0", features = ["log"] }
|
||||
|
||||
# Project-specific
|
||||
mcutie = { version = "0.3.0", default-features = false, features = ["log", "homeassistant"] }
|
||||
no-panic = "0.1.36"
|
||||
|
||||
[patch.crates-io]
|
||||
mcutie = { git = 'https://github.com/empirephoenix/mcutie.git' }
|
||||
#bq34z100 = { path = "../../bq34z100_rust" }
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
3
Software/MainBoard/rust/TODO
Normal file
3
Software/MainBoard/rust/TODO
Normal file
@@ -0,0 +1,3 @@
|
||||
One Wire does not seem to work.
|
||||
Flow Sensor does not seem to work.
|
||||
PlantProfiles with a dry out phase needs to be implemented + Memory for this
|
||||
@@ -49,5 +49,8 @@ fn linker_be_nice() {
|
||||
|
||||
fn main() {
|
||||
linker_be_nice();
|
||||
// Non-existent path causes Cargo to always re-run this script,
|
||||
// keeping VERGEN_BUILD_TIMESTAMP fresh on every build.
|
||||
println!("cargo:rerun-if-changed=ALWAYS_REBUILD_SENTINEL");
|
||||
let _ = EmitBuilder::builder().all_git().all_build().emit();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use crate::hal::PLANT_COUNT;
|
||||
use crate::plant_state::PlantWateringMode;
|
||||
use alloc::string::{String, ToString};
|
||||
use bincode::{Decode, Encode};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct NetworkConfig {
|
||||
pub ap_ssid: String,
|
||||
@@ -31,7 +30,7 @@ impl Default for NetworkConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct NightLampConfig {
|
||||
pub enabled: bool,
|
||||
@@ -54,7 +53,7 @@ impl Default for NightLampConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct TankConfig {
|
||||
pub tank_sensor_enabled: bool,
|
||||
@@ -79,20 +78,20 @@ impl Default for TankConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
||||
pub enum BatteryBoardVersion {
|
||||
#[default]
|
||||
Disabled,
|
||||
WchI2cSlave,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
||||
pub enum BoardVersion {
|
||||
Initial,
|
||||
#[default]
|
||||
V4,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
||||
pub struct BoardHardware {
|
||||
pub board: BoardVersion,
|
||||
pub battery: BatteryBoardVersion,
|
||||
@@ -100,7 +99,7 @@ pub struct BoardHardware {
|
||||
pub pump_corrosion_protection: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
||||
#[serde(default)]
|
||||
pub struct PlantControllerConfig {
|
||||
pub hardware: BoardHardware,
|
||||
@@ -111,7 +110,7 @@ pub struct PlantControllerConfig {
|
||||
pub timezone: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||
#[serde(default)]
|
||||
pub struct PlantConfig {
|
||||
pub mode: PlantWateringMode,
|
||||
@@ -130,6 +129,8 @@ pub struct PlantConfig {
|
||||
pub min_pump_current_ma: u16,
|
||||
pub max_pump_current_ma: u16,
|
||||
pub ignore_current_error: bool,
|
||||
pub fertilizer_s: u16,
|
||||
pub fertilizer_cooldown_min: u16,
|
||||
}
|
||||
|
||||
impl Default for PlantConfig {
|
||||
@@ -151,6 +152,8 @@ impl Default for PlantConfig {
|
||||
min_pump_current_ma: 10,
|
||||
max_pump_current_ma: 3000,
|
||||
ignore_current_error: true,
|
||||
fertilizer_s: 0,
|
||||
fertilizer_cooldown_min: 1440, // 1 day default
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,16 +230,8 @@ impl<E: fmt::Debug> From<ExpanderError<I2cDeviceError<E>>> for FatError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bincode::error::DecodeError> for FatError {
|
||||
fn from(value: bincode::error::DecodeError) -> Self {
|
||||
FatError::Eeprom24x {
|
||||
error: format!("{value:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bincode::error::EncodeError> for FatError {
|
||||
fn from(value: bincode::error::EncodeError) -> Self {
|
||||
impl From<postcard::Error> for FatError {
|
||||
fn from(value: postcard::Error) -> Self {
|
||||
FatError::Eeprom24x {
|
||||
error: format!("{value:?}"),
|
||||
}
|
||||
@@ -324,9 +316,12 @@ impl From<sntpc::Error> for FatError {
|
||||
impl From<BmsProtocolError> for FatError {
|
||||
fn from(value: BmsProtocolError) -> Self {
|
||||
match value {
|
||||
BmsProtocolError::I2cCommunicationError => FatError::String {
|
||||
BmsProtocolError::I2cCommunicationError =>FatError::String {
|
||||
error: "I2C communication error".to_string(),
|
||||
},
|
||||
BmsProtocolError::ChecksumError => FatError::String {
|
||||
error: "BMS checksum error".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@ use alloc::{format, string::String, vec, vec::Vec};
|
||||
use core::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use core::sync::atomic::Ordering;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::udp::UdpSocket;
|
||||
use embassy_net::{DhcpConfig, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
|
||||
use embassy_net::dns::DnsQueryType;
|
||||
use embassy_net::udp::{PacketMetadata, UdpSocket};
|
||||
use embassy_net::{DhcpConfig, IpAddress, Ipv4Cidr, Runner, Stack, StackResources, StaticConfigV4};
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
@@ -33,25 +34,21 @@ use esp_hal::system::software_reset;
|
||||
use esp_hal::uart::Uart;
|
||||
use esp_hal::Blocking;
|
||||
use esp_println::println;
|
||||
use esp_radio::wifi::{
|
||||
AccessPointConfig, AccessPointInfo, AuthMethod, ClientConfig, ModeConfig, ScanConfig,
|
||||
ScanTypeConfig, WifiController, WifiDevice, WifiStaState,
|
||||
};
|
||||
use esp_radio::wifi::ap::{AccessPointConfig, AccessPointInfo};
|
||||
use esp_radio::wifi::scan::{ScanConfig, ScanTypeConfig};
|
||||
use esp_radio::wifi::sta::StationConfig;
|
||||
use esp_radio::wifi::{AuthenticationMethod, Config, Interface, WifiController};
|
||||
use log::{error, info, warn};
|
||||
use mcutie::{
|
||||
Error, McutieBuilder, McutieReceiver, McutieTask, MqttMessage, PublishDisplay, Publishable,
|
||||
QoS, Topic,
|
||||
};
|
||||
use portable_atomic::AtomicBool;
|
||||
use smoltcp::socket::udp::PacketMetadata;
|
||||
use smoltcp::wire::DnsQueryType;
|
||||
use sntpc::{get_time, NtpContext, NtpTimestampGenerator};
|
||||
use sntpc::{get_time, NtpContext, NtpTimestampGenerator, NtpUdpSocket};
|
||||
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut CONSECUTIVE_WATERING_PLANT: [u32; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut LAST_FERTILIZER_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut LOW_VOLTAGE_DETECTED: i8 = 0;
|
||||
#[esp_hal::ram(unstable(rtc_fast), unstable(persistent))]
|
||||
static mut RESTART_TO_CONF: i8 = 0;
|
||||
@@ -60,16 +57,44 @@ static mut LAST_CORROSION_PROTECTION_CHECK_DAY: i8 = -1;
|
||||
|
||||
const NTP_SERVER: &str = "pool.ntp.org";
|
||||
|
||||
static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
||||
static MQTT_BASE_TOPIC: OnceLock<String> = OnceLock::new();
|
||||
|
||||
#[derive(Copy, Clone, Default)]
|
||||
struct Timestamp {
|
||||
stamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
struct EmbassyNtpSocket<'a, 'b> {
|
||||
socket: &'a UdpSocket<'b>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> EmbassyNtpSocket<'a, 'b> {
|
||||
fn new(socket: &'a UdpSocket<'b>) -> Self {
|
||||
Self { socket }
|
||||
}
|
||||
}
|
||||
|
||||
impl NtpUdpSocket for EmbassyNtpSocket<'_, '_> {
|
||||
async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> sntpc::Result<usize> {
|
||||
self.socket
|
||||
.send_to(buf, addr)
|
||||
.await
|
||||
.map_err(|_| sntpc::Error::Network)?;
|
||||
Ok(buf.len())
|
||||
}
|
||||
|
||||
async fn recv_from(&self, buf: &mut [u8]) -> sntpc::Result<(usize, SocketAddr)> {
|
||||
let (len, metadata) = self
|
||||
.socket
|
||||
.recv_from(buf)
|
||||
.await
|
||||
.map_err(|_| sntpc::Error::Network)?;
|
||||
let addr = match metadata.endpoint.addr {
|
||||
IpAddress::Ipv4(ip) => IpAddr::V4(ip),
|
||||
IpAddress::Ipv6(ip) => IpAddr::V6(ip),
|
||||
};
|
||||
Ok((len, SocketAddr::new(addr, metadata.endpoint.port)))
|
||||
}
|
||||
}
|
||||
|
||||
// Minimal esp-idf equivalent for gpio_hold on esp32c6 via ROM functions
|
||||
extern "C" {
|
||||
fn gpio_pad_hold(gpio_num: u32);
|
||||
@@ -104,8 +129,8 @@ pub struct Esp<'a> {
|
||||
pub savegame: SavegameManager,
|
||||
pub rng: Rng,
|
||||
//first starter (ap or sta will take these)
|
||||
pub interface_sta: Option<WifiDevice<'static>>,
|
||||
pub interface_ap: Option<WifiDevice<'static>>,
|
||||
pub interface_sta: Option<Interface<'static>>,
|
||||
pub interface_ap: Option<Interface<'static>>,
|
||||
pub controller: Arc<Mutex<CriticalSectionRawMutex, WifiController<'static>>>,
|
||||
|
||||
pub boot_button: Input<'a>,
|
||||
@@ -249,6 +274,7 @@ impl Esp<'_> {
|
||||
socket.bind(123).context("Could not bind UDP socket")?;
|
||||
|
||||
let context = NtpContext::new(Timestamp::default());
|
||||
let ntp_socket = EmbassyNtpSocket::new(&socket);
|
||||
|
||||
let ntp_addrs = stack
|
||||
.dns_query(NTP_SERVER, DnsQueryType::A)
|
||||
@@ -264,7 +290,7 @@ impl Esp<'_> {
|
||||
let mut counter = 0;
|
||||
loop {
|
||||
let addr: IpAddr = ntp.into();
|
||||
let timeout = get_time(SocketAddr::from((addr, 123)), &socket, context)
|
||||
let timeout = get_time(SocketAddr::from((addr, 123)), &ntp_socket, context)
|
||||
.with_timeout(Duration::from_millis((_max_wait_ms / 10) as u64))
|
||||
.await;
|
||||
|
||||
@@ -292,10 +318,10 @@ impl Esp<'_> {
|
||||
let mut lock = self.controller.try_lock()?;
|
||||
info!("start wifi scan lock");
|
||||
let scan_config = ScanConfig::default().with_scan_type(ScanTypeConfig::Active {
|
||||
min: Default::default(),
|
||||
max: Default::default(),
|
||||
min: esp_hal::time::Duration::from_millis(0),
|
||||
max: esp_hal::time::Duration::from_millis(0),
|
||||
});
|
||||
let rv = lock.scan_with_config_async(scan_config).await?;
|
||||
let rv = lock.scan_async(&scan_config).await?;
|
||||
info!("end wifi scan lock");
|
||||
Ok(rv)
|
||||
}
|
||||
@@ -309,6 +335,14 @@ impl Esp<'_> {
|
||||
LAST_WATERING_TIMESTAMP[plant] = time.timestamp_millis();
|
||||
}
|
||||
}
|
||||
pub(crate) fn last_fertilizer_time(&self, plant: usize) -> i64 {
|
||||
unsafe { LAST_FERTILIZER_TIMESTAMP[plant] }
|
||||
}
|
||||
pub(crate) fn store_last_fertilizer_time(&mut self, plant: usize, time: DateTime<Utc>) {
|
||||
unsafe {
|
||||
LAST_FERTILIZER_TIMESTAMP[plant] = time.timestamp_millis();
|
||||
}
|
||||
}
|
||||
pub(crate) fn set_low_voltage_in_cycle(&mut self) {
|
||||
unsafe {
|
||||
LOW_VOLTAGE_DETECTED = 1;
|
||||
@@ -383,15 +417,13 @@ impl Esp<'_> {
|
||||
let stack = mk_static!(Stack, stack);
|
||||
|
||||
let client_config =
|
||||
ModeConfig::AccessPoint(AccessPointConfig::default().with_ssid(ssid.clone()));
|
||||
Config::AccessPoint(AccessPointConfig::default().with_ssid(ssid.clone()));
|
||||
self.controller.lock().await.set_config(&client_config)?;
|
||||
|
||||
println!("start new");
|
||||
self.controller.lock().await.start()?;
|
||||
println!("start net task");
|
||||
spawner.spawn(net_task(runner)).ok();
|
||||
spawner.spawn(net_task(runner)?);
|
||||
println!("run dhcp");
|
||||
spawner.spawn(run_dhcp(*stack, gw_ip_addr)).ok();
|
||||
spawner.spawn(run_dhcp(*stack, gw_ip_addr)?);
|
||||
|
||||
loop {
|
||||
if stack.is_link_up() {
|
||||
@@ -451,52 +483,28 @@ impl Esp<'_> {
|
||||
);
|
||||
let stack = mk_static!(Stack, stack);
|
||||
|
||||
let client_config = ClientConfig::default()
|
||||
let auth_method = if password.is_empty() {
|
||||
AuthenticationMethod::None
|
||||
} else {
|
||||
AuthenticationMethod::Wpa2Personal
|
||||
};
|
||||
let client_config = StationConfig::default()
|
||||
.with_ssid(ssid)
|
||||
.with_auth_method(AuthMethod::Wpa2Personal)
|
||||
.with_auth_method(auth_method)
|
||||
.with_password(password);
|
||||
|
||||
self.controller
|
||||
.lock()
|
||||
.await
|
||||
.set_config(&ModeConfig::Client(client_config))?;
|
||||
spawner.spawn(net_task(runner)).ok();
|
||||
self.controller.lock().await.start_async().await?;
|
||||
|
||||
let res = async {
|
||||
loop {
|
||||
let state = esp_radio::wifi::sta_state();
|
||||
if state == WifiStaState::Started {
|
||||
self.controller.lock().await.connect()?;
|
||||
break;
|
||||
}
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(max_wait as u64 * 1000))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
bail!("Timeout waiting for wifi sta ready")
|
||||
}
|
||||
|
||||
let res = async {
|
||||
loop {
|
||||
let state = esp_radio::wifi::sta_state();
|
||||
if state == WifiStaState::Connected {
|
||||
break;
|
||||
}
|
||||
Timer::after(Duration::from_millis(500)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(max_wait as u64 * 1000))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
bail!("Timeout waiting for wifi sta connected")
|
||||
}
|
||||
.set_config(&Config::Station(client_config))?;
|
||||
spawner.spawn(net_task(runner)?);
|
||||
self.controller
|
||||
.lock()
|
||||
.await
|
||||
.connect_async()
|
||||
.with_timeout(Duration::from_millis(max_wait as u64 * 1000))
|
||||
.await
|
||||
.context("Timeout waiting for wifi sta connected")??;
|
||||
|
||||
let res = async {
|
||||
while !stack.is_link_up() {
|
||||
@@ -575,7 +583,15 @@ impl Esp<'_> {
|
||||
/// Retries once on flash error.
|
||||
pub(crate) async fn save_config(&mut self, config: Vec<u8>) -> FatResult<()> {
|
||||
let timestamp = self.get_time().to_rfc3339();
|
||||
self.savegame.save(config.as_slice(), ×tamp)
|
||||
self.savegame.save(config.as_slice(), ×tamp)?;
|
||||
|
||||
match self.savegame.load_latest()? {
|
||||
None => bail!("Config save verification failed: no latest save found"),
|
||||
Some(data) => {
|
||||
let _: PlantControllerConfig = serde_json::from_slice(&data)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a specific save slot by erasing it on flash.
|
||||
@@ -640,234 +656,10 @@ impl Esp<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn mqtt(
|
||||
&mut self,
|
||||
network_config: &'static NetworkConfig,
|
||||
stack: Stack<'static>,
|
||||
spawner: Spawner,
|
||||
) -> FatResult<()> {
|
||||
let base_topic = network_config
|
||||
.base_topic
|
||||
.as_ref()
|
||||
.context("missing base topic")?;
|
||||
if base_topic.is_empty() {
|
||||
bail!("Mqtt base_topic was empty")
|
||||
}
|
||||
MQTT_BASE_TOPIC
|
||||
.init(base_topic.to_string())
|
||||
.map_err(|_| FatError::String {
|
||||
error: "Error setting basetopic".to_string(),
|
||||
})?;
|
||||
|
||||
let mqtt_url = network_config
|
||||
.mqtt_url
|
||||
.as_ref()
|
||||
.context("missing mqtt url")?;
|
||||
if mqtt_url.is_empty() {
|
||||
bail!("Mqtt url was empty")
|
||||
}
|
||||
|
||||
let last_will_topic = format!("{base_topic}/state");
|
||||
let round_trip_topic = format!("{base_topic}/internal/roundtrip");
|
||||
let stay_alive_topic = format!("{base_topic}/stay_alive");
|
||||
|
||||
let mut builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 0> =
|
||||
McutieBuilder::new(stack, "plant ctrl", mqtt_url);
|
||||
if let (Some(mqtt_user), Some(mqtt_password)) = (
|
||||
network_config.mqtt_user.as_ref(),
|
||||
network_config.mqtt_password.as_ref(),
|
||||
) {
|
||||
builder = builder.with_authentication(mqtt_user, mqtt_password);
|
||||
info!("With authentification");
|
||||
}
|
||||
|
||||
let lwt = Topic::General(last_will_topic);
|
||||
let lwt = mk_static!(Topic<String>, lwt);
|
||||
let lwt = lwt.with_display("lost").retain(true).qos(QoS::AtLeastOnce);
|
||||
builder = builder.with_last_will(lwt);
|
||||
//TODO make configurable
|
||||
builder = builder.with_device_id("plantctrl");
|
||||
|
||||
let builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 2> = builder
|
||||
.with_subscriptions([
|
||||
Topic::General(round_trip_topic.clone()),
|
||||
Topic::General(stay_alive_topic.clone()),
|
||||
]);
|
||||
|
||||
let keep_alive = Duration::from_secs(60 * 60 * 2).as_secs() as u16;
|
||||
let (receiver, task) = builder.build(keep_alive);
|
||||
|
||||
spawner.spawn(mqtt_incoming_task(
|
||||
receiver,
|
||||
round_trip_topic.clone(),
|
||||
stay_alive_topic.clone(),
|
||||
))?;
|
||||
spawner.spawn(mqtt_runner(task))?;
|
||||
|
||||
log(LogMessage::StayAlive, 0, 0, "", &stay_alive_topic);
|
||||
|
||||
log(LogMessage::MqttInfo, 0, 0, "", mqtt_url);
|
||||
|
||||
let mqtt_timeout = 15000;
|
||||
let res = async {
|
||||
while !MQTT_CONNECTED_EVENT_RECEIVED.load(Ordering::Relaxed) {
|
||||
crate::hal::PlantHal::feed_watchdog();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(mqtt_timeout as u64))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
bail!("Timeout waiting MQTT connect event")
|
||||
}
|
||||
|
||||
let _ = Topic::General(round_trip_topic.clone())
|
||||
.with_display("online_text")
|
||||
.publish()
|
||||
.await;
|
||||
|
||||
let res = async {
|
||||
while !MQTT_ROUND_TRIP_RECEIVED.load(Ordering::Relaxed) {
|
||||
crate::hal::PlantHal::feed_watchdog();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(mqtt_timeout as u64))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
//ensure we do not further try to publish
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(false, Ordering::Relaxed);
|
||||
bail!("Timeout waiting MQTT roundtrip")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn mqtt_inner(&mut self, subtopic: &str, message: &str) -> FatResult<()> {
|
||||
if !subtopic.starts_with("/") {
|
||||
bail!("Subtopic without / at start {}", subtopic);
|
||||
}
|
||||
if subtopic.len() > 192 {
|
||||
bail!("Subtopic exceeds 192 chars {}", subtopic);
|
||||
}
|
||||
let base_topic = MQTT_BASE_TOPIC
|
||||
.try_get()
|
||||
.context("missing base topic in static!")?;
|
||||
|
||||
let full_topic = format!("{base_topic}{subtopic}");
|
||||
|
||||
loop {
|
||||
let result = Topic::General(full_topic.as_str())
|
||||
.with_display(message)
|
||||
.retain(true)
|
||||
.publish()
|
||||
.await;
|
||||
match result {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(err) => {
|
||||
let retry = match err {
|
||||
Error::IOError => false,
|
||||
Error::TimedOut => true,
|
||||
Error::TooLarge => false,
|
||||
Error::PacketError => false,
|
||||
Error::Invalid => false,
|
||||
};
|
||||
if !retry {
|
||||
bail!(
|
||||
"Error during mqtt send on topic {} with message {:#?} error is {:?}",
|
||||
&full_topic,
|
||||
message,
|
||||
err
|
||||
);
|
||||
}
|
||||
info!(
|
||||
"Retransmit for {} with message {:#?} error is {:?} retrying {}",
|
||||
&full_topic, message, err, retry
|
||||
);
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(crate) async fn mqtt_publish(&mut self, subtopic: &str, message: &str) {
|
||||
let online = MQTT_CONNECTED_EVENT_RECEIVED.load(Ordering::Relaxed);
|
||||
if !online {
|
||||
return;
|
||||
}
|
||||
let roundtrip_ok = MQTT_ROUND_TRIP_RECEIVED.load(Ordering::Relaxed);
|
||||
if !roundtrip_ok {
|
||||
info!("MQTT roundtrip not received yet, dropping message");
|
||||
return;
|
||||
}
|
||||
match self.mqtt_inner(subtopic, message).await {
|
||||
Ok(()) => {}
|
||||
Err(err) => {
|
||||
info!(
|
||||
"Error during mqtt send on topic {subtopic} with message {message:#?} error is {err:?}"
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn mqtt_runner(
|
||||
task: McutieTask<'static, String, PublishDisplay<'static, String, &'static str>, 2>,
|
||||
) {
|
||||
task.run().await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn mqtt_incoming_task(
|
||||
receiver: McutieReceiver,
|
||||
round_trip_topic: String,
|
||||
stay_alive_topic: String,
|
||||
) {
|
||||
loop {
|
||||
let message = receiver.receive().await;
|
||||
match message {
|
||||
MqttMessage::Connected => {
|
||||
info!("Mqtt connected");
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(true, Ordering::Relaxed);
|
||||
}
|
||||
MqttMessage::Publish(topic, payload) => match topic {
|
||||
Topic::DeviceType(_type_topic) => {}
|
||||
Topic::Device(_device_topic) => {}
|
||||
Topic::General(topic) => {
|
||||
let subtopic = topic.as_str();
|
||||
|
||||
if subtopic.eq(round_trip_topic.as_str()) {
|
||||
MQTT_ROUND_TRIP_RECEIVED.store(true, Ordering::Relaxed);
|
||||
} else if subtopic.eq(stay_alive_topic.as_str()) {
|
||||
let value = payload.eq_ignore_ascii_case("true".as_ref())
|
||||
|| payload.eq_ignore_ascii_case("1".as_ref());
|
||||
let a = match value {
|
||||
true => 1,
|
||||
false => 0,
|
||||
};
|
||||
log(LogMessage::MqttStayAliveRec, a, 0, "", "");
|
||||
MQTT_STAY_ALIVE.store(value, Ordering::Relaxed);
|
||||
} else {
|
||||
log(LogMessage::UnknownTopic, 0, 0, "", &topic);
|
||||
}
|
||||
}
|
||||
},
|
||||
MqttMessage::Disconnected => {
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(false, Ordering::Relaxed);
|
||||
info!("Mqtt disconnected");
|
||||
}
|
||||
MqttMessage::HomeAssistantOnline => {
|
||||
info!("Home assistant is online");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[embassy_executor::task(pool_size = 2)]
|
||||
async fn net_task(mut runner: Runner<'static, WifiDevice<'static>>) {
|
||||
async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
|
||||
runner.run().await;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,6 @@ use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use bincode::{Decode, Encode};
|
||||
use canapi::SensorSlot;
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use core::cell::RefCell;
|
||||
@@ -95,7 +94,6 @@ use esp_hal::system::reset_reason;
|
||||
use esp_hal::timer::timg::{MwdtStage, TimerGroup, Wdt};
|
||||
use esp_hal::uart::Uart;
|
||||
use esp_hal::Blocking;
|
||||
use esp_radio::{init, Controller};
|
||||
use esp_storage::FlashStorage;
|
||||
use log::{info, warn};
|
||||
use portable_atomic::AtomicBool;
|
||||
@@ -119,7 +117,7 @@ pub static I2C_DRIVER: OnceLock<
|
||||
embassy_sync::blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<I2c<Blocking>>>,
|
||||
> = OnceLock::new();
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Encode, Decode)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
||||
pub enum Sensor {
|
||||
A,
|
||||
B,
|
||||
@@ -151,7 +149,7 @@ pub trait BoardInteraction<'a> {
|
||||
async fn get_time(&mut self) -> DateTime<Utc>;
|
||||
async fn set_time(&mut self, time: &DateTime<FixedOffset>) -> FatResult<()>;
|
||||
async fn set_charge_indicator(&mut self, charging: bool) -> Result<(), FatError>;
|
||||
async fn deep_sleep(&mut self, duration_in_ms: u64) -> !;
|
||||
async fn deep_sleep_ms(&mut self, duration_in_ms: u64) -> !;
|
||||
|
||||
fn is_day(&self) -> bool;
|
||||
//should be multsampled
|
||||
@@ -166,16 +164,22 @@ pub trait BoardInteraction<'a> {
|
||||
async fn get_mptt_voltage(&mut self) -> FatResult<Voltage>;
|
||||
async fn get_mptt_current(&mut self) -> FatResult<Current>;
|
||||
async fn can_power(&mut self, state: bool) -> FatResult<()>;
|
||||
async fn fertilizer_pump(&mut self, enable: bool) -> FatResult<()>;
|
||||
|
||||
async fn backup_config(&mut self, config: &PlantControllerConfig) -> FatResult<()>;
|
||||
async fn read_backup(&mut self) -> FatResult<PlantControllerConfig>;
|
||||
async fn backup_info(&mut self) -> FatResult<BackupHeader>;
|
||||
|
||||
// Return JSON string with autodetected sensors per plant. Default: not supported.
|
||||
async fn detect_sensors(&mut self, _request: Detection) -> FatResult<Detection> {
|
||||
async fn detect_sensors(&mut self, _request: DetectionRequest) -> FatResult<Detection> {
|
||||
bail!("Autodetection is only available on v4 HAL with CAN bus");
|
||||
}
|
||||
|
||||
/// Return the last known firmware build timestamps per sensor, set during detect_sensors.
|
||||
fn get_sensor_build_minutes(&self) -> ([Option<u32>; PLANT_COUNT], [Option<u32>; PLANT_COUNT]) {
|
||||
([None; PLANT_COUNT], [None; PLANT_COUNT])
|
||||
}
|
||||
|
||||
async fn progress(&mut self, counter: u32) {
|
||||
// Indicate progress is active to suppress default wait_infinity blinking
|
||||
PROGRESS_ACTIVE.store(true, core::sync::atomic::Ordering::Relaxed);
|
||||
@@ -280,21 +284,14 @@ impl PlantHal {
|
||||
let wake_gpio1 = peripherals.GPIO1;
|
||||
|
||||
let rng = Rng::new();
|
||||
let esp_wifi_ctrl = &*mk_static!(
|
||||
Controller<'static>,
|
||||
init().map_err(|e| FatError::String {
|
||||
error: format!("Could not init wifi controller: {:?}", e)
|
||||
})?
|
||||
);
|
||||
|
||||
let (controller, interfaces) =
|
||||
esp_radio::wifi::new(esp_wifi_ctrl, peripherals.WIFI, Default::default()).map_err(
|
||||
|e| FatError::String {
|
||||
error: format!("Could not init wifi: {:?}", e),
|
||||
},
|
||||
)?;
|
||||
let (controller, interfaces) = esp_radio::wifi::new(peripherals.WIFI, Default::default())
|
||||
.map_err(|e| FatError::String {
|
||||
error: format!("Could not init wifi: {:?}", e),
|
||||
})?;
|
||||
|
||||
let pcnt_module = Pcnt::new(peripherals.PCNT);
|
||||
let mut pcnt_module = Pcnt::new(peripherals.PCNT);
|
||||
pcnt_module.set_interrupt_handler(water::flow_interrupt_handler);
|
||||
|
||||
let free_pins = FreePeripherals {
|
||||
gpio0: peripherals.GPIO0,
|
||||
@@ -349,16 +346,13 @@ impl PlantHal {
|
||||
.context("No OTA data partition found")?
|
||||
);
|
||||
|
||||
let ota_data = mk_static!(
|
||||
FlashRegion<RmwNorFlashStorage<&mut MutexFlashStorage>>,
|
||||
ota_data.as_embedded_storage(mk_static!(
|
||||
RmwNorFlashStorage<&mut MutexFlashStorage>,
|
||||
RmwNorFlashStorage::new(flash_storage_2, mk_static!([u8; 4096], [0_u8; 4096]))
|
||||
))
|
||||
);
|
||||
let mut ota_data = ota_data.as_embedded_storage(mk_static!(
|
||||
RmwNorFlashStorage<&mut MutexFlashStorage>,
|
||||
RmwNorFlashStorage::new(flash_storage_2, mk_static!([u8; 4096], [0_u8; 4096]))
|
||||
));
|
||||
|
||||
let state_0 = ota_state(AppPartitionSubType::Ota0, ota_data);
|
||||
let state_1 = ota_state(AppPartitionSubType::Ota1, ota_data);
|
||||
let state_0 = ota_state(AppPartitionSubType::Ota0, &mut ota_data);
|
||||
let state_1 = ota_state(AppPartitionSubType::Ota1, &mut ota_data);
|
||||
let mut ota = Ota::new(ota_data, 2)?;
|
||||
let running = get_current_slot(&pt, &mut ota)?;
|
||||
let target = next_partition(running)?;
|
||||
@@ -411,8 +405,8 @@ impl PlantHal {
|
||||
error: "Uart creation failed".to_string(),
|
||||
})?;
|
||||
|
||||
let ap = interfaces.ap;
|
||||
let sta = interfaces.sta;
|
||||
let ap = interfaces.access_point;
|
||||
let sta = interfaces.station;
|
||||
let mut esp = Esp {
|
||||
savegame,
|
||||
rng,
|
||||
@@ -670,14 +664,34 @@ pub fn next_partition(current: AppPartitionSubType) -> FatResult<AppPartitionSub
|
||||
pub struct Moistures {
|
||||
pub sensor_a_hz: [Option<f32>; PLANT_COUNT],
|
||||
pub sensor_b_hz: [Option<f32>; PLANT_COUNT],
|
||||
pub sensor_a_build_minutes: [Option<u32>; PLANT_COUNT],
|
||||
pub sensor_b_build_minutes: [Option<u32>; PLANT_COUNT],
|
||||
}
|
||||
|
||||
/// Request: which sensors to send IDENTIFY_CMD to.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct DetectionRequest {
|
||||
pub plant: [SensorRequest; PLANT_COUNT],
|
||||
}
|
||||
|
||||
/// Per-sensor portion of a detection request.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct SensorRequest {
|
||||
pub sensor_a: bool,
|
||||
pub sensor_b: bool,
|
||||
}
|
||||
|
||||
/// Response: detection result per plant.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct Detection {
|
||||
plant: [DetectionSensorResult; PLANT_COUNT],
|
||||
pub plant: [DetectionSensorResult; PLANT_COUNT],
|
||||
}
|
||||
|
||||
/// Per-sensor detection result.
|
||||
/// `Some(build_minutes)` = sensor responded; value is its firmware build timestamp
|
||||
/// (minutes since Unix epoch, or 0 if not reported). `None` = not detected.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct DetectionSensorResult {
|
||||
sensor_a: bool,
|
||||
sensor_b: bool,
|
||||
pub sensor_a: Option<u32>,
|
||||
pub sensor_b: Option<u32>,
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::hal::Box;
|
||||
use async_trait::async_trait;
|
||||
use bincode::{Decode, Encode};
|
||||
use chrono::{DateTime, Utc};
|
||||
use ds323x::ic::DS3231;
|
||||
use ds323x::interface::I2cInterface;
|
||||
@@ -29,7 +28,7 @@ pub trait RTCModuleInteraction {
|
||||
fn read(&mut self, offset: u32, data: &mut [u8]) -> FatResult<()>;
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
|
||||
pub struct BackupHeader {
|
||||
pub timestamp: i64,
|
||||
pub(crate) crc16: u16,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use alloc::string::ToString;
|
||||
use alloc::vec::Vec;
|
||||
use bincode::{Decode, Encode};
|
||||
use embedded_savegame::storage::{Flash, Storage};
|
||||
use embedded_storage::nor_flash::{NorFlash, ReadNorFlash};
|
||||
use esp_bootloader_esp_idf::partitions::{Error as PartitionError, FlashRegion};
|
||||
@@ -25,13 +25,57 @@ pub struct SaveInfo {
|
||||
pub created_at: Option<alloc::string::String>,
|
||||
}
|
||||
|
||||
/// Wrapper that includes both the config data and metadata like creation timestamp.
|
||||
#[derive(Serialize, Debug, Encode, Decode)]
|
||||
struct SaveWrapper {
|
||||
/// UTC timestamp in RFC3339 format
|
||||
created_at: alloc::string::String,
|
||||
/// Raw config JSON data
|
||||
data: Vec<u8>,
|
||||
const SAVE_MAGIC: [u8; 4] = *b"SGM1";
|
||||
const SAVE_HEADER_LEN: usize = 6; // magic (4) + timestamp_len (u16)
|
||||
|
||||
struct ParsedSave<'a> {
|
||||
created_at: &'a str,
|
||||
data: &'a [u8],
|
||||
}
|
||||
|
||||
fn strip_padding(data: &[u8]) -> &[u8] {
|
||||
let mut end = data.len();
|
||||
while end > 0 {
|
||||
let b = data[end - 1];
|
||||
if b == 0x00 || b == 0xFF {
|
||||
end -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
&data[..end]
|
||||
}
|
||||
|
||||
fn parse_save(data: &[u8]) -> FatResult<ParsedSave<'_>> {
|
||||
if data.len() < SAVE_HEADER_LEN {
|
||||
return Err(FatError::String {
|
||||
error: "Save payload too short".into(),
|
||||
});
|
||||
}
|
||||
if data[..4] != SAVE_MAGIC {
|
||||
return Err(FatError::String {
|
||||
error: "Save payload has invalid magic".into(),
|
||||
});
|
||||
}
|
||||
|
||||
let timestamp_len = u16::from_le_bytes([data[4], data[5]]) as usize;
|
||||
let timestamp_end = SAVE_HEADER_LEN + timestamp_len;
|
||||
if timestamp_end > data.len() {
|
||||
return Err(FatError::String {
|
||||
error: "Save payload timestamp length exceeds data".into(),
|
||||
});
|
||||
}
|
||||
|
||||
let created_at = core::str::from_utf8(&data[SAVE_HEADER_LEN..timestamp_end]).map_err(|e| {
|
||||
FatError::String {
|
||||
error: alloc::format!("Save payload contains invalid timestamp UTF-8: {e:?}"),
|
||||
}
|
||||
})?;
|
||||
|
||||
Ok(ParsedSave {
|
||||
created_at,
|
||||
data: strip_padding(&data[timestamp_end..]),
|
||||
})
|
||||
}
|
||||
|
||||
// ── Flash adapter ──────────────────────────────────────────────────────────────
|
||||
@@ -134,11 +178,21 @@ impl SavegameManager {
|
||||
/// slot before `append()` writes to the next free one.
|
||||
/// Both operations are performed atomically on the same Storage instance.
|
||||
pub fn save(&mut self, data: &[u8], timestamp: &str) -> FatResult<()> {
|
||||
let wrapper = SaveWrapper {
|
||||
created_at: alloc::string::String::from(timestamp),
|
||||
data: data.to_vec(),
|
||||
};
|
||||
let mut serialized = bincode::encode_to_vec(&wrapper, bincode::config::standard())?;
|
||||
let timestamp_bytes = timestamp.as_bytes();
|
||||
let timestamp_len: u16 =
|
||||
timestamp_bytes
|
||||
.len()
|
||||
.try_into()
|
||||
.map_err(|_| FatError::String {
|
||||
error: "Timestamp too long for save header".into(),
|
||||
})?;
|
||||
|
||||
let mut serialized =
|
||||
Vec::with_capacity(SAVE_HEADER_LEN + timestamp_bytes.len() + data.len());
|
||||
serialized.extend_from_slice(&SAVE_MAGIC);
|
||||
serialized.extend_from_slice(×tamp_len.to_le_bytes());
|
||||
serialized.extend_from_slice(timestamp_bytes);
|
||||
serialized.extend_from_slice(data);
|
||||
|
||||
// Flash storage often requires length to be a multiple of 4.
|
||||
let padding = (4 - (serialized.len() % 4)) % 4;
|
||||
@@ -169,12 +223,8 @@ impl SavegameManager {
|
||||
match self.storage.read(idx, &mut buf)? {
|
||||
None => Ok(None),
|
||||
Some(data) => {
|
||||
// Try to deserialize as SaveWrapper (new Bincode format)
|
||||
let (wrapper, _) = bincode::decode_from_slice::<SaveWrapper, _>(
|
||||
data,
|
||||
bincode::config::standard(),
|
||||
)?;
|
||||
Ok(Some(wrapper.data))
|
||||
let parsed = parse_save(data)?;
|
||||
Ok(Some(parsed.data.to_vec()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,17 +242,22 @@ impl SavegameManager {
|
||||
let mut buf = alloc::vec![0u8; SAVEGAME_SLOT_SIZE];
|
||||
for idx in 0..SAVEGAME_SLOT_COUNT {
|
||||
if let Some(data) = self.storage.read(idx, &mut buf)? {
|
||||
// Try to deserialize as SaveWrapper (new Bincode format)
|
||||
let (wrapper, _) = bincode::decode_from_slice::<SaveWrapper, _>(
|
||||
data,
|
||||
bincode::config::standard(),
|
||||
)?;
|
||||
|
||||
saves.push(SaveInfo {
|
||||
idx,
|
||||
len: wrapper.data.len() as u32,
|
||||
created_at: Some(wrapper.created_at),
|
||||
});
|
||||
match parse_save(data) {
|
||||
Ok(save) => {
|
||||
saves.push(SaveInfo {
|
||||
idx,
|
||||
len: save.data.len() as u32,
|
||||
created_at: Some(alloc::string::String::from(save.created_at)),
|
||||
});
|
||||
}
|
||||
Err(err) => {
|
||||
saves.push(SaveInfo {
|
||||
idx,
|
||||
len: 0,
|
||||
created_at: Some(err.to_string()),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(saves)
|
||||
|
||||
@@ -6,13 +6,13 @@ use crate::hal::esp::{hold_disable, hold_enable, Esp};
|
||||
use crate::hal::rtc::{BackupHeader, RTCModuleInteraction, EEPROM_PAGE, X25};
|
||||
use crate::hal::water::TankSensor;
|
||||
use crate::hal::{
|
||||
BoardInteraction, Detection, FreePeripherals, Moistures, Sensor, I2C_DRIVER, PLANT_COUNT,
|
||||
BoardInteraction, Detection, DetectionRequest, FreePeripherals, Moistures, Sensor, I2C_DRIVER,
|
||||
PLANT_COUNT,
|
||||
};
|
||||
use crate::log::{log, LogMessage};
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::ToString;
|
||||
use async_trait::async_trait;
|
||||
use bincode::config;
|
||||
use canapi::id::{classify, plant_id, MessageKind, IDENTIFY_CMD_OFFSET};
|
||||
use canapi::SensorSlot;
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
@@ -36,7 +36,6 @@ use measurements::{Current, Voltage};
|
||||
use pca9535::{GPIOBank, Pca9535Immediate, StandardExpanderInterface};
|
||||
|
||||
pub const BACKUP_HEADER_MAX_SIZE: usize = 64;
|
||||
const CONFIG: config::Configuration = config::standard();
|
||||
|
||||
const MPPT_CURRENT_SHUNT_OHMS: f64 = 0.05_f64;
|
||||
const TWAI_BAUDRATE: twai::BaudRate = twai::BaudRate::Custom(twai::TimingConfig {
|
||||
@@ -145,6 +144,11 @@ pub struct V4<'a> {
|
||||
extra1: Output<'a>,
|
||||
extra2: Output<'a>,
|
||||
twai_config: Option<TwaiConfiguration<'static, Blocking>>,
|
||||
|
||||
/// Last known firmware build timestamps per sensor (minutes since Unix epoch).
|
||||
/// Updated during detect_sensors; preserved across normal measurement cycles.
|
||||
sensor_a_build_minutes: [Option<u32>; PLANT_COUNT],
|
||||
sensor_b_build_minutes: [Option<u32>; PLANT_COUNT],
|
||||
}
|
||||
|
||||
pub(crate) async fn create_v4(
|
||||
@@ -157,9 +161,11 @@ pub(crate) async fn create_v4(
|
||||
info!("Start v4");
|
||||
let mut awake = Output::new(peripherals.gpio21, Level::High, OutputConfig::default());
|
||||
awake.set_high();
|
||||
info!("v4: gpio21 awake ok");
|
||||
|
||||
let mut general_fault = Output::new(peripherals.gpio23, Level::Low, OutputConfig::default());
|
||||
general_fault.set_low();
|
||||
info!("v4: gpio23 general_fault ok");
|
||||
|
||||
let twai_config = Some(TwaiConfiguration::new(
|
||||
peripherals.twai,
|
||||
@@ -168,17 +174,24 @@ pub(crate) async fn create_v4(
|
||||
TWAI_BAUDRATE,
|
||||
TwaiMode::Normal,
|
||||
));
|
||||
info!("v4: twai config ok");
|
||||
|
||||
let extra1 = Output::new(peripherals.gpio6, Level::Low, OutputConfig::default());
|
||||
info!("v4: gpio6 extra1 ok");
|
||||
let extra2 = Output::new(peripherals.gpio15, Level::Low, OutputConfig::default());
|
||||
info!("v4: gpio15 extra2 ok");
|
||||
|
||||
let one_wire_pin = Flex::new(peripherals.gpio18);
|
||||
info!("v4: gpio18 one_wire ok");
|
||||
let tank_power_pin = Output::new(peripherals.gpio11, Level::Low, OutputConfig::default());
|
||||
info!("v4: gpio11 tank_power ok");
|
||||
let flow_sensor_pin = Input::new(
|
||||
peripherals.gpio4,
|
||||
InputConfig::default().with_pull(Pull::Up),
|
||||
);
|
||||
info!("v4: gpio4 flow_sensor ok");
|
||||
|
||||
info!("v4: creating tank sensor");
|
||||
let tank_sensor = TankSensor::create(
|
||||
one_wire_pin,
|
||||
peripherals.adc1,
|
||||
@@ -187,12 +200,17 @@ pub(crate) async fn create_v4(
|
||||
flow_sensor_pin,
|
||||
peripherals.pcnt1,
|
||||
)?;
|
||||
info!("v4: tank sensor ok");
|
||||
|
||||
let can_power = Output::new(peripherals.gpio22, Level::Low, OutputConfig::default());
|
||||
info!("v4: gpio22 can_power ok");
|
||||
|
||||
let solar_is_day = Input::new(peripherals.gpio7, InputConfig::default());
|
||||
info!("v4: gpio7 solar_is_day ok");
|
||||
let light = Output::new(peripherals.gpio10, Level::Low, Default::default());
|
||||
info!("v4: gpio10 light ok");
|
||||
let charge_indicator = Output::new(peripherals.gpio3, Level::Low, Default::default());
|
||||
info!("v4: gpio3 charge_indicator ok");
|
||||
|
||||
info!("Start pump expander");
|
||||
let pump_device = I2cDevice::new(I2C_DRIVER.get().await);
|
||||
@@ -274,6 +292,8 @@ pub(crate) async fn create_v4(
|
||||
extra2,
|
||||
can_power,
|
||||
twai_config,
|
||||
sensor_a_build_minutes: [None; PLANT_COUNT],
|
||||
sensor_b_build_minutes: [None; PLANT_COUNT],
|
||||
};
|
||||
Ok(Box::new(v))
|
||||
}
|
||||
@@ -314,7 +334,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
self.charger.set_charge_indicator(charging)
|
||||
}
|
||||
|
||||
async fn deep_sleep(&mut self, duration_in_ms: u64) -> ! {
|
||||
async fn deep_sleep_ms(&mut self, duration_in_ms: u64) -> ! {
|
||||
self.awake.set_low();
|
||||
self.charger.power_save();
|
||||
self.esp.deep_sleep(duration_in_ms);
|
||||
@@ -385,7 +405,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
|
||||
let mut moistures = Moistures::default();
|
||||
let _ = wait_for_can_measurements(&mut twai, &mut moistures)
|
||||
.with_timeout(Duration::from_millis(1000))
|
||||
.with_timeout(Duration::from_millis(5000))
|
||||
.await;
|
||||
Ok(moistures)
|
||||
})
|
||||
@@ -395,83 +415,19 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
self.twai_config.replace(config);
|
||||
self.can_power.set_low();
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
async fn detect_sensors(&mut self, request: Detection) -> FatResult<Detection> {
|
||||
self.can_power.set_high();
|
||||
Timer::after_millis(500).await;
|
||||
let config = self.twai_config.take().context("twai config not set")?;
|
||||
let mut twai = config.into_async().start();
|
||||
|
||||
if twai.is_bus_off() {
|
||||
info!("Bus offline after start, attempting recovery");
|
||||
// Re-start to initiate recovery
|
||||
twai = twai.stop().start();
|
||||
}
|
||||
|
||||
let res = (async {
|
||||
Timer::after_millis(1000).await;
|
||||
info!("Sending info messages now");
|
||||
// Send a few test messages per potential sensor node
|
||||
for plant in 0..PLANT_COUNT {
|
||||
for sensor in [Sensor::A, Sensor::B] {
|
||||
let detect = if sensor == Sensor::A {
|
||||
request.plant[plant].sensor_a
|
||||
} else {
|
||||
request.plant[plant].sensor_b
|
||||
};
|
||||
if !detect {
|
||||
continue;
|
||||
}
|
||||
let target = StandardId::new(plant_id(
|
||||
IDENTIFY_CMD_OFFSET,
|
||||
sensor.into(),
|
||||
(plant + 1) as u16,
|
||||
))
|
||||
.context(">> Could not create address for sensor! (plant: {}) <<")?;
|
||||
let can_buffer = [0_u8; 0];
|
||||
info!(
|
||||
"Sending test message to plant {} sensor {sensor:?} with id {}",
|
||||
plant + 1,
|
||||
target.as_raw()
|
||||
);
|
||||
if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) {
|
||||
// Try a few times; we intentionally ignore rx here and rely on stub logic
|
||||
let resu = twai
|
||||
.transmit_async(&frame)
|
||||
.with_timeout(Duration::from_millis(500))
|
||||
.await;
|
||||
match resu {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
info!(
|
||||
"Error sending test message to plant {} sensor {sensor:?}: {err:?}",
|
||||
plant + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!("Error building CAN frame");
|
||||
}
|
||||
// Persist any firmware build timestamps received alongside moisture data.
|
||||
if let Ok(ref moistures) = res {
|
||||
for (i, v) in moistures.sensor_a_build_minutes.iter().enumerate() {
|
||||
if v.is_some() {
|
||||
self.sensor_a_build_minutes[i] = *v;
|
||||
}
|
||||
}
|
||||
|
||||
let mut moistures = Moistures::default();
|
||||
let _ = wait_for_can_measurements(&mut twai, &mut moistures)
|
||||
.with_timeout(Duration::from_millis(3000))
|
||||
.await;
|
||||
|
||||
let result: Detection = moistures.into();
|
||||
|
||||
info!("Autodetection result: {result:?}");
|
||||
Ok(result)
|
||||
})
|
||||
.await;
|
||||
|
||||
let config = twai.stop().into_blocking();
|
||||
self.twai_config.replace(config);
|
||||
self.can_power.set_low();
|
||||
for (i, v) in moistures.sensor_b_build_minutes.iter().enumerate() {
|
||||
if v.is_some() {
|
||||
self.sensor_b_build_minutes[i] = *v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
}
|
||||
@@ -541,9 +497,19 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn fertilizer_pump(&mut self, enable: bool) -> FatResult<()> {
|
||||
if enable {
|
||||
self.extra2.set_high();
|
||||
} else {
|
||||
self.extra2.set_low();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn backup_config(&mut self, controller_config: &PlantControllerConfig) -> FatResult<()> {
|
||||
let mut buffer: [u8; 4096 - BACKUP_HEADER_MAX_SIZE] = [0; 4096 - BACKUP_HEADER_MAX_SIZE];
|
||||
let length = bincode::encode_into_slice(controller_config, &mut buffer, CONFIG)?;
|
||||
let length = postcard::to_slice(controller_config, &mut buffer)?.len();
|
||||
info!("Writing backup config of size {}", length);
|
||||
let mut checksum = X25.digest();
|
||||
checksum.update(&buffer[..length]);
|
||||
@@ -556,7 +522,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
size: length as u16,
|
||||
};
|
||||
info!("Header is {:?}", header);
|
||||
bincode::encode_into_slice(&header, &mut header_page_buffer, CONFIG)?;
|
||||
postcard::to_slice(&header, &mut header_page_buffer)?;
|
||||
info!("Header is serialized");
|
||||
self.get_rtc_module().write(0, &header_page_buffer)?;
|
||||
info!("Header written");
|
||||
@@ -583,7 +549,6 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
self.clear_progress().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn read_backup(&mut self) -> FatResult<PlantControllerConfig> {
|
||||
let info = self.backup_info().await?;
|
||||
let mut store = alloc::vec![0_u8; info.size as usize];
|
||||
@@ -600,7 +565,7 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
bail!("CRC mismatch in backup data")
|
||||
}
|
||||
info!("CRC is correct");
|
||||
let (decoded, _) = bincode::decode_from_slice(&store[..], CONFIG)?;
|
||||
let decoded = postcard::from_bytes(&store[..])?;
|
||||
info!("Backup data decoded");
|
||||
Ok(decoded)
|
||||
}
|
||||
@@ -609,12 +574,101 @@ impl<'a> BoardInteraction<'a> for V4<'a> {
|
||||
let mut header_page_buffer = [0_u8; BACKUP_HEADER_MAX_SIZE];
|
||||
self.get_rtc_module().read(0, &mut header_page_buffer)?;
|
||||
info!("Read header page");
|
||||
let info: Result<(BackupHeader, usize), bincode::error::DecodeError> =
|
||||
bincode::decode_from_slice(&header_page_buffer[..], CONFIG);
|
||||
let info = postcard::take_from_bytes::<BackupHeader>(&header_page_buffer[..]);
|
||||
info!("decoding header: {:?}", info);
|
||||
let (header, _) = info.context("Could not read backup header")?;
|
||||
Ok(header)
|
||||
}
|
||||
|
||||
async fn detect_sensors(&mut self, request: DetectionRequest) -> FatResult<Detection> {
|
||||
self.can_power.set_high();
|
||||
Timer::after_millis(500).await;
|
||||
let config = self.twai_config.take().context("twai config not set")?;
|
||||
let mut twai = config.into_async().start();
|
||||
|
||||
if twai.is_bus_off() {
|
||||
info!("Bus offline after start, attempting recovery");
|
||||
// Re-start to initiate recovery
|
||||
twai = twai.stop().start();
|
||||
}
|
||||
|
||||
let res = (async {
|
||||
Timer::after_millis(1000).await;
|
||||
info!("Sending info messages now");
|
||||
// Send a few test messages per potential sensor node
|
||||
for plant in 0..PLANT_COUNT {
|
||||
for sensor in [Sensor::A, Sensor::B] {
|
||||
let detect = if sensor == Sensor::A {
|
||||
request.plant[plant].sensor_a
|
||||
} else {
|
||||
request.plant[plant].sensor_b
|
||||
};
|
||||
|
||||
if !detect {
|
||||
continue;
|
||||
}
|
||||
let target = StandardId::new(plant_id(
|
||||
IDENTIFY_CMD_OFFSET,
|
||||
sensor.into(),
|
||||
(plant + 1) as u16,
|
||||
))
|
||||
.context(">> Could not create address for sensor! (plant: {}) <<")?;
|
||||
let can_buffer = [0_u8; 0];
|
||||
info!(
|
||||
"Sending test message to plant {} sensor {sensor:?} with id {}",
|
||||
plant + 1,
|
||||
target.as_raw()
|
||||
);
|
||||
if let Some(frame) = EspTwaiFrame::new(target, &can_buffer) {
|
||||
// Try a few times; we intentionally ignore rx here and rely on stub logic
|
||||
let resu = twai
|
||||
.transmit_async(&frame)
|
||||
.with_timeout(Duration::from_millis(500))
|
||||
.await;
|
||||
match resu {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
info!(
|
||||
"Error sending test message to plant {} sensor {sensor:?}: {err:?}",
|
||||
plant + 1
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!("Error building CAN frame");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut moistures = Moistures::default();
|
||||
let _ = wait_for_can_measurements(&mut twai, &mut moistures)
|
||||
.with_timeout(Duration::from_millis(3000))
|
||||
.await;
|
||||
|
||||
let result: Detection = moistures.into();
|
||||
|
||||
info!("Autodetection result: {result:?}");
|
||||
Ok((result, moistures.sensor_a_build_minutes, moistures.sensor_b_build_minutes))
|
||||
})
|
||||
.await;
|
||||
|
||||
let config = twai.stop().into_blocking();
|
||||
self.twai_config.replace(config);
|
||||
self.can_power.set_low();
|
||||
|
||||
match res {
|
||||
Ok((detection, a_builds, b_builds)) => {
|
||||
self.sensor_a_build_minutes = a_builds;
|
||||
self.sensor_b_build_minutes = b_builds;
|
||||
Ok(detection)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sensor_build_minutes(&self) -> ([Option<u32>; PLANT_COUNT], [Option<u32>; PLANT_COUNT]) {
|
||||
(self.sensor_a_build_minutes, self.sensor_b_build_minutes)
|
||||
}
|
||||
}
|
||||
|
||||
async fn wait_for_can_measurements(
|
||||
@@ -634,10 +688,10 @@ async fn wait_for_can_measurements(
|
||||
"received message of kind {:?} (plant: {}, sensor: {:?})",
|
||||
msg.0, msg.1, msg.2
|
||||
);
|
||||
let plant = msg.1 as usize;
|
||||
let sensor = msg.2;
|
||||
let data = can_frame.data();
|
||||
if msg.0 == MessageKind::MoistureData {
|
||||
let plant = msg.1 as usize;
|
||||
let sensor = msg.2;
|
||||
let data = can_frame.data();
|
||||
info!("Received moisture data: {:?}", data);
|
||||
if let Ok(bytes) = data.try_into() {
|
||||
let frequency = u32::from_be_bytes(bytes);
|
||||
@@ -654,6 +708,23 @@ async fn wait_for_can_measurements(
|
||||
} else {
|
||||
error!("Received moisture data with invalid length: {} (expected 4)", data.len());
|
||||
}
|
||||
} else if msg.0 == MessageKind::FirmwareBuild {
|
||||
info!("Received firmware build data: {:?}", data);
|
||||
if let Ok(bytes) = data.try_into() {
|
||||
let build_minutes = u32::from_be_bytes(bytes);
|
||||
match sensor {
|
||||
SensorSlot::A => {
|
||||
moistures.sensor_a_build_minutes[plant - 1] =
|
||||
Some(build_minutes);
|
||||
}
|
||||
SensorSlot::B => {
|
||||
moistures.sensor_b_build_minutes[plant - 1] =
|
||||
Some(build_minutes);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("Received firmware build data with invalid length: {} (expected 4)", data.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -680,10 +751,17 @@ impl From<Moistures> for Detection {
|
||||
fn from(value: Moistures) -> Self {
|
||||
let mut result = Detection::default();
|
||||
for (plant, sensor) in value.sensor_a_hz.iter().enumerate() {
|
||||
result.plant[plant].sensor_a = sensor.is_some();
|
||||
if sensor.is_some() {
|
||||
// Sensor responded; include build timestamp (0 = timestamp not reported)
|
||||
result.plant[plant].sensor_a =
|
||||
Some(value.sensor_a_build_minutes[plant].unwrap_or(0));
|
||||
}
|
||||
}
|
||||
for (plant, sensor) in value.sensor_b_hz.iter().enumerate() {
|
||||
result.plant[plant].sensor_b = sensor.is_some();
|
||||
if sensor.is_some() {
|
||||
result.plant[plant].sensor_b =
|
||||
Some(value.sensor_b_build_minutes[plant].unwrap_or(0));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
@@ -4,21 +4,26 @@ use crate::hal::{ADC1, TANK_MULTI_SAMPLE};
|
||||
use embassy_time::Timer;
|
||||
use esp_hal::analog::adc::{Adc, AdcCalLine, AdcConfig, AdcPin, Attenuation};
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Flex, Input, Output, OutputConfig, Pull};
|
||||
use esp_hal::gpio::{DriveMode, Flex, Input, InputConfig, Output, OutputConfig, Pull};
|
||||
use esp_hal::pcnt::channel::CtrlMode::Keep;
|
||||
use esp_hal::pcnt::channel::EdgeMode::{Hold, Increment};
|
||||
use esp_hal::pcnt::unit::Unit;
|
||||
use esp_hal::peripherals::GPIO5;
|
||||
use esp_hal::Blocking;
|
||||
use esp_println::println;
|
||||
use esp_hal::Async;
|
||||
use log::info;
|
||||
use onewire::{ds18b20, Device, DeviceSearch, OneWire, DS18B20};
|
||||
use portable_atomic::{AtomicUsize, Ordering};
|
||||
|
||||
unsafe impl Send for TankSensor<'_> {}
|
||||
|
||||
static FLOW_OVERFLOW_COUNTER: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
pub struct TankSensor<'a> {
|
||||
one_wire_bus: OneWire<Flex<'a>>,
|
||||
tank_channel: Adc<'a, ADC1<'a>, Blocking>,
|
||||
tank_channel: Adc<'a, ADC1<'a>, Async>,
|
||||
tank_power: Output<'a>,
|
||||
tank_pin: AdcPin<GPIO5<'a>, ADC1<'a>, AdcCalLine<ADC1<'a>>>,
|
||||
flow_counter: Unit<'a, 1>,
|
||||
flow_unit: Unit<'static, 1>,
|
||||
}
|
||||
|
||||
impl<'a> TankSensor<'a> {
|
||||
@@ -28,68 +33,123 @@ impl<'a> TankSensor<'a> {
|
||||
gpio5: GPIO5<'a>,
|
||||
tank_power: Output<'a>,
|
||||
flow_sensor: Input,
|
||||
pcnt1: Unit<'a, 1>,
|
||||
pcnt1: Unit<'static, 1>,
|
||||
) -> Result<TankSensor<'a>, FatError> {
|
||||
one_wire_pin.apply_output_config(&OutputConfig::default().with_pull(Pull::None));
|
||||
one_wire_pin.apply_output_config(
|
||||
&OutputConfig::default()
|
||||
.with_drive_mode(DriveMode::OpenDrain)
|
||||
.with_pull(Pull::None),
|
||||
);
|
||||
one_wire_pin.apply_input_config(&InputConfig::default().with_pull(Pull::None));
|
||||
one_wire_pin.set_high();
|
||||
one_wire_pin.set_input_enable(true);
|
||||
one_wire_pin.set_output_enable(true);
|
||||
info!("tank: one_wire pin config ok");
|
||||
|
||||
let mut adc1_config = AdcConfig::new();
|
||||
info!("tank: adc config created");
|
||||
let tank_pin =
|
||||
adc1_config.enable_pin_with_cal::<_, AdcCalLine<_>>(gpio5, Attenuation::_11dB);
|
||||
let tank_channel = Adc::new(adc1, adc1_config);
|
||||
info!("tank: adc pin cal ok");
|
||||
let tank_channel = Adc::new(adc1, adc1_config).into_async();
|
||||
info!("tank: adc channel ok");
|
||||
|
||||
let one_wire_bus = OneWire::new(one_wire_pin, false);
|
||||
info!("tank: one_wire bus ok");
|
||||
|
||||
pcnt1.set_high_limit(Some(i16::MAX))?;
|
||||
info!("tank: pcnt high limit ok");
|
||||
// Reject pulses shorter than ~12.8 µs (1023 APB cycles @ 80 MHz) to suppress EMI noise
|
||||
// on the sensor cable. Real flow pulses are in the millisecond range.
|
||||
pcnt1.set_filter(Some(1023)).unwrap();
|
||||
|
||||
let ch0 = &pcnt1.channel0;
|
||||
ch0.set_edge_signal(flow_sensor.peripheral_input());
|
||||
info!("tank: pcnt edge signal ok");
|
||||
ch0.set_input_mode(Hold, Increment);
|
||||
ch0.set_ctrl_mode(Keep, Keep);
|
||||
info!("tank: pcnt input/ctrl mode ok");
|
||||
|
||||
pcnt1.listen();
|
||||
info!("tank: pcnt listen ok");
|
||||
|
||||
Ok(TankSensor {
|
||||
one_wire_bus,
|
||||
tank_channel,
|
||||
tank_power,
|
||||
tank_pin,
|
||||
flow_counter: pcnt1,
|
||||
flow_unit: pcnt1,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn reset_flow_meter(&mut self) {
|
||||
self.flow_counter.pause();
|
||||
self.flow_counter.clear();
|
||||
// Pause, clear counter, clear any pending interrupt, then reset the overflow counter —
|
||||
// all inside a single critical section to prevent a race where the interrupt fires
|
||||
// between the overflow reset and the pause.
|
||||
critical_section::with(|_| {
|
||||
self.flow_unit.pause();
|
||||
self.flow_unit.clear();
|
||||
self.flow_unit.reset_interrupt();
|
||||
FLOW_OVERFLOW_COUNTER.store(0, Ordering::SeqCst);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn start_flow_meter(&mut self) {
|
||||
self.flow_counter.resume();
|
||||
}
|
||||
|
||||
pub fn get_flow_meter_value(&mut self) -> i16 {
|
||||
self.flow_counter.value()
|
||||
self.flow_unit.resume();
|
||||
}
|
||||
|
||||
pub fn stop_flow_meter(&mut self) -> i16 {
|
||||
self.flow_counter.pause();
|
||||
self.get_flow_meter_value()
|
||||
critical_section::with(|_| {
|
||||
let val = self.flow_unit.value();
|
||||
self.flow_unit.pause();
|
||||
val
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_full_flow_count(&self) -> u32 {
|
||||
// Read both values inside a single critical section so an overflow interrupt cannot
|
||||
// fire between the two reads and produce an inconsistent result.
|
||||
critical_section::with(|_| {
|
||||
let overflowed = FLOW_OVERFLOW_COUNTER.load(Ordering::SeqCst) as u32;
|
||||
let current = self.flow_unit.value() as u32;
|
||||
overflowed * (i16::MAX as u32 + 1) + current
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn water_temperature_c(&mut self) -> Result<f32, FatError> {
|
||||
//multisample should be moved to water_temperature_c
|
||||
let mut attempt = 1;
|
||||
let mut delay = Delay::new();
|
||||
self.one_wire_bus.reset(&mut delay)?;
|
||||
|
||||
let presence = self.one_wire_bus.reset(&mut delay)?;
|
||||
info!("OneWire: reset presence pulse = {}", presence);
|
||||
if !presence {
|
||||
info!("OneWire: no device responded to reset — check pull-up resistor and wiring");
|
||||
}
|
||||
|
||||
let mut search = DeviceSearch::new();
|
||||
let mut water_temp_sensor: Option<Device> = None;
|
||||
let mut devices_found = 0u8;
|
||||
while let Some(device) = self.one_wire_bus.search_next(&mut search, &mut delay)? {
|
||||
devices_found += 1;
|
||||
info!(
|
||||
"OneWire: found device #{} family=0x{:02X} addr={:02X?}",
|
||||
devices_found, device.address[0], device.address
|
||||
);
|
||||
if device.address[0] == ds18b20::FAMILY_CODE {
|
||||
water_temp_sensor = Some(device);
|
||||
break;
|
||||
} else {
|
||||
info!("OneWire: skipping device — not a DS18B20 (family 0x{:02X} != 0x{:02X})", device.address[0], ds18b20::FAMILY_CODE);
|
||||
}
|
||||
}
|
||||
if devices_found == 0 {
|
||||
info!("OneWire: search found zero devices on the bus");
|
||||
}
|
||||
|
||||
match water_temp_sensor {
|
||||
Some(device) => {
|
||||
println!("Found one wire device: {:?}", device);
|
||||
info!("Found one wire device: {:?}", device);
|
||||
let mut water_temp_sensor = DS18B20::new(device)?;
|
||||
|
||||
let water_temp: Result<f32, FatError> = loop {
|
||||
@@ -98,11 +158,11 @@ impl<'a> TankSensor<'a> {
|
||||
.await;
|
||||
match &temp {
|
||||
Ok(res) => {
|
||||
println!("Water temp is {}", res);
|
||||
info!("Water temp is {}", res);
|
||||
break temp;
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Could not get water temp {} attempt {}", err, attempt)
|
||||
info!("Could not get water temp {} attempt {}", err, attempt)
|
||||
}
|
||||
}
|
||||
if attempt == 5 {
|
||||
@@ -139,15 +199,9 @@ impl<'a> TankSensor<'a> {
|
||||
|
||||
let mut store = [0_u16; TANK_MULTI_SAMPLE];
|
||||
for sample in store.iter_mut() {
|
||||
let value = self.tank_channel.read_oneshot(&mut self.tank_pin);
|
||||
//force yield
|
||||
*sample = self.tank_channel.read_oneshot(&mut self.tank_pin).await;
|
||||
//force yield between successful samples
|
||||
Timer::after_millis(10).await;
|
||||
match value {
|
||||
Ok(v) => *sample = v,
|
||||
Err(e) => {
|
||||
bail!("ADC Hardware error: {:?}", e);
|
||||
}
|
||||
};
|
||||
}
|
||||
self.tank_power.set_low();
|
||||
|
||||
@@ -156,3 +210,15 @@ impl<'a> TankSensor<'a> {
|
||||
Ok(median_mv / 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[esp_hal::handler]
|
||||
pub fn flow_interrupt_handler() {
|
||||
use esp_hal::peripherals::PCNT;
|
||||
let pcnt = PCNT::regs();
|
||||
if pcnt.int_raw().read().cnt_thr_event_u(1).bit() {
|
||||
if pcnt.u_status(1).read().h_lim().bit() {
|
||||
FLOW_OVERFLOW_COUNTER.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
pcnt.int_clr().write(|w| w.cnt_thr_event_u(1).set_bit());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,42 +2,84 @@ use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::blocking_mutex::Mutex as BlockingMutex;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use log::{error, LevelFilter, Log, Metadata, Record};
|
||||
use log::{LevelFilter, Log, Metadata, Record};
|
||||
|
||||
const MAX_LIVE_LOG_ENTRIES: usize = 128;
|
||||
|
||||
struct LiveLogBuffer {
|
||||
entries: Vec<(u64, String)>,
|
||||
next_seq: u64,
|
||||
}
|
||||
|
||||
impl LiveLogBuffer {
|
||||
const fn new() -> Self {
|
||||
Self {
|
||||
entries: Vec::new(),
|
||||
next_seq: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn push(&mut self, text: String) {
|
||||
if self.entries.len() >= MAX_LIVE_LOG_ENTRIES {
|
||||
self.entries.remove(0);
|
||||
}
|
||||
self.entries.push((self.next_seq, text));
|
||||
self.next_seq += 1;
|
||||
}
|
||||
|
||||
fn get_after(&self, after: Option<u64>) -> (Vec<(u64, String)>, bool, u64) {
|
||||
let next_seq = self.next_seq;
|
||||
match after {
|
||||
None => (self.entries.clone(), false, next_seq),
|
||||
Some(after_seq) => {
|
||||
let result: Vec<_> = self.entries
|
||||
.iter()
|
||||
.filter(|(seq, _)| *seq > after_seq)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
// Dropped if there are entries that should exist (seq > after_seq) but
|
||||
// the oldest retained entry has a higher seq than after_seq + 1.
|
||||
let dropped = if next_seq > after_seq.saturating_add(1) {
|
||||
if let Some((oldest_seq, _)) = self.entries.first() {
|
||||
*oldest_seq > after_seq.saturating_add(1)
|
||||
} else {
|
||||
// Buffer empty but entries were written — all dropped
|
||||
true
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
(result, dropped, next_seq)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InterceptorLogger {
|
||||
// Async mutex for start/stop capture from async context
|
||||
async_capture: Mutex<CriticalSectionRawMutex, ()>,
|
||||
// Blocking mutex for the actual data to be used in sync log()
|
||||
sync_capture: BlockingMutex<CriticalSectionRawMutex, core::cell::RefCell<Option<Vec<String>>>>,
|
||||
live_log: BlockingMutex<CriticalSectionRawMutex, core::cell::RefCell<LiveLogBuffer>>,
|
||||
}
|
||||
|
||||
impl InterceptorLogger {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
async_capture: Mutex::new(()),
|
||||
sync_capture: BlockingMutex::new(core::cell::RefCell::new(None)),
|
||||
live_log: BlockingMutex::new(core::cell::RefCell::new(LiveLogBuffer::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_capture(&self) {
|
||||
let _guard = self.async_capture.lock().await;
|
||||
self.sync_capture.lock(|capture| {
|
||||
*capture.borrow_mut() = Some(Vec::new());
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn stop_capture(&self) -> Option<Vec<String>> {
|
||||
let _guard = self.async_capture.lock().await;
|
||||
self.sync_capture
|
||||
.lock(|capture| capture.borrow_mut().take())
|
||||
/// Returns (entries_after, dropped, next_seq).
|
||||
/// Pass `after = None` to retrieve the entire current buffer.
|
||||
/// Pass `after = Some(seq)` to retrieve only entries with seq > that value.
|
||||
pub fn get_live_logs(&self, after: Option<u64>) -> (Vec<(u64, String)>, bool, u64) {
|
||||
self.live_log.lock(|buf| buf.borrow().get_after(after))
|
||||
}
|
||||
|
||||
pub fn init(&'static self) {
|
||||
match log::set_logger(self).map(|()| log::set_max_level(LevelFilter::Info)) {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
error!("Logger already set: {}", e);
|
||||
Err(_e) => {
|
||||
esp_println::println!("ERROR: Logger already set");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,16 +92,14 @@ impl Log for InterceptorLogger {
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
let message = alloc::format!("{}", record.args());
|
||||
let message = alloc::format!("{}: {}", record.level(), record.args());
|
||||
|
||||
// Print to serial using esp_println
|
||||
esp_println::println!("{}: {}", record.level(), message);
|
||||
// Print to serial
|
||||
esp_println::println!("{}", message);
|
||||
|
||||
// Capture if active
|
||||
self.sync_capture.lock(|capture| {
|
||||
if let Some(ref mut buffer) = *capture.borrow_mut() {
|
||||
buffer.push(alloc::format!("{}: {}", record.level(), message));
|
||||
}
|
||||
// Store in live log ring buffer
|
||||
self.live_log.lock(|buf| {
|
||||
buf.borrow_mut().push(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,6 +311,10 @@ pub enum LogMessage {
|
||||
PumpOpenLoopCurrent,
|
||||
#[strum(serialize = "Pump Open current sensor required but did not work: ${number_a}")]
|
||||
PumpMissingSensorCurrent,
|
||||
#[strum(
|
||||
serialize = "Fertilizer applied for ${number_a}s on plant ${number_b} (last application ${txt_short} minutes ago)"
|
||||
)]
|
||||
FertilizerApplied,
|
||||
#[strum(serialize = "MPPT Current sensor could not be reached")]
|
||||
MPPTError,
|
||||
#[strum(
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
esp_bootloader_esp_idf::esp_app_desc!();
|
||||
use esp_backtrace as _;
|
||||
|
||||
use crate::hal::PROGRESS_ACTIVE;
|
||||
use crate::config::{NetworkConfig, PlantConfig, PlantControllerConfig};
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::hal::esp::MQTT_STAY_ALIVE;
|
||||
use crate::hal::PROGRESS_ACTIVE;
|
||||
|
||||
use crate::log::log;
|
||||
use crate::tank::{determine_tank_state, TankError, TankState, WATER_FROZEN_THRESH};
|
||||
use crate::webserver::http_server;
|
||||
@@ -67,6 +67,7 @@ mod config;
|
||||
mod fat_error;
|
||||
mod hal;
|
||||
mod log;
|
||||
mod mqtt;
|
||||
mod plant_state;
|
||||
mod tank;
|
||||
mod webserver;
|
||||
@@ -83,12 +84,6 @@ enum WaitType {
|
||||
MqttConfig,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||
struct Solar {
|
||||
current_ma: u32,
|
||||
voltage_ma: u32,
|
||||
}
|
||||
|
||||
impl WaitType {
|
||||
fn blink_pattern(&self) -> u64 {
|
||||
match self {
|
||||
@@ -114,17 +109,6 @@ struct LightState {
|
||||
is_day: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
||||
///mqtt struct to track pump activities
|
||||
struct PumpInfo {
|
||||
enabled: bool,
|
||||
pump_ineffective: bool,
|
||||
median_current_ma: u16,
|
||||
max_current_ma: u16,
|
||||
min_current_ma: u16,
|
||||
error: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct PumpResult {
|
||||
median_current_ma: u16,
|
||||
@@ -132,8 +116,9 @@ pub struct PumpResult {
|
||||
min_current_ma: u16,
|
||||
error: String,
|
||||
flow_value_ml: f32,
|
||||
flow_value_count: i16,
|
||||
flow_value_count: u32,
|
||||
pump_time_s: u16,
|
||||
overcurrent_ma: Option<u16>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, PartialEq)]
|
||||
@@ -239,7 +224,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
println!("starting webserver");
|
||||
|
||||
let _ = http_server(reboot_now.clone(), stack);
|
||||
spawner.spawn(http_server(reboot_now.clone(), stack)?);
|
||||
wait_infinity(board, WaitType::MissingConfig, reboot_now.clone(), UTC).await;
|
||||
}
|
||||
|
||||
@@ -317,7 +302,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
let stack_val = stack.take();
|
||||
if let Some(s) = stack_val {
|
||||
spawner.spawn(http_server(reboot_now.clone(), s))?;
|
||||
spawner.spawn(http_server(reboot_now.clone(), s)?);
|
||||
} else {
|
||||
bail!("Network stack missing, hard abort")
|
||||
}
|
||||
@@ -326,7 +311,9 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
log(LogMessage::NormalRun, 0, 0, "", "");
|
||||
}
|
||||
|
||||
let dry_run = MQTT_STAY_ALIVE.load(Ordering::Relaxed);
|
||||
// if stay alive is true then the hardware will determine state and pretend to do all actions with logging
|
||||
// this is to help debug what the hardware would do with the current settings applied
|
||||
let dry_run = mqtt::is_stay_alive();
|
||||
|
||||
let tank_state = determine_tank_state(&mut board).await;
|
||||
|
||||
@@ -384,7 +371,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
let moisture = board.board_hal.measure_moisture_hz().await?;
|
||||
|
||||
let plantstate: [PlantState; PLANT_COUNT] = [
|
||||
let mut plantstate: [PlantState; PLANT_COUNT] = [
|
||||
PlantState::interpret_raw_values(moisture, 0, &mut board).await,
|
||||
PlantState::interpret_raw_values(moisture, 1, &mut board).await,
|
||||
PlantState::interpret_raw_values(moisture, 2, &mut board).await,
|
||||
@@ -408,6 +395,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
&& !water_frozen;
|
||||
if pump_required {
|
||||
log(LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
let mut overcurrent_results: [Option<u16>; PLANT_COUNT] = [None; PLANT_COUNT];
|
||||
for (plant_id, (state, plant_config)) in plantstate
|
||||
.iter()
|
||||
.zip(&board.board_hal.get_config().plants.clone())
|
||||
@@ -454,12 +442,15 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
0,
|
||||
0,
|
||||
String::new(),
|
||||
0,
|
||||
0.0,
|
||||
)
|
||||
.await;
|
||||
|
||||
let result = do_secure_pump(&mut board, plant_id, plant_config, dry_run).await;
|
||||
match result {
|
||||
Ok(state) => {
|
||||
overcurrent_results[plant_id] = state.overcurrent_ma;
|
||||
pump_info(
|
||||
&mut board,
|
||||
plant_id,
|
||||
@@ -469,6 +460,8 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
state.max_current_ma,
|
||||
state.min_current_ma,
|
||||
state.error,
|
||||
state.flow_value_count,
|
||||
state.flow_value_ml,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -482,6 +475,8 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
0,
|
||||
0,
|
||||
format!("{err:?}"),
|
||||
0,
|
||||
0.0,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -497,6 +492,16 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
.store_consecutive_pump_count(plant_id, 0);
|
||||
}
|
||||
}
|
||||
for (plant_id, overcurrent) in overcurrent_results.iter().enumerate() {
|
||||
if let Some(current_ma) = *overcurrent {
|
||||
plantstate[plant_id].pump.overcurrent_error = Some(current_ma);
|
||||
}
|
||||
}
|
||||
publish_plant_states(&mut board, &timezone_time.clone(), &plantstate)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
error!("Error publishing plant states after pumping {e}");
|
||||
});
|
||||
} else {
|
||||
// Pump corrosion protection: pulses each pump once a week for 2s around midday.
|
||||
let last_check_day = board
|
||||
@@ -622,11 +627,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
match &serde_json::to_string(&light_state) {
|
||||
Ok(state) => {
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/light", state)
|
||||
.await;
|
||||
let _ = mqtt::publish("/light", state).await;
|
||||
}
|
||||
Err(err) => {
|
||||
info!("Error publishing lightstate {err}");
|
||||
@@ -636,44 +637,29 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
let deep_sleep_duration_minutes: u32 =
|
||||
// if battery soc is unknown assume battery has enough change
|
||||
if matches!(battery_state, BatteryState::Info(data) if data.state_of_charge < 10) {
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/deepsleep", "low Volt 12h").await;
|
||||
let _ = mqtt::publish("/deepsleep", "low Volt 12h").await;
|
||||
12 * 60
|
||||
} else if is_day {
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/deepsleep", "normal 20m").await;
|
||||
let _ = mqtt::publish("/deepsleep", "normal 20m").await;
|
||||
20
|
||||
} else {
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/deepsleep", "night 1h").await;
|
||||
let _ = mqtt::publish("/deepsleep", "night 1h").await;
|
||||
60
|
||||
};
|
||||
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/state", "sleep")
|
||||
.await;
|
||||
info!("Go to sleep for {deep_sleep_duration_minutes} minutes");
|
||||
let _ = mqtt::publish("/state", "sleep").await;
|
||||
|
||||
//determine next event
|
||||
//is light out of work trigger soon?
|
||||
//is battery low ??
|
||||
//is deep sleep
|
||||
|
||||
let stay_alive = MQTT_STAY_ALIVE.load(Ordering::Relaxed);
|
||||
info!("Check stay alive, current state is {stay_alive}");
|
||||
let stay_alive = mqtt::is_stay_alive();
|
||||
info!("Check stay alive, current state is {}", stay_alive);
|
||||
|
||||
if stay_alive {
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
if let Some(s) = stack.take() {
|
||||
spawner.spawn(http_server(reboot_now.clone(), s))?;
|
||||
spawner.spawn(http_server(reboot_now.clone(), s)?);
|
||||
wait_infinity(board, WaitType::MqttConfig, reboot_now.clone(), timezone).await;
|
||||
} else {
|
||||
bail!("Network Stack missing, hard abort");
|
||||
@@ -690,9 +676,9 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
Timer::after_millis(5000).await;
|
||||
|
||||
board.board_hal.get_esp().set_restart_to_conf(false);
|
||||
let _ = board
|
||||
board
|
||||
.board_hal
|
||||
.deep_sleep(1000 * 1000 * 60 * deep_sleep_duration_minutes as u64)
|
||||
.deep_sleep_ms(1000 * 60 * deep_sleep_duration_minutes as u64)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -709,19 +695,58 @@ pub async fn do_secure_pump(
|
||||
let steps_in_50ms = plant_config.pump_time_s as usize * 20;
|
||||
|
||||
let mut current_collector = vec![0_u16; steps_in_50ms];
|
||||
let mut flow_collector = vec![0_i16; steps_in_50ms];
|
||||
let mut flow_collector = vec![0_u32; steps_in_50ms];
|
||||
let mut error = String::new();
|
||||
let mut first_error = true;
|
||||
let mut pump_time_ms: u32 = 0;
|
||||
let mut overcurrent_ma: Option<u16> = None;
|
||||
|
||||
if !dry_run {
|
||||
// Run fertilizer pump first if configured and not in cooldown
|
||||
if plant_config.fertilizer_s > 0 {
|
||||
let current_time = board.board_hal.get_time().await;
|
||||
let last_fertilizer = board.board_hal.get_esp().last_fertilizer_time(plant_id);
|
||||
let elapsed_minutes = (current_time.timestamp() - last_fertilizer) / 60;
|
||||
|
||||
if elapsed_minutes >= plant_config.fertilizer_cooldown_min as i64 {
|
||||
info!(
|
||||
"Starting fertilizer pump for {} seconds (last fertilizer was {} minutes ago)",
|
||||
plant_config.fertilizer_s, elapsed_minutes
|
||||
);
|
||||
log(
|
||||
LogMessage::FertilizerApplied,
|
||||
plant_config.fertilizer_s as u32,
|
||||
(plant_id + 1) as u32,
|
||||
&elapsed_minutes.to_string(),
|
||||
"",
|
||||
);
|
||||
board.board_hal.fertilizer_pump(true).await?;
|
||||
Timer::after_millis(plant_config.fertilizer_s as u64 * 1000).await;
|
||||
board.board_hal.fertilizer_pump(false).await?;
|
||||
info!("Fertilizer pump stopped");
|
||||
|
||||
// Store the current time as last fertilizer time
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.store_last_fertilizer_time(plant_id, current_time);
|
||||
} else {
|
||||
let remaining_minutes =
|
||||
plant_config.fertilizer_cooldown_min as i64 - elapsed_minutes;
|
||||
info!(
|
||||
"Skipping fertilizer (cooldown: {} minutes remaining)",
|
||||
remaining_minutes
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
board.board_hal.get_tank_sensor()?.reset_flow_meter();
|
||||
board.board_hal.get_tank_sensor()?.start_flow_meter();
|
||||
board.board_hal.pump(plant_id, true).await?;
|
||||
|
||||
for step in 0..steps_in_50ms {
|
||||
let step_start = Instant::now();
|
||||
let flow_value = board.board_hal.get_tank_sensor()?.get_flow_meter_value();
|
||||
let flow_value = board.board_hal.get_tank_sensor()?.get_full_flow_count();
|
||||
flow_collector[step] = flow_value;
|
||||
let flow_value_ml = flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
|
||||
|
||||
@@ -752,6 +777,7 @@ pub async fn do_secure_pump(
|
||||
plant_config.max_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
);
|
||||
overcurrent_ma = Some(current_ma);
|
||||
error = err_msg;
|
||||
} else if high_current && first_error {
|
||||
let err_msg = format!("OverCurrent: {}mA", current_ma);
|
||||
@@ -762,6 +788,7 @@ pub async fn do_secure_pump(
|
||||
plant_config.max_pump_current_ma.to_string().as_str(),
|
||||
step.to_string().as_str(),
|
||||
);
|
||||
overcurrent_ma = Some(current_ma);
|
||||
board.board_hal.general_fault(true).await;
|
||||
board.board_hal.fault(plant_id, true).await?;
|
||||
if !plant_config.ignore_current_error {
|
||||
@@ -831,7 +858,7 @@ pub async fn do_secure_pump(
|
||||
pump_time_ms = 1337;
|
||||
}
|
||||
board.board_hal.get_tank_sensor()?.stop_flow_meter();
|
||||
let final_flow_value = board.board_hal.get_tank_sensor()?.get_flow_meter_value();
|
||||
let final_flow_value = board.board_hal.get_tank_sensor()?.get_full_flow_count();
|
||||
let flow_value_ml = final_flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
|
||||
info!("Final flow value is {final_flow_value} with {flow_value_ml} ml");
|
||||
current_collector.sort();
|
||||
@@ -843,6 +870,7 @@ pub async fn do_secure_pump(
|
||||
flow_value_count: final_flow_value,
|
||||
pump_time_s: (pump_time_ms / 1000) as u16,
|
||||
error,
|
||||
overcurrent_ma,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -867,11 +895,7 @@ async fn publish_tank_state(
|
||||
let state = serde_json::to_string(
|
||||
&tank_state.as_mqtt_info(&board.board_hal.get_config().tank, &water_temp),
|
||||
)?;
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/water", &state)
|
||||
.await;
|
||||
let _ = mqtt::publish("/water", &*state).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -887,11 +911,7 @@ async fn publish_plant_states(
|
||||
{
|
||||
let state = serde_json::to_string(&plant_state.to_mqtt_info(plant_conf, timezone_time))?;
|
||||
let plant_topic = format!("/plant{}", plant_id + 1);
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish(&plant_topic, &state)
|
||||
.await;
|
||||
let _ = mqtt::publish(&plant_topic, &state).await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -902,13 +922,12 @@ async fn publish_firmware_info(
|
||||
ip_address: &str,
|
||||
timezone_time: &str,
|
||||
) {
|
||||
let esp = board.board_hal.get_esp();
|
||||
esp.mqtt_publish("/firmware/address", ip_address).await;
|
||||
esp.mqtt_publish("/firmware/state", format!("{:?}", &version).as_str())
|
||||
mqtt::publish("/firmware/address", ip_address).await;
|
||||
mqtt::publish("/firmware/state", format!("{:?}", &version).as_str())
|
||||
.await;
|
||||
esp.mqtt_publish("/firmware/last_online", timezone_time)
|
||||
mqtt::publish("/firmware/last_online", timezone_time)
|
||||
.await;
|
||||
esp.mqtt_publish("/state", "online").await;
|
||||
mqtt::publish("/state", "online").await;
|
||||
}
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
@@ -948,12 +967,7 @@ async fn try_connect_wifi_sntp_mqtt(
|
||||
let mqtt_connected = if board.board_hal.get_config().network.mqtt_url.is_some() {
|
||||
let nw_config = board.board_hal.get_config().network.clone();
|
||||
let nw_config = mk_static!(NetworkConfig, nw_config);
|
||||
match board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt(nw_config, stack, spawner)
|
||||
.await
|
||||
{
|
||||
match mqtt::mqtt_init(nw_config, stack, spawner).await {
|
||||
Ok(_) => {
|
||||
info!("Mqtt connection ready");
|
||||
true
|
||||
@@ -997,24 +1011,24 @@ async fn pump_info(
|
||||
max_current_ma: u16,
|
||||
min_current_ma: u16,
|
||||
error: String,
|
||||
flow_raw: u32,
|
||||
flow_ml: f32,
|
||||
) {
|
||||
let pump_info = PumpInfo {
|
||||
let pump_info = mqtt::PumpInfo {
|
||||
enabled: pump_active,
|
||||
pump_ineffective,
|
||||
median_current_ma,
|
||||
max_current_ma,
|
||||
min_current_ma,
|
||||
error,
|
||||
flow_raw,
|
||||
flow_ml,
|
||||
};
|
||||
let pump_topic = format!("/pump{}", plant_id + 1);
|
||||
|
||||
match serde_json::to_string(&pump_info) {
|
||||
Ok(state) => {
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish(&pump_topic, &state)
|
||||
.await;
|
||||
let _ = mqtt::publish(&pump_topic, &state).await;
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("Error publishing pump state {err}");
|
||||
@@ -1027,16 +1041,12 @@ async fn publish_mppt_state(
|
||||
) -> FatResult<()> {
|
||||
let current = board.board_hal.get_mptt_current().await?;
|
||||
let voltage = board.board_hal.get_mptt_voltage().await?;
|
||||
let solar_state = Solar {
|
||||
let solar_state = mqtt::Solar {
|
||||
current_ma: current.as_milliamperes() as u32,
|
||||
voltage_ma: voltage.as_millivolts() as u32,
|
||||
};
|
||||
if let Ok(serialized_solar_state_bytes) = serde_json::to_string(&solar_state) {
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/mppt", &serialized_solar_state_bytes)
|
||||
.await;
|
||||
let _ = mqtt::publish("/mppt", &serialized_solar_state_bytes).await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1053,11 +1063,7 @@ async fn publish_battery_state(
|
||||
Err(_) => "error".to_owned(),
|
||||
};
|
||||
{
|
||||
let _ = board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.mqtt_publish("/battery", &value)
|
||||
.await;
|
||||
let _ = mqtt::publish("/battery", &*value).await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1122,7 +1128,7 @@ async fn wait_infinity(
|
||||
board.board_hal.get_esp().set_restart_to_conf(false);
|
||||
// ensure clean http answer / visible confirmation
|
||||
Timer::after_millis(500).await;
|
||||
board.board_hal.deep_sleep(0).await;
|
||||
board.board_hal.deep_sleep_ms(0).await;
|
||||
}
|
||||
|
||||
// Short tick while holding so the pattern updates smoothly.
|
||||
@@ -1162,9 +1168,8 @@ async fn wait_infinity(
|
||||
let cur = board.board_hal.get_time().await;
|
||||
let timezone_time = cur.with_timezone(&timezone);
|
||||
|
||||
let esp = board.board_hal.get_esp();
|
||||
esp.mqtt_publish("/state", "config").await;
|
||||
esp.mqtt_publish("/firmware/last_online", &timezone_time.to_rfc3339())
|
||||
mqtt::publish("/state", "config").await;
|
||||
mqtt::publish("/firmware/last_online", &timezone_time.to_rfc3339())
|
||||
.await;
|
||||
last_mqtt_update = Some(now);
|
||||
}
|
||||
@@ -1218,7 +1223,7 @@ async fn wait_infinity(
|
||||
|
||||
hal::PlantHal::feed_watchdog();
|
||||
|
||||
if wait_type == WaitType::MqttConfig && !MQTT_STAY_ALIVE.load(Ordering::Relaxed) {
|
||||
if wait_type == WaitType::MqttConfig && !mqtt::is_stay_alive() {
|
||||
reboot_now.store(true, Ordering::Relaxed);
|
||||
}
|
||||
if reboot_now.load(Ordering::Relaxed) {
|
||||
@@ -1231,7 +1236,7 @@ async fn wait_infinity(
|
||||
.lock()
|
||||
.await
|
||||
.board_hal
|
||||
.deep_sleep(0)
|
||||
.deep_sleep_ms(0)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
@@ -1282,7 +1287,7 @@ use embassy_time::WithTimeout;
|
||||
async fn main(spawner: Spawner) -> ! {
|
||||
// intialize embassy
|
||||
crate::log::INTERCEPTOR.init();
|
||||
spawner.must_spawn(crate::log::log_task());
|
||||
spawner.spawn(log::log_task().unwrap());
|
||||
//force init here!
|
||||
match BOARD_ACCESS.init(
|
||||
PlantHal::create()
|
||||
@@ -1327,12 +1332,17 @@ async fn get_version(
|
||||
let hash = &env!("VERGEN_GIT_SHA")[0..8];
|
||||
|
||||
let board = board.board_hal.get_esp();
|
||||
let heap = esp_alloc::HEAP.stats();
|
||||
VersionInfo {
|
||||
git_hash: branch + "@" + hash,
|
||||
build_time: env!("VERGEN_BUILD_TIMESTAMP").to_owned(),
|
||||
current: format!("{:?}", board.current),
|
||||
slot0_state: format!("{:?}", board.slot0_state),
|
||||
slot1_state: format!("{:?}", board.slot1_state),
|
||||
heap_total: heap.size,
|
||||
heap_used: heap.current_usage,
|
||||
heap_free: heap.size.saturating_sub(heap.current_usage),
|
||||
heap_max_used: heap.max_usage,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1343,4 +1353,8 @@ struct VersionInfo {
|
||||
current: String,
|
||||
slot0_state: String,
|
||||
slot1_state: String,
|
||||
heap_total: usize,
|
||||
heap_used: usize,
|
||||
heap_free: usize,
|
||||
heap_max_used: usize,
|
||||
}
|
||||
|
||||
34
Software/MainBoard/rust/src/mcutie_3_0_0/Cargo.toml
Normal file
34
Software/MainBoard/rust/src/mcutie_3_0_0/Cargo.toml
Normal file
@@ -0,0 +1,34 @@
|
||||
[package]
|
||||
name = "mcutie"
|
||||
version = "3.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
path = "lib.rs"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
homeassistant = []
|
||||
serde = ["dep:serde", "heapless/serde"]
|
||||
defmt = []
|
||||
log = ["dep:log"]
|
||||
|
||||
[dependencies]
|
||||
embassy-net = { version = "0.8.0", default-features = false, features = ["tcp", "dns", "proto-ipv4", "proto-ipv6", "medium-ethernet"] }
|
||||
embassy-sync = { version = "0.8.0", default-features = false }
|
||||
embassy-time = { version = "0.5.1", default-features = false }
|
||||
embassy-futures = { version = "0.1.2", default-features = false }
|
||||
embedded-io = { version = "0.7.1", default-features = false }
|
||||
embedded-io-async = { version = "0.7.0", default-features = false }
|
||||
heapless = { version = "0.7.17", default-features = false }
|
||||
mqttrs = { version = "0.4.1", default-features = false }
|
||||
once_cell = { version = "1.21.3", default-features = false, features = ["critical-section"] }
|
||||
pin-project = { version = "1.1.10", default-features = false }
|
||||
hex = { version = "0.4.3", default-features = false }
|
||||
serde = { version = "1.0.228", default-features = false, features = ["derive"], optional = true }
|
||||
log = { version = "0.4.28", default-features = false, optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
futures-executor = "0.3.31"
|
||||
futures-timer = "3.0.3"
|
||||
futures-util = "0.3.31"
|
||||
124
Software/MainBoard/rust/src/mcutie_3_0_0/buffer.rs
Normal file
124
Software/MainBoard/rust/src/mcutie_3_0_0/buffer.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use core::{cmp, fmt, ops::Deref};
|
||||
|
||||
use embedded_io::{SliceWriteError, Write};
|
||||
use mqttrs::{encode_slice, Packet};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
/// A stack allocated buffer that can be written to and then read back from.
|
||||
/// Dereferencing as a [`u8`] slice allows access to previously written data.
|
||||
///
|
||||
/// Can be written to with [`write!`] and supports [`embedded_io::Write`] and
|
||||
/// [`embedded_io_async::Write`].
|
||||
pub struct Buffer<const N: usize> {
|
||||
bytes: [u8; N],
|
||||
cursor: usize,
|
||||
}
|
||||
|
||||
impl<const N: usize> Default for Buffer<N> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Buffer<N> {
|
||||
/// Creates a new buffer.
|
||||
pub(crate) const fn new() -> Self {
|
||||
Self {
|
||||
bytes: [0; N],
|
||||
cursor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new buffer and writes the given data into it.
|
||||
pub(crate) fn from(buf: &[u8]) -> Result<Self, Error> {
|
||||
let mut buffer = Self::new();
|
||||
match buffer.write_all(buf) {
|
||||
Ok(()) => Ok(buffer),
|
||||
Err(_) => Err(Error::TooLarge),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn encode_packet(&mut self, packet: &Packet<'_>) -> Result<(), mqttrs::Error> {
|
||||
let len = encode_slice(packet, &mut self.bytes[self.cursor..])?;
|
||||
self.cursor += len;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
/// Serializes a value into this buffer using JSON.
|
||||
pub(crate) fn serialize_json<T: serde::Serialize>(
|
||||
&mut self,
|
||||
value: &T,
|
||||
) -> Result<(), serde_json_core::ser::Error> {
|
||||
let len = serde_json_core::to_slice(value, &mut self.bytes[self.cursor..])?;
|
||||
self.cursor += len;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
/// Deserializes this buffer using JSON into the given type.
|
||||
pub fn deserialize_json<'a, T: serde::Deserialize<'a>>(
|
||||
&'a self,
|
||||
) -> Result<T, serde_json_core::de::Error> {
|
||||
let (result, _) = serde_json_core::from_slice(self)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// The number of bytes available for writing into this buffer.
|
||||
pub fn available(&self) -> usize {
|
||||
N - self.cursor
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> Deref for Buffer<N> {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.bytes[0..self.cursor]
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> fmt::Write for Buffer<N> {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> embedded_io::ErrorType for Buffer<N> {
|
||||
type Error = SliceWriteError;
|
||||
}
|
||||
|
||||
impl<const N: usize> embedded_io::Write for Buffer<N> {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
if buf.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let writable = cmp::min(self.available(), buf.len());
|
||||
if writable == 0 {
|
||||
Err(SliceWriteError::Full)
|
||||
} else {
|
||||
self.bytes[self.cursor..self.cursor + writable].copy_from_slice(buf);
|
||||
self.cursor += writable;
|
||||
Ok(writable)
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> embedded_io_async::Write for Buffer<N> {
|
||||
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
<Self as embedded_io::Write>::write(self, buf)
|
||||
}
|
||||
|
||||
async fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
80
Software/MainBoard/rust/src/mcutie_3_0_0/fmt.rs
Normal file
80
Software/MainBoard/rust/src/mcutie_3_0_0/fmt.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
#![macro_use]
|
||||
|
||||
#[cfg(all(feature = "defmt", feature = "log"))]
|
||||
compile_error!("The `defmt` and `log` features cannot both be enabled at the same time.");
|
||||
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
use core::fmt;
|
||||
|
||||
#[cfg(feature = "defmt")]
|
||||
pub(crate) use ::defmt::Debug2Format;
|
||||
|
||||
#[cfg(not(feature = "defmt"))]
|
||||
pub(crate) struct Debug2Format<D: fmt::Debug>(pub(crate) D);
|
||||
|
||||
#[cfg(feature = "log")]
|
||||
impl<D: fmt::Debug> fmt::Debug for Debug2Format<D> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! trace {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::trace!($s $(, $x)*);
|
||||
#[cfg(feature = "log")]
|
||||
::log::trace!($s $(, $x)*);
|
||||
#[cfg(not(any(feature="defmt", feature="log")))]
|
||||
let _ = ($( & $x ),*);
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! debug {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::debug!($s $(, $x)*);
|
||||
#[cfg(feature = "log")]
|
||||
::log::debug!($s $(, $x)*);
|
||||
#[cfg(not(any(feature="defmt", feature="log")))]
|
||||
let _ = ($( & $x ),*);
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! info {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::info!($s $(, $x)*);
|
||||
#[cfg(feature = "log")]
|
||||
::log::info!($s $(, $x)*);
|
||||
#[cfg(not(any(feature="defmt", feature="log")))]
|
||||
let _ = ($( & $x ),*);
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! warn {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::warn!($s $(, $x)*);
|
||||
#[cfg(feature = "log")]
|
||||
::log::warn!($s $(, $x)*);
|
||||
#[cfg(not(any(feature="defmt", feature="log")))]
|
||||
let _ = ($( & $x ),*);
|
||||
};
|
||||
}
|
||||
|
||||
#[collapse_debuginfo(yes)]
|
||||
macro_rules! error {
|
||||
($s:literal $(, $x:expr)* $(,)?) => {
|
||||
#[cfg(feature = "defmt")]
|
||||
::defmt::error!($s $(, $x)*);
|
||||
#[cfg(feature = "log")]
|
||||
::log::error!($s $(, $x)*);
|
||||
#[cfg(not(any(feature="defmt", feature="log")))]
|
||||
let _ = ($( & $x ),*);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
//! Tools for publishing a [Home Assistant binary sensor](https://www.home-assistant.io/integrations/binary_sensor.mqtt/).
|
||||
use core::ops::Deref;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{homeassistant::Component, Error, Publishable, Topic};
|
||||
|
||||
/// The state of the sensor. Can be easily converted to or from a [`bool`].
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(from = "&str", into = "&'static str")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum BinarySensorState {
|
||||
On,
|
||||
Off,
|
||||
}
|
||||
|
||||
impl From<BinarySensorState> for &'static str {
|
||||
fn from(state: BinarySensorState) -> Self {
|
||||
match state {
|
||||
BinarySensorState::On => "ON",
|
||||
BinarySensorState::Off => "OFF",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for BinarySensorState {
|
||||
fn from(st: &'a str) -> Self {
|
||||
if st == "ON" {
|
||||
Self::On
|
||||
} else {
|
||||
Self::Off
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<bool> for BinarySensorState {
|
||||
fn from(val: bool) -> Self {
|
||||
if val {
|
||||
BinarySensorState::On
|
||||
} else {
|
||||
BinarySensorState::Off
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BinarySensorState> for bool {
|
||||
fn from(val: BinarySensorState) -> Self {
|
||||
match val {
|
||||
BinarySensorState::On => true,
|
||||
BinarySensorState::Off => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for BinarySensorState {
|
||||
fn as_ref(&self) -> &'static [u8] {
|
||||
match self {
|
||||
Self::On => "ON".as_bytes(),
|
||||
Self::Off => "OFF".as_bytes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The type of sensor.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum BinarySensorClass {
|
||||
Battery,
|
||||
BatteryCharging,
|
||||
CarbonMonoxide,
|
||||
Cold,
|
||||
Connectivity,
|
||||
Door,
|
||||
GarageDoor,
|
||||
Gas,
|
||||
Heat,
|
||||
Light,
|
||||
Lock,
|
||||
Moisture,
|
||||
Motion,
|
||||
Moving,
|
||||
Occupancy,
|
||||
Opening,
|
||||
Plug,
|
||||
Power,
|
||||
Presence,
|
||||
Problem,
|
||||
Running,
|
||||
Safety,
|
||||
Smoke,
|
||||
Sound,
|
||||
Tamper,
|
||||
Update,
|
||||
Vibration,
|
||||
Window,
|
||||
}
|
||||
|
||||
/// A binary sensor that can publish a [`BinarySensorState`] status.
|
||||
#[derive(Serialize)]
|
||||
pub struct BinarySensor {
|
||||
/// The type of sensor
|
||||
pub device_class: Option<BinarySensorClass>,
|
||||
}
|
||||
|
||||
impl Component for BinarySensor {
|
||||
type State = BinarySensorState;
|
||||
|
||||
fn platform() -> &'static str {
|
||||
"binary_sensor"
|
||||
}
|
||||
|
||||
async fn publish_state<T: Deref<Target = str>>(
|
||||
&self,
|
||||
topic: &Topic<T>,
|
||||
state: Self::State,
|
||||
) -> Result<(), Error> {
|
||||
topic.with_bytes(state).publish().await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//! Tools for publishing a [Home Assistant button](https://www.home-assistant.io/integrations/button.mqtt/).
|
||||
use core::ops::Deref;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{homeassistant::Component, Error, Topic};
|
||||
|
||||
/// The type of button.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum ButtonClass {
|
||||
Identify,
|
||||
Restart,
|
||||
Update,
|
||||
}
|
||||
|
||||
/// A button that can be pressed.
|
||||
#[derive(Serialize)]
|
||||
pub struct Button {
|
||||
/// The type of button.
|
||||
pub device_class: Option<ButtonClass>,
|
||||
}
|
||||
|
||||
impl Component for Button {
|
||||
type State = ();
|
||||
|
||||
fn platform() -> &'static str {
|
||||
"button"
|
||||
}
|
||||
|
||||
async fn publish_state<T: Deref<Target = str>>(
|
||||
&self,
|
||||
_topic: &Topic<T>,
|
||||
_state: Self::State,
|
||||
) -> Result<(), Error> {
|
||||
// Buttons don't have a state
|
||||
Err(Error::Invalid)
|
||||
}
|
||||
}
|
||||
384
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/light.rs
Normal file
384
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/light.rs
Normal file
@@ -0,0 +1,384 @@
|
||||
//! Tools for publishing a [Home Assistant light](https://www.home-assistant.io/integrations/light.mqtt/).
|
||||
use core::{ops::Deref, str};
|
||||
|
||||
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
|
||||
|
||||
use crate::{
|
||||
fmt::Debug2Format,
|
||||
homeassistant::{binary_sensor::BinarySensorState, ser::List, Component},
|
||||
Error, Payload, Publishable, Topic,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum SupportedColorMode {
|
||||
OnOff,
|
||||
Brightness,
|
||||
#[serde(rename = "color_temp")]
|
||||
ColorTemp,
|
||||
Hs,
|
||||
Xy,
|
||||
Rgb,
|
||||
Rgbw,
|
||||
Rgbww,
|
||||
White,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
struct SerializedColor {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
h: Option<f32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
s: Option<f32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
x: Option<f32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
y: Option<f32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
r: Option<u8>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
g: Option<u8>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
b: Option<u8>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
w: Option<u8>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
c: Option<u8>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LedPayload<'a> {
|
||||
state: BinarySensorState,
|
||||
#[serde(default)]
|
||||
brightness: Option<u8>,
|
||||
#[serde(default)]
|
||||
color_temp: Option<u32>,
|
||||
#[serde(default)]
|
||||
color: Option<SerializedColor>,
|
||||
#[serde(default)]
|
||||
effect: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// The color of the light in various forms.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "lowercase", tag = "color_mode", content = "color")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum Color {
|
||||
None,
|
||||
Brightness(u8),
|
||||
ColorTemp(u32),
|
||||
Hs {
|
||||
#[serde(rename = "h")]
|
||||
hue: f32,
|
||||
#[serde(rename = "s")]
|
||||
saturation: f32,
|
||||
},
|
||||
Xy {
|
||||
x: f32,
|
||||
y: f32,
|
||||
},
|
||||
Rgb {
|
||||
#[serde(rename = "r")]
|
||||
red: u8,
|
||||
#[serde(rename = "g")]
|
||||
green: u8,
|
||||
#[serde(rename = "b")]
|
||||
blue: u8,
|
||||
},
|
||||
Rgbw {
|
||||
#[serde(rename = "r")]
|
||||
red: u8,
|
||||
#[serde(rename = "g")]
|
||||
green: u8,
|
||||
#[serde(rename = "b")]
|
||||
blue: u8,
|
||||
#[serde(rename = "w")]
|
||||
white: u8,
|
||||
},
|
||||
Rgbww {
|
||||
#[serde(rename = "r")]
|
||||
red: u8,
|
||||
#[serde(rename = "g")]
|
||||
green: u8,
|
||||
#[serde(rename = "b")]
|
||||
blue: u8,
|
||||
#[serde(rename = "c")]
|
||||
cool_white: u8,
|
||||
#[serde(rename = "w")]
|
||||
warm_white: u8,
|
||||
},
|
||||
}
|
||||
|
||||
/// The state of the light. This can be sent to the broker and received as a
|
||||
/// command from Home Assistant.
|
||||
pub struct LightState<'a> {
|
||||
/// Whether the light is on or off.
|
||||
pub state: BinarySensorState,
|
||||
/// The color of the light.
|
||||
pub color: Color,
|
||||
/// Any effect that is applied.
|
||||
pub effect: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> LightState<'a> {
|
||||
/// Parses the state from a command payload.
|
||||
pub fn from_payload(payload: &'a Payload) -> Result<Self, Error> {
|
||||
let parsed: LedPayload<'a> = match payload.deserialize_json() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
warn!("Failed to deserialize packet: {:?}", Debug2Format(&e));
|
||||
if let Ok(s) = str::from_utf8(payload) {
|
||||
trace!("{}", s);
|
||||
}
|
||||
return Err(Error::PacketError);
|
||||
}
|
||||
};
|
||||
|
||||
let color = if let Some(color) = parsed.color {
|
||||
if let Some(x) = color.x {
|
||||
Color::Xy {
|
||||
x,
|
||||
y: color.y.unwrap_or_default(),
|
||||
}
|
||||
} else if let Some(h) = color.h {
|
||||
Color::Hs {
|
||||
hue: h,
|
||||
saturation: color.s.unwrap_or_default(),
|
||||
}
|
||||
} else if let Some(c) = color.c {
|
||||
Color::Rgbww {
|
||||
red: color.r.unwrap_or_default(),
|
||||
green: color.g.unwrap_or_default(),
|
||||
blue: color.b.unwrap_or_default(),
|
||||
cool_white: c,
|
||||
warm_white: color.w.unwrap_or_default(),
|
||||
}
|
||||
} else if let Some(w) = color.w {
|
||||
Color::Rgbw {
|
||||
red: color.r.unwrap_or_default(),
|
||||
green: color.g.unwrap_or_default(),
|
||||
blue: color.b.unwrap_or_default(),
|
||||
white: w,
|
||||
}
|
||||
} else {
|
||||
Color::Rgb {
|
||||
red: color.r.unwrap_or_default(),
|
||||
green: color.g.unwrap_or_default(),
|
||||
blue: color.b.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
} else if let Some(color_temp) = parsed.color_temp {
|
||||
Color::ColorTemp(color_temp)
|
||||
} else if let Some(brightness) = parsed.brightness {
|
||||
Color::Brightness(brightness)
|
||||
} else {
|
||||
Color::None
|
||||
};
|
||||
|
||||
Ok(LightState {
|
||||
state: parsed.state,
|
||||
color,
|
||||
effect: parsed.effect,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for LightState<'_> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut len = 1;
|
||||
|
||||
if self.effect.is_some() {
|
||||
len += 1;
|
||||
}
|
||||
|
||||
match self.color {
|
||||
Color::None => {}
|
||||
Color::Brightness(_) | Color::ColorTemp(_) => len += 1,
|
||||
_ => len += 2,
|
||||
}
|
||||
|
||||
let mut serializer = serializer.serialize_struct("LightState", len)?;
|
||||
|
||||
serializer.serialize_field("state", &self.state)?;
|
||||
|
||||
if let Some(effect) = self.effect {
|
||||
serializer.serialize_field("effect", effect)?;
|
||||
} else {
|
||||
serializer.skip_field("effect")?;
|
||||
}
|
||||
|
||||
match self.color {
|
||||
Color::None => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
serializer.skip_field("color")?;
|
||||
}
|
||||
Color::Brightness(b) => {
|
||||
serializer.skip_field("color_temp")?;
|
||||
serializer.skip_field("color")?;
|
||||
|
||||
serializer.serialize_field("brightness", &b)?
|
||||
}
|
||||
Color::ColorTemp(c) => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color")?;
|
||||
|
||||
serializer.serialize_field("color_temp", &c)?
|
||||
}
|
||||
Color::Hs { hue, saturation } => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
|
||||
serializer.serialize_field("color_mode", "hs")?;
|
||||
|
||||
let color = SerializedColor {
|
||||
h: Some(hue),
|
||||
s: Some(saturation),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
serializer.serialize_field("color", &color)?
|
||||
}
|
||||
Color::Xy { x, y } => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
|
||||
serializer.serialize_field("color_mode", "xy")?;
|
||||
|
||||
let color = SerializedColor {
|
||||
x: Some(x),
|
||||
y: Some(y),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
serializer.serialize_field("color", &color)?
|
||||
}
|
||||
Color::Rgb { red, green, blue } => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
|
||||
serializer.serialize_field("color_mode", "rgb")?;
|
||||
|
||||
let color = SerializedColor {
|
||||
r: Some(red),
|
||||
g: Some(green),
|
||||
b: Some(blue),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
serializer.serialize_field("color", &color)?
|
||||
}
|
||||
Color::Rgbw {
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
white,
|
||||
} => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
|
||||
serializer.serialize_field("color_mode", "rgbw")?;
|
||||
|
||||
let color = SerializedColor {
|
||||
r: Some(red),
|
||||
g: Some(green),
|
||||
b: Some(blue),
|
||||
w: Some(white),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
serializer.serialize_field("color", &color)?
|
||||
}
|
||||
Color::Rgbww {
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
cool_white,
|
||||
warm_white,
|
||||
} => {
|
||||
serializer.skip_field("brightness")?;
|
||||
serializer.skip_field("color_temp")?;
|
||||
|
||||
serializer.serialize_field("color_mode", "rgbww")?;
|
||||
|
||||
let color = SerializedColor {
|
||||
r: Some(red),
|
||||
g: Some(green),
|
||||
b: Some(blue),
|
||||
c: Some(cool_white),
|
||||
w: Some(warm_white),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
serializer.serialize_field("color", &color)?
|
||||
}
|
||||
}
|
||||
|
||||
serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// A light entity
|
||||
pub struct Light<'a, const C: usize, const E: usize> {
|
||||
/// The color modes supported by the light.
|
||||
pub supported_color_modes: [SupportedColorMode; C],
|
||||
/// Any effects that can be used.
|
||||
pub effects: [&'a str; E],
|
||||
}
|
||||
|
||||
impl<const C: usize, const E: usize> Serialize for Light<'_, C, E> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut len = 2;
|
||||
|
||||
if C > 0 {
|
||||
len += 1;
|
||||
}
|
||||
|
||||
if E > 0 {
|
||||
len += 2;
|
||||
}
|
||||
|
||||
let mut serializer = serializer.serialize_struct("Light", len)?;
|
||||
|
||||
serializer.serialize_field("schema", "json")?;
|
||||
|
||||
if C > 0 {
|
||||
serializer.serialize_field("sup_clrm", &List::new(&self.supported_color_modes))?;
|
||||
} else {
|
||||
serializer.skip_field("sup_clrm")?;
|
||||
}
|
||||
|
||||
if E > 0 {
|
||||
serializer.serialize_field("effect", &true)?;
|
||||
serializer.serialize_field("fx_list", &List::new(&self.effects))?;
|
||||
} else {
|
||||
serializer.skip_field("effect")?;
|
||||
serializer.skip_field("fx_list")?;
|
||||
}
|
||||
|
||||
serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const C: usize, const E: usize> Component for Light<'_, C, E> {
|
||||
type State = LightState<'static>;
|
||||
|
||||
fn platform() -> &'static str {
|
||||
"light"
|
||||
}
|
||||
|
||||
async fn publish_state<T: Deref<Target = str>>(
|
||||
&self,
|
||||
topic: &Topic<T>,
|
||||
state: Self::State,
|
||||
) -> Result<(), Error> {
|
||||
topic.with_json(state).publish().await
|
||||
}
|
||||
}
|
||||
295
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/mod.rs
Normal file
295
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/mod.rs
Normal file
@@ -0,0 +1,295 @@
|
||||
//! Home Assistant auto-discovery and related messages.
|
||||
//!
|
||||
//! Normally you would declare your entities statically in your binary. It is
|
||||
//! then trivial to send out discovery messages or state changes.
|
||||
//!
|
||||
//! ```
|
||||
//! # use mcutie::{Publishable, Topic};
|
||||
//! # use mcutie::homeassistant::{Entity, Device, Origin, AvailabilityState, AvailabilityTopics};
|
||||
//! # use mcutie::homeassistant::binary_sensor::{BinarySensor, BinarySensorClass, BinarySensorState};
|
||||
//! const DEVICE_AVAILABILITY_TOPIC: Topic<&'static str> = Topic::Device("status");
|
||||
//! const MOTION_STATE_TOPIC: Topic<&'static str> = Topic::Device("motion/status");
|
||||
//!
|
||||
//! const DEVICE: Device<'static> = Device::new();
|
||||
//! const ORIGIN: Origin<'static> = Origin::new();
|
||||
//!
|
||||
//! const MOTION_SENSOR: Entity<'static, 1, BinarySensor> = Entity {
|
||||
//! device: DEVICE,
|
||||
//! origin: ORIGIN,
|
||||
//! object_id: "motion",
|
||||
//! unique_id: Some("motion"),
|
||||
//! name: "Motion",
|
||||
//! availability: AvailabilityTopics::All([DEVICE_AVAILABILITY_TOPIC]),
|
||||
//! state_topic: Some(MOTION_STATE_TOPIC),
|
||||
//! command_topic: None,
|
||||
//! component: BinarySensor {
|
||||
//! device_class: Some(BinarySensorClass::Motion),
|
||||
//! },
|
||||
//! };
|
||||
//!
|
||||
//! async fn send_discovery_messages() {
|
||||
//! MOTION_SENSOR.publish_discovery().await.unwrap();
|
||||
//! DEVICE_AVAILABILITY_TOPIC.with_bytes(AvailabilityState::Online).publish().await.unwrap();
|
||||
//! }
|
||||
//!
|
||||
//! async fn send_state(state: BinarySensorState) {
|
||||
//! MOTION_SENSOR.publish_state(state).await.unwrap();
|
||||
//! }
|
||||
//! ```
|
||||
use core::{future::Future, ops::Deref};
|
||||
|
||||
use mqttrs::QoS;
|
||||
use serde::{
|
||||
ser::{Error as _, SerializeStruct},
|
||||
Serialize, Serializer,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
device_id, device_type, homeassistant::ser::DiscoverySerializer, io::publish, Error,
|
||||
McutieTask, MqttMessage, Payload, Publishable, Topic, TopicString, DATA_CHANNEL,
|
||||
};
|
||||
|
||||
pub mod binary_sensor;
|
||||
pub mod button;
|
||||
pub mod light;
|
||||
pub mod sensor;
|
||||
mod ser;
|
||||
|
||||
const HA_STATUS_TOPIC: Topic<&'static str> = Topic::General("homeassistant/status");
|
||||
const STATE_ONLINE: &str = "online";
|
||||
const STATE_OFFLINE: &str = "offline";
|
||||
|
||||
/// A trait representing a specific type of entity in Home Assistant
|
||||
pub trait Component: Serialize {
|
||||
/// The state to publish.
|
||||
type State;
|
||||
|
||||
/// The platform identifier for this entity. Internal.
|
||||
fn platform() -> &'static str;
|
||||
|
||||
/// Publishes this entity's state to the MQTT broker.
|
||||
fn publish_state<T: Deref<Target = str>>(
|
||||
&self,
|
||||
topic: &Topic<T>,
|
||||
state: Self::State,
|
||||
) -> impl Future<Output = Result<(), Error>>;
|
||||
}
|
||||
|
||||
impl<'t, T, L, const S: usize> McutieTask<'t, T, L, S>
|
||||
where
|
||||
T: Deref<Target = str> + 't,
|
||||
L: Publishable + 't,
|
||||
{
|
||||
pub(super) async fn ha_after_connected(&self) {
|
||||
let _ = HA_STATUS_TOPIC.subscribe(false).await;
|
||||
}
|
||||
|
||||
pub(super) async fn ha_handle_update(
|
||||
&self,
|
||||
topic: &Topic<TopicString>,
|
||||
payload: &Payload,
|
||||
) -> bool {
|
||||
if topic == &HA_STATUS_TOPIC {
|
||||
if payload.as_ref() == STATE_ONLINE.as_bytes() {
|
||||
DATA_CHANNEL.send(MqttMessage::HomeAssistantOnline).await;
|
||||
}
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deref<Target = str>> Serialize for Topic<T> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let mut topic = TopicString::new();
|
||||
self.to_string(&mut topic)
|
||||
.map_err(|_| S::Error::custom("topic was too large to serialize"))?;
|
||||
serializer.serialize_str(&topic)
|
||||
}
|
||||
}
|
||||
|
||||
fn name_or_device<S>(name: &Option<&str>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(name.unwrap_or_else(|| device_type()))
|
||||
}
|
||||
|
||||
/// Represents the device in Home Assistant.
|
||||
///
|
||||
/// Can just be the default in which case useful properties such as the ID are
|
||||
/// automatically included.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct Device<'a> {
|
||||
/// A name to identify the device. If not provided the default device type is
|
||||
/// used.
|
||||
pub name: Option<&'a str>,
|
||||
/// An optional configuration URL for the device.
|
||||
pub configuration_url: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl Device<'_> {
|
||||
/// Creates a new default device.
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
name: None,
|
||||
configuration_url: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Device<'_> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut len = 2;
|
||||
if self.configuration_url.is_some() {
|
||||
len += 1;
|
||||
}
|
||||
|
||||
let mut serializer = serializer.serialize_struct("Device", len)?;
|
||||
|
||||
serializer.serialize_field("name", self.name.unwrap_or_else(|| device_type()))?;
|
||||
serializer.serialize_field("ids", device_id())?;
|
||||
|
||||
if let Some(cu) = self.configuration_url {
|
||||
serializer.serialize_field("cu", cu)?;
|
||||
} else {
|
||||
serializer.skip_field("cu")?;
|
||||
}
|
||||
|
||||
serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the device's origin in Home Assistant.
|
||||
///
|
||||
/// Can just be the default in which case useful properties are automatically
|
||||
/// included.
|
||||
#[derive(Clone, Copy, Default, Serialize)]
|
||||
pub struct Origin<'a> {
|
||||
/// A name to identify the device's origin. If not provided the default
|
||||
/// device type is used.
|
||||
#[serde(serialize_with = "name_or_device")]
|
||||
pub name: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl Origin<'_> {
|
||||
/// Creates a new default origin.
|
||||
pub const fn new() -> Self {
|
||||
Self { name: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// A single entity for Home Assistant.
|
||||
///
|
||||
/// Calling [`Entity::publish_discovery`] will publish the discovery message to
|
||||
/// allow Home Assistant to detect this entity. Read the
|
||||
/// [Home Assistant MQTT docs](https://www.home-assistant.io/integrations/mqtt/)
|
||||
/// for information on what some of these properties mean.
|
||||
pub struct Entity<'a, const A: usize, C: Component> {
|
||||
/// The device this entity is a part of.
|
||||
pub device: Device<'a>,
|
||||
/// The origin of the device.
|
||||
pub origin: Origin<'a>,
|
||||
/// An object identifier to allow for entity ID customisation in Home Assistant.
|
||||
pub object_id: &'a str,
|
||||
/// An optional unique identifier for the entity.
|
||||
pub unique_id: Option<&'a str>,
|
||||
/// A friendly name for the entity.
|
||||
pub name: &'a str,
|
||||
/// Specifies the availability topics that Home Assistant will listen to to
|
||||
/// determine this entity's availability.
|
||||
pub availability: AvailabilityTopics<'a, A>,
|
||||
/// The state topic that this entity's state is published to.
|
||||
pub state_topic: Option<Topic<&'a str>>,
|
||||
/// The command topic that this entity receives commands from.
|
||||
pub command_topic: Option<Topic<&'a str>>,
|
||||
/// The specific entity.
|
||||
pub component: C,
|
||||
}
|
||||
|
||||
impl<const A: usize, C: Component> Entity<'_, A, C> {
|
||||
/// Publishes the discovery message for this entity to the broker.
|
||||
pub async fn publish_discovery(&self) -> Result<(), Error> {
|
||||
let mut topic = TopicString::new();
|
||||
topic
|
||||
.push_str(option_env!("HA_DISCOVERY_PREFIX").unwrap_or("homeassistant"))
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
topic.push('/').map_err(|_| Error::TooLarge)?;
|
||||
topic.push_str(C::platform()).map_err(|_| Error::TooLarge)?;
|
||||
topic.push('/').map_err(|_| Error::TooLarge)?;
|
||||
topic
|
||||
.push_str(self.object_id)
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
topic.push_str("/config").map_err(|_| Error::TooLarge)?;
|
||||
|
||||
let mut payload = Payload::new();
|
||||
payload.serialize_json(self).map_err(|_| Error::TooLarge)?;
|
||||
|
||||
publish(&topic, &payload, QoS::AtMostOnce, false).await
|
||||
}
|
||||
|
||||
/// Publishes this entity's state to the broker.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// - [`Error::Invalid`] if the entity doesn't have a state topic.
|
||||
pub async fn publish_state(&self, state: C::State) -> Result<(), Error> {
|
||||
if let Some(topic) = self.state_topic {
|
||||
self.component.publish_state(&topic, state).await
|
||||
} else {
|
||||
Err(Error::Invalid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A payload representing a device or entity's availability.
|
||||
#[allow(missing_docs)]
|
||||
pub enum AvailabilityState {
|
||||
Online,
|
||||
Offline,
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for AvailabilityState {
|
||||
fn as_ref(&self) -> &'static [u8] {
|
||||
match self {
|
||||
Self::Online => STATE_ONLINE.as_bytes(),
|
||||
Self::Offline => STATE_OFFLINE.as_bytes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The availiabity topics that home assistant will use to determine an entity's
|
||||
/// availability.
|
||||
pub enum AvailabilityTopics<'a, const A: usize> {
|
||||
/// The entity is always available.
|
||||
None,
|
||||
/// The entity is available if all of the topics are publishes as online.
|
||||
All([Topic<&'a str>; A]),
|
||||
/// The entity is available if any of the topics are publishes as online.
|
||||
Any([Topic<&'a str>; A]),
|
||||
/// The entity is available based on the most recent of the topics to
|
||||
/// publish state.
|
||||
Latest([Topic<&'a str>; A]),
|
||||
}
|
||||
|
||||
impl<const A: usize, C: Component> Serialize for Entity<'_, A, C> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let outer = DiscoverySerializer {
|
||||
discovery: self,
|
||||
inner: serializer,
|
||||
};
|
||||
|
||||
self.component.serialize(outer)
|
||||
}
|
||||
}
|
||||
103
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/sensor.rs
Normal file
103
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/sensor.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
//! Tools for publishing a [Home Assistant sensor](https://www.home-assistant.io/integrations/sensor.mqtt/).
|
||||
use core::ops::Deref;
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{homeassistant::Component, Error, Publishable, Topic};
|
||||
|
||||
/// The type of sensor.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[allow(missing_docs)]
|
||||
pub enum SensorClass {
|
||||
ApparentPower,
|
||||
Aqi,
|
||||
AtmosphericPressure,
|
||||
Battery,
|
||||
CarbonDioxide,
|
||||
CarbonMonoxide,
|
||||
Current,
|
||||
DataRate,
|
||||
DataSize,
|
||||
Date,
|
||||
Distance,
|
||||
Duration,
|
||||
Energy,
|
||||
EnergyStorage,
|
||||
Enum,
|
||||
Frequency,
|
||||
Gas,
|
||||
Humidity,
|
||||
Illuminance,
|
||||
Irradiance,
|
||||
Moisture,
|
||||
Monetary,
|
||||
NitrogenDioxide,
|
||||
NitrogenMonoxide,
|
||||
NitrousOxide,
|
||||
Ozone,
|
||||
Ph,
|
||||
Pm1,
|
||||
Pm25,
|
||||
Pm10,
|
||||
PowerFactor,
|
||||
Power,
|
||||
Precipitation,
|
||||
PrecipitationIntensity,
|
||||
Pressure,
|
||||
ReactivePower,
|
||||
SignalStrength,
|
||||
SoundPressure,
|
||||
Speed,
|
||||
SulphurDioxide,
|
||||
Temperature,
|
||||
Timestamp,
|
||||
VolatileOrganicCompounds,
|
||||
VolatileOrganicCompoundsParts,
|
||||
Voltage,
|
||||
Volume,
|
||||
VolumeFlowRate,
|
||||
VolumeStorage,
|
||||
Water,
|
||||
Weight,
|
||||
WindSpeed,
|
||||
}
|
||||
|
||||
/// The type of measurement that this entity publishes.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SensorStateClass {
|
||||
/// A measurement at a singe point in time.
|
||||
Measurement,
|
||||
/// A cumulative total that can increase or decrease over time.
|
||||
Total,
|
||||
/// A cumulative total that can only increase.
|
||||
TotalIncreasing,
|
||||
}
|
||||
|
||||
/// A binary sensor that can publish a [`f32`] value.
|
||||
#[derive(Serialize)]
|
||||
pub struct Sensor<'u> {
|
||||
/// The type of sensor.
|
||||
pub device_class: Option<SensorClass>,
|
||||
/// The type of measurement that this sensor reports.
|
||||
pub state_class: Option<SensorStateClass>,
|
||||
/// The unit of measurement for this sensor.
|
||||
pub unit_of_measurement: Option<&'u str>,
|
||||
}
|
||||
|
||||
impl Component for Sensor<'_> {
|
||||
type State = f32;
|
||||
|
||||
fn platform() -> &'static str {
|
||||
"sensor"
|
||||
}
|
||||
|
||||
async fn publish_state<T: Deref<Target = str>>(
|
||||
&self,
|
||||
topic: &Topic<T>,
|
||||
state: Self::State,
|
||||
) -> Result<(), Error> {
|
||||
topic.with_display(state).publish().await
|
||||
}
|
||||
}
|
||||
333
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/ser.rs
Normal file
333
Software/MainBoard/rust/src/mcutie_3_0_0/homeassistant/ser.rs
Normal file
@@ -0,0 +1,333 @@
|
||||
use core::ops::Deref;
|
||||
|
||||
use serde::{
|
||||
ser::{SerializeSeq, SerializeStruct},
|
||||
Serialize, Serializer,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
homeassistant::{AvailabilityTopics, Component, Entity},
|
||||
Topic,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub(super) struct AvailabilityTopicItem<'a> {
|
||||
topic: Topic<&'a str>,
|
||||
}
|
||||
|
||||
struct AvailabilityTopicList<'a, T: Deref<Target = str>, const N: usize> {
|
||||
list: &'a [Topic<T>; N],
|
||||
}
|
||||
|
||||
impl<'a, const N: usize, T: Deref<Target = str>> AvailabilityTopicList<'a, T, N> {
|
||||
pub(super) fn new(list: &'a [Topic<T>; N]) -> Self {
|
||||
Self { list }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deref<Target = str>, const N: usize> Serialize for AvailabilityTopicList<'_, T, N> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut serializer = serializer.serialize_seq(Some(N))?;
|
||||
|
||||
for topic in self.list {
|
||||
serializer.serialize_element(&AvailabilityTopicItem {
|
||||
topic: topic.as_ref(),
|
||||
})?;
|
||||
}
|
||||
|
||||
serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct List<'a, T: Serialize, const N: usize> {
|
||||
list: &'a [T; N],
|
||||
}
|
||||
|
||||
impl<'a, T: Serialize, const N: usize> List<'a, T, N> {
|
||||
pub(super) fn new(list: &'a [T; N]) -> Self {
|
||||
Self { list }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, const N: usize> Serialize for List<'_, T, N> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut serializer = serializer.serialize_seq(Some(N))?;
|
||||
|
||||
for item in self.list {
|
||||
serializer.serialize_element(item)?;
|
||||
}
|
||||
|
||||
serializer.end()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct DiscoverySerializer<'a, const A: usize, C: Component, S: Serializer> {
|
||||
pub(super) discovery: &'a Entity<'a, A, C>,
|
||||
pub(super) inner: S,
|
||||
}
|
||||
|
||||
impl<const A: usize, C: Component, S: Serializer> Serializer for DiscoverySerializer<'_, A, C, S> {
|
||||
type Ok = S::Ok;
|
||||
type Error = S::Error;
|
||||
type SerializeSeq = S::SerializeSeq;
|
||||
type SerializeTuple = S::SerializeTuple;
|
||||
type SerializeTupleStruct = S::SerializeTupleStruct;
|
||||
type SerializeTupleVariant = S::SerializeTupleVariant;
|
||||
type SerializeMap = S::SerializeMap;
|
||||
type SerializeStruct = S::SerializeStruct;
|
||||
type SerializeStructVariant = S::SerializeStructVariant;
|
||||
|
||||
fn serialize_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
mut len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||
len += 5;
|
||||
if self.discovery.state_topic.is_some() {
|
||||
len += 1;
|
||||
}
|
||||
if self.discovery.command_topic.is_some() {
|
||||
len += 1;
|
||||
}
|
||||
if self.discovery.unique_id.is_some() {
|
||||
len += 1;
|
||||
}
|
||||
if !matches!(self.discovery.availability, AvailabilityTopics::None) {
|
||||
len += 2;
|
||||
}
|
||||
|
||||
let mut serializer = self.inner.serialize_struct(name, len)?;
|
||||
|
||||
serializer.serialize_field("dev", &self.discovery.device)?;
|
||||
serializer.serialize_field("o", &self.discovery.origin)?;
|
||||
serializer.serialize_field("p", C::platform())?;
|
||||
serializer.serialize_field("obj_id", self.discovery.object_id)?;
|
||||
|
||||
serializer.serialize_field("name", self.discovery.name)?;
|
||||
|
||||
if let Some(t) = self.discovery.state_topic {
|
||||
serializer.serialize_field("stat_t", &t)?;
|
||||
} else {
|
||||
serializer.skip_field("stat_t")?;
|
||||
}
|
||||
|
||||
if let Some(t) = self.discovery.command_topic {
|
||||
serializer.serialize_field("cmd_t", &t)?;
|
||||
} else {
|
||||
serializer.skip_field("cmd_t")?;
|
||||
}
|
||||
|
||||
match &self.discovery.availability {
|
||||
AvailabilityTopics::None => {
|
||||
serializer.skip_field("avty")?;
|
||||
serializer.skip_field("avty_mode")?;
|
||||
}
|
||||
AvailabilityTopics::All(topics) => {
|
||||
serializer.serialize_field("avty_mode", "all")?;
|
||||
serializer.serialize_field("avty", &AvailabilityTopicList::new(topics))?;
|
||||
}
|
||||
AvailabilityTopics::Any(topics) => {
|
||||
serializer.serialize_field("avty_mode", "any")?;
|
||||
serializer.serialize_field("avty", &AvailabilityTopicList::new(topics))?;
|
||||
}
|
||||
AvailabilityTopics::Latest(topics) => {
|
||||
serializer.serialize_field("avty_mode", "latest")?;
|
||||
serializer.serialize_field("avty", &AvailabilityTopicList::new(topics))?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(v) = self.discovery.unique_id {
|
||||
serializer.serialize_field("uniq_id", v)?;
|
||||
} else {
|
||||
serializer.skip_field("uniq_id")?;
|
||||
}
|
||||
|
||||
Ok(serializer)
|
||||
}
|
||||
|
||||
fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_some<T>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T>(self, _: &'static str, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T>(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: &T,
|
||||
) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: ?Sized + Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_: &'static str,
|
||||
_: u32,
|
||||
_: &'static str,
|
||||
_: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_i128(self, _: i128) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn serialize_u128(self, _: u128) -> Result<Self::Ok, Self::Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn collect_seq<I>(self, _: I) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
I: IntoIterator,
|
||||
<I as IntoIterator>::Item: Serialize,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn collect_map<K, V, I>(self, _: I) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
K: Serialize,
|
||||
V: Serialize,
|
||||
I: IntoIterator<Item = (K, V)>,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn collect_str<T>(self, _: &T) -> Result<Self::Ok, Self::Error>
|
||||
where
|
||||
T: ?Sized + core::fmt::Display,
|
||||
{
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn is_human_readable(&self) -> bool {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
483
Software/MainBoard/rust/src/mcutie_3_0_0/io.rs
Normal file
483
Software/MainBoard/rust/src/mcutie_3_0_0/io.rs
Normal file
@@ -0,0 +1,483 @@
|
||||
use core::ops::Deref;
|
||||
|
||||
pub(crate) use atomic16::assign_pid;
|
||||
use embassy_futures::select::{select, select4, Either};
|
||||
use embassy_net::{
|
||||
dns::DnsQueryType,
|
||||
tcp::{TcpReader, TcpSocket, TcpWriter},
|
||||
Stack,
|
||||
};
|
||||
use embassy_sync::{
|
||||
blocking_mutex::raw::CriticalSectionRawMutex,
|
||||
pubsub::{PubSubChannel, Subscriber, WaitResult},
|
||||
};
|
||||
use embassy_time::Timer;
|
||||
use embedded_io_async::Write;
|
||||
use mqttrs::{
|
||||
decode_slice, Connect, ConnectReturnCode, LastWill, Packet, Pid, Protocol, Publish, QoS, QosPid,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
device_id, fmt::Debug2Format, pipe::ConnectedPipe, ControlMessage, Error, MqttMessage, Payload,
|
||||
Publishable, Topic, TopicString, CONFIRMATION_TIMEOUT, DATA_CHANNEL, DEFAULT_BACKOFF,
|
||||
RESET_BACKOFF,
|
||||
};
|
||||
|
||||
static SEND_QUEUE: ConnectedPipe<CriticalSectionRawMutex, Payload, 10> = ConnectedPipe::new();
|
||||
|
||||
pub(crate) static CONTROL_CHANNEL: PubSubChannel<CriticalSectionRawMutex, ControlMessage, 2, 5, 0> =
|
||||
PubSubChannel::new();
|
||||
|
||||
type ControlSubscriber = Subscriber<'static, CriticalSectionRawMutex, ControlMessage, 2, 5, 0>;
|
||||
|
||||
pub(crate) async fn subscribe() -> ControlSubscriber {
|
||||
loop {
|
||||
if let Ok(sub) = CONTROL_CHANNEL.subscriber() {
|
||||
return sub;
|
||||
}
|
||||
|
||||
Timer::after_millis(50).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_has_atomic = "16")]
|
||||
mod atomic16 {
|
||||
use core::sync::atomic::{AtomicU16, Ordering};
|
||||
|
||||
use mqttrs::Pid;
|
||||
|
||||
static PID: AtomicU16 = AtomicU16::new(0);
|
||||
|
||||
pub(crate) async fn assign_pid() -> Pid {
|
||||
Pid::new() + PID.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_has_atomic = "16"))]
|
||||
mod atomic16 {
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
|
||||
use mqttrs::Pid;
|
||||
|
||||
static PID_MUTEX: Mutex<CriticalSectionRawMutex, u16> = Mutex::new(0);
|
||||
|
||||
pub(crate) async fn assign_pid() -> Pid {
|
||||
let mut locked = PID_MUTEX.lock().await;
|
||||
*locked += 1;
|
||||
|
||||
Pid::new() + *locked
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn send_packet(packet: Packet<'_>) -> Result<(), Error> {
|
||||
let mut buffer = Payload::new();
|
||||
|
||||
match buffer.encode_packet(&packet) {
|
||||
Ok(()) => {
|
||||
debug!(
|
||||
"Sending packet to broker: {:?}",
|
||||
Debug2Format(&packet.get_type())
|
||||
);
|
||||
SEND_QUEUE.push(buffer).await;
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
error!("Failed to send packet");
|
||||
Err(Error::PacketError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn wait_for_publish(
|
||||
mut subscriber: ControlSubscriber,
|
||||
expected_pid: Pid,
|
||||
) -> Result<(), Error> {
|
||||
match select(
|
||||
async {
|
||||
loop {
|
||||
match subscriber.next_message().await {
|
||||
WaitResult::Lagged(_) => {
|
||||
// Maybe we missed the message?
|
||||
}
|
||||
WaitResult::Message(ControlMessage::Published(published_pid)) => {
|
||||
if published_pid == expected_pid {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
Timer::after_millis(CONFIRMATION_TIMEOUT),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::First(r) => r,
|
||||
Either::Second(_) => Err(Error::TimedOut),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn publish(
|
||||
topic_name: &str,
|
||||
payload: &[u8],
|
||||
qos: QoS,
|
||||
retain: bool,
|
||||
) -> Result<(), Error> {
|
||||
let subscriber = subscribe().await;
|
||||
|
||||
let (qospid, pid) = match qos {
|
||||
QoS::AtMostOnce => (QosPid::AtMostOnce, None),
|
||||
QoS::AtLeastOnce => {
|
||||
let pid = assign_pid().await;
|
||||
(QosPid::AtLeastOnce(pid), Some(pid))
|
||||
}
|
||||
QoS::ExactlyOnce => {
|
||||
let pid = assign_pid().await;
|
||||
(QosPid::ExactlyOnce(pid), Some(pid))
|
||||
}
|
||||
};
|
||||
|
||||
let packet = Packet::Publish(Publish {
|
||||
dup: false,
|
||||
qospid,
|
||||
retain,
|
||||
topic_name,
|
||||
payload,
|
||||
});
|
||||
|
||||
send_packet(packet).await?;
|
||||
|
||||
if let Some(expected_pid) = pid {
|
||||
wait_for_publish(subscriber, expected_pid).await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn packet_size(buffer: &[u8]) -> Option<usize> {
|
||||
let mut pos = 1;
|
||||
let mut multiplier = 1;
|
||||
let mut value = 0;
|
||||
|
||||
while pos < buffer.len() {
|
||||
value += (buffer[pos] & 127) as usize * multiplier;
|
||||
multiplier *= 128;
|
||||
|
||||
if (buffer[pos] & 128) == 0 {
|
||||
return Some(value + pos + 1);
|
||||
}
|
||||
|
||||
pos += 1;
|
||||
if pos == 5 {
|
||||
return Some(0);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// The MQTT task that must be run in order for the stack to operate.
|
||||
pub struct McutieTask<'t, T, L, const S: usize>
|
||||
where
|
||||
T: Deref<Target = str> + 't,
|
||||
L: Publishable + 't,
|
||||
{
|
||||
pub(crate) network: Stack<'t>,
|
||||
pub(crate) broker: &'t str,
|
||||
pub(crate) last_will: Option<L>,
|
||||
pub(crate) username: Option<&'t str>,
|
||||
pub(crate) password: Option<&'t str>,
|
||||
pub(crate) subscriptions: [Topic<T>; S],
|
||||
pub(crate) keep_alive: u16
|
||||
}
|
||||
|
||||
impl<'t, T, L, const S: usize> McutieTask<'t, T, L, S>
|
||||
where
|
||||
T: Deref<Target = str> + 't,
|
||||
L: Publishable + 't,
|
||||
{
|
||||
#[cfg(not(feature = "homeassistant"))]
|
||||
async fn ha_handle_update(&self, _topic: &Topic<TopicString>, _payload: &Payload) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
async fn recv_loop(&self, mut reader: TcpReader<'_>) -> Result<(), Error> {
|
||||
let mut buffer = [0_u8; 4096];
|
||||
let mut cursor: usize = 0;
|
||||
|
||||
let controller = CONTROL_CHANNEL.immediate_publisher();
|
||||
|
||||
loop {
|
||||
match reader.read(&mut buffer[cursor..]).await {
|
||||
Ok(0) => {
|
||||
error!("Receive socket closed");
|
||||
return Ok(());
|
||||
}
|
||||
Ok(len) => {
|
||||
cursor += len;
|
||||
}
|
||||
Err(_) => {
|
||||
error!("I/O failure reading packet");
|
||||
return Err(Error::IOError);
|
||||
}
|
||||
}
|
||||
|
||||
let mut start_pos = 0;
|
||||
loop {
|
||||
let packet_length = match packet_size(&buffer[start_pos..cursor]) {
|
||||
Some(0) => {
|
||||
error!("Invalid MQTT packet");
|
||||
return Err(Error::PacketError);
|
||||
}
|
||||
Some(len) => len,
|
||||
None => {
|
||||
// None is returned when there is not yet enough data to decode a packet.
|
||||
if start_pos != 0 {
|
||||
// Adjust the buffer to reclaim any unused data
|
||||
buffer.copy_within(start_pos..cursor, 0);
|
||||
cursor -= start_pos;
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
let packet = match decode_slice(&buffer[start_pos..(start_pos + packet_length)]) {
|
||||
Ok(Some(p)) => p,
|
||||
Ok(None) => {
|
||||
error!("Packet length calculation failed.");
|
||||
return Err(Error::PacketError);
|
||||
}
|
||||
Err(_) => {
|
||||
error!("Invalid MQTT packet");
|
||||
return Err(Error::PacketError);
|
||||
}
|
||||
};
|
||||
|
||||
debug!(
|
||||
"Received packet from broker: {:?}",
|
||||
Debug2Format(&packet.get_type())
|
||||
);
|
||||
|
||||
match packet {
|
||||
Packet::Connack(connack) => match connack.code {
|
||||
ConnectReturnCode::Accepted => {
|
||||
#[cfg(feature = "homeassistant")]
|
||||
self.ha_after_connected().await;
|
||||
|
||||
for topic in &self.subscriptions {
|
||||
let _ = topic.subscribe(false).await;
|
||||
}
|
||||
|
||||
DATA_CHANNEL.send(MqttMessage::Connected).await;
|
||||
}
|
||||
_ => {
|
||||
error!("Connection request to broker was not accepted");
|
||||
return Err(Error::IOError);
|
||||
}
|
||||
},
|
||||
Packet::Pingresp => {}
|
||||
|
||||
Packet::Publish(publish) => {
|
||||
match (
|
||||
Topic::from_str(publish.topic_name),
|
||||
Payload::from(publish.payload),
|
||||
) {
|
||||
(Ok(topic), Ok(payload)) => {
|
||||
if !self.ha_handle_update(&topic, &payload).await {
|
||||
DATA_CHANNEL
|
||||
.send(MqttMessage::Publish(topic, payload))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
error!("Unable to process publish data as it was too large");
|
||||
}
|
||||
}
|
||||
|
||||
match publish.qospid {
|
||||
mqttrs::QosPid::AtMostOnce => {}
|
||||
mqttrs::QosPid::AtLeastOnce(pid) => {
|
||||
send_packet(Packet::Puback(pid)).await?;
|
||||
}
|
||||
mqttrs::QosPid::ExactlyOnce(pid) => {
|
||||
send_packet(Packet::Pubrec(pid)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Packet::Puback(pid) => {
|
||||
controller.publish_immediate(ControlMessage::Published(pid));
|
||||
}
|
||||
Packet::Pubrec(pid) => {
|
||||
controller.publish_immediate(ControlMessage::Published(pid));
|
||||
send_packet(Packet::Pubrel(pid)).await?;
|
||||
}
|
||||
Packet::Pubrel(pid) => send_packet(Packet::Pubrel(pid)).await?,
|
||||
Packet::Pubcomp(_) => {}
|
||||
|
||||
Packet::Suback(suback) => {
|
||||
if let Some(return_code) = suback.return_codes.first() {
|
||||
controller.publish_immediate(ControlMessage::Subscribed(
|
||||
suback.pid,
|
||||
*return_code,
|
||||
));
|
||||
} else {
|
||||
warn!("Unexpected suback with no return codes");
|
||||
}
|
||||
}
|
||||
Packet::Unsuback(pid) => {
|
||||
controller.publish_immediate(ControlMessage::Unsubscribed(pid));
|
||||
}
|
||||
|
||||
Packet::Connect(_)
|
||||
| Packet::Subscribe(_)
|
||||
| Packet::Pingreq
|
||||
| Packet::Unsubscribe(_)
|
||||
| Packet::Disconnect => {
|
||||
debug!(
|
||||
"Unexpected packet from broker: {:?}",
|
||||
Debug2Format(&packet.get_type())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
start_pos += packet_length;
|
||||
if start_pos == cursor {
|
||||
cursor = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn write_loop(&self, mut writer: TcpWriter<'_>) {
|
||||
let mut buffer = Payload::new();
|
||||
|
||||
let mut last_will_topic = TopicString::new();
|
||||
let mut last_will_payload = Payload::new();
|
||||
|
||||
let last_will = self.last_will.as_ref().and_then(|p| {
|
||||
if p.write_topic(&mut last_will_topic).is_ok()
|
||||
&& p.write_payload(&mut last_will_payload).is_ok()
|
||||
{
|
||||
Some(LastWill {
|
||||
topic: &last_will_topic,
|
||||
message: &last_will_payload,
|
||||
qos: p.qos(),
|
||||
retain: p.retain(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
// Send our connection request.
|
||||
if buffer
|
||||
.encode_packet(&Packet::Connect(Connect {
|
||||
protocol: Protocol::MQTT311,
|
||||
keep_alive: self.keep_alive,
|
||||
client_id: device_id(),
|
||||
clean_session: true,
|
||||
last_will,
|
||||
username: self.username,
|
||||
password: self.password.map(|s| s.as_bytes()),
|
||||
}))
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to encode connection packet");
|
||||
return;
|
||||
}
|
||||
|
||||
if let Err(e) = writer.write_all(&buffer).await {
|
||||
error!("Failed to send connection packet: {:?}", e);
|
||||
return;
|
||||
}
|
||||
|
||||
let reader = SEND_QUEUE.reader();
|
||||
|
||||
loop {
|
||||
let buffer = reader.receive().await;
|
||||
|
||||
trace!("Writer sending packet");
|
||||
if let Err(e) = writer.write_all(&buffer).await {
|
||||
error!("Failed to send data: {:?}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the MQTT stack. The future returned from this must be awaited for everything to work.
|
||||
pub async fn run(self) {
|
||||
let mut timeout: Option<u64> = None;
|
||||
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [0; 4096];
|
||||
|
||||
loop {
|
||||
if let Some(millis) = timeout.replace(DEFAULT_BACKOFF) {
|
||||
Timer::after_millis(millis).await;
|
||||
}
|
||||
|
||||
if !self.network.is_config_up() {
|
||||
debug!("Waiting for network to configure.");
|
||||
self.network.wait_config_up().await;
|
||||
debug!("Network configured.");
|
||||
}
|
||||
|
||||
let ip_addrs = match self.network.dns_query(self.broker, DnsQueryType::A).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
error!("Failed to lookup '{}' for broker: {:?}", self.broker, e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let ip = match ip_addrs.first() {
|
||||
Some(i) => *i,
|
||||
None => {
|
||||
error!("No IP address found for broker '{}'", self.broker);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Connecting to {}:1883", ip);
|
||||
|
||||
let mut socket = TcpSocket::new(self.network, &mut rx_buffer, &mut tx_buffer);
|
||||
if let Err(e) = socket.connect((ip, 1883)).await {
|
||||
error!("Failed to connect to {}:1883: {:?}", ip, e);
|
||||
continue;
|
||||
}
|
||||
|
||||
info!("Connected to {}", self.broker);
|
||||
timeout = Some(RESET_BACKOFF);
|
||||
|
||||
let (reader, writer) = socket.split();
|
||||
|
||||
let recv_loop = self.recv_loop(reader);
|
||||
let send_loop = self.write_loop(writer);
|
||||
|
||||
let ping_loop = async {
|
||||
loop {
|
||||
Timer::after_secs(45).await;
|
||||
|
||||
let _ = send_packet(Packet::Pingreq).await;
|
||||
}
|
||||
};
|
||||
|
||||
let link_down = async {
|
||||
self.network.wait_link_down().await;
|
||||
warn!("Network link lost");
|
||||
};
|
||||
|
||||
let ip_down = async {
|
||||
self.network.wait_config_down().await;
|
||||
warn!("Network config lost");
|
||||
};
|
||||
|
||||
select4(send_loop, ping_loop, recv_loop, select(link_down, ip_down)).await;
|
||||
|
||||
socket.close();
|
||||
|
||||
warn!("Lost connection with broker");
|
||||
DATA_CHANNEL.send(MqttMessage::Disconnected).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
227
Software/MainBoard/rust/src/mcutie_3_0_0/lib.rs
Normal file
227
Software/MainBoard/rust/src/mcutie_3_0_0/lib.rs
Normal file
@@ -0,0 +1,227 @@
|
||||
#![no_std]
|
||||
#![deny(unreachable_pub)]
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
//! MQTT client support crate vendored into this repository.
|
||||
|
||||
use core::{ops::Deref, str};
|
||||
|
||||
pub use buffer::Buffer;
|
||||
use embassy_net::{HardwareAddress, Stack};
|
||||
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
|
||||
use heapless::String;
|
||||
pub use io::McutieTask;
|
||||
pub use mqttrs::QoS;
|
||||
use mqttrs::{Pid, SubscribeReturnCodes};
|
||||
use once_cell::sync::OnceCell;
|
||||
pub use publish::*;
|
||||
pub use topic::Topic;
|
||||
|
||||
// This must come first so the macros are visible
|
||||
pub(crate) mod fmt;
|
||||
|
||||
mod buffer;
|
||||
#[cfg(feature = "homeassistant")]
|
||||
pub mod homeassistant;
|
||||
mod io;
|
||||
mod pipe;
|
||||
mod publish;
|
||||
mod topic;
|
||||
|
||||
// This really needs to match that used by mqttrs.
|
||||
const TOPIC_LENGTH: usize = 256;
|
||||
const PAYLOAD_LENGTH: usize = 2048;
|
||||
|
||||
/// A fixed length stack allocated string. The length is fixed by the mqttrs crate.
|
||||
pub type TopicString = String<TOPIC_LENGTH>;
|
||||
/// A fixed length buffer of 2048 bytes.
|
||||
pub type Payload = Buffer<PAYLOAD_LENGTH>;
|
||||
|
||||
// By default in the event of an error connecting to the broker we will wait for 5s.
|
||||
const DEFAULT_BACKOFF: u64 = 5000;
|
||||
// If the connection dropped then re-connect more quickly.
|
||||
const RESET_BACKOFF: u64 = 200;
|
||||
// How long to wait for the broker to confirm actions.
|
||||
const CONFIRMATION_TIMEOUT: u64 = 2000;
|
||||
|
||||
static DATA_CHANNEL: Channel<CriticalSectionRawMutex, MqttMessage, 10> = Channel::new();
|
||||
|
||||
static DEVICE_TYPE: OnceCell<String<32>> = OnceCell::new();
|
||||
static DEVICE_ID: OnceCell<String<32>> = OnceCell::new();
|
||||
|
||||
fn device_id() -> &'static str {
|
||||
DEVICE_ID.get().unwrap()
|
||||
}
|
||||
|
||||
fn device_type() -> &'static str {
|
||||
DEVICE_TYPE.get().unwrap()
|
||||
}
|
||||
|
||||
/// Various errors
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum Error {
|
||||
/// An IO error occured.
|
||||
IOError,
|
||||
/// The operation timed out.
|
||||
TimedOut,
|
||||
/// An attempt was made to encode something too large.
|
||||
TooLarge,
|
||||
/// A packet or payload could not be decoded or encoded.
|
||||
PacketError,
|
||||
/// An invalid or unsupported operation was attempted.
|
||||
Invalid,
|
||||
/// A value was rejected.
|
||||
Rejected,
|
||||
}
|
||||
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
/// A message from the MQTT broker.
|
||||
pub enum MqttMessage {
|
||||
/// The broker has been connected to successfully. Generally in response to this message a
|
||||
/// device should subscribe to topics of interest and send out any device state.
|
||||
Connected,
|
||||
/// New data received from the broker.
|
||||
Publish(Topic<TopicString>, Payload),
|
||||
/// The connection to the broker has been dropped.
|
||||
Disconnected,
|
||||
/// Home Assistant has come online and you should send any discovery messages.
|
||||
#[cfg(feature = "homeassistant")]
|
||||
HomeAssistantOnline,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum ControlMessage {
|
||||
Published(Pid),
|
||||
Subscribed(Pid, SubscribeReturnCodes),
|
||||
Unsubscribed(Pid),
|
||||
}
|
||||
|
||||
/// Receives messages from the broker.
|
||||
pub struct McutieReceiver;
|
||||
|
||||
impl McutieReceiver {
|
||||
/// Waits for the next message from the broker.
|
||||
pub async fn receive(&self) -> MqttMessage {
|
||||
DATA_CHANNEL.receive().await
|
||||
}
|
||||
}
|
||||
|
||||
/// A builder to configure the MQTT stack.
|
||||
pub struct McutieBuilder<'t, T, L, const S: usize>
|
||||
where
|
||||
T: Deref<Target = str> + 't,
|
||||
L: Publishable + 't,
|
||||
{
|
||||
network: Stack<'t>,
|
||||
device_type: &'t str,
|
||||
device_id: Option<&'t str>,
|
||||
broker: &'t str,
|
||||
last_will: Option<L>,
|
||||
username: Option<&'t str>,
|
||||
password: Option<&'t str>,
|
||||
subscriptions: [Topic<T>; S],
|
||||
}
|
||||
|
||||
impl<'t, T: Deref<Target = str> + 't, L: Publishable + 't> McutieBuilder<'t, T, L, 0> {
|
||||
/// Creates a new builder with the initial required configuration.
|
||||
///
|
||||
/// `device_type` is expected to be the same for all devices of the same type.
|
||||
/// `broker` may be an IP address or a DNS name for the broker to connect to.
|
||||
pub fn new(network: Stack<'t>, device_type: &'t str, broker: &'t str) -> Self {
|
||||
Self {
|
||||
network,
|
||||
device_type,
|
||||
broker,
|
||||
device_id: None,
|
||||
last_will: None,
|
||||
username: None,
|
||||
password: None,
|
||||
subscriptions: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t, T: Deref<Target = str> + 't, L: Publishable + 't, const S: usize>
|
||||
McutieBuilder<'t, T, L, S>
|
||||
{
|
||||
/// Add some default topics to subscribe to.
|
||||
pub fn with_subscriptions<const N: usize>(
|
||||
self,
|
||||
subscriptions: [Topic<T>; N],
|
||||
) -> McutieBuilder<'t, T, L, N> {
|
||||
McutieBuilder {
|
||||
network: self.network,
|
||||
device_type: self.device_type,
|
||||
broker: self.broker,
|
||||
device_id: self.device_id,
|
||||
last_will: self.last_will,
|
||||
username: self.username,
|
||||
password: self.password,
|
||||
subscriptions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t, T: Deref<Target = str> + 't, L: Publishable + 't, const S: usize>
|
||||
McutieBuilder<'t, T, L, S>
|
||||
{
|
||||
/// Adds authentication for the broker.
|
||||
pub fn with_authentication(self, username: &'t str, password: &'t str) -> Self {
|
||||
Self {
|
||||
username: Some(username),
|
||||
password: Some(password),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a last will message to be published in the event of disconnection.
|
||||
pub fn with_last_will(self, last_will: L) -> Self {
|
||||
Self {
|
||||
last_will: Some(last_will),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets a custom unique device identifier. If none is set then the network
|
||||
/// MAC address is used.
|
||||
pub fn with_device_id(self, device_id: &'t str) -> Self {
|
||||
Self {
|
||||
device_id: Some(device_id),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialises the MQTT stack returning a receiver for listening to
|
||||
/// messages from the broker and a future that must be run in order for the
|
||||
/// stack to operate.
|
||||
pub fn build(self, keep_alive: u16) -> (McutieReceiver, McutieTask<'t, T, L, S>) {
|
||||
let mut dtype = String::<32>::new();
|
||||
dtype.push_str(self.device_type).unwrap();
|
||||
DEVICE_TYPE.set(dtype).unwrap();
|
||||
|
||||
let mut did = String::<32>::new();
|
||||
if let Some(device_id) = self.device_id {
|
||||
did.push_str(device_id).unwrap();
|
||||
} else if let HardwareAddress::Ethernet(address) = self.network.hardware_address() {
|
||||
let mut buffer = [0_u8; 12];
|
||||
hex::encode_to_slice(address.as_bytes(), &mut buffer).unwrap();
|
||||
did.push_str(str::from_utf8(&buffer).unwrap()).unwrap();
|
||||
}
|
||||
|
||||
DEVICE_ID.set(did).unwrap();
|
||||
|
||||
(
|
||||
McutieReceiver {},
|
||||
McutieTask {
|
||||
network: self.network,
|
||||
broker: self.broker,
|
||||
last_will: self.last_will,
|
||||
username: self.username,
|
||||
password: self.password,
|
||||
subscriptions: self.subscriptions,
|
||||
keep_alive,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
267
Software/MainBoard/rust/src/mcutie_3_0_0/pipe.rs
Normal file
267
Software/MainBoard/rust/src/mcutie_3_0_0/pipe.rs
Normal file
@@ -0,0 +1,267 @@
|
||||
use core::{
|
||||
cell::RefCell,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll, Waker},
|
||||
};
|
||||
|
||||
use embassy_sync::blocking_mutex::{raw::RawMutex, Mutex};
|
||||
use pin_project::pin_project;
|
||||
|
||||
struct PipeData<T, const N: usize> {
|
||||
connect_count: usize,
|
||||
receiver_waker: Option<Waker>,
|
||||
sender_waker: Option<Waker>,
|
||||
pending: Option<T>,
|
||||
}
|
||||
|
||||
fn swap_wakers(waker: &mut Option<Waker>, new_waker: &Waker) {
|
||||
if let Some(old_waker) = waker.take() {
|
||||
if old_waker.will_wake(new_waker) {
|
||||
*waker = Some(old_waker)
|
||||
} else {
|
||||
if !new_waker.will_wake(&old_waker) {
|
||||
old_waker.wake();
|
||||
}
|
||||
|
||||
*waker = Some(new_waker.clone());
|
||||
}
|
||||
} else {
|
||||
*waker = Some(new_waker.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ReceiveFuture<'a, M: RawMutex, T, const N: usize> {
|
||||
pipe: &'a ConnectedPipe<M, T, N>,
|
||||
}
|
||||
|
||||
impl<M: RawMutex, T, const N: usize> Future for ReceiveFuture<'_, M, T, N> {
|
||||
type Output = T;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
self.pipe.inner.lock(|cell| {
|
||||
let mut inner = cell.borrow_mut();
|
||||
|
||||
if let Some(waker) = inner.sender_waker.take() {
|
||||
waker.wake();
|
||||
}
|
||||
|
||||
if let Some(item) = inner.pending.take() {
|
||||
if let Some(old_waker) = inner.receiver_waker.take() {
|
||||
old_waker.wake();
|
||||
}
|
||||
|
||||
Poll::Ready(item)
|
||||
} else {
|
||||
swap_wakers(&mut inner.receiver_waker, cx.waker());
|
||||
Poll::Pending
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct PipeReader<'a, M: RawMutex, T, const N: usize> {
|
||||
pipe: &'a ConnectedPipe<M, T, N>,
|
||||
}
|
||||
|
||||
impl<M: RawMutex, T, const N: usize> PipeReader<'_, M, T, N> {
|
||||
#[must_use]
|
||||
pub(crate) fn receive(&self) -> ReceiveFuture<'_, M, T, N> {
|
||||
ReceiveFuture { pipe: self.pipe }
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: RawMutex, T, const N: usize> Drop for PipeReader<'_, M, T, N> {
|
||||
fn drop(&mut self) {
|
||||
self.pipe.inner.lock(|cell| {
|
||||
let mut inner = cell.borrow_mut();
|
||||
inner.connect_count -= 1;
|
||||
|
||||
if inner.connect_count == 0 {
|
||||
inner.pending = None;
|
||||
}
|
||||
|
||||
if let Some(waker) = inner.sender_waker.take() {
|
||||
waker.wake();
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project]
|
||||
pub(crate) struct PushFuture<'a, M: RawMutex, T, const N: usize> {
|
||||
data: Option<T>,
|
||||
pipe: &'a ConnectedPipe<M, T, N>,
|
||||
}
|
||||
|
||||
impl<M: RawMutex, T, const N: usize> Future for PushFuture<'_, M, T, N> {
|
||||
type Output = ();
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
self.pipe.inner.lock(|cell| {
|
||||
let project = self.project();
|
||||
let mut inner = cell.borrow_mut();
|
||||
|
||||
if let Some(receiver) = inner.receiver_waker.take() {
|
||||
receiver.wake();
|
||||
}
|
||||
|
||||
if project.data.is_none() || inner.connect_count == 0 {
|
||||
trace!("Dropping packet");
|
||||
Poll::Ready(())
|
||||
} else if inner.pending.is_some() {
|
||||
swap_wakers(&mut inner.sender_waker, cx.waker());
|
||||
Poll::Pending
|
||||
} else {
|
||||
inner.pending = project.data.take();
|
||||
|
||||
Poll::Ready(())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A pipe that knows whether a receiver is connected. If so pushing to the
|
||||
/// queue waits until there is space in the queue, otherwise data is simply
|
||||
/// dropped.
|
||||
pub(crate) struct ConnectedPipe<M: RawMutex, T, const N: usize> {
|
||||
inner: Mutex<M, RefCell<PipeData<T, N>>>,
|
||||
}
|
||||
|
||||
impl<M: RawMutex, T, const N: usize> ConnectedPipe<M, T, N> {
|
||||
pub(crate) const fn new() -> Self {
|
||||
Self {
|
||||
inner: Mutex::new(RefCell::new(PipeData {
|
||||
connect_count: 0,
|
||||
receiver_waker: None,
|
||||
sender_waker: None,
|
||||
pending: None,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
/// A future that waits for a new item to be available.
|
||||
pub(crate) fn reader(&self) -> PipeReader<'_, M, T, N> {
|
||||
self.inner.lock(|cell| {
|
||||
let mut inner = cell.borrow_mut();
|
||||
inner.connect_count += 1;
|
||||
|
||||
PipeReader { pipe: self }
|
||||
})
|
||||
}
|
||||
|
||||
/// Pushes an item to the reader, waiting for a slot to become available if
|
||||
/// connected.
|
||||
#[must_use]
|
||||
pub(crate) fn push(&self, data: T) -> PushFuture<'_, M, T, N> {
|
||||
PushFuture {
|
||||
data: Some(data),
|
||||
pipe: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::time::Duration;
|
||||
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use futures_executor::{LocalPool, ThreadPool};
|
||||
use futures_timer::Delay;
|
||||
use futures_util::{future::select, pin_mut, task::SpawnExt, FutureExt};
|
||||
|
||||
use super::ConnectedPipe;
|
||||
|
||||
async fn wait_milis(milis: u64) {
|
||||
Delay::new(Duration::from_millis(milis)).await;
|
||||
}
|
||||
|
||||
// #[futures_test::test]
|
||||
#[test]
|
||||
fn test_send_receive() {
|
||||
let mut executor = LocalPool::new();
|
||||
let spawner = executor.spawner();
|
||||
|
||||
static PIPE: ConnectedPipe<CriticalSectionRawMutex, usize, 5> = ConnectedPipe::new();
|
||||
|
||||
// Task that sends
|
||||
spawner
|
||||
.spawn(async {
|
||||
wait_milis(10).await;
|
||||
|
||||
PIPE.push(23).await;
|
||||
PIPE.push(56).await;
|
||||
PIPE.push(67).await;
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// Task that receives
|
||||
spawner
|
||||
.spawn(async {
|
||||
let reader = PIPE.reader();
|
||||
let value = reader.receive().await;
|
||||
assert_eq!(value, 23);
|
||||
let value = reader.receive().await;
|
||||
assert_eq!(value, 56);
|
||||
let value = reader.receive().await;
|
||||
assert_eq!(value, 67);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
executor.run();
|
||||
}
|
||||
|
||||
#[futures_test::test]
|
||||
async fn test_send_drop() {
|
||||
static PIPE: ConnectedPipe<CriticalSectionRawMutex, usize, 5> = ConnectedPipe::new();
|
||||
|
||||
PIPE.push(23).await;
|
||||
PIPE.push(56).await;
|
||||
PIPE.push(67).await;
|
||||
|
||||
// Create a reader after sending
|
||||
let reader = PIPE.reader();
|
||||
let receive = reader.receive().fuse();
|
||||
pin_mut!(receive);
|
||||
|
||||
let timeout = wait_milis(50).fuse();
|
||||
pin_mut!(timeout);
|
||||
|
||||
let either = select(receive, timeout).await;
|
||||
|
||||
match either {
|
||||
futures_util::future::Either::Left(_) => {
|
||||
panic!("There should be nothing to receive!");
|
||||
}
|
||||
futures_util::future::Either::Right(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[futures_test::test]
|
||||
async fn test_bulk_send_publish() {
|
||||
static PIPE: ConnectedPipe<CriticalSectionRawMutex, usize, 5> = ConnectedPipe::new();
|
||||
|
||||
let executor = ThreadPool::new().unwrap();
|
||||
|
||||
executor
|
||||
.spawn(async {
|
||||
for i in 0..1000 {
|
||||
PIPE.push(i).await;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
executor
|
||||
.spawn(async {
|
||||
for i in 1000..2000 {
|
||||
PIPE.push(i).await;
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let reader = PIPE.reader();
|
||||
for _ in 0..800 {
|
||||
reader.receive().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
173
Software/MainBoard/rust/src/mcutie_3_0_0/publish.rs
Normal file
173
Software/MainBoard/rust/src/mcutie_3_0_0/publish.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
use core::{fmt::Display, future::Future, ops::Deref};
|
||||
|
||||
use embedded_io::Write;
|
||||
use mqttrs::QoS;
|
||||
|
||||
use crate::{io::publish, Error, Payload, Topic, TopicString};
|
||||
|
||||
/// A message that can be published to an MQTT broker.
|
||||
pub trait Publishable {
|
||||
/// Write this message's topic into the supplied buffer.
|
||||
fn write_topic(&self, buffer: &mut TopicString) -> Result<(), Error>;
|
||||
|
||||
/// Write this message's payload into the supplied buffer.
|
||||
fn write_payload(&self, buffer: &mut Payload) -> Result<(), Error>;
|
||||
|
||||
/// Get this message's QoS level.
|
||||
fn qos(&self) -> QoS {
|
||||
QoS::AtMostOnce
|
||||
}
|
||||
|
||||
/// Whether the broker should retain this message.
|
||||
fn retain(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Publishes this message to the broker. If the stack has not yet been
|
||||
/// initialized this is likely to panic.
|
||||
fn publish(&self) -> impl Future<Output = Result<(), Error>> {
|
||||
async {
|
||||
let mut topic = TopicString::new();
|
||||
self.write_topic(&mut topic)?;
|
||||
|
||||
let mut payload = Payload::new();
|
||||
self.write_payload(&mut payload)?;
|
||||
|
||||
publish(&topic, &payload, self.qos(), self.retain()).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Publishable`] with a raw byte payload.
|
||||
pub struct PublishBytes<'a, T, B: AsRef<[u8]>> {
|
||||
pub(crate) topic: &'a Topic<T>,
|
||||
pub(crate) data: B,
|
||||
pub(crate) qos: QoS,
|
||||
pub(crate) retain: bool,
|
||||
}
|
||||
|
||||
impl<T, B: AsRef<[u8]>> PublishBytes<'_, T, B> {
|
||||
/// Sets the QoS level for this message.
|
||||
pub fn qos(mut self, qos: QoS) -> Self {
|
||||
self.qos = qos;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets whether the broker should retain this message.
|
||||
pub fn retain(mut self, retain: bool) -> Self {
|
||||
self.retain = retain;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Deref<Target = str> + 'a, B: AsRef<[u8]>> Publishable for PublishBytes<'a, T, B> {
|
||||
fn write_topic(&self, buffer: &mut TopicString) -> Result<(), Error> {
|
||||
self.topic.to_string(buffer)
|
||||
}
|
||||
|
||||
fn write_payload(&self, buffer: &mut Payload) -> Result<(), Error> {
|
||||
buffer
|
||||
.write_all(self.data.as_ref())
|
||||
.map_err(|_| Error::TooLarge)
|
||||
}
|
||||
|
||||
fn qos(&self) -> QoS {
|
||||
self.qos
|
||||
}
|
||||
|
||||
fn retain(&self) -> bool {
|
||||
self.retain
|
||||
}
|
||||
|
||||
async fn publish(&self) -> Result<(), Error> {
|
||||
let mut topic = TopicString::new();
|
||||
self.write_topic(&mut topic)?;
|
||||
|
||||
publish(&topic, self.data.as_ref(), self.qos(), self.retain()).await
|
||||
}
|
||||
}
|
||||
|
||||
/// A [`Publishable`] with a payload that implements [`Display`].
|
||||
pub struct PublishDisplay<'a, T, D: Display> {
|
||||
pub(crate) topic: &'a Topic<T>,
|
||||
pub(crate) data: D,
|
||||
pub(crate) qos: QoS,
|
||||
pub(crate) retain: bool,
|
||||
}
|
||||
|
||||
impl<T, D: Display> PublishDisplay<'_, T, D> {
|
||||
/// Sets the QoS level for this message.
|
||||
pub fn qos(mut self, qos: QoS) -> Self {
|
||||
self.qos = qos;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets whether the broker should retain this message.
|
||||
pub fn retain(mut self, retain: bool) -> Self {
|
||||
self.retain = retain;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Deref<Target = str> + 'a, D: Display> Publishable for PublishDisplay<'a, T, D> {
|
||||
fn write_topic(&self, buffer: &mut TopicString) -> Result<(), Error> {
|
||||
self.topic.to_string(buffer)
|
||||
}
|
||||
|
||||
fn write_payload(&self, buffer: &mut Payload) -> Result<(), Error> {
|
||||
write!(buffer, "{}", self.data).map_err(|_| Error::TooLarge)
|
||||
}
|
||||
|
||||
fn qos(&self) -> QoS {
|
||||
self.qos
|
||||
}
|
||||
|
||||
fn retain(&self) -> bool {
|
||||
self.retain
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
/// A [`Publishable`] with that serializes a JSON payload.
|
||||
pub struct PublishJson<'a, T, D: serde::Serialize> {
|
||||
pub(crate) topic: &'a Topic<T>,
|
||||
pub(crate) data: D,
|
||||
pub(crate) qos: QoS,
|
||||
pub(crate) retain: bool,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<T, D: serde::Serialize> PublishJson<'_, T, D> {
|
||||
/// Sets the QoS level for this message.
|
||||
pub fn qos(mut self, qos: QoS) -> Self {
|
||||
self.qos = qos;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets whether the broker should retain this message.
|
||||
pub fn retain(mut self, retain: bool) -> Self {
|
||||
self.retain = retain;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'a, T: Deref<Target = str> + 'a, D: serde::Serialize> Publishable for PublishJson<'a, T, D> {
|
||||
fn write_topic(&self, buffer: &mut TopicString) -> Result<(), Error> {
|
||||
self.topic.to_string(buffer)
|
||||
}
|
||||
|
||||
fn write_payload(&self, buffer: &mut Payload) -> Result<(), Error> {
|
||||
buffer
|
||||
.serialize_json(&self.data)
|
||||
.map_err(|_| Error::TooLarge)
|
||||
}
|
||||
|
||||
fn qos(&self) -> QoS {
|
||||
self.qos
|
||||
}
|
||||
|
||||
fn retain(&self) -> bool {
|
||||
self.retain
|
||||
}
|
||||
}
|
||||
284
Software/MainBoard/rust/src/mcutie_3_0_0/topic.rs
Normal file
284
Software/MainBoard/rust/src/mcutie_3_0_0/topic.rs
Normal file
@@ -0,0 +1,284 @@
|
||||
use core::{fmt::Display, ops::Deref};
|
||||
|
||||
use embassy_futures::select::{select, Either};
|
||||
use embassy_sync::pubsub::WaitResult;
|
||||
use embassy_time::Timer;
|
||||
use heapless::{String, Vec};
|
||||
use mqttrs::{Packet, QoS, Subscribe, SubscribeReturnCodes, SubscribeTopic, Unsubscribe};
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use crate::publish::PublishJson;
|
||||
use crate::{
|
||||
device_id, device_type,
|
||||
io::{assign_pid, send_packet, subscribe},
|
||||
publish::{PublishBytes, PublishDisplay},
|
||||
ControlMessage, Error, TopicString, CONFIRMATION_TIMEOUT,
|
||||
};
|
||||
|
||||
/// An MQTT topic that is optionally prefixed with the device type and unique ID.
|
||||
/// Normally you will define all your application's topics as consts with static
|
||||
/// lifetimes.
|
||||
///
|
||||
/// A [`Topic`] is the main entry to publishing messages to the broker.
|
||||
///
|
||||
/// ```
|
||||
/// # use mcutie::{Publishable, Topic};
|
||||
/// const DEVICE_AVAILABILITY: Topic<&'static str> = Topic::Device("state");
|
||||
///
|
||||
/// async fn send_status(status: &'static str) {
|
||||
/// let _ = DEVICE_AVAILABILITY.with_bytes(status.as_bytes()).publish().await;
|
||||
/// }
|
||||
/// ```
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Topic<T> {
|
||||
/// A topic that is prefixed with the device type.
|
||||
DeviceType(T),
|
||||
/// A topic that is prefixed with the device type and unique ID.
|
||||
Device(T),
|
||||
/// Any topic.
|
||||
General(T),
|
||||
}
|
||||
|
||||
impl<A, B> PartialEq<Topic<A>> for Topic<B>
|
||||
where
|
||||
B: PartialEq<A>,
|
||||
{
|
||||
fn eq(&self, other: &Topic<A>) -> bool {
|
||||
match (self, other) {
|
||||
(Topic::DeviceType(l0), Topic::DeviceType(r0)) => l0 == r0,
|
||||
(Topic::Device(l0), Topic::Device(r0)) => l0 == r0,
|
||||
(Topic::General(l0), Topic::General(r0)) => l0 == r0,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Topic<T> {
|
||||
/// Creates a publishable message with something that can return a reference
|
||||
/// to the payload in bytes.
|
||||
///
|
||||
/// Defaults to non-retained with QoS of 0 (AtMostOnce).
|
||||
pub fn with_bytes<B: AsRef<[u8]>>(&self, data: B) -> PublishBytes<'_, T, B> {
|
||||
PublishBytes {
|
||||
topic: self,
|
||||
data,
|
||||
qos: QoS::AtMostOnce,
|
||||
retain: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a publishable message with something that implements [`Display`].
|
||||
///
|
||||
/// Defaults to non-retained with QoS of 0 (AtMostOnce).
|
||||
pub fn with_display<D: Display>(&self, data: D) -> PublishDisplay<'_, T, D> {
|
||||
PublishDisplay {
|
||||
topic: self,
|
||||
data,
|
||||
qos: QoS::AtMostOnce,
|
||||
retain: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
/// Creates a publishable message with something that can be serialized to
|
||||
/// JSON.
|
||||
///
|
||||
/// Defaults to non-retained with QoS of 0 (AtMostOnce).
|
||||
pub fn with_json<D: serde::Serialize>(&self, data: D) -> PublishJson<'_, T, D> {
|
||||
PublishJson {
|
||||
topic: self,
|
||||
data,
|
||||
qos: QoS::AtMostOnce,
|
||||
retain: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Topic<TopicString> {
|
||||
pub(crate) fn from_str(mut st: &str) -> Result<Self, Error> {
|
||||
let mut strip_prefix = |pr: &str| -> bool {
|
||||
if st.starts_with(pr) && st.len() > pr.len() && &st[pr.len()..pr.len() + 1] == "/" {
|
||||
st = &st[pr.len() + 1..];
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if strip_prefix(device_type()) {
|
||||
if strip_prefix(device_id()) {
|
||||
let mut topic = TopicString::new();
|
||||
topic.push_str(st).map_err(|_| Error::TooLarge)?;
|
||||
Ok(Topic::Device(topic))
|
||||
} else {
|
||||
let mut topic = TopicString::new();
|
||||
topic.push_str(st).map_err(|_| Error::TooLarge)?;
|
||||
Ok(Topic::DeviceType(topic))
|
||||
}
|
||||
} else {
|
||||
let mut topic = TopicString::new();
|
||||
topic.push_str(st).map_err(|_| Error::TooLarge)?;
|
||||
Ok(Topic::General(topic))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Deref<Target = str>> Topic<T> {
|
||||
pub(crate) fn to_string<const N: usize>(&self, result: &mut String<N>) -> Result<(), Error> {
|
||||
match self {
|
||||
Topic::Device(st) => {
|
||||
result
|
||||
.push_str(device_type())
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
result.push_str("/").map_err(|_| Error::TooLarge)?;
|
||||
result.push_str(device_id()).map_err(|_| Error::TooLarge)?;
|
||||
result.push_str("/").map_err(|_| Error::TooLarge)?;
|
||||
result.push_str(st.as_ref()).map_err(|_| Error::TooLarge)?;
|
||||
}
|
||||
Topic::DeviceType(st) => {
|
||||
result
|
||||
.push_str(device_type())
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
result.push_str("/").map_err(|_| Error::TooLarge)?;
|
||||
result.push_str(st.as_ref()).map_err(|_| Error::TooLarge)?;
|
||||
}
|
||||
Topic::General(st) => {
|
||||
result.push_str(st.as_ref()).map_err(|_| Error::TooLarge)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Converts to a topic containing an [`str`]. Particularly useful for converting from an owned
|
||||
/// string for match patterns.
|
||||
pub fn as_ref(&self) -> Topic<&str> {
|
||||
match self {
|
||||
Topic::DeviceType(st) => Topic::DeviceType(st.as_ref()),
|
||||
Topic::Device(st) => Topic::Device(st.as_ref()),
|
||||
Topic::General(st) => Topic::General(st.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Subscribes to this topic. If `wait_for_ack` is true then this will wait until confirmation
|
||||
/// is received from the broker before returning.
|
||||
pub async fn subscribe(&self, wait_for_ack: bool) -> Result<(), Error> {
|
||||
let mut subscriber = subscribe().await;
|
||||
|
||||
let mut topic_path = TopicString::new();
|
||||
if self.to_string(&mut topic_path).is_err() {
|
||||
return Err(Error::TooLarge);
|
||||
}
|
||||
|
||||
let pid = assign_pid().await;
|
||||
|
||||
let mut subscribe_topic_path = String::<256>::new();
|
||||
subscribe_topic_path
|
||||
.push_str(topic_path.as_str())
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
let subscribe_topic = SubscribeTopic {
|
||||
topic_path: subscribe_topic_path,
|
||||
qos: QoS::AtLeastOnce,
|
||||
};
|
||||
|
||||
// The size of this vec must match that used by mqttrs.
|
||||
let topics = match Vec::<SubscribeTopic, 5>::from_slice(&[subscribe_topic]) {
|
||||
Ok(t) => t,
|
||||
Err(_) => return Err(Error::TooLarge),
|
||||
};
|
||||
|
||||
let packet = Packet::Subscribe(Subscribe { pid, topics });
|
||||
|
||||
send_packet(packet).await?;
|
||||
|
||||
if wait_for_ack {
|
||||
match select(
|
||||
async {
|
||||
loop {
|
||||
match subscriber.next_message().await {
|
||||
WaitResult::Lagged(_) => {
|
||||
// Maybe we missed the message?
|
||||
}
|
||||
WaitResult::Message(ControlMessage::Subscribed(
|
||||
subscribed_pid,
|
||||
return_code,
|
||||
)) => {
|
||||
if subscribed_pid == pid {
|
||||
if matches!(return_code, SubscribeReturnCodes::Success(_)) {
|
||||
return Ok(());
|
||||
} else {
|
||||
return Err(Error::IOError);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
Timer::after_millis(CONFIRMATION_TIMEOUT),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::First(r) => r,
|
||||
Either::Second(_) => Err(Error::TimedOut),
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsubscribes from a topic. If `wait_for_ack` is true then this will wait until confirmation is
|
||||
/// received from the broker before returning.
|
||||
pub async fn unsubscribe(&self, wait_for_ack: bool) -> Result<(), Error> {
|
||||
let mut subscriber = subscribe().await;
|
||||
|
||||
let mut topic_path = TopicString::new();
|
||||
if self.to_string(&mut topic_path).is_err() {
|
||||
return Err(Error::TooLarge);
|
||||
}
|
||||
|
||||
let pid = assign_pid().await;
|
||||
|
||||
// The size of this vec must match that used by mqttrs.
|
||||
let mut unsubscribe_topic_path = String::<256>::new();
|
||||
unsubscribe_topic_path
|
||||
.push_str(topic_path.as_str())
|
||||
.map_err(|_| Error::TooLarge)?;
|
||||
let topics = match Vec::<String<256>, 5>::from_slice(&[unsubscribe_topic_path]) {
|
||||
Ok(t) => t,
|
||||
Err(_) => return Err(Error::TooLarge),
|
||||
};
|
||||
|
||||
let packet = Packet::Unsubscribe(Unsubscribe { pid, topics });
|
||||
|
||||
send_packet(packet).await?;
|
||||
|
||||
if wait_for_ack {
|
||||
match select(
|
||||
async {
|
||||
loop {
|
||||
match subscriber.next_message().await {
|
||||
WaitResult::Lagged(_) => {
|
||||
// Maybe we missed the message?
|
||||
}
|
||||
WaitResult::Message(ControlMessage::Unsubscribed(subscribed_pid)) => {
|
||||
if subscribed_pid == pid {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
},
|
||||
Timer::after_millis(CONFIRMATION_TIMEOUT),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Either::First(r) => r,
|
||||
Either::Second(_) => Err(Error::TimedOut),
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
278
Software/MainBoard/rust/src/mqtt.rs
Normal file
278
Software/MainBoard/rust/src/mqtt.rs
Normal file
@@ -0,0 +1,278 @@
|
||||
use crate::bail;
|
||||
use crate::config::NetworkConfig;
|
||||
use crate::fat_error::{ContextExt, FatError, FatResult};
|
||||
use crate::hal::PlantHal;
|
||||
use crate::log::{log, LogMessage};
|
||||
use alloc::string::String;
|
||||
use alloc::{format, string::ToString};
|
||||
use core::sync::atomic::Ordering;
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::Stack;
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
use embassy_time::{Duration, Timer, WithTimeout};
|
||||
use log::info;
|
||||
use mcutie::{
|
||||
Error, McutieBuilder, McutieReceiver, McutieTask, MqttMessage, PublishDisplay, Publishable,
|
||||
QoS, Topic,
|
||||
};
|
||||
use portable_atomic::AtomicBool;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
|
||||
///mqtt struct to track pump activities
|
||||
pub struct PumpInfo {
|
||||
pub enabled: bool,
|
||||
pub pump_ineffective: bool,
|
||||
pub median_current_ma: u16,
|
||||
pub max_current_ma: u16,
|
||||
pub min_current_ma: u16,
|
||||
pub error: String,
|
||||
pub flow_raw: u32,
|
||||
pub flow_ml: f32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug, PartialEq)]
|
||||
pub struct Solar {
|
||||
pub current_ma: u32,
|
||||
pub voltage_ma: u32,
|
||||
}
|
||||
|
||||
static MQTT_CONNECTED_EVENT_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||
static MQTT_ROUND_TRIP_RECEIVED: AtomicBool = AtomicBool::new(false);
|
||||
pub static MQTT_STAY_ALIVE: AtomicBool = AtomicBool::new(false);
|
||||
static MQTT_BASE_TOPIC: OnceLock<String> = OnceLock::new();
|
||||
|
||||
pub fn is_stay_alive() -> bool {
|
||||
MQTT_STAY_ALIVE.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub async fn publish(subtopic: &str, message: &str) {
|
||||
let online = MQTT_CONNECTED_EVENT_RECEIVED.load(Ordering::Relaxed);
|
||||
if !online {
|
||||
return;
|
||||
}
|
||||
let roundtrip_ok = MQTT_ROUND_TRIP_RECEIVED.load(Ordering::Relaxed);
|
||||
if !roundtrip_ok {
|
||||
info!("MQTT roundtrip not received yet, dropping message");
|
||||
return;
|
||||
}
|
||||
match publish_inner(subtopic, message).await {
|
||||
Ok(()) => {}
|
||||
Err(err) => {
|
||||
info!(
|
||||
"Error during mqtt send on topic {subtopic} with message {message:#?} error is {err:?}"
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async fn publish_inner(subtopic: &str, message: &str) -> FatResult<()> {
|
||||
if !subtopic.starts_with("/") {
|
||||
bail!("Subtopic without / at start {}", subtopic);
|
||||
}
|
||||
if subtopic.len() > 192 {
|
||||
bail!("Subtopic exceeds 192 chars {}", subtopic);
|
||||
}
|
||||
let base_topic = MQTT_BASE_TOPIC
|
||||
.try_get()
|
||||
.context("missing base topic in static!")?;
|
||||
|
||||
let full_topic = format!("{base_topic}{subtopic}");
|
||||
|
||||
loop {
|
||||
let result = Topic::General(full_topic.as_str())
|
||||
.with_display(message)
|
||||
.retain(true)
|
||||
.publish()
|
||||
.await;
|
||||
match result {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(err) => {
|
||||
let retry = match err {
|
||||
Error::IOError => false,
|
||||
Error::TimedOut => true,
|
||||
Error::TooLarge => false,
|
||||
Error::PacketError => false,
|
||||
Error::Invalid => false,
|
||||
Error::Rejected => false,
|
||||
};
|
||||
if !retry {
|
||||
bail!(
|
||||
"Error during mqtt send on topic {} with message {:#?} error is {:?}",
|
||||
&full_topic,
|
||||
message,
|
||||
err
|
||||
);
|
||||
}
|
||||
info!(
|
||||
"Retransmit for {} with message {:#?} error is {:?} retrying {}",
|
||||
&full_topic, message, err, retry
|
||||
);
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! mk_static {
|
||||
($t:ty,$val:expr) => {{
|
||||
static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
|
||||
#[deny(unused_attributes)]
|
||||
let x = STATIC_CELL.uninit().write(($val));
|
||||
x
|
||||
}};
|
||||
}
|
||||
|
||||
pub async fn mqtt_init(
|
||||
network_config: &'static NetworkConfig,
|
||||
stack: Stack<'static>,
|
||||
spawner: Spawner,
|
||||
) -> FatResult<()> {
|
||||
let base_topic = network_config
|
||||
.base_topic
|
||||
.as_ref()
|
||||
.context("missing base topic")?;
|
||||
if base_topic.is_empty() {
|
||||
bail!("Mqtt base_topic was empty")
|
||||
}
|
||||
MQTT_BASE_TOPIC
|
||||
.init(base_topic.to_string())
|
||||
.map_err(|_| FatError::String {
|
||||
error: "Error setting basetopic".to_string(),
|
||||
})?;
|
||||
|
||||
let mqtt_url = network_config
|
||||
.mqtt_url
|
||||
.as_ref()
|
||||
.context("missing mqtt url")?;
|
||||
if mqtt_url.is_empty() {
|
||||
bail!("Mqtt url was empty")
|
||||
}
|
||||
|
||||
let last_will_topic = format!("{base_topic}/state");
|
||||
let round_trip_topic = format!("{base_topic}/internal/roundtrip");
|
||||
let stay_alive_topic = format!("{base_topic}/stay_alive");
|
||||
|
||||
let mut builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 0> =
|
||||
McutieBuilder::new(stack, "plant ctrl", mqtt_url);
|
||||
if let (Some(mqtt_user), Some(mqtt_password)) = (
|
||||
network_config.mqtt_user.as_ref(),
|
||||
network_config.mqtt_password.as_ref(),
|
||||
) {
|
||||
builder = builder.with_authentication(mqtt_user, mqtt_password);
|
||||
info!("With authentification");
|
||||
}
|
||||
|
||||
let lwt = Topic::General(last_will_topic);
|
||||
let lwt = mk_static!(Topic<String>, lwt);
|
||||
let lwt = lwt.with_display("lost").retain(true).qos(QoS::AtLeastOnce);
|
||||
builder = builder.with_last_will(lwt);
|
||||
//TODO make configurable
|
||||
builder = builder.with_device_id("plantctrl");
|
||||
|
||||
let builder: McutieBuilder<'_, String, PublishDisplay<String, &str>, 2> = builder
|
||||
.with_subscriptions([
|
||||
Topic::General(round_trip_topic.clone()),
|
||||
Topic::General(stay_alive_topic.clone()),
|
||||
]);
|
||||
|
||||
let keep_alive = Duration::from_secs(60 * 60 * 2).as_secs() as u16;
|
||||
let (receiver, task) = builder.build(keep_alive);
|
||||
|
||||
spawner.spawn(mqtt_incoming_task(
|
||||
receiver,
|
||||
round_trip_topic.clone(),
|
||||
stay_alive_topic.clone(),
|
||||
)?);
|
||||
spawner.spawn(mqtt_runner(task)?);
|
||||
|
||||
log(LogMessage::StayAlive, 0, 0, "", &stay_alive_topic);
|
||||
|
||||
log(LogMessage::MqttInfo, 0, 0, "", mqtt_url);
|
||||
|
||||
let mqtt_timeout = 15000;
|
||||
let res = async {
|
||||
while !MQTT_CONNECTED_EVENT_RECEIVED.load(Ordering::Relaxed) {
|
||||
PlantHal::feed_watchdog();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(mqtt_timeout as u64))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
bail!("Timeout waiting MQTT connect event")
|
||||
}
|
||||
|
||||
let _ = Topic::General(round_trip_topic.clone())
|
||||
.with_display("online_text")
|
||||
.publish()
|
||||
.await;
|
||||
|
||||
let res = async {
|
||||
while !MQTT_ROUND_TRIP_RECEIVED.load(Ordering::Relaxed) {
|
||||
PlantHal::feed_watchdog();
|
||||
Timer::after(Duration::from_millis(100)).await;
|
||||
}
|
||||
Ok::<(), FatError>(())
|
||||
}
|
||||
.with_timeout(Duration::from_millis(mqtt_timeout as u64))
|
||||
.await;
|
||||
|
||||
if res.is_err() {
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(false, Ordering::Relaxed);
|
||||
bail!("Timeout waiting MQTT roundtrip")
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn mqtt_runner(
|
||||
task: McutieTask<'static, String, PublishDisplay<'static, String, &'static str>, 2>,
|
||||
) {
|
||||
task.run().await;
|
||||
}
|
||||
|
||||
#[embassy_executor::task]
|
||||
async fn mqtt_incoming_task(
|
||||
receiver: McutieReceiver,
|
||||
round_trip_topic: String,
|
||||
stay_alive_topic: String,
|
||||
) {
|
||||
loop {
|
||||
let message = receiver.receive().await;
|
||||
match message {
|
||||
MqttMessage::Connected => {
|
||||
info!("Mqtt connected");
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(true, Ordering::Relaxed);
|
||||
}
|
||||
MqttMessage::Publish(topic, payload) => match topic {
|
||||
Topic::DeviceType(_type_topic) => {}
|
||||
Topic::Device(_device_topic) => {}
|
||||
Topic::General(topic) => {
|
||||
let subtopic = topic.as_str();
|
||||
|
||||
if subtopic.eq(round_trip_topic.as_str()) {
|
||||
MQTT_ROUND_TRIP_RECEIVED.store(true, Ordering::Relaxed);
|
||||
} else if subtopic.eq(stay_alive_topic.as_str()) {
|
||||
let value = payload.eq_ignore_ascii_case("true".as_ref())
|
||||
|| payload.eq_ignore_ascii_case("1".as_ref());
|
||||
let a = match value {
|
||||
true => 1,
|
||||
false => 0,
|
||||
};
|
||||
log(LogMessage::MqttStayAliveRec, a, 0, "", "");
|
||||
MQTT_STAY_ALIVE.store(value, Ordering::Relaxed);
|
||||
} else {
|
||||
log(LogMessage::UnknownTopic, 0, 0, "", &topic);
|
||||
}
|
||||
}
|
||||
},
|
||||
MqttMessage::Disconnected => {
|
||||
MQTT_CONNECTED_EVENT_RECEIVED.store(false, Ordering::Relaxed);
|
||||
info!("Mqtt disconnected");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
use crate::hal::Moistures;
|
||||
use crate::{config::PlantConfig, hal::HAL, in_time_range};
|
||||
use bincode::{Decode, Encode};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use chrono_tz::Tz;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: f32 = 7500.; // 60kHz (500Hz margin)
|
||||
const MOIST_SENSOR_MIN_FREQUENCY: f32 = 150.; // this is really, really dry, think like cactus levels
|
||||
const MOIST_SENSOR_MAX_FREQUENCY: f32 = 70000.; // 70kHz
|
||||
const MOIST_SENSOR_MIN_FREQUENCY: f32 = 400.; // this is really, really dry, think like cactus levels
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize)]
|
||||
pub enum MoistureSensorError {
|
||||
@@ -52,16 +51,27 @@ pub enum PumpError {
|
||||
failed_attempts: usize,
|
||||
max_allowed_failures: usize,
|
||||
},
|
||||
OverCurrent {
|
||||
current_ma: u16,
|
||||
max_allowed_ma: u16,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct PumpState {
|
||||
consecutive_pump_count: u32,
|
||||
previous_pump: Option<DateTime<Utc>>,
|
||||
pub overcurrent_error: Option<u16>,
|
||||
}
|
||||
|
||||
impl PumpState {
|
||||
fn is_err(&self, plant_config: &PlantConfig) -> Option<PumpError> {
|
||||
if let Some(current_ma) = self.overcurrent_error {
|
||||
return Some(PumpError::OverCurrent {
|
||||
current_ma,
|
||||
max_allowed_ma: plant_config.max_pump_current_ma,
|
||||
});
|
||||
}
|
||||
if self.consecutive_pump_count > plant_config.max_consecutive_pump_count as u32 {
|
||||
Some(PumpError::PumpNotWorking {
|
||||
failed_attempts: self.consecutive_pump_count as usize,
|
||||
@@ -73,7 +83,7 @@ impl PumpState {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Encode, Decode)]
|
||||
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq)]
|
||||
pub enum PlantWateringMode {
|
||||
Off,
|
||||
TargetMoisture,
|
||||
@@ -85,6 +95,13 @@ pub struct PlantState {
|
||||
pub sensor_a: MoistureSensorState,
|
||||
pub sensor_b: MoistureSensorState,
|
||||
pub pump: PumpState,
|
||||
/// Last known firmware build timestamp for sensor A (minutes since Unix epoch).
|
||||
/// Set during sensor detection; None if detection has not been run yet.
|
||||
pub sensor_a_firmware_build_minutes: Option<u32>,
|
||||
/// Last known firmware build timestamp for sensor B.
|
||||
pub sensor_b_firmware_build_minutes: Option<u32>,
|
||||
/// Last time fertilizer was applied (Unix timestamp in seconds).
|
||||
pub last_fertilizer_time: i64,
|
||||
}
|
||||
|
||||
fn map_range_moisture(
|
||||
@@ -158,13 +175,19 @@ impl PlantState {
|
||||
|
||||
let previous_pump = board.board_hal.get_esp().last_pump_time(plant_id);
|
||||
let consecutive_pump_count = board.board_hal.get_esp().consecutive_pump_count(plant_id);
|
||||
let last_fertilizer_time = board.board_hal.get_esp().last_fertilizer_time(plant_id);
|
||||
let (a_builds, b_builds) = board.board_hal.get_sensor_build_minutes();
|
||||
let state = Self {
|
||||
sensor_a,
|
||||
sensor_b,
|
||||
pump: PumpState {
|
||||
consecutive_pump_count,
|
||||
previous_pump,
|
||||
overcurrent_error: None,
|
||||
},
|
||||
sensor_a_firmware_build_minutes: a_builds[plant_id],
|
||||
sensor_b_firmware_build_minutes: b_builds[plant_id],
|
||||
last_fertilizer_time,
|
||||
};
|
||||
if state.is_err() {
|
||||
let _ = board.board_hal.fault(plant_id, true).await;
|
||||
@@ -287,6 +310,9 @@ impl PlantState {
|
||||
} else {
|
||||
None
|
||||
},
|
||||
sensor_a_firmware_build_minutes: self.sensor_a_firmware_build_minutes,
|
||||
sensor_b_firmware_build_minutes: self.sensor_b_firmware_build_minutes,
|
||||
last_fertilizer_time: self.last_fertilizer_time,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -315,4 +341,10 @@ pub struct PlantInfo<'a> {
|
||||
last_pump: Option<DateTime<Tz>>,
|
||||
/// next time when pump should activate
|
||||
next_pump: Option<DateTime<Tz>>,
|
||||
/// firmware build timestamp of sensor A (minutes since Unix epoch); None if unknown
|
||||
sensor_a_firmware_build_minutes: Option<u32>,
|
||||
/// firmware build timestamp of sensor B (minutes since Unix epoch); None if unknown
|
||||
sensor_b_firmware_build_minutes: Option<u32>,
|
||||
/// last time when fertilizer was applied
|
||||
last_fertilizer_time: i64,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::fat_error::{FatError, FatResult};
|
||||
use crate::hal::savegame_manager::SAVEGAME_SLOT_SIZE;
|
||||
use crate::webserver::read_up_to_bytes_from_request;
|
||||
use crate::BOARD_ACCESS;
|
||||
use alloc::borrow::ToOwned;
|
||||
@@ -46,7 +47,7 @@ pub(crate) async fn backup_config<T, const N: usize>(
|
||||
where
|
||||
T: Read + Write,
|
||||
{
|
||||
let input = read_up_to_bytes_from_request(conn, Some(4096)).await?;
|
||||
let input = read_up_to_bytes_from_request(conn, Some(SAVEGAME_SLOT_SIZE)).await?;
|
||||
info!("Read input with length {}", input.len());
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
let config_to_backup = serde_json::from_slice(&input)?;
|
||||
|
||||
@@ -109,7 +109,7 @@ pub(crate) async fn get_solar_state<T, const N: usize>(
|
||||
Ok(Some(serde_json::to_string(&state)?))
|
||||
}
|
||||
|
||||
pub(crate) async fn get_version_web<T, const N: usize>(
|
||||
pub(crate) async fn get_firmware_info_web<T, const N: usize>(
|
||||
_request: &mut Connection<'_, T, N>,
|
||||
) -> FatResult<Option<String>> {
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::log::LOG_ACCESS;
|
||||
use alloc::string::String;
|
||||
use alloc::vec::Vec;
|
||||
use edge_http::io::server::Connection;
|
||||
use edge_nal::io::{Read, Write};
|
||||
use serde::Serialize;
|
||||
|
||||
pub(crate) async fn get_log<T, const N: usize>(
|
||||
conn: &mut Connection<'_, T, N>,
|
||||
@@ -34,3 +37,29 @@ where
|
||||
conn.write_all("]".as_bytes()).await?;
|
||||
Ok(Some(200))
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct LiveLogEntry {
|
||||
seq: u64,
|
||||
text: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct LiveLogResponse {
|
||||
entries: Vec<LiveLogEntry>,
|
||||
dropped: bool,
|
||||
next_seq: u64,
|
||||
}
|
||||
|
||||
pub(crate) async fn get_live_log(after: Option<u64>) -> FatResult<Option<String>> {
|
||||
let (raw_entries, dropped, next_seq) = crate::log::INTERCEPTOR.get_live_logs(after);
|
||||
let response = LiveLogResponse {
|
||||
entries: raw_entries
|
||||
.into_iter()
|
||||
.map(|(seq, text)| LiveLogEntry { seq, text })
|
||||
.collect(),
|
||||
dropped,
|
||||
next_seq,
|
||||
};
|
||||
Ok(Some(serde_json::to_string(&response)?))
|
||||
}
|
||||
|
||||
@@ -11,14 +11,14 @@ use crate::fat_error::{FatError, FatResult};
|
||||
use crate::webserver::backup_manager::{backup_config, backup_info, get_backup_config};
|
||||
use crate::webserver::get_json::{
|
||||
delete_save, get_battery_state, get_config, get_live_moisture, get_log_localization_config,
|
||||
get_solar_state, get_time, get_timezones, get_version_web, list_saves, tank_info,
|
||||
get_firmware_info_web, get_solar_state, get_time, get_timezones, list_saves, tank_info,
|
||||
};
|
||||
use crate::webserver::get_log::get_log;
|
||||
use crate::webserver::get_log::{get_live_log, get_log};
|
||||
use crate::webserver::get_static::{serve_bundle, serve_favicon, serve_index};
|
||||
use crate::webserver::ota::ota_operations;
|
||||
use crate::webserver::post_json::{
|
||||
board_test, can_power, detect_sensors, night_lamp_test, pump_test, set_config, wifi_scan,
|
||||
write_time,
|
||||
board_test, can_power, detect_sensors, fertilizer_pump_test, night_lamp_test, pump_test,
|
||||
set_config, wifi_scan, write_time,
|
||||
};
|
||||
use crate::{bail, BOARD_ACCESS};
|
||||
use alloc::borrow::ToOwned;
|
||||
@@ -64,7 +64,6 @@ impl Handler for HTTPRequestRouter {
|
||||
e
|
||||
})?
|
||||
} else {
|
||||
crate::log::INTERCEPTOR.start_capture().await;
|
||||
match method {
|
||||
Method::Get => match path {
|
||||
"/favicon.ico" => serve_favicon(conn).await?,
|
||||
@@ -74,7 +73,7 @@ impl Handler for HTTPRequestRouter {
|
||||
"/get_backup_config" => get_backup_config(conn).await?,
|
||||
&_ => {
|
||||
let json = match path {
|
||||
"/version" => Some(get_version_web(conn).await),
|
||||
"/firmware_info" => Some(get_firmware_info_web(conn).await),
|
||||
"/time" => Some(get_time(conn).await),
|
||||
"/battery" => Some(get_battery_state(conn).await),
|
||||
"/solar" => Some(get_solar_state(conn).await),
|
||||
@@ -84,6 +83,14 @@ impl Handler for HTTPRequestRouter {
|
||||
"/timezones" => Some(get_timezones().await),
|
||||
"/moisture" => Some(get_live_moisture(conn).await),
|
||||
"/list_saves" => Some(list_saves(conn).await),
|
||||
// /live_log accepts an optional ?after=N query parameter
|
||||
p if p == "/live_log" || p.starts_with("/live_log?") => {
|
||||
let after: Option<u64> = p
|
||||
.find("after=")
|
||||
.and_then(|pos| p[pos + 6..].split('&').next())
|
||||
.and_then(|s| s.parse().ok());
|
||||
Some(get_live_log(after).await)
|
||||
}
|
||||
// /get_config accepts an optional ?saveidx=N query parameter
|
||||
p if p == "/get_config" || p.starts_with("/get_config?") => {
|
||||
let saveidx: Option<usize> = p
|
||||
@@ -109,6 +116,7 @@ impl Handler for HTTPRequestRouter {
|
||||
"/pumptest" => Some(pump_test(conn).await),
|
||||
"/can_power" => Some(can_power(conn).await),
|
||||
"/lamptest" => Some(night_lamp_test(conn).await),
|
||||
"/fertilizerpumptest" => Some(fertilizer_pump_test(conn).await),
|
||||
"/boardtest" => Some(board_test().await),
|
||||
"/detect_sensors" => Some(detect_sensors(conn).await),
|
||||
"/reboot" => {
|
||||
@@ -167,7 +175,6 @@ impl Handler for HTTPRequestRouter {
|
||||
let response_time = Instant::now().duration_since(start).as_millis();
|
||||
|
||||
info!("\"{method} {path}\" {code} {response_time}ms");
|
||||
crate::log::INTERCEPTOR.stop_capture().await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -265,17 +272,9 @@ where
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
let mut error_text = err.to_string();
|
||||
let error_text = err.to_string();
|
||||
info!("error handling process {error_text}");
|
||||
|
||||
if let Some(logs) = crate::log::INTERCEPTOR.stop_capture().await {
|
||||
error_text.push_str("\n\nCaptured Logs:\n");
|
||||
for log in logs {
|
||||
error_text.push_str(&log);
|
||||
error_text.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
conn.initiate_response(
|
||||
500,
|
||||
Some("OK"),
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
use crate::config::PlantControllerConfig;
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::hal::Detection;
|
||||
use crate::hal::DetectionRequest;
|
||||
use crate::webserver::read_up_to_bytes_from_request;
|
||||
use crate::{do_secure_pump, BOARD_ACCESS};
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec::Vec;
|
||||
use chrono::DateTime;
|
||||
use edge_http::io::server::Connection;
|
||||
use edge_nal::io::{Read, Write};
|
||||
use esp_radio::wifi::ap::AccessPointInfo;
|
||||
use log::info;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -40,10 +42,10 @@ pub(crate) async fn wifi_scan<T, const N: usize>(
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
info!("start wifi scan");
|
||||
let mut ssids: Vec<String> = Vec::new();
|
||||
let scan_result = board.board_hal.get_esp().wifi_scan().await?;
|
||||
let scan_result: Vec<AccessPointInfo> = board.board_hal.get_esp().wifi_scan().await?;
|
||||
scan_result
|
||||
.iter()
|
||||
.for_each(|s| ssids.push(s.ssid.to_string()));
|
||||
.for_each(|s| ssids.push(s.ssid.as_str().to_owned()));
|
||||
let ssid_json = serde_json::to_string(&SSIDList { ssids })?;
|
||||
info!("Sending ssid list {}", &ssid_json);
|
||||
Ok(Some(ssid_json))
|
||||
@@ -62,7 +64,7 @@ where
|
||||
T: Read + Write,
|
||||
{
|
||||
let actual_data = read_up_to_bytes_from_request(request, None).await?;
|
||||
let detect: Detection = serde_json::from_slice(&actual_data)?;
|
||||
let detect: DetectionRequest = serde_json::from_slice(&actual_data)?;
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
let result = board.board_hal.detect_sensors(detect).await?;
|
||||
let json = serde_json::to_string(&result)?;
|
||||
@@ -100,6 +102,19 @@ where
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) async fn fertilizer_pump_test<T, const N: usize>(
|
||||
_request: &mut Connection<'_, T, N>,
|
||||
) -> FatResult<Option<String>>
|
||||
where
|
||||
T: Read + Write,
|
||||
{
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
board.board_hal.fertilizer_pump(true).await?;
|
||||
embassy_time::Timer::after_millis(1000).await;
|
||||
board.board_hal.fertilizer_pump(false).await?;
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) async fn write_time<T, const N: usize>(
|
||||
request: &mut Connection<'_, T, N>,
|
||||
) -> FatResult<Option<String>>
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
export interface LogArray extends Array<LogEntry> {
|
||||
}
|
||||
|
||||
export interface LiveLogEntry {
|
||||
seq: number,
|
||||
text: string,
|
||||
}
|
||||
|
||||
export interface LiveLogResponse {
|
||||
entries: LiveLogEntry[],
|
||||
dropped: boolean,
|
||||
next_seq: number,
|
||||
}
|
||||
|
||||
export interface LogEntry {
|
||||
timestamp: string,
|
||||
message_id: number,
|
||||
@@ -117,6 +128,8 @@ export interface PlantConfig {
|
||||
min_moisture: number,
|
||||
pump_time_s: number,
|
||||
pump_cooldown_min: number,
|
||||
fertilizer_s: number,
|
||||
fertilizer_cooldown_min: number,
|
||||
pump_hour_start: number,
|
||||
pump_hour_end: number,
|
||||
pump_limit_ml: number,
|
||||
@@ -172,6 +185,10 @@ export interface VersionInfo {
|
||||
current: string,
|
||||
slot0_state: string,
|
||||
slot1_state: string,
|
||||
heap_total: number,
|
||||
heap_used: number,
|
||||
heap_free: number,
|
||||
heap_max_used: number,
|
||||
}
|
||||
|
||||
export interface BatteryState {
|
||||
@@ -184,9 +201,22 @@ export interface BatteryState {
|
||||
state_of_health: string
|
||||
}
|
||||
|
||||
export interface DetectionPlant {
|
||||
/// Request: which sensors to send IDENTIFY_CMD to.
|
||||
export interface SensorRequest {
|
||||
sensor_a: boolean,
|
||||
sensor_b: boolean
|
||||
sensor_b: boolean,
|
||||
}
|
||||
|
||||
export interface DetectionRequest {
|
||||
plant: SensorRequest[]
|
||||
}
|
||||
|
||||
/// Response: detection result per plant.
|
||||
/// sensor_a / sensor_b: firmware build timestamp in minutes since Unix epoch,
|
||||
/// or null if the sensor did not respond.
|
||||
export interface DetectionPlant {
|
||||
sensor_a: number | null,
|
||||
sensor_b: number | null,
|
||||
}
|
||||
|
||||
export interface Detection {
|
||||
|
||||
@@ -22,3 +22,8 @@
|
||||
<div class="boardkey">Pump corrosion protection (weekly)</div>
|
||||
<input type="checkbox" id="hardware_pump_corrosion_protection">
|
||||
</div>
|
||||
|
||||
<div class="subtitle">Fertilizer Pump:</div>
|
||||
<div class="flexcontainer">
|
||||
<button class="subtitle" id="fertilizer_pump_test">Test Fertilizer Pump</button>
|
||||
</div>
|
||||
|
||||
@@ -5,6 +5,7 @@ export class HardwareConfigView {
|
||||
private readonly hardware_board_value: HTMLSelectElement;
|
||||
private readonly hardware_battery_value: HTMLSelectElement;
|
||||
private readonly hardware_pump_corrosion_protection: HTMLInputElement;
|
||||
private readonly fertilizer_pump_test: HTMLButtonElement;
|
||||
constructor(controller:Controller){
|
||||
(document.getElementById("hardwareview") as HTMLElement).innerHTML = require('./hardware.html') as string;
|
||||
|
||||
@@ -33,6 +34,11 @@ export class HardwareConfigView {
|
||||
|
||||
this.hardware_pump_corrosion_protection = document.getElementById("hardware_pump_corrosion_protection") as HTMLInputElement;
|
||||
this.hardware_pump_corrosion_protection.onchange = controller.configChanged
|
||||
|
||||
this.fertilizer_pump_test = document.getElementById("fertilizer_pump_test") as HTMLButtonElement;
|
||||
this.fertilizer_pump_test.onclick = () => {
|
||||
controller.testFertilizerPump();
|
||||
}
|
||||
}
|
||||
|
||||
setConfig(hardware: BoardHardware) {
|
||||
|
||||
@@ -1,7 +1,48 @@
|
||||
<style>
|
||||
#livelogpanel {
|
||||
font-family: monospace;
|
||||
font-size: 0.85em;
|
||||
background: #1a1a1a;
|
||||
color: #d4d4d4;
|
||||
padding: 8px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
border: 1px solid #444;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.livelog-dropped {
|
||||
color: #f0a500;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.log-accordion-header {
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.log-accordion-header::before {
|
||||
content: "▶";
|
||||
font-size: 0.75em;
|
||||
transition: transform 0.15s;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.log-accordion-header.open::before {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
#logpanel {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
<button id="loadLog">Load Logs</button>
|
||||
<div id="logpanel">
|
||||
|
||||
</div>
|
||||
|
||||
<h4 id="logAccordionHeader" class="log-accordion-header">Application Log</h4>
|
||||
<div id="logpanel"></div>
|
||||
|
||||
<h4>Live Log</h4>
|
||||
<div id="livelogpanel"></div>
|
||||
|
||||
@@ -1,19 +1,38 @@
|
||||
import { Controller } from "./main";
|
||||
import {LogArray, LogLocalisation} from "./api";
|
||||
import {LiveLogResponse, LogArray, LogLocalisation} from "./api";
|
||||
|
||||
const LIVE_LOG_POLL_INTERVAL_MS = 2000;
|
||||
|
||||
export class LogView {
|
||||
private readonly logpanel: HTMLElement;
|
||||
private readonly loadLog: HTMLButtonElement;
|
||||
private readonly livelogpanel: HTMLElement;
|
||||
private readonly accordionHeader: HTMLElement;
|
||||
loglocale: LogLocalisation | undefined;
|
||||
|
||||
private liveLogNextSeq: number | undefined = undefined;
|
||||
private liveLogTimer: ReturnType<typeof setTimeout> | undefined = undefined;
|
||||
private structuredLogLoaded = false;
|
||||
|
||||
constructor(controller: Controller) {
|
||||
(document.getElementById("logview") as HTMLElement).innerHTML = require('./log.html') as string;
|
||||
this.logpanel = document.getElementById("logpanel") as HTMLElement
|
||||
this.loadLog = document.getElementById("loadLog") as HTMLButtonElement
|
||||
this.logpanel = document.getElementById("logpanel") as HTMLElement;
|
||||
this.livelogpanel = document.getElementById("livelogpanel") as HTMLElement;
|
||||
this.accordionHeader = document.getElementById("logAccordionHeader") as HTMLElement;
|
||||
|
||||
this.loadLog.onclick = () => {
|
||||
controller.loadLog();
|
||||
}
|
||||
this.accordionHeader.onclick = () => {
|
||||
const isOpen = this.logpanel.style.display !== "none";
|
||||
if (isOpen) {
|
||||
this.logpanel.style.display = "none";
|
||||
this.accordionHeader.classList.remove("open");
|
||||
} else {
|
||||
this.logpanel.style.display = "";
|
||||
this.accordionHeader.classList.add("open");
|
||||
if (!this.structuredLogLoaded) {
|
||||
this.structuredLogLoaded = true;
|
||||
controller.loadLog();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
setLogLocalisation(loglocale: LogLocalisation) {
|
||||
@@ -21,10 +40,10 @@ export class LogView {
|
||||
}
|
||||
|
||||
setLog(logs: LogArray) {
|
||||
this.logpanel.textContent = ""
|
||||
this.logpanel.textContent = "";
|
||||
logs.forEach(entry => {
|
||||
let message = this.loglocale!![entry.message_id];
|
||||
let template = message.message
|
||||
let template = message.message;
|
||||
template = template.replace("${number_a}", entry.a.toString());
|
||||
template = template.replace("${number_b}", entry.b.toString());
|
||||
template = template.replace("${txt_short}", entry.txt_short.toString());
|
||||
@@ -32,15 +51,67 @@ export class LogView {
|
||||
|
||||
let ts = new Date(entry.timestamp);
|
||||
|
||||
let div = document.createElement("div")
|
||||
let timestampDiv = document.createElement("div")
|
||||
let messageDiv = document.createElement("div")
|
||||
let div = document.createElement("div");
|
||||
let timestampDiv = document.createElement("div");
|
||||
let messageDiv = document.createElement("div");
|
||||
timestampDiv.innerText = ts.toISOString();
|
||||
messageDiv.innerText = template;
|
||||
div.appendChild(timestampDiv)
|
||||
div.appendChild(messageDiv)
|
||||
this.logpanel.appendChild(div)
|
||||
}
|
||||
)
|
||||
div.appendChild(timestampDiv);
|
||||
div.appendChild(messageDiv);
|
||||
this.logpanel.appendChild(div);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
startLivePoll(publicUrl: string) {
|
||||
if (this.liveLogTimer !== undefined) {
|
||||
return;
|
||||
}
|
||||
const poll = async () => {
|
||||
try {
|
||||
const url = this.liveLogNextSeq !== undefined
|
||||
? `${publicUrl}/live_log?after=${this.liveLogNextSeq}`
|
||||
: `${publicUrl}/live_log`;
|
||||
const response = await fetch(url);
|
||||
const data = await response.json() as LiveLogResponse;
|
||||
this.appendLiveLog(data);
|
||||
} catch (_e) {
|
||||
// network error — silently ignore, will retry next interval
|
||||
}
|
||||
this.liveLogTimer = setTimeout(poll, LIVE_LOG_POLL_INTERVAL_MS);
|
||||
};
|
||||
// Kick off immediately
|
||||
this.liveLogTimer = setTimeout(poll, 0);
|
||||
}
|
||||
|
||||
stopLivePoll() {
|
||||
if (this.liveLogTimer !== undefined) {
|
||||
clearTimeout(this.liveLogTimer);
|
||||
this.liveLogTimer = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private appendLiveLog(data: LiveLogResponse) {
|
||||
const panel = this.livelogpanel;
|
||||
const wasAtBottom = panel.scrollHeight - panel.scrollTop <= panel.clientHeight + 4;
|
||||
|
||||
if (data.dropped) {
|
||||
const marker = document.createElement("div");
|
||||
marker.className = "livelog-dropped";
|
||||
marker.textContent = "[..]";
|
||||
panel.appendChild(marker);
|
||||
}
|
||||
|
||||
for (const entry of data.entries) {
|
||||
const line = document.createElement("div");
|
||||
line.textContent = entry.text;
|
||||
panel.appendChild(line);
|
||||
}
|
||||
|
||||
this.liveLogNextSeq = data.next_seq;
|
||||
|
||||
// Auto-scroll to bottom only if user was already at the bottom
|
||||
if (wasAtBottom) {
|
||||
panel.scrollTop = panel.scrollHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ import {
|
||||
SetTime, SSIDList, TankInfo,
|
||||
TestPump,
|
||||
VersionInfo,
|
||||
SaveInfo, SolarState, PumpTestResult, Detection, CanPower
|
||||
SaveInfo, SolarState, PumpTestResult, Detection, DetectionRequest, CanPower
|
||||
} from "./api";
|
||||
import {SolarView} from "./solarview";
|
||||
import {toast} from "./toast";
|
||||
@@ -194,7 +194,7 @@ export class Controller {
|
||||
|
||||
async version(): Promise<void> {
|
||||
controller.progressview.addIndeterminate("version", "Getting buildVersion")
|
||||
const response = await fetch(PUBLIC_URL + "/version");
|
||||
const response = await fetch(PUBLIC_URL + "/firmware_info");
|
||||
const json = await response.json();
|
||||
const versionInfo = json as VersionInfo;
|
||||
controller.progressview.removeProgress("version");
|
||||
@@ -304,6 +304,12 @@ export class Controller {
|
||||
})
|
||||
}
|
||||
|
||||
testFertilizerPump() {
|
||||
fetch(PUBLIC_URL + "/fertilizerpumptest", {
|
||||
method: "POST"
|
||||
})
|
||||
}
|
||||
|
||||
testPlant(plantId: number) {
|
||||
let counter = 0
|
||||
let limit = 30
|
||||
@@ -339,7 +345,7 @@ export class Controller {
|
||||
)
|
||||
}
|
||||
|
||||
async detectSensors(detection: Detection, silent: boolean = false) {
|
||||
async detectSensors(detection: DetectionRequest, silent: boolean = false) {
|
||||
let counter = 0
|
||||
let limit = 5
|
||||
if (!silent) {
|
||||
@@ -499,7 +505,7 @@ export class Controller {
|
||||
|
||||
waitForReboot() {
|
||||
console.log("Check if controller online again")
|
||||
fetch(PUBLIC_URL + "/version", {
|
||||
fetch(PUBLIC_URL + "/firmware_info", {
|
||||
method: "GET",
|
||||
signal: AbortSignal.timeout(5000)
|
||||
}).then(response => {
|
||||
@@ -577,7 +583,7 @@ export class Controller {
|
||||
this.hardwareView = new HardwareConfigView(this)
|
||||
this.detectBtn = document.getElementById("detect_sensors") as HTMLButtonElement
|
||||
this.detectBtn.onclick = () => {
|
||||
const detection: Detection = {
|
||||
const detection: DetectionRequest = {
|
||||
plant: Array.from({length: PLANT_COUNT}, () => ({
|
||||
sensor_a: true,
|
||||
sensor_b: true,
|
||||
@@ -615,7 +621,7 @@ export class Controller {
|
||||
|
||||
try {
|
||||
await this.measure_moisture(true);
|
||||
const detection: Detection = {
|
||||
const detection: DetectionRequest = {
|
||||
plant: Array.from({length: PLANT_COUNT}, () => ({
|
||||
sensor_a: true,
|
||||
sensor_b: true,
|
||||
@@ -668,7 +674,8 @@ async function executeTasksSequentially() {
|
||||
}
|
||||
|
||||
executeTasksSequentially().then(() => {
|
||||
controller.progressview.removeProgress("initial")
|
||||
controller.progressview.removeProgress("initial");
|
||||
controller.logView.startLivePoll(PUBLIC_URL);
|
||||
});
|
||||
|
||||
controller.progressview.removeProgress("rebooting");
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<div class="subtitle">
|
||||
Current Firmware
|
||||
</div>
|
||||
<button style="margin-left: auto;" type="button" id="refresh_firmware_info">Refresh</button>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Buildtime:</span>
|
||||
@@ -42,12 +43,35 @@
|
||||
<span class="otakey">State1:</span>
|
||||
<span class="otavalue" id="firmware_state1"></span>
|
||||
</div>
|
||||
|
||||
<div class="flexcontainer">
|
||||
<form class="otaform" id="upload_form" method="post">
|
||||
<input class="otachooser" type="file" name="file1" id="firmware_file"><br>
|
||||
</form>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<div class="subtitle">
|
||||
Heap Memory
|
||||
</div>
|
||||
<div></div>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Free:</span>
|
||||
<span class="otavalue" id="heap_free"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Used:</span>
|
||||
<span class="otavalue" id="heap_used"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Total:</span>
|
||||
<span class="otavalue" id="heap_total"></span>
|
||||
</div>
|
||||
<div class="flexcontainer">
|
||||
<span class="otakey">Peak used:</span>
|
||||
<span class="otavalue" id="heap_max_used"></span>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="display:flex">
|
||||
<button style="margin-left: 16px; margin-top: 8px;" class="col-6" type="button" id="test">Self-Test</button>
|
||||
</div>
|
||||
@@ -1,6 +1,10 @@
|
||||
import {Controller} from "./main";
|
||||
import {VersionInfo} from "./api";
|
||||
|
||||
function fmtBytes(n: number): string {
|
||||
return `${n} B (${(n / 1024).toFixed(1)} KiB)`;
|
||||
}
|
||||
|
||||
export class OTAView {
|
||||
readonly file1Upload: HTMLInputElement;
|
||||
readonly firmware_buildtime: HTMLDivElement;
|
||||
@@ -8,19 +12,26 @@ export class OTAView {
|
||||
readonly firmware_partition: HTMLDivElement;
|
||||
readonly firmware_state0: HTMLDivElement;
|
||||
readonly firmware_state1: HTMLDivElement;
|
||||
readonly heap_free: HTMLDivElement;
|
||||
readonly heap_used: HTMLDivElement;
|
||||
readonly heap_total: HTMLDivElement;
|
||||
readonly heap_max_used: HTMLDivElement;
|
||||
|
||||
constructor(controller: Controller) {
|
||||
(document.getElementById("firmwareview") as HTMLElement).innerHTML = require("./ota.html")
|
||||
|
||||
let test = document.getElementById("test") as HTMLButtonElement;
|
||||
let refresh = document.getElementById("refresh_firmware_info") as HTMLButtonElement;
|
||||
|
||||
this.firmware_buildtime = document.getElementById("firmware_buildtime") as HTMLDivElement;
|
||||
this.firmware_githash = document.getElementById("firmware_githash") as HTMLDivElement;
|
||||
this.firmware_partition = document.getElementById("firmware_partition") as HTMLDivElement;
|
||||
|
||||
this.firmware_state0 = document.getElementById("firmware_state0") as HTMLDivElement;
|
||||
this.firmware_state1 = document.getElementById("firmware_state1") as HTMLDivElement;
|
||||
|
||||
this.heap_free = document.getElementById("heap_free") as HTMLDivElement;
|
||||
this.heap_used = document.getElementById("heap_used") as HTMLDivElement;
|
||||
this.heap_total = document.getElementById("heap_total") as HTMLDivElement;
|
||||
this.heap_max_used = document.getElementById("heap_max_used") as HTMLDivElement;
|
||||
|
||||
const file = document.getElementById("firmware_file") as HTMLInputElement;
|
||||
this.file1Upload = file
|
||||
@@ -36,6 +47,10 @@ export class OTAView {
|
||||
test.onclick = () => {
|
||||
controller.selfTest();
|
||||
}
|
||||
|
||||
refresh.onclick = () => {
|
||||
controller.version();
|
||||
}
|
||||
}
|
||||
|
||||
setVersion(versionInfo: VersionInfo) {
|
||||
@@ -44,5 +59,9 @@ export class OTAView {
|
||||
this.firmware_partition.innerText = versionInfo.current;
|
||||
this.firmware_state0.innerText = versionInfo.slot0_state;
|
||||
this.firmware_state1.innerText = versionInfo.slot1_state;
|
||||
this.heap_free.innerText = fmtBytes(versionInfo.heap_free);
|
||||
this.heap_used.innerText = fmtBytes(versionInfo.heap_used);
|
||||
this.heap_total.innerText = fmtBytes(versionInfo.heap_total);
|
||||
this.heap_max_used.innerText = fmtBytes(versionInfo.heap_max_used);
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,16 @@
|
||||
<input class="plantvalue" id="plant_${plantId}_pump_cooldown_min" type="number" min="0" max="600"
|
||||
placeholder="30">
|
||||
</div>
|
||||
<div class="flexcontainer plantPumpEnabledOnly_${plantId}">
|
||||
<div class="plantkey">Fertilizer (s):</div>
|
||||
<input class="plantvalue" id="plant_${plantId}_fertilizer_s" type="number" min="0" max="60"
|
||||
placeholder="0">
|
||||
</div>
|
||||
<div class="flexcontainer plantPumpEnabledOnly_${plantId}">
|
||||
<div class="plantkey">Fertilizer Cooldown (m):</div>
|
||||
<input class="plantvalue" id="plant_${plantId}_fertilizer_cooldown_min" type="number" min="0" max="20160"
|
||||
placeholder="1440">
|
||||
</div>
|
||||
<div class="flexcontainer plantPumpEnabledOnly_${plantId}">
|
||||
<div class="plantkey">"Pump Hour Start":</div>
|
||||
<select class="plantvalue" id="plant_${plantId}_pump_hour_start">10</select>
|
||||
@@ -126,17 +136,25 @@
|
||||
<div class="subtitle">Live:</div>
|
||||
</div>
|
||||
<div class="flexcontainer plantSensorEnabledOnly_${plantId}">
|
||||
<button class="subtitle" id="plant_${plantId}_test_sensor_a">Test Sensor A</button>
|
||||
<button class="subtitle" id="plant_${plantId}_test_sensor_b">Test Sensor B</button>
|
||||
<button class="subtitle" id="plant_${plantId}_test_sensor_a">Identify Sensor A</button>
|
||||
<button class="subtitle" id="plant_${plantId}_test_sensor_b">Identify Sensor B</button>
|
||||
</div>
|
||||
<div class="flexcontainer plantSensorEnabledOnly_${plantId}">
|
||||
<span class="plantsensorkey">Sensor A:</span>
|
||||
<span class="plantsensorvalue" id="plant_${plantId}_moisture_a">not measured</span>
|
||||
</div>
|
||||
<div class="flexcontainer plantSensorEnabledOnly_${plantId}">
|
||||
<span class="plantsensorkey">Sensor A FW:</span>
|
||||
<span class="plantsensorvalue" id="plant_${plantId}_sensor_a_fw_build">unknown</span>
|
||||
</div>
|
||||
<div class="flexcontainer plantSensorEnabledOnly_${plantId}">
|
||||
<div class="plantsensorkey">Sensor B:</div>
|
||||
<span class="plantsensorvalue" id="plant_${plantId}_moisture_b">not measured</span>
|
||||
</div>
|
||||
<div class="flexcontainer plantSensorEnabledOnly_${plantId}">
|
||||
<span class="plantsensorkey">Sensor B FW:</span>
|
||||
<span class="plantsensorvalue" id="plant_${plantId}_sensor_b_fw_build">unknown</span>
|
||||
</div>
|
||||
<div class="flexcontainer plantPumpEnabledOnly_${plantId}">
|
||||
<div class="plantsensorkey">Max Current</div>
|
||||
<span class="plantsensorvalue" id="plant_${plantId}_pump_test_current_max">not_tested</span>
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import {DetectionPlant, Detection, PlantConfig, PumpTestResult} from "./api";
|
||||
import {Detection, DetectionPlant, DetectionRequest, PlantConfig, PumpTestResult} from "./api";
|
||||
|
||||
export const PLANT_COUNT = 8;
|
||||
|
||||
/** Format a firmware build timestamp (minutes since Unix epoch) as a human-readable date/time. */
|
||||
function formatBuildMinutes(buildMinutes: number | null): string {
|
||||
if (buildMinutes === null) return "not detected";
|
||||
if (buildMinutes === 0) return "detected (no timestamp)";
|
||||
const ms = buildMinutes * 60 * 1000;
|
||||
return new Date(ms).toISOString().replace("T", " ").slice(0, 16) + " UTC";
|
||||
}
|
||||
|
||||
import {Controller} from "./main";
|
||||
|
||||
@@ -72,6 +79,8 @@ export class PlantView {
|
||||
private readonly minMoisture: HTMLInputElement;
|
||||
private readonly pumpTimeS: HTMLInputElement;
|
||||
private readonly pumpCooldown: HTMLInputElement;
|
||||
private readonly fertilizerS: HTMLInputElement;
|
||||
private readonly fertilizerCooldownMin: HTMLInputElement;
|
||||
private readonly pumpHourStart: HTMLSelectElement;
|
||||
private readonly pumpHourEnd: HTMLSelectElement;
|
||||
private readonly sensorAInstalled: HTMLInputElement;
|
||||
@@ -79,6 +88,8 @@ export class PlantView {
|
||||
private readonly mode: HTMLSelectElement;
|
||||
private readonly moistureA: HTMLElement;
|
||||
private readonly moistureB: HTMLElement;
|
||||
private readonly sensorAFwBuild: HTMLElement;
|
||||
private readonly sensorBFwBuild: HTMLElement;
|
||||
private readonly maxConsecutivePumpCount: HTMLInputElement;
|
||||
private readonly minPumpCurrentMa: HTMLInputElement;
|
||||
private readonly maxPumpCurrentMa: HTMLInputElement;
|
||||
@@ -109,6 +120,8 @@ export class PlantView {
|
||||
|
||||
this.moistureA = document.getElementById("plant_" + plantId + "_moisture_a")! as HTMLElement;
|
||||
this.moistureB = document.getElementById("plant_" + plantId + "_moisture_b")! as HTMLElement;
|
||||
this.sensorAFwBuild = document.getElementById("plant_" + plantId + "_sensor_a_fw_build")! as HTMLElement;
|
||||
this.sensorBFwBuild = document.getElementById("plant_" + plantId + "_sensor_b_fw_build")! as HTMLElement;
|
||||
|
||||
this.pump_test_current_max = document.getElementById("plant_" + plantId + "_pump_test_current_max")! as HTMLElement;
|
||||
this.pump_test_current_min = document.getElementById("plant_" + plantId + "_pump_test_current_min")! as HTMLElement;
|
||||
@@ -124,7 +137,7 @@ export class PlantView {
|
||||
|
||||
this.testSensorAButton = document.getElementById("plant_" + plantId + "_test_sensor_a")! as HTMLButtonElement;
|
||||
this.testSensorAButton.onclick = () => {
|
||||
const detection: Detection = {
|
||||
const detection: DetectionRequest = {
|
||||
plant: Array.from({length: PLANT_COUNT}, (_v, idx) => ({
|
||||
sensor_a: idx === plantId,
|
||||
sensor_b: false,
|
||||
@@ -135,7 +148,7 @@ export class PlantView {
|
||||
|
||||
this.testSensorBButton = document.getElementById("plant_" + plantId + "_test_sensor_b")! as HTMLButtonElement;
|
||||
this.testSensorBButton.onclick = () => {
|
||||
const detection: Detection = {
|
||||
const detection: DetectionRequest = {
|
||||
plant: Array.from({length: PLANT_COUNT}, (_v, idx) => ({
|
||||
sensor_a: false,
|
||||
sensor_b: idx === plantId,
|
||||
@@ -169,6 +182,16 @@ export class PlantView {
|
||||
controller.configChanged()
|
||||
}
|
||||
|
||||
this.fertilizerS = document.getElementById("plant_" + plantId + "_fertilizer_s") as HTMLInputElement;
|
||||
this.fertilizerS.onchange = function () {
|
||||
controller.configChanged()
|
||||
}
|
||||
|
||||
this.fertilizerCooldownMin = document.getElementById("plant_" + plantId + "_fertilizer_cooldown_min") as HTMLInputElement;
|
||||
this.fertilizerCooldownMin.onchange = function () {
|
||||
controller.configChanged()
|
||||
}
|
||||
|
||||
this.pumpHourStart = document.getElementById("plant_" + plantId + "_pump_hour_start") as HTMLSelectElement;
|
||||
this.pumpHourStart.onchange = function () {
|
||||
controller.configChanged()
|
||||
@@ -317,6 +340,8 @@ export class PlantView {
|
||||
this.minMoisture.value = plantConfig.min_moisture?.toString() || "";
|
||||
this.pumpTimeS.value = plantConfig.pump_time_s.toString();
|
||||
this.pumpCooldown.value = plantConfig.pump_cooldown_min.toString();
|
||||
this.fertilizerS.value = plantConfig.fertilizer_s?.toString() || "0";
|
||||
this.fertilizerCooldownMin.value = plantConfig.fertilizer_cooldown_min?.toString() || "1440";
|
||||
this.pumpHourStart.value = plantConfig.pump_hour_start.toString();
|
||||
this.pumpHourEnd.value = plantConfig.pump_hour_end.toString();
|
||||
this.sensorBInstalled.checked = plantConfig.sensor_b;
|
||||
@@ -344,6 +369,8 @@ export class PlantView {
|
||||
pump_time_s: this.pumpTimeS.valueAsNumber,
|
||||
pump_limit_ml: 5000,
|
||||
pump_cooldown_min: this.pumpCooldown.valueAsNumber,
|
||||
fertilizer_s: this.fertilizerS.valueAsNumber || 0,
|
||||
fertilizer_cooldown_min: this.fertilizerCooldownMin.valueAsNumber || 1440,
|
||||
pump_hour_start: +this.pumpHourStart.value,
|
||||
pump_hour_end: +this.pumpHourEnd.value,
|
||||
sensor_b: this.sensorBInstalled.checked,
|
||||
@@ -360,19 +387,23 @@ export class PlantView {
|
||||
}
|
||||
|
||||
setDetectionResult(plantResult: DetectionPlant) {
|
||||
console.log("setDetectionResult plantResult: " + plantResult.sensor_a + " " + plantResult.sensor_b)
|
||||
const sensorADetected = plantResult.sensor_a !== null;
|
||||
const sensorBDetected = plantResult.sensor_b !== null;
|
||||
console.log("setDetectionResult plantResult: a=" + plantResult.sensor_a + " b=" + plantResult.sensor_b);
|
||||
var changed = false;
|
||||
if (this.sensorAInstalled.checked != plantResult.sensor_a) {
|
||||
if (this.sensorAInstalled.checked != sensorADetected) {
|
||||
changed = true;
|
||||
this.sensorAInstalled.checked = plantResult.sensor_a;
|
||||
this.sensorAInstalled.checked = sensorADetected;
|
||||
}
|
||||
if (this.sensorBInstalled.checked != plantResult.sensor_b) {
|
||||
if (this.sensorBInstalled.checked != sensorBDetected) {
|
||||
changed = true;
|
||||
this.sensorBInstalled.checked = plantResult.sensor_b;
|
||||
this.sensorBInstalled.checked = sensorBDetected;
|
||||
}
|
||||
if (changed) {
|
||||
this.controller.configChanged();
|
||||
}
|
||||
|
||||
this.sensorAFwBuild.innerText = formatBuildMinutes(plantResult.sensor_a);
|
||||
this.sensorBFwBuild.innerText = formatBuildMinutes(plantResult.sensor_b);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ pub mod id {
|
||||
// Message group base offsets relative to SENSOR_BASE_ADDRESS
|
||||
pub const MOISTURE_DATA_OFFSET: u16 = 0; // periodic data from sensor (sensor -> controller)
|
||||
pub const IDENTIFY_CMD_OFFSET: u16 = 32; // identify LED command (controller -> sensor)
|
||||
pub const FIRMWARE_BUILD_OFFSET: u16 = 64; // firmware build timestamp (sensor -> controller, sent after identify)
|
||||
|
||||
#[inline]
|
||||
pub const fn plant_id(message_type_offset: u16, sensor: SensorSlot, plant: u16) -> u16 {
|
||||
@@ -55,8 +56,9 @@ pub mod id {
|
||||
/// Kinds of message spaces recognized by the addressing scheme.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MessageKind {
|
||||
MoistureData, // sensor -> controller
|
||||
IdentifyCmd, // controller -> sensor
|
||||
MoistureData, // sensor -> controller
|
||||
IdentifyCmd, // controller -> sensor
|
||||
FirmwareBuild, // sensor -> controller, sent after receiving identify cmd
|
||||
}
|
||||
|
||||
/// Try to classify a received 11-bit standard ID into a known message kind and extract plant and sensor slot.
|
||||
@@ -93,6 +95,9 @@ pub mod id {
|
||||
if let Some((plant, slot)) = decode_in_group(rel, IDENTIFY_CMD_OFFSET) {
|
||||
return Some((MessageKind::IdentifyCmd, plant, slot));
|
||||
}
|
||||
if let Some((plant, slot)) = decode_in_group(rel, FIRMWARE_BUILD_OFFSET) {
|
||||
return Some((MessageKind::FirmwareBuild, plant, slot));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -3,39 +3,55 @@ title: "BatteryManagement"
|
||||
date: 2025-01-27
|
||||
draft: false
|
||||
description: "a description"
|
||||
tags: ["battery", "bq34z100"]
|
||||
tags: ["battery", "bms"]
|
||||
---
|
||||
# Battery Management Module
|
||||
The project contains an additional companion board (Fuel Gauge), with a bq34z100 battery management IC.
|
||||
|
||||
It allows to track the health and charge for an external battery and is supposed to be soldered directly to the battery.
|
||||
The MainBoard contains a connector for power, and additionally a two-pin I2C bus to communicate with the Battery Management module.
|
||||
The PlantCtrl system uses an external **Battery Management System (BMS)** board that connects to the MainBoard. This module monitors battery voltage, current, and health metrics and communicates with the ESP32-C6 via I2C.
|
||||
|
||||
<!-- TODO: Add photo of the new modular Battery Management board -->
|
||||
|
||||
# Setup
|
||||
{{< alert >}}
|
||||
A protected Battery is required. There is only a very simplistic output voltage adjustment for the MPPT system and no charge termination. It is expected that the battery itself protects against overcharging and deep discharges!
|
||||
The open-bms is a custom battery management board designed for this project. It uses a CH32V203 microcontroller to handle battery monitoring and protection. The older bq34z100-based battery management board is deprecated and located in the `__Legay_Unused` folder.
|
||||
{{< /alert >}}
|
||||
* BatteryManagement is purely optional, but recommended for solar power.
|
||||
* If available it will be used for an extended low power deep sleep in case of critical charge.
|
||||
* If available it will also be used, to reduce the nightlight, if the charge drops to a predefined level, so the nightlight cannot drain to much battery
|
||||
* If available, all relevant battery metrics will be published via mqtt
|
||||
|
||||
Currently the setup requires a custom Ev2400 flasher and the properitary windows software from texas instruments.
|
||||
{{< alert >}}
|
||||
Before soldering to the battery
|
||||
{{< /alert >}}
|
||||
1. The voltage devider high side must be bridged, while being connected to the computer and being supplied with around 4.2 V from the battery solder leads.
|
||||
2. Then the data/register for low voltage flash write protection should be set to 0V, as else with the voltage divider and no further configuration, the IC will refuse all write requests.
|
||||
3. After this the supplied golden image can be used, it will setup the battery for 6Ah and a 4S lifepo. Different values can be adjusted after this to the users liking.
|
||||
## Hardware
|
||||
|
||||
The Battery Management Board features:
|
||||
* CH32V203 RISC-V microcontroller for battery monitoring
|
||||
* I2C interface for communication with the MainBoard
|
||||
* Battery voltage and current sensing
|
||||
|
||||
{{< alert >}}
|
||||
The main board, does not care or process any of the charge discharge limits that can be set. Ensure that the battery can supply enough current as well as accept a 2.4A charging current from the MPPT system.
|
||||
The open-bms board does not use the bq34z100 fuel gauge IC. That component was used in an older legacy design now located in the `__Legay_Unused` folder.
|
||||
{{< /alert >}}
|
||||
|
||||
The golden image sets the statups led up, to be in blinky mode. one very long interval means, that the battery is pretty much full. A few very short flashes mean that the battery is nearly empty. No light means, that the battery is in discharge protection and shut down.
|
||||
## Integration with MainBoard
|
||||
|
||||
If the red error led lights, something is wrong with the battery. This can be abnormal voltages or a very low health state.
|
||||
The battery management board:
|
||||
* Connects to the MainBoard via a two-pin I2C bus
|
||||
* Provides power connection to the battery
|
||||
* Reports battery metrics via MQTT (if configured)
|
||||
|
||||
# Todo?
|
||||
If the battery reports that no discharging should occure, report this and then shutdown without using pumps
|
||||
## Usage
|
||||
|
||||
* If available, the system will use battery metrics for deep sleep management when charge is critical
|
||||
* The nightlight can be automatically disabled if battery level drops below a predefined threshold
|
||||
* All battery metrics are published via MQTT when configured
|
||||
* The system includes safety mechanisms to prevent overcharging and deep discharges through the battery's built-in protection circuitry
|
||||
|
||||
## Safety Notes
|
||||
|
||||
{{< alert >}}
|
||||
The system requires a battery with built-in protection circuitry. The MPPT system does not include charge termination or overcharge protection - the battery itself must provide these safety features.
|
||||
{{< /alert >}}
|
||||
|
||||
The CH32V203-based BMS monitors battery health and provides status information but does not control the charge/discharge limits. Ensure your battery can handle the maximum charging current from the MPPT system (up to 2.4A).
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Connect Battery:** Connect your protected battery to the BMS board
|
||||
2. **_connect MainBoard:** Connect the Battery Management Board to the MainBoard via the I2C bus connector
|
||||
3. **Power On:** Power on the system and verify communication via MQTT
|
||||
|
||||
## Status Indicators
|
||||
|
||||
The BMS board includes status LEDs, they behave like every normal powerbank (1-5 lights, animted if charging)
|
||||
@@ -65,13 +65,9 @@ Software and Hardware may fail: It is your responsibility to ensure that a stuck
|
||||
{{< /alert >}}
|
||||
|
||||
|
||||
# Todo
|
||||
## Flow Sensor
|
||||
There is a input for a flow sensor, currently it is not used as the software is missing.
|
||||
* Allow monitoring if pumps are actually moving water
|
||||
* Allow to set limits for how much ml are allowed additinally to the current time limit per watering run
|
||||
|
||||
|
||||
|
||||
Currently it cannot be set how two sensor should be interpreted and they are only averaged. More complex functions would be nice here, eg. allowing a user settable interpolation (0.8*a+0.2*b)/2 and Min(a,b) as well as max(a,b)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,8 +11,6 @@ tags: ["esp32", "hardware"]
|
||||
<img src="pcb_back.png" class="grid-w50" />
|
||||
{{< /gallery >}}
|
||||
|
||||
<!-- TODO: Add new screenshots of the modular PCB setup -->
|
||||
|
||||
{{< gitea server="https://git.mannheim.ccc.de/" repo="C3MA/PlantCtrl" >}}
|
||||
|
||||
## Modular Design
|
||||
@@ -27,17 +25,25 @@ The system now consists of a **MainBoard** which acts as the controller and seve
|
||||
* **Fully Open Source:** Designed in KiCad
|
||||
|
||||
## Available Modules
|
||||
* **MPPT Charger:** Efficient solar charging for batteries.
|
||||
* **Pump Driver:** High-current outputs for pumps and valves.
|
||||
* **Sensor Interface:** Support for multiple moisture sensors.
|
||||
* **Light Controller:** For LED nightlights or growth lights.
|
||||
* **MPPT Charger:** Efficient solar charging for batteries using CN3795.
|
||||
* **Pump Driver:** High-current outputs (up to 3A) for pumps and valves.
|
||||
* **Sensor Module:** CAN bus-based moisture sensors using CH32V203 microcontroller.
|
||||
* **Battery Management:** External BMS board with CH32V203 for battery monitoring.
|
||||
* **Light Controller:** For LED nightlights or growth lights using AP63200.
|
||||
|
||||
## Sensor Module (CAN bus)
|
||||
The standard sensor module features its own **CH32V203 RISC-V microcontroller**, which handles the measurement of soil moisture and communicates the results back to the MainBoard via the CAN bus.
|
||||
|
||||
* **Capacity:** Supports up to 16 sensors (typically 8 plants with an A and B sensor each).
|
||||
* **Reliability:** Digital communication via CAN bus ensures data integrity even over longer cable runs and in electrically noisy environments.
|
||||
* **Addressing:** The A sensor is always used; the B sensor is optional and suggested for larger planters to provide a better average of the soil moisture.
|
||||
|
||||
## Capabilities
|
||||
* **Moisture Sensors:** Supports multiple capacitive or resistive sensors via expansion modules.
|
||||
* **Moisture Sensors:** Supports multiple capacitive or resistive sensors via CAN bus-based Sensor Modules.
|
||||
* **Pumps/Valves:** Support for multiple independent watering zones.
|
||||
* **Power:**
|
||||
* Solar powered with MPPT
|
||||
* Battery powered with optional Battery Management (Fuel Gauge)
|
||||
* Battery powered with optional Battery Management System (BMS)
|
||||
* Can also be used with a standard power supply (7-24V)
|
||||
* **Efficient Power:** Use of high-efficiency DC-DC converters for 3.3V and peripherals.
|
||||
|
||||
|
||||
@@ -6,9 +6,12 @@ description: "a description"
|
||||
tags: ["firmeware", "upload"]
|
||||
---
|
||||
# From Source
|
||||
|
||||
The PlantCtrl firmware is written in Rust for the ESP32-C6 RISC-V microcontroller.
|
||||
|
||||
## Preconditions
|
||||
* **Rust:** Current version of `rustup`.
|
||||
* **ESP32 Toolchain:** `espup` installed and configured.
|
||||
* **ESP32 Toolchain:** `espup` installed and configured for ESP32-C6.
|
||||
* **espflash:** Installed via `cargo install espflash`.
|
||||
* **Node.js:** `npm` installed (for the web interface).
|
||||
|
||||
@@ -37,10 +40,8 @@ You can use the provided bash scripts to automate the build and flash process:
|
||||
You can also update the firmware wirelessly if the system is already running and connected to your network.
|
||||
|
||||
1. Generate the OTA binary:
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
2. The binary will be at `target/riscv32imac-unknown-none-elf/release/plant-ctrl2`.
|
||||
**`./image.sh`**
|
||||
2. The binary will be `image.bin`.
|
||||
3. Open the PlantCtrl web interface in your browser.
|
||||
4. Navigate to the **OTA** section.
|
||||
5. Upload the `plant-ctrl2` file.
|
||||
|
||||
@@ -6,23 +6,26 @@ description: "a description"
|
||||
tags: ["mqtt", "esp"]
|
||||
---
|
||||
# MQTT
|
||||
A configured MQTT server will receive statistical and status data from the controller.
|
||||
|
||||
The PlantCtrl firmware publishes comprehensive status and telemetry data via MQTT when configured. The system uses the **mcutie** crate for Home Assistant integration and standard MQTT topics.
|
||||
|
||||
### Topics
|
||||
|
||||
| Topic | Example | Description |
|
||||
|-------|---------|-------------|
|
||||
| `firmware/address` | `192.168.1.2` | IP address in station mode |
|
||||
| `firmware/state` | `VersionInfo { ... }` | Debug information about the current firmware and OTA slots |
|
||||
| `firmware/state` | `{...}` | Debug information about the current firmware and OTA slots |
|
||||
| `firmware/last_online` | `2025-01-22T08:56:46.664+01:00` | Last time the board was online |
|
||||
| `state` | `online` | Current state of the controller |
|
||||
| `mppt` | `{"current_ma":1200,"voltage_ma":18500}` | MPPT charging metrics |
|
||||
| `battery` | `{"Info":{"voltage_milli_volt":12860,"average_current_milli_ampere":-16,...}}` | Battery health and charge data |
|
||||
| `water` | `{"enough_water":true,"warn_level":false,"left_ml":1337,...}` | Water tank status |
|
||||
| `plant{1-8}` | `{"sensor_a":...,"sensor_b":...,"mode":"TargetMoisture",...}` | Detailed status for each plant slot |
|
||||
| `pump{1-8}` | `{"enabled":true,"pump_ineffective":false,...}` | Metrics for the last pump activity |
|
||||
| `mppt` | `{"current_ma":1200,"voltage_ma":18500}` | MPPT charging metrics (current and voltage from solar panel) |
|
||||
| `battery` | `{"Info":{"voltage_milli_volt":12860,"state_of_charge":95,...}}` | Battery health and charge data from the BMS |
|
||||
| `water` | `{"enough_water":true,"warn_level":false,"left_ml":1337,...}` | Water tank status (level, temperature, frozen detection) |
|
||||
| `plant{1-8}` | `{"sensor_a":...,"sensor_b":...,"mode":"TargetMoisture",...}` | Detailed status for each plant slot including moisture sensors |
|
||||
| `pump{1-8}` | `{"enabled":true,"median_current_ma":500,...}` | Metrics for each pump output |
|
||||
| `light` | `{"enabled":true,"active":true,...}` | Night light status |
|
||||
| `deepsleep` | `night 1h` | Why and how long the ESP will sleep |
|
||||
| `deepsleep` | `night 1h` | Reason and duration of deep sleep |
|
||||
|
||||
Note: The batteries `average_current_milli_ampere` field uses a placeholder value (1337) and should be updated with actual current sensor readings when available.
|
||||
|
||||
### Data Structures
|
||||
|
||||
@@ -39,14 +42,15 @@ Contains a debug dump of the `VersionInfo` struct:
|
||||
- `voltage_ma`: Solar panel voltage in mV
|
||||
|
||||
#### Battery (`battery`)
|
||||
Can be `"Unknown"` or an `Info` object:
|
||||
- `voltage_milli_volt`: Battery voltage
|
||||
- `average_current_milli_ampere`: Current draw/charge
|
||||
- `design_milli_ampere_hour`: Battery capacity
|
||||
- `remaining_milli_ampere_hour`: Remaining capacity
|
||||
Can be `"Unknown"` or an `Info` object. The battery data comes from a custom BMS (Battery Management System) board that uses the CH32V203 microcontroller with I2C communication.
|
||||
|
||||
- `voltage_milli_volt`: Battery voltage in millivolts
|
||||
- `average_current_milli_ampere`: Current draw/charge in milliamperes (placeholder: 1337)
|
||||
- `design_milli_ampere_hour`: Battery design capacity in milliampere-hours
|
||||
- `remaining_milli_ampere_hour`: Remaining capacity in milliampere-hours
|
||||
- `state_of_charge`: Charge percentage (0-100)
|
||||
- `state_of_health`: Health percentage (0-100)
|
||||
- `temperature`: Temperature in degrees Celsius
|
||||
- `state_of_health`: Health percentage (0-100) based onLifetime capacity vs design capacity
|
||||
- `temperature`: Battery temperature in degrees Celsius
|
||||
|
||||
#### Water (`water`)
|
||||
- `enough_water`: Boolean, true if level is above empty threshold
|
||||
|
||||
@@ -6,9 +6,9 @@ description: "How to compile the project"
|
||||
tags: ["clone", "compile"]
|
||||
---
|
||||
# Preconditions:
|
||||
* **Rust:** `rustup` installed.
|
||||
* **ESP32 Toolchain:** `espup` installed.
|
||||
* **Build Utilities:** `ldproxy` and `espflash` installed.
|
||||
* **Rust:** `rustup` installed with the Rust toolchain.
|
||||
* **ESP32 Toolchain:** `espup` installed for ESP32 support.
|
||||
* **Build Utilities:** `ldproxy` and `espflash` installed via cargo.
|
||||
* **Node.js:** `npm` installed (for the web interface).
|
||||
|
||||
# Cloning the Repository
|
||||
@@ -19,24 +19,16 @@ cd PlantCtrl/Software/MainBoard/rust
|
||||
```
|
||||
|
||||
# Toolchain Setup
|
||||
1. **Install Rust:** If not already done, visit [rustup.rs](https://rustup.rs/).
|
||||
2. **Install ldproxy:**
|
||||
|
||||
The project uses Rust with ESP32-C6 support. The toolchain setup involves installing the necessary components:
|
||||
|
||||
1. **Rust Toolchain:**
|
||||
```bash
|
||||
cargo install ldproxy
|
||||
```
|
||||
3. **Install espup:**
|
||||
```bash
|
||||
cargo install espup
|
||||
```
|
||||
4. **Install ESP toolchain:**
|
||||
```bash
|
||||
espup install
|
||||
```
|
||||
5. **Install espflash:**
|
||||
```bash
|
||||
cargo install espflash
|
||||
rustup toolchain install stable
|
||||
rustup default stable
|
||||
```
|
||||
|
||||
|
||||
# Building the Web Interface
|
||||
The configuration website is built using TypeScript and Webpack, then embedded into the Rust binary.
|
||||
```bash
|
||||
@@ -46,14 +38,7 @@ npx webpack
|
||||
cd ..
|
||||
```
|
||||
|
||||
# Compiling the Firmware
|
||||
Build the project using Cargo:
|
||||
```bash
|
||||
cargo build --release
|
||||
```
|
||||
The resulting binary will be located in `target/riscv32imac-unknown-none-elf/release/plant-ctrl2`.
|
||||
|
||||
# Using Build Scripts
|
||||
# Compiling the Firmware using Build Scripts
|
||||
To simplify the process, several bash scripts are provided in the `Software/MainBoard/rust` directory:
|
||||
|
||||
* **`image_build.sh`**: Automatically builds the web interface, compiles the Rust firmware in release mode, and creates a flashable `image.bin`.
|
||||
|
||||
Reference in New Issue
Block a user