Hello, I am working on a project that is in charge of measuring temperature and sending values via BLE. I am working with the DS18B20 sensor and I have helped myself a lot with the examples that Zephyr offers.
I have 2 devices connected to one One-wire bus, and in order to receive values from each sensor, I have to manually enter their ROM addresses.
This manual entry is inefficient and I would like to make a "ROM finder" that would at least print the ROM addresses of devices connected to the bus.
J3 and J4 represent the header on which the one-wire is connected and with the help of GPIO 1 and GPIO 3 I am able to read data from the sensor (only if GPIO_0 and GPIO_2 are in the state of logic 1)
This is my device tree:
&arduino_serial {
status = "okay";
w1_0: w1-zephyr-serial-0 {
compatible = "zephyr,w1-serial";
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
ds18b20_0 {
label = "SENSOR_DS18B20_0";
compatible = "maxim,ds18b20";
family-code = <0x28>;
resolution = <12>;
status = "okay";
};
ds18b20_1 {
label = "SENSOR_DS18B20_1";
compatible = "maxim,ds18b20";
family-code = <0x28>;
resolution = <12>;
status = "okay";
};
};
};
#pragma once
#include <zephyr/kernel.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/types.h>
#define MAX_DEVICES 2
void print_rom_address(const struct w1_rom *rom) {
uint64_t rom_value = w1_rom_to_uint64(rom);
LOG_INF("ROM Address: %016llX", rom_value);
}
void scan_ds18b20_sensors(const struct device *w1_dev) {
struct w1_rom rom;
uint8_t last_discrepancy = 0;
int found_devices = 0;
int ret;
if (!w1_dev || !device_is_ready(w1_dev)) {
LOG_ERR("1-Wire device is not ready or found.");
return;
}
LOG_INF("Scanning 1-Wire bus for DS18B20 devices...");
w1_reset_bus(w1_dev);
while (found_devices < MAX_DEVICES) {
ret = w1_search_rom(w1_dev, &rom, &last_discrepancy);
if (ret) {
break;
}
LOG_INF("Found DS18B20 sensor %d:", found_devices + 1);
print_rom_address(&rom);
found_devices++;
}
if (found_devices == 0) {
LOG_WRN("No DS18B20 sensors found.");
} else {
LOG_INF("Total found DS18B20 sensors: %d", found_devices);
}
}
main:
int main() {
while (true) {
const struct device *w1_dev = DEVICE_DT_GET(DT_NODELABEL(w1_0));
if (!device_is_ready(w1_dev)) {
LOG_ERR("1-Wire device not found!");
return;
}
LOG_INF("Starting DS18B20 scanning...");
scan_ds18b20_sensors(w1_dev);
k_sleep(K_SECONDS(1));
}
return 0;
}I am currently receiving invalid addresses that I cannot use.
I will be very happy for any advice. I REALLY can't move on from this….