// nezapomen nadefinovat datový PIN pro DS18B20 !!! //Last edit: 2016-10-07 #include #include #include #include #include //////////////////////////////////////////////////////////////////////////////// #define ONE_WIRE_BUS 7 #define BAUD_RATE 115200 //------------------------------ define displays ------------------------------- // 8x1 8x2 16x1 16x2 16x4 20x2 20x4 #define WIDTHSS 20 #define HEIGHTSS 4 #define WIDTHMJ 20 #define HEIGHTMJ 4 //////////////////////////////////////////////////////////////////////////////// #define SS_I2C_ADDR 0x27 // define your I2C address of PCF8574A #define SS_BACKLIGHT_PIN 3 #define SS_En_pin 2 #define SS_Rw_pin 1 #define SS_Rs_pin 0 #define SS_D4_pin 4 #define SS_D5_pin 5 #define SS_D6_pin 6 #define SS_D7_pin 7 LiquidCrystal_I2C sstwilcd(SS_I2C_ADDR, SS_En_pin, SS_Rw_pin, SS_Rs_pin, SS_D4_pin, SS_D5_pin, SS_D6_pin, SS_D7_pin, SS_BACKLIGHT_PIN, POSITIVE); #define MJKDZ_I2C_ADDR 0x20 // define your I2C address of PCF8574x #define MJKDZ_BACKLIGHT_PIN 7 #define MJKDZ_En_pin 4 #define MJKDZ_Rw_pin 5 #define MJKDZ_Rs_pin 6 #define MJKDZ_D4_pin 0 #define MJKDZ_D5_pin 1 #define MJKDZ_D6_pin 2 #define MJKDZ_D7_pin 3 LiquidCrystal_I2C mjtwilcd(MJKDZ_I2C_ADDR, MJKDZ_En_pin, MJKDZ_Rw_pin, MJKDZ_Rs_pin, MJKDZ_D4_pin, MJKDZ_D5_pin, MJKDZ_D6_pin, MJKDZ_D7_pin, MJKDZ_BACKLIGHT_PIN, NEGATIVE); //////////////////////////////////////////////////////////////////////////////// OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); #if !defined(DEVICE_DISCONNECTED) #define DEVICE_DISCONNECTED -127 #endif #define TEMPERATURE_PRECISION 12 //#define TEMPERATURE_PRECISION 11 //#define TEMPERATURE_PRECISION 10 //#define TEMPERATURE_PRECISION 9 #define MAX_SENSORS 10 DeviceAddress Thermometer[MAX_SENSORS]; float Temperature[MAX_SENSORS] = { DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED, DEVICE_DISCONNECTED }; byte numberOfDevices; byte Limit; //cislice ve tvaru "000,00" #define FLOAT_A 3 #define FLOAT_B 2 //////////////////////////////////////////////////////////////////////////////// bool sstwilcdispresent = false; bool mjtwilcdispresent = false; //////////////////////////////////////////////////////////////////////////////// byte customChar_je[8] = { 0b01010, 0b00100, 0b01110, 0b10001, 0b11111, 0b10000, 0b01111, 0b00000 }; byte customChar_s[8] = { 0b01010, 0b00100, 0b01110, 0b10000, 0b01110, 0b00001, 0b11110, 0b00000 }; byte customChar_c[8] = { 0b01010, 0b00100, 0b01110, 0b10000, 0b10000, 0b10001, 0b01110, 0b00000 }; byte customChar_r[8] = { 0b01010, 0b00100, 0b10110, 0b11001, 0b10000, 0b10000, 0b10000, 0b00000 }; byte customChar_z[8] = { 0b01010, 0b00100, 0b11111, 0b00010, 0b00100, 0b01000, 0b11111, 0b00000 }; byte customChar_y[8] = { 0b00010, 0b00100, 0b10001, 0b10001, 0b01111, 0b00001, 0b01110, 0b00000 }; byte customChar_a[8] = { 0b00010, 0b00100, 0b01110, 0b00001, 0b01111, 0b10001, 0b01111, 0b00000 }; byte customChar_i[8] = { 0b00010, 0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b01110, 0b00000 }; //////////////////////////////////////////////////////////////////////////////// //override printf output int my_putc(char outtext, FILE *t) { Serial.print( outtext ); if ( sstwilcdispresent) { sstwilcd.print( outtext ); }; if ( mjtwilcdispresent) { mjtwilcd.print( outtext ); }; }; //////////////////////////////////////////////////////////////////////////////// void PrintAddress(byte addr, byte details) { if ( ( details == 1 ) or ( details == 5 ) ) { Serial.print(F(" at address 0x")); }; if ( details == 3 ) { Serial.print(F("(0x")); }; if ( details == 4 ) { Serial.print(F("0x")); }; if ( addr < 16 ) { Serial.print("0"); }; String hexString = String(addr, HEX); hexString.toUpperCase(); Serial.print(hexString); if ( details == 1 ) { Serial.print(" ("); Serial.print(addr); Serial.println(")"); }; if ( details == 2 ) { Serial.print(" "); Serial.print(addr); Serial.print(" "); char mychar = (char)addr; Serial.println(mychar); }; if ( details == 3 ) { Serial.println(")"); }; if ( details == 4 ) { Serial.print(" "); }; if ( details == 5 ) { Serial.print(" ("); Serial.print(addr); Serial.print(") "); }; }; //------------------------------------------------------------------------------ void I2C_Scanner() { byte error, address; int nDevices; int aDevices[125]; Serial.println(F("Scanning...\n")); nDevices = 0; for ( address = 0; address < 126; address++ ) { aDevices[address] = 0; }; for ( address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if ( error == 0 ) { Serial.print(F("I2C device found")); PrintAddress(address, 5); aDevices[nDevices] = address; nDevices++; if ( ( address >= 0x20 ) and ( address <= 0x27 ) ) { Serial.print(F("it could be a LCD Backpack (PCF8574x)")); if ( address == 0x20 ) { mjtwilcdispresent = true; }; if ( address == 0x27 ) { sstwilcdispresent = true; }; }; if ( ( address >= 0x38 ) and ( address <= 0x3F ) ) { Serial.print(F("it could be a LCD Backpack (PCF8574A)")); }; if ( ( address >= 0x50 ) and ( address <= 0x57 ) ) { Serial.print(F("it could be a EEPROM AT24Cxxxx")); }; if ( address == 0x68 ) { Serial.print(F("it could be a Real Time Clock DS3231/DS1337/DS1307/...")); }; Serial.println(); } else if ( error == 4 ) { Serial.print(F("Unknown error")); PrintAddress(address, 1); }; Wire.endTransmission(); }; if ( nDevices == 0 ) { Serial.println(F("No I2C devices found\n")); } else { Serial.println(F("\nDone!\n")); }; }; //////////////////////////////////////////////////////////////////////////////// void printOneWireAddress(DeviceAddress deviceAddress) { char str_buffer[255]; sprintf(str_buffer, "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\r\n", deviceAddress[0], deviceAddress[1], deviceAddress[2], deviceAddress[3], deviceAddress[4], deviceAddress[5], deviceAddress[6], deviceAddress[7]); Serial.print(str_buffer); }; //////////////////////////////////////////////////////////////////////////////// void setup() { fdevopen( &my_putc, 0 ); //override printf output, this allows to print to more than one output device Wire.begin(); sensors.begin(); Serial.begin(BAUD_RATE); // RT0/TX0 Serial.println(F("Starting I2C Scanner...\r\n")); I2C_Scanner(); if ( sstwilcdispresent ) { sstwilcd.begin(WIDTHSS, HEIGHTSS); sstwilcd.setBacklight(HIGH); sstwilcd.home(); sstwilcd.clear(); // create a new custom character sstwilcd.createChar(0, customChar_je); sstwilcd.createChar(1, customChar_s); sstwilcd.createChar(2, customChar_c); sstwilcd.createChar(3, customChar_r); sstwilcd.createChar(4, customChar_z); sstwilcd.createChar(5, customChar_y); sstwilcd.createChar(6, customChar_a); sstwilcd.createChar(7, customChar_i); sstwilcd.setCursor(0, 0); sstwilcd.print("Termostat"); sstwilcd.setCursor(0, 1); sstwilcd.print("Inicializace"); sstwilcd.setCursor(0, 2); sstwilcd.print(WIDTHSS); sstwilcd.print("x"); sstwilcd.print(HEIGHTSS); }; if ( mjtwilcdispresent ) { mjtwilcd.begin(WIDTHMJ, HEIGHTMJ); mjtwilcd.setBacklight(HIGH); mjtwilcd.home(); mjtwilcd.clear(); // create a new custom character mjtwilcd.createChar(0, customChar_je); mjtwilcd.createChar(1, customChar_s); mjtwilcd.createChar(2, customChar_c); mjtwilcd.createChar(3, customChar_r); mjtwilcd.createChar(4, customChar_z); mjtwilcd.createChar(5, customChar_y); mjtwilcd.createChar(6, customChar_a); mjtwilcd.createChar(7, customChar_i); mjtwilcd.setCursor(0, 0); mjtwilcd.print("Termostat"); mjtwilcd.setCursor(0, 1); mjtwilcd.print("Inicializace"); mjtwilcd.setCursor(0, 2); mjtwilcd.print(WIDTHMJ); mjtwilcd.print("x"); mjtwilcd.print(HEIGHTMJ); }; Serial.println(F("Looking for OneWire bus devices...")); numberOfDevices = sensors.getDeviceCount(); char strbuffer[255]; sprintf(strbuffer, "Found %d devices.\r\n", numberOfDevices); Serial.print(strbuffer); Serial.print(F("Parasite power is: ")); if (sensors.isParasitePowerMode()) { Serial.print(F("ON")); } else { Serial.print(F("OFF")); }; Serial.print("\r\n"); if (numberOfDevices > MAX_SENSORS) { Limit = MAX_SENSORS; } else { Limit = numberOfDevices; }; for (byte index = 0; index < Limit; index++) { if (sensors.getAddress(Thermometer[index], index)) { sprintf(strbuffer, "Device %d address:\r\n", index); Serial.print(strbuffer); printOneWireAddress(Thermometer[index]); Serial.print(F("Setting resolution to ")); Serial.println(TEMPERATURE_PRECISION, DEC); sensors.setResolution(Thermometer[index], TEMPERATURE_PRECISION); delay(750 / (1 << (12 - TEMPERATURE_PRECISION))); Serial.print(F("Resolution actually set to: ")); Serial.println(sensors.getResolution(Thermometer[index])); } else { Serial.print(F("Found ghost device at ")); Serial.print(String(index)); Serial.print(F(" but could not detect address. Check power and cabling")); }; }; delay(1000); Serial.println(F("\r\nGetting temperatures...")); }; ///////////////////////////////////////////////////////////////////////////////////// void loop() { sensors.requestTemperatures(); //take some time, it causes blink for (byte index = 0; index < Limit; index++) { Temperature[index] = sensors.getTempC(Thermometer[index]); if (Temperature[index] == DEVICE_DISCONNECTED) { Serial.print(F("Error on index: ")); Serial.println(index); } else { char dtostrfbuffer[15]; dtostrf(Temperature[index], FLOAT_A, FLOAT_B, dtostrfbuffer); MyClearScr(); MyPrint(dtostrfbuffer); MyPrintLN(F("\xB0\x43")); //°C }; switch (TEMPERATURE_PRECISION) { case 9: delay(94); break; case 10: delay(188); break; case 11: delay(375); break; case 12: default: delay(750); break; }; }; delay( 5000 ); }; ///////////////////////////////////////////////////////////////////////////////////// void MyPrint(String OutText) { Serial.print( OutText ); if (OutText == "\xB0\x43") { OutText = "\xDF\x43"; }; if ( sstwilcdispresent) { sstwilcd.print( OutText ); }; if ( mjtwilcdispresent) { mjtwilcd.print( OutText ); }; }; void MyPrintLN(String OutTextLN) { MyPrint( OutTextLN ); Serial.println(); }; void MyClearScr() { if ( sstwilcdispresent ) { sstwilcd.home(); sstwilcd.clear(); }; if ( mjtwilcdispresent ) { mjtwilcd.home(); mjtwilcd.clear(); }; }; /////////////////////////////////////////////////////////////////////////////////////