Dear "JCA34F",
Thanks for your answer, however, I said, I will later include the correct addresses, once this is clarified with the libraries, and the code compiles. I use Arduiono Nano.
Dear "Nick_Pyner",
ONE MILLION THANKS FOR YOUR CONCRETE STATEMENTS: "Use the Miles Burton, because everybody uses that" , and "Do not use additionally the DS18B20.h" library, because its possibly only "crucifying your code." (See #68).
Exactly such someone I needed to say, which is the common direction, the common mainstream what "everybody" uses, and which works reliably. Because there are so many other solutions existing, and how shall I know! Ok, of couse, I remember, you once said it "Use the Hacktronix" tutorial, but you know, this was one opinion of one forum user, and there are many opinions from many users, as you see. But even then, I said, I will follow your advice. And now, it is / was time to do that. So, I followed the tutorial and based on that, my code now is working, and looks like follows:
// This Arduino sketch reads DS18B20 "1-Wire" digital
// temperature sensors.
// Tutorial:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
// OLED-Display
#include <U8x8lib.h>
// 1-wire
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin X on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress S01_ColdIntoPWT = { 0x28, 0x41, 0x6B, 0x07, 0xD6, 0x01, 0x3C, 0xAA }; // S01_ColdIntoPWT = TempSensors_Temps[0]
DeviceAddress S02_ColdFromPWT = { 0x28, 0x4F, 0xEA, 0x07, 0xD6, 0x01, 0x3C, 0xBF }; // S02_ColdFromPWT = TempSensors_Temps[1]
DeviceAddress S03_HotFromCompr = { 0x28, 0xCF, 0xC2, 0x07, 0xD6, 0x01, 0x3C, 0x93 }; // S03_HotFromCompr = TempSensors_Temps[2]
DeviceAddress S04_HotFromPWT = { 0x28, 0xD4, 0x45, 0x07, 0xD6, 0x01, 0x3C, 0xAA }; // S04_HotFromPWT = TempSensors_Temps[3]
DeviceAddress S05_AmbientOfUnit = { 0x28, 0xC9, 0x05, 0x07, 0xD6, 0x01, 0x3C, 0x52 }; // S05_AmbientOfUnit = TempSensors_Temps[4]
DeviceAddress S06_InsideFluid = { 0x28, 0xAA, 0xD5, 0xAC, 0x1D, 0x13, 0x02, 0xE5 }; // S06_InsideFluid = TempSensors_Temps[5]
DeviceAddress S07_CoolBoxS01 = { 0x28, 0xD4, 0xD1, 0xEA, 0x32, 0x14, 0x01, 0xDF }; // S07_CoolBoxS01 = TempSensors_Temps[6]
DeviceAddress S08_CoolBoxS02 = { 0x28, 0xAA, 0xC3, 0xDB, 0x1D, 0x13, 0x02, 0x2B }; // S08_CoolBoxS02 = TempSensors_Temps[7]
static float TempSensors_Temps[8]; // Array which holds the current temperatures of all 1-wire temperature sensors
/****************** Constructors ******************************************************/
// OLED-Display
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE); // Please update the pin numbers according to your setup. Use U8X8_PIN_NONE if the reset pin is not connected
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 12 bit
sensors.setResolution(S01_ColdIntoPWT, 12);
sensors.setResolution(S02_ColdFromPWT, 12);
sensors.setResolution(S03_HotFromCompr, 12);
sensors.setResolution(S04_HotFromPWT, 12);
sensors.setResolution(S05_AmbientOfUnit, 12);
sensors.setResolution(S06_InsideFluid, 12);
sensors.setResolution(S07_CoolBoxS01, 12);
sensors.setResolution(S08_CoolBoxS02, 12);
// OLED-Display
u8x8.begin();
u8x8.setPowerSave(0);
// Write to OLED-Display
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.drawString(0,0,"Starting up...");
u8x8.refreshDisplay(); // only required for SSD1606/7
// Show setup display 2 seconds long
delay(2000);
// Clear display in order that some remaining characters are not displayed during the loop. E.g. If here in setup it says "Temperatures:" and later in the loop just say "Temps:" in the same line, that it doesn't write: "Temps:atures:"
u8x8.clear();
}
float printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
return(tempC);
}
void loop(void)
{
delay(2000);
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
Serial.print("S01_ColdIntoPWT: ");
printTemperature(S01_ColdIntoPWT);
Serial.print("\n\r");
Serial.print("S02_ColdFromPWT: ");
printTemperature(S02_ColdFromPWT);
Serial.print("\n\r");
Serial.print("S03_HotFromCompr: ");
printTemperature(S03_HotFromCompr);
Serial.print("\n\r");
Serial.print("S04_HotFromPWT: ");
printTemperature(S04_HotFromPWT);
Serial.print("\n\r");
Serial.print("S05_AmbientOfUnit: ");
printTemperature(S05_AmbientOfUnit);
Serial.print("\n\r");
Serial.print("S06_InsideFluid: ");
printTemperature(S06_InsideFluid);
Serial.print("\n\r");
Serial.print("S07_CoolBoxS01: ");
printTemperature(S07_CoolBoxS01);
Serial.print("\n\r");
Serial.print("S08_CoolBoxS02: ");
printTemperature(S08_CoolBoxS02);
Serial.print("\n\r\n\r");
// Get the temperatures into a variable
TempSensors_Temps[0] = printTemperature(S01_ColdIntoPWT);
TempSensors_Temps[1] = printTemperature(S02_ColdFromPWT);
TempSensors_Temps[2] = printTemperature(S03_HotFromCompr);
TempSensors_Temps[3] = printTemperature(S04_HotFromPWT);
TempSensors_Temps[4] = printTemperature(S05_AmbientOfUnit);
TempSensors_Temps[5] = printTemperature(S06_InsideFluid);
TempSensors_Temps[6] = printTemperature(S07_CoolBoxS01);
TempSensors_Temps[7] = printTemperature(S08_CoolBoxS02);
// Write temperatures to OLED-Display
u8x8.drawString(0, 0, "CI"); // Cold Into the plate heat exchanger
u8x8.drawString(0, 1, "CO"); // Cold Out of the plate heat exchanger
u8x8.drawString(0, 2, "HC"); // Hot out of the Compressor
u8x8.drawString(0, 3, "HO"); // Hot Out of the plate heat exchanger
u8x8.drawString(0, 4, "AT"); // Ambient Temperature
u8x8.drawString(0, 5, "IF"); // Inside Fluid
u8x8.drawString(0, 6, "S1"); // CoolBox inside Sensor 01
u8x8.drawString(0, 7, "S2"); // CoolBox inside Sensor 02
u8x8.setCursor(3,0); // Set cursor to column 3 of display (starting with column "0")
for (uint8_t i = 0; i < 8; i++) {
u8x8.setCursor(3,i); // Set cursor to column 3 of display (starting with column "0")
u8x8.print(TempSensors_Temps[i],2);
}
}
I needed to add / change a bit: I introduced a variable "static float TempSensors_Temps[8];" where I can store all the temperatures and finally write it to an OLED display, that I can see and check now, if the microcontroller did not hang up and if there are no "+85" and or "-127" degrees reading, and I don't need to connect to a computer over the serial port each time. So I started it now, and will see, if tomorrow the thing is still alive. And I changed one subprogram from original to "float printTemperature(DeviceAddress deviceAddress)" and included "return(tempC);" Ah, and yes,and I am sorry, I recently wrote "I have 7 sensors" but actually I have 8 (from 0 to 7), so sorry for any confusion caused, this was a mistake.
About your comment about the 2nd line: If everything is working fine, I do not need the 2nd GPIO 1-wire bus line, just in case it's not, I found it a good idea of "zbylan" to split it into 2 groups.
Since programming is not my strength, I hope, I did these small modifications correctly? Is it ok? I know, it can be programmed more elegantly, but I mean now generally with the data types, or that I did not introduce a line, where the code could hang up?
And a question remains: I let now the "2s" delay from the tutorial, but for other reasons I need to go to the "milis" with loop time "1s". Will the sensors with this above code now be read asynchronously and when the conversion is finished, just write their temperatures in the next loop cycle? That would be fine for me. Important is not to block / extend the 1s loop program, because the flow meter depends on correct 1s timing. And I have to have enough time margin since other things shall be included (I2C handling, etc.).
THANK YOU SO MUCH AGAIN, "Nick_Pyner" for clearly stating this what to use, what not to use. This is helping me really further.
Bye,
bernd2700