простое обращение к датчику температуры DS18B20
- Войдите на сайт для отправки комментариев
Ср, 28/03/2018 - 08:03
Тема конечно избитая, но как то не получается просто изменив код указав адрес обратится к конкретному датчику из двух.
Подскажите в чем проблема?
Делаю:
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
//outsideThermometer = { 28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 };
outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };
//oneWire.search(deviceAddress)
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
//if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
//if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// assigns the seconds address found to outsideThermometer
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
из :
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
//outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// assigns the seconds address found to outsideThermometer
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
// show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
Serial.print("Device 1 Address: ");
printAddress(outsideThermometer);
Serial.println();
// set the resolution to 9 bit
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
Serial.print("Device 1 Resolution: ");
Serial.print(sensors.getResolution(outsideThermometer), DEC);
Serial.println();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
Serial.print("Resolution: ");
Serial.print(sensors.getResolution(deviceAddress));
Serial.println();
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
// print the device information
printData(insideThermometer);
printData(outsideThermometer);
}
и не работает.
Есть, кто может помочь в вопросе обращения к датчику по заранее известному адрессу?
Без использования языка С и ему подобных.
Или ссылку на материал укажите, пожалуйста!
Есть, кто может помочь в вопросе обращения к датчику по заранее известному адрессу?
Без использования языка С и ему подобных.
Какой же прикажете использовать - армейский матерный?
А есть способ используя эти строки
08insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 };09outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };и не углублясь в С
обращаться к датчикам.
Почему не работает обращение как сделано выше. Что не хватает?
Почему не работает обращение как сделано выше. Что не хватает?
а где там вообще скетч для обращения по адресу? там какие-то обрывки. приведите полный код
Нет такого способа. То, что вы написали - это ID-датчика. Он должен передаваться в функцию считывания. Это всё описывается на языке Wiring, Wiring есть обсахаренный C++. Круг замкнулся.
полный код это второй. он рабочий.
В него вставляем строки 01-034 вместо строк 037-065 и получаем полный код но не рабтающий.
Может можете код набрасать или сложно будет?
Вон оно чего...
Ну, тада, наверное надо так - 050-051 закомментировать, в районе 015 изобразить следующее:
//DeviceAddress insideThermometer, outsideThermometer; DeviceAddress insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 }, outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };1//DeviceAddress insideThermometer, outsideThermometer;2DeviceAddress insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 },3outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };ругается и не компилирует
1//DeviceAddress insideThermometer, outsideThermometer;2DeviceAddress insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 },3DeviceAddressoutsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };компилирует но не работает
вот так сделайте:
//DeviceAddress insideThermometer, outsideThermometer; DeviceAddress insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 }; DeviceAddress outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 };но работать от этого не начнет - ошибка я вно не в этом.
"Не работает" - в чем это выражается? Адреса датчиков печатает? Разрешение данных?
У меня всё отлично компилируется. А датчиков под рукой нет, так что проверять нечем.
Адреса датчиков печатает.
Но
"Locating devices... 0
Device 0 Resolution 0
Device 1 Resolution 0
и температуры 0С и 32F
Резистор-то поставили на шину?
Конечно.
Ведь 1й полный код работает корректно.
то есть в вашем коде не работает чуть ли не первый же запрос к датчикам, а в исходном коде - все работает? значит накосячили, когда переделывали код.
Выложите именно тот код, на котором пробуете - и который не работает, не надо этих ребусов в стиле "вот в этом коде заменяем строки 32-34 на строки 45-66"....
этот код работает корректно:
#include <OneWire.h> #include <DallasTemperature.h> // Data wire is plugged into port 2 on the Arduino #define ONE_WIRE_BUS 4 #define TEMPERATURE_PRECISION 9 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); // arrays to hold device addresses DeviceAddress insideThermometer, outsideThermometer; void setup(void) { // start serial port Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library Demo"); // Start up the library sensors.begin(); // locate devices on the bus Serial.print("Locating devices..."); Serial.print("Found "); Serial.print(sensors.getDeviceCount(), DEC); Serial.println(" devices."); // report parasite power requirements Serial.print("Parasite power is: "); if (sensors.isParasitePowerMode()) Serial.println("ON"); else Serial.println("off"); // assign address manually. the addresses below will beed to be changed // to valid device addresses on your bus. device address can be retrieved // by using either oneWire.search(deviceAddress) or individually via // sensors.getAddress(deviceAddress, 1 ) //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 }; //outsideThermometer = { 28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 }; // insideThermometer = { 0x28, 0x71, 0xF0, 0xB1, 0x06, 0x00, 0x00, 0xD8 }; // outsideThermometer = { 0x28, 0x07, 0x5F, 0x59, 0x07, 0x00, 0x00, 0x50 }; //oneWire.search(deviceAddress) // search for devices on the bus and assign based on an index. ideally, // you would do this to initially discover addresses on the bus and then // use those addresses and manually assign them (see above) once you know // the devices on your bus (and assuming they don't change). // // method 1: by index if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0"); if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1"); // method 2: search() // search() looks for the next device. Returns 1 if a new address has been // returned. A zero might mean that the bus is shorted, there are no devices, // or you have already retrieved all of them. It might be a good idea to // check the CRC to make sure you didn't get garbage. The order is // deterministic. You will always get the same devices in the same order // // Must be called before search() //oneWire.reset_search(); // assigns the first address found to insideThermometer //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer"); // assigns the seconds address found to outsideThermometer //if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer"); // show the addresses we found on the bus Serial.print("Device 0 Address: "); printAddress(insideThermometer); Serial.println(); Serial.print("Device 1 Address: "); printAddress(outsideThermometer); Serial.println(); // set the resolution to 9 bit sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION); sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION); Serial.print("Device 9 Resolution: "); Serial.print(sensors.getResolution(insideThermometer), DEC); Serial.println(); Serial.print("Device 1 Resolution: "); Serial.print(sensors.getResolution(outsideThermometer), DEC); Serial.println(); } // function to print a device address void printAddress(DeviceAddress deviceAddress) { for (uint8_t i = 0; i < 8; i++) { // zero pad the address if necessary if (deviceAddress[i] < 16) Serial.print("0"); Serial.print(deviceAddress[i], HEX); } } // function to print the temperature for a device void printTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); Serial.print("Temp C: "); Serial.print(tempC); Serial.print(" Temp F: "); Serial.print(DallasTemperature::toFahrenheit(tempC)); } // function to print a device's resolution void printResolution(DeviceAddress deviceAddress) { Serial.print("Resolution: "); Serial.print(sensors.getResolution(deviceAddress)); Serial.println(); } // main function to print information about a device void printData(DeviceAddress deviceAddress) { Serial.print("Device Address: "); printAddress(deviceAddress); Serial.print(" "); printTemperature(deviceAddress); Serial.println(); } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); // print the device information printData(insideThermometer); printData(outsideThermometer); delay(5000); // Пауза между измерениями }переделываем в этот:
Точно работает
Если изменить адресс - то не находит датчик.
СПАСИБО за помощь