Ds18b20 1 pin multisensor auto detect LCD2004 Code

User avatar
veeroos
Posts: 1081
Joined: Sun Mar 20, 2022 9:30 am
Location: Głogów
Has thanked: 1 time
Been thanked: 9 times

Post

Code: Select all

#define DIODA_LED 2
#define PRZYCISK_KONFIGURACJA 4
#define PIN_DS18B20 15 // Wspólny pin dla wszystkich czujników DS18B20
#define ilosc_czujnikow 10
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // https://docs.arduino.cc/libraries/liquidcrystal/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/control/button.h>
#include <supla/device/status_led.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/device/supla_ca_cert.h>
#include <supla/sensor/thermometer.h>
#include <supla/events.h>
#include <supla/storage/littlefs_config.h>
#include <supla/storage/eeprom.h>
 
Supla::Eeprom eeprom;
Supla::LittleFsConfig configSupla;
Supla::ESPWifi wifi;
Supla::Device::StatusLed statusLed(DIODA_LED, true);
Supla::EspWebServer suplaServer;
LiquidCrystal_I2C lcd(0x27, 20, 4); 
OneWire oneWire(PIN_DS18B20);
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
uint32_t lastTime = 0;
 
class Czujnik_DS18B20 : public Supla::Sensor::Thermometer {
public:
    Czujnik_DS18B20(DallasTemperature* sensors, int index)
        : sensors(sensors), index(index) {}
    void onInit() override {
        channel.setNewValue(getValue());
    }
    double getValue() {
        sensors->requestTemperatures();
        double temp = sensors->getTempCByIndex(index); // Odczyt temperatury na podstawie indexu
        return temp;
    }
    void iterateAlways() override {
        if (millis() - lastReadTime > 1000) { // Aktualizacja co 1 sekundę
            lastReadTime = millis();
            channel.setNewValue(getValue());
        }
    }
private:
    DallasTemperature* sensors;
    unsigned long lastReadTime;
    int index;
};
Czujnik_DS18B20* Czujnik[ilosc_czujnikow];
 
void setup() {
    Serial.begin(115200);
    Wire.begin(); // Domyślne piny SDA i SCL dla ESP32 (GPIO21 i GPIO22)
    sensors.begin();
    deviceCount = sensors.getDeviceCount();
    lcd.begin(); // Dodano argumenty do inicjalizacji LCD
    lcd.backlight();
    auto konfiguracja = new Supla::Control::Button(PRZYCISK_KONFIGURACJA, true, true);
    konfiguracja->configureAsConfigButton(&SuplaDevice);
    for (int i = 0; i < deviceCount; ++i){
      char bufor[12];
      snprintf(bufor, sizeof(bufor), "DS18B20 - %d", i);
      Czujnik[i] = new Czujnik_DS18B20(&sensors, i);
      Czujnik[i]->setInitialCaption(bufor);
    }
    new Supla::Html::DeviceInfo(&SuplaDevice);
    new Supla::Html::WifiParameters;
    new Supla::Html::ProtocolParameters;
    new Supla::Html::StatusLedParameters;
    SuplaDevice.setName("TERMOMETR WIFI SUPLA");
    SuplaDevice.setSuplaCACert(suplaCACert);
    SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);
    SuplaDevice.begin();
}
 
void loop() {
    SuplaDevice.iterate();
  if (millis() - lastTime > 1000) {
    lastTime = millis();
    lcd.clear(); // Usuwamy poprzednią zawartość
    lcd.setCursor(0, 0); // Pierwsza linia
    lcd.print("TERMOMETR WIFI SUPLA");
 
    for (int i = 0; i < deviceCount; ++i){
      lcd.setCursor(0, i+1);
      lcd.print("temp ");
      lcd.print(i+1);
      lcd.setCursor(13, i+1);
      lcd.print(Czujnik[i]->getValue(), 1);
      lcd.print((char)223); // Symbol stopnia
      lcd.print("C");
    }
  }
}
Jakby się komuś chciało poprawiać program, lub zrobić pożytek i przerobić oficjalne biblioteki to śmiało. Mi działa i nie chce mi się kombinować ;) obsługa do 3 czujników (ograniczenie LCD) jak ktoś wywali z kodu LCD to w sumie może więcej dodać 😁.
Zamel Mew-01, Zamel PEW-01, Zamel SBW-01, Zamel THW-01, Zamel SRW-01, Zamel mSLW-02, Auraton BOX + Indor Sensor, Airly Gate By Veeroos, Zigbee to Supla Gateway + sensors, Zamel SGW-01 and more

https://github.com/v33r005

Return to “Projekty użytkowników”