Esp home with DHT11, relay and SSD1306

Hi community,
My first post, so, pls be patient, Im familiar with arduinos, esp8266, etc on IDE, but yaml is a shot in my foot.
so far Im spent more than two weeks with home Assistant, currently runing on Raspi 4 and micro sd (today I will install an external ssd 128gb)
My first project is an wemos D1 mini Pro with an DHT11, relay 5v, and one lcd ssd1306 using I2C
The overall idea is a box wich controls an exaust fan over the shower, once the humidity reach certain value the relay goes on and of course turn on the fan, humidity goes down and the fan goes off with an hysteresis of 2 degrees,
So far I was succed creating T & H entities and one relay entity wich doesnt work, I coudn’t control the relay at all, this is the first issue******* SOLVED … the problem was the 3.3v from wemos, Ive put one led in series with the relay command and it works ************
The second problem is to establish a global variable capable to be modified via HA, for instance, default setpoint for relay ON is 60% humidity, but if you want you can change it to 50%
Here part of the code

globals:
   - id: "SetPointHumedad"
     type: int
     restore_value: no
     initial_value: '58'

switch:
  - platform: gpio
    pin: GPIO12
    id: "relay"
    name: "ducha_extractor"
    
sensor:
  - platform: dht
    model: "DHT11"
    pin: D3
    temperature:
     id: "temperatura"
     name: "ducha_temperature"
    humidity:
     id: "humedad"
     name: "ducha_humidity"
    
     on_value_range:
      - above: 60.0
        then:
          - switch.turn_on: "relay"
      - below: 58.0
        then:
          - switch.turn_off: "relay"
    update_interval: 10s
    
i2c:
  sda: D2
  scl: D1

font:
  - file: 'NunitoSans-Regular.ttf'
    id: font1
    size: 17
    
  - file: 'NunitoSans-Regular.ttf'    
    id: font2
    size: 22
    
  - file: 'NunitoSans-Regular.ttf'   
    id: font3
    size: 40   

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperatura).state, id(humedad).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humedad"); 
     it.printf(80, 20, id(font3), "60"); ### harcoded just to see how it looks ###


INFO Reading configuration /config/esphome/ducha-suite.yaml...
INFO Starting log output from 192.168.68.110 using esphome API
INFO Successfully connected to 192.168.68.110
[13:58:14][I][app:102]: ESPHome version 2022.5.1 compiled on Jun 14 2022, 12:07:24
[13:58:14][C][wifi:491]: WiFi:
[13:58:14][C][wifi:353]:   Local MAC: CC:50:E3:63:70:48
[13:58:14][C][wifi:354]:   SSID: [redacted]
[13:58:14][C][wifi:355]:   IP Address: 192.168.68.110
[13:58:14][C][wifi:356]:   BSSID: [redacted]
[13:58:14][C][wifi:358]:   Hostname: 'ducha-suite'
[13:58:14][C][wifi:360]:   Signal strength: -70 dB ▂▄▆█
[13:58:14][C][wifi:364]:   Channel: 8
[13:58:14][C][wifi:365]:   Subnet: 255.255.255.0
[13:58:14][C][wifi:366]:   Gateway: 192.168.68.1
[13:58:14][C][wifi:367]:   DNS1: 0.0.0.0
[13:58:14][C][wifi:368]:   DNS2: 0.0.0.0
[13:58:14][C][logger:274]: Logger:
[13:58:14][C][logger:275]:   Level: DEBUG
[13:58:14][C][logger:276]:   Log Baud Rate: 115200
[13:58:14][C][logger:277]:   Hardware UART: UART0
[13:58:14][C][i2c.arduino:038]: I2C Bus:
[13:58:14][C][i2c.arduino:039]:   SDA Pin: GPIO4
[13:58:14][C][i2c.arduino:040]:   SCL Pin: GPIO5
[13:58:14][C][i2c.arduino:041]:   Frequency: 50000 Hz
[13:58:14][C][i2c.arduino:044]:   Recovery: bus successfully recovered
[13:58:14][I][i2c.arduino:054]: Results from i2c bus scan:
[13:58:14][I][i2c.arduino:060]: Found i2c device at address 0x3C
[13:58:14][C][switch.gpio:050]: GPIO Switch 'ducha_extractor'
[13:58:14][C][switch.gpio:051]:   Pin: GPIO12
[13:58:14][C][switch.gpio:073]:   Restore Mode: Restore (Defaults to OFF)
[13:58:14][C][dht:017]: DHT:
[13:58:14][C][dht:018]:   Pin: GPIO0
[13:58:14][C][dht:022]:   Model: DHT11
[13:58:14][C][dht:027]:   Update Interval: 10.0s
[13:58:14][C][dht:029]:   Temperature 'ducha_temperature'
[13:58:14][C][dht:029]:     Device Class: 'temperature'
[13:58:14][C][dht:029]:     State Class: 'measurement'
[13:58:14][C][dht:029]:     Unit of Measurement: '°C'
[13:58:14][C][dht:029]:     Accuracy Decimals: 1
[13:58:14][C][dht:030]:   Humidity 'ducha_humidity'
[13:58:14][C][dht:030]:     Device Class: 'humidity'
[13:58:14][C][dht:030]:     State Class: 'measurement'
[13:58:14][C][dht:030]:     Unit of Measurement: '%'
[13:58:14][C][dht:030]:     Accuracy Decimals: 0
[13:58:14][C][ssd1306_i2c:023]: I2C SSD1306
[13:58:14][C][ssd1306_i2c:023]:   Rotations: 0 °
[13:58:14][C][ssd1306_i2c:023]:   Dimensions: 128px x 64px
[13:58:14][C][ssd1306_i2c:024]:   Address: 0x3C
[13:58:14][C][ssd1306_i2c:025]:   Model: SSD1306 128x64
[13:58:14][C][ssd1306_i2c:027]:   External VCC: NO
[13:58:14][C][ssd1306_i2c:028]:   Flip X: YES
[13:58:14][C][ssd1306_i2c:029]:   Flip Y: YES
[13:58:14][C][ssd1306_i2c:030]:   Offset X: 0
[13:58:14][C][ssd1306_i2c:031]:   Offset Y: 0
[13:58:14][C][ssd1306_i2c:032]:   Inverted Color: NO
[13:58:14][C][ssd1306_i2c:033]:   Update Interval: 1.0s
[13:58:14][C][captive_portal:088]: Captive Portal:
[13:58:14][C][mdns:084]: mDNS:
[13:58:14][C][mdns:085]:   Hostname: ducha-suite
[13:58:15][C][ota:085]: Over-The-Air Updates:
[13:58:15][C][ota:086]:   Address: 192.168.68.110:8266
[13:58:15][C][ota:089]:   Using Password.
[13:58:15][C][api:138]: API Server:
[13:58:15][C][api:139]:   Address: 192.168.68.110:6053
[13:58:15][C][api:141]:   Using noise encryption: YES
[13:58:21][D][dht:048]: Got Temperature=25.9°C Humidity=42.0%
[13:58:21][D][sensor:124]: 'ducha_temperature': Sending state 25.90000 °C with 1 decimals of accuracy
[13:58:21][D][sensor:124]: 'ducha_humidity': Sending state 42.00000 % with 0 decimals of accuracy
[13:58:28][D][switch:013]: 'ducha_extractor' Turning ON.
[13:58:28][D][switch:037]: 'ducha_extractor': Sending state ON
[13:58:29][D][switch:017]: 'ducha_extractor' Turning OFF.
[13:58:29][D][switch:037]: 'ducha_extractor': Sending state OFF
[13:58:29][D][switch:013]: 'ducha_extractor' Turning ON.
[13:58:29][D][switch:037]: 'ducha_extractor': Sending state ON
[13:58:30][D][esp8266.preferences:236]: Saving preferences to flash...
[13:58:30][D][switch:017]: 'ducha_extractor' Turning OFF.
[13:58:30][D][switch:037]: 'ducha_extractor': Sending state OFF
[13:58:30][D][switch:013]: 'ducha_extractor' Turning ON.
[13:58:30][D][switch:037]: 'ducha_extractor': Sending state ON
[13:58:31][D][dht:048]: Got Temperature=25.9°C Humidity=42.0%
[13:58:31][D][sensor:124]: 'ducha_temperature': Sending state 25.90000 °C with 1 decimals of accuracy
[13:58:31][D][sensor:124]: 'ducha_humidity': Sending state 42.00000 % with 0 decimals of accuracy
[13:58:31][D][switch:017]: 'ducha_extractor' Turning OFF.
[13:58:31][D][switch:037]: 'ducha_extractor': Sending state OFF
[13:58:31][D][switch:013]: 'ducha_extractor' Turning ON.
[13:58:31][D][switch:037]: 'ducha_extractor': Sending state ON
[13:58:32][D][switch:017]: 'ducha_extractor' Turning OFF.
[13:58:32][D][switch:037]: 'ducha_extractor': Sending state OFF
[13:58:41][D][dht:048]: Got Temperature=25.8°C Humidity=42.0%
[13:58:41][D][sensor:124]: 'ducha_temperature': Sending state 25.80000 °C with 1 decimals of accuracy
[13:58:41][D][sensor:124]: 'ducha_humidity': Sending state 42.00000 % with 0 decimals of accuracy

Thanks in advance
Gus

Anyone ? :slight_smile:

I don’t have an answer but rather a question since I’m also trying to hook up a SSD1306. Any chance you could share your wiring specs? I think I’ve connected like you have but nothing happens.

For the first question, which appears to be “why doesn’t my relay work?”, we’d need to see wiring photos or diagrams.

For the second question: Home Assistant Sensor — ESPHome

I use this to control an OLED display brightness based on an HA sensor taking into account ambient light levels and house occupancy:

sensor:
  - platform: homeassistant
    name: "Screen brightness"
    entity_id: sensor.hall_screen_brightness
    id: screen_bright
    internal: true
    on_value:
      then:
        - lambda: |-
            id(the_display).set_contrast(id(screen_bright).state);
        - logger.log:
            format: "Setting display contrast to %.2f."
            args: ['id(screen_bright).state']

display:
  - platform: ssd1306_i2c
    model: "SH1106 128x64"
    address: 0x3C
    id: the_display

was the relay itself… and the wemos output voltage, solved puting a led in series with relay command

That sounds like more of a lucky bodge than a fix.

1 Like
Wemos Di mini Pro	        SSD1306
GND	                        GND
5V	                        VCC
D2	                        SDA
D1	                        SCK
	
	                        DHT11
GND	                        GND
3.3V	                    VCC
D3	                        DATA
	
	                        RELAY
GND	                        GND
5V	                        VCC
D6                          IN
(in my case I have 
to put a Led in series 
between D6 and Relay input 
to make it work)	        

1 Like

Here is the code (not perfect almost shure…) :slight_smile:

esphome:
  name: ducha-suite
  platform: esp8266
  board: d1_mini_pro
  esp8266_restore_from_flash: true


# Enable logging
logger:

# Enable Home Assistant API
api:
 encryption:
    key: "Zt9yjmP3pQLGc17g28gsCrmgDvMeartzwc3YeWcYTU8="
    
ota:

wifi:
  ssid: USERNAME of your WIFI
  password: PASSWORD of your WIFI
  use_address: 192.168.68.150
  
  manual_ip:
    static_ip: 192.168.68.150
    gateway: 192.168.68.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esphome-Web-637048"
    password: "I7XxZFz2PIZO"

captive_portal:
    
switch:
  - platform: gpio
    pin: D6
    id: "relay"
    name: "shower_extractor"
    inverted: true
    
sensor:
  - platform: dht
    model: "DHT11"
    pin: D3
    temperature:
     id: "temperature"
     name: "shower_temperature"
    humidity:
     id: "humidity"
     name: "shower_humidity"
    
     on_value_range:
      - above: 60.0
        then:
          - switch.turn_on: "relay"
      - below: 58.0
        then:
          - switch.turn_off: "relay"
    update_interval: 10s

i2c:
  sda: D2
  scl: D1

font:
  - file: 'NunitoSans-Regular.ttf'
    id: font1
    size: 17
    
  - file: 'NunitoSans-Regular.ttf'    
    id: font2
    size: 22
    
  - file: 'NunitoSans-Regular.ttf'   
    id: font3
    size: 40   

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
     it.printf(0, 0, id(font2), "%.1f°C  %.f%%", id(temperature).state, id(humidity).state);
     it.printf(0, 25, id(font1), "Setpoint"); 
     it.printf(0, 42, id(font1), "Humidity"); 
     it.printf(80, 20, id(font3), "60"); ##### 60 is the desired setpoint working on it... I have put it just to see how it looks###  
1 Like

I didn’t realize the was a circiut problem, I use normally Arduino Nano and wemos d1 mini (actually I’ve used wemos & 4 relays PCB without any issue at all) with relays, I was not aware about the output voltages of esp8266 (my fault) I saw that the esp logs were ok, but the relay didn’t change status… until Ive read a someone post with almos this issue, in there he suggest to use 1n4148 diode in series with the relay control, so in my case one was not enough, so Ive used a led :wink:
It was a shot in the air at the time. And yes was a lucky shot. Now I have variable problem… will see…