-
Notifications
You must be signed in to change notification settings - Fork 588
Initial RAK One-Wire implementation for WisMesh Solar Repeater #1847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hb9fxq
wants to merge
3
commits into
meshcore-dev:dev
Choose a base branch
from
hb9fxq:feature/WisMesh-Repeater-RAK9154
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+423
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,272 @@ | ||
| #include "OneWireSensorHub.h" | ||
|
|
||
| #ifdef ENV_INCLUDE_ONEWIRE | ||
|
|
||
| #include <Mesh.h> | ||
|
|
||
| static SoftwareHalfSerial oneWireSerial(ONEWIRE_PIN); | ||
|
|
||
| OneWireSensorHub* OneWireSensorHub::_instance = nullptr; | ||
|
|
||
| void OneWireSensorHub::_onewire_callback(const uint8_t pid, const uint8_t sid, | ||
| const SNHUBAPI_EVT_E eid, uint8_t* msg, uint16_t len) { | ||
| if (_instance) { | ||
| _instance->handleEvent(pid, sid, eid, msg, len); | ||
| } | ||
| } | ||
|
|
||
| bool OneWireSensorHub::begin() { | ||
| _instance = this; | ||
| _found_pid_count = 0; | ||
| _has_voltage = false; | ||
|
|
||
| for (int i = 0; i < ONEWIRE_MAX_PIDS; i++) { | ||
| _found_pids[i] = 0xFF; | ||
| } | ||
|
|
||
| pinMode(WB_IO2, OUTPUT); | ||
| digitalWrite(WB_IO2, HIGH); | ||
| delay(100); | ||
|
|
||
| oneWireSerial.begin(9600); | ||
| RakSNHub_Protocl_API.init(_onewire_callback); | ||
|
|
||
| MESH_DEBUG_PRINTLN("OneWire: Scanning for sensor probes (%d ms)...", ONEWIRE_DISCOVERY_TIMEOUT_MS); | ||
|
|
||
| unsigned long start = millis(); | ||
| unsigned long last_rx = 0; | ||
|
|
||
| while ((millis() - start) < ONEWIRE_DISCOVERY_TIMEOUT_MS) { | ||
| while (oneWireSerial.available()) { | ||
| if (_rxlen < sizeof(_rxbuf)) { | ||
| _rxbuf[_rxlen++] = oneWireSerial.read(); | ||
| } | ||
| last_rx = millis(); | ||
| } | ||
|
|
||
| if (_rxlen > 0 && (millis() - last_rx) >= 10) { | ||
| RakSNHub_Protocl_API.process(_rxbuf, _rxlen); | ||
| _rxlen = 0; | ||
| } | ||
| } | ||
|
|
||
| MESH_DEBUG_PRINTLN("OneWire: Discovery complete. Found %d sensor probe(s)", _found_pid_count); | ||
|
|
||
| if (_found_pid_count == 0) { | ||
| digitalWrite(WB_IO2, LOW); | ||
| return false; | ||
| } | ||
|
|
||
| _next_poll_ms = millis() + 10000; // First request within ~10 seconds only returned 0.0 values. | ||
| _current_poll_idx = 0; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void OneWireSensorHub::loop() { | ||
| if (_found_pid_count == 0) return; | ||
|
|
||
| while (oneWireSerial.available()) { | ||
| if (_rxlen < sizeof(_rxbuf)) { | ||
| _rxbuf[_rxlen++] = oneWireSerial.read(); | ||
| } else { | ||
| break; | ||
| } | ||
| _last_rx_time = millis(); | ||
| } | ||
|
|
||
| if (_rxlen > 0 && (millis() - _last_rx_time) >= 10) { | ||
| RakSNHub_Protocl_API.process(_rxbuf, _rxlen); | ||
| _rxlen = 0; | ||
| } | ||
|
|
||
| if (millis() >= _next_poll_ms) { | ||
| if (_current_poll_idx < _found_pid_count) { | ||
| uint8_t pid = _found_pids[_current_poll_idx]; | ||
| if (pid != 0xFF) { | ||
| RakSNHub_Protocl_API.get.data(pid); | ||
| MESH_DEBUG_PRINTLN("OneWire: Requested data from PID %d", pid); | ||
| } | ||
| _current_poll_idx++; | ||
| } | ||
|
|
||
| if (_current_poll_idx >= _found_pid_count) { | ||
| _current_poll_idx = 0; | ||
| _next_poll_ms = millis() + ONEWIRE_POLL_INTERVAL_MS; | ||
| } else { | ||
| // Stagger between PID requests | ||
| _next_poll_ms = millis() + 500; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void OneWireSensorHub::handleEvent(uint8_t pid, uint8_t sid, SNHUBAPI_EVT_E eid, | ||
| uint8_t* msg, uint16_t len) { | ||
| switch (eid) { | ||
| case SNHUBAPI_EVT_QSEND: | ||
| if (oneWireSerial.write(msg, len) != len) { | ||
| MESH_DEBUG_PRINTLN("OneWire: write failed"); | ||
| } | ||
| break; | ||
|
|
||
| case SNHUBAPI_EVT_ADD_PID: | ||
| registerPid(msg[0]); | ||
| break; | ||
|
|
||
| case SNHUBAPI_EVT_ADD_SID: | ||
| MESH_DEBUG_PRINTLN("OneWire: Added SID 0x%02X for PID %d", msg[0], pid); | ||
| break; | ||
|
|
||
| case SNHUBAPI_EVT_SDATA_REQ: { | ||
| if (len < 2) break; | ||
| uint8_t ipso_type = msg[0]; | ||
| uint16_t val_len = len - 1; | ||
|
|
||
| uint8_t ordered[256]; | ||
| if (val_len > 256) val_len = 256; | ||
| for (uint16_t i = 0; i < val_len; i += 2) { | ||
hb9fxq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (i + 1 < val_len) { | ||
| ordered[i] = msg[1 + i + 1]; | ||
| ordered[i + 1] = msg[1 + i]; | ||
| } else { | ||
| ordered[i] = msg[1 + i]; | ||
| } | ||
| } | ||
| MESH_DEBUG_PRINTLN("OneWire: SDATA_REQ SID=0x%02X IPSO=%d len=%d", sid, ipso_type, val_len); | ||
| parseSensorData(sid, ipso_type, ordered, val_len); | ||
| break; | ||
| } | ||
|
|
||
| case SNHUBAPI_EVT_REPORT: { | ||
| if (len < 2) break; | ||
| uint8_t ipso_type = msg[0]; | ||
| uint16_t val_len = len - 1; | ||
| MESH_DEBUG_PRINTLN("OneWire: REPORT SID=0x%02X IPSO=%d len=%d", sid, ipso_type, val_len); | ||
| parseSensorData(sid, ipso_type, &msg[1], val_len); | ||
| break; | ||
| } | ||
|
|
||
| case SNHUBAPI_EVT_CHKSUM_ERR: | ||
| MESH_DEBUG_PRINTLN("OneWire: Checksum error"); | ||
| break; | ||
|
|
||
| case SNHUBAPI_EVT_SEQ_ERR: | ||
| MESH_DEBUG_PRINTLN("OneWire: Sequence error"); | ||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void OneWireSensorHub::registerPid(uint8_t pid) { | ||
| for (int i = 0; i < ONEWIRE_MAX_PIDS; i++) { | ||
| if (_found_pids[i] == pid) { | ||
| MESH_DEBUG_PRINTLN("OneWire: PID %d already registered", pid); | ||
| return; | ||
| } | ||
| } | ||
| for (int i = 0; i < ONEWIRE_MAX_PIDS; i++) { | ||
| if (_found_pids[i] == 0xFF) { | ||
| _found_pids[i] = pid; | ||
| _found_pid_count++; | ||
| MESH_DEBUG_PRINTLN("OneWire: Registered PID %d (total: %d)", pid, _found_pid_count); | ||
| return; | ||
| } | ||
| } | ||
| MESH_DEBUG_PRINTLN("OneWire: No slots for PID %d", pid); | ||
| } | ||
|
|
||
| void OneWireSensorHub::parseSensorData(uint8_t sid, uint8_t ipso_type, uint8_t* data, uint16_t data_len) { | ||
| switch (ipso_type) { | ||
| case RAK_IPSO_BATTERVALUE: // 116 (3316-3200): battery voltage, 2 bytes, /100 | ||
| case RAK_IPSO_DC_VOLTAGE: { // 186 (3386-3200): DC voltage, 2 bytes, /100 | ||
| if (data_len >= 2) { | ||
| int16_t raw = ((int16_t)data[0] << 8) | (int16_t)data[1]; | ||
| _cached_voltage = (float)raw / 100.0f; | ||
| _has_voltage = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery Voltage = %.2fV (IPSO %d, raw=%d)", _cached_voltage, ipso_type, raw); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case RAK_IPSO_DC_CURRENT: { // 185 (3385-3200): DC current, 2 bytes, raw * 0.01A | ||
| if (data_len >= 2) { | ||
| int16_t raw = ((int16_t)data[0] << 8) | (int16_t)data[1]; | ||
| _cached_current_ma = raw * 10; // convert raw (centiamps) to mA | ||
| _has_current = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery Current = %dmA (IPSO %d, raw=%d)", _cached_current_ma, ipso_type, raw); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case RAK_IPSO_CAPACITY: { // 184 (3384-3200): battery percentage, 1 byte | ||
| if (data_len >= 1) { | ||
| _cached_battery_pct = data[0]; | ||
| if (_cached_battery_pct > 100) _cached_battery_pct = 100; | ||
| _has_battery_pct = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery SOC = %d%% (IPSO %d)", _cached_battery_pct, ipso_type); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case RAK_IPSO_TEMP_SENSOR: { // 103 (3303-3200): temperature, 2 bytes, /10 | ||
| if (data_len >= 2) { | ||
| int16_t raw = ((int16_t)data[0] << 8) | (int16_t)data[1]; | ||
| _cached_temperature = (float)raw / 10.0f; | ||
| _has_temperature = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery Temperature = %.1fC (IPSO %d, raw=%d)", _cached_temperature, ipso_type, raw); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case RAK_IPSO_SSN: { // 126 (3326-3200): serial number, 3 bytes | ||
| if (data_len >= 3) { | ||
| _cached_serial = ((uint32_t)data[0] << 16) | ((uint32_t)data[1] << 8) | data[2]; | ||
| _has_serial = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Serial Number = %06lX (IPSO %d)", (unsigned long)_cached_serial, ipso_type); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case RAK_IPSO_BINARY2BYTE: { // 243 (0xF3): 2-byte binary, SID distinguishes error vs FW version | ||
| if (data_len >= 2) { | ||
| uint16_t val = ((uint16_t)data[0] << 8) | data[1]; | ||
| if (sid == 0x19) { | ||
| _cached_error = val; | ||
| _has_error = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery Error = 0x%04X (IPSO %d, SID 0x%02X)", val, ipso_type, sid); | ||
| } else if (sid == 0x1A) { | ||
| _cached_fw_version = val; | ||
| _has_fw_version = true; | ||
| MESH_DEBUG_PRINTLN("OneWire: Battery FW Version = v%02d.%02d (IPSO %d, SID 0x%02X)", val >> 8, val & 0xFF, ipso_type, sid); | ||
| } else { | ||
| MESH_DEBUG_PRINTLN("OneWire: BINARY2BYTE = 0x%04X (IPSO %d, SID 0x%02X)", val, ipso_type, sid); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| default: | ||
| MESH_DEBUG_PRINTLN("OneWire: Unhandled IPSO %d (len=%d)", ipso_type, data_len); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| bool OneWireSensorHub::hasVoltage() const { return _has_voltage; } | ||
| float OneWireSensorHub::getVoltage() const { return _cached_voltage; } | ||
| bool OneWireSensorHub::hasCurrent() const { return _has_current; } | ||
| float OneWireSensorHub::getCurrent() const { return (float)_cached_current_ma / 1000.0f; } | ||
| bool OneWireSensorHub::hasBatteryPercent() const { return _has_battery_pct; } | ||
| uint8_t OneWireSensorHub::getBatteryPercent() const { return _cached_battery_pct; } | ||
| bool OneWireSensorHub::hasTemperature() const { return _has_temperature; } | ||
| float OneWireSensorHub::getTemperature() const { return _cached_temperature; } | ||
| bool OneWireSensorHub::hasSerialNumber() const { return _has_serial; } | ||
| uint32_t OneWireSensorHub::getSerialNumber() const { return _cached_serial; } | ||
| bool OneWireSensorHub::hasError() const { return _has_error; } | ||
| uint16_t OneWireSensorHub::getError() const { return _cached_error; } | ||
| bool OneWireSensorHub::hasFwVersion() const { return _has_fw_version; } | ||
| uint16_t OneWireSensorHub::getFwVersion() const { return _cached_fw_version; } | ||
| uint8_t OneWireSensorHub::getNumPids() const { return _found_pid_count; } | ||
|
|
||
| #endif // ENV_INCLUDE_ONEWIRE | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.