# Complete project details at https://RandomNerdTutorials.com/micropython-hc-sr04-ultrasonic-esp32-esp8266/
from hcsr04 import HCSR04
from machine import Pin, SoftI2C
import ssd1306
import dht 
from time import sleep

# ESP32
sensor1 = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
sensor = dht.DHT22(Pin(4))

# ESP8266
#sensor = HCSR04(trigger_pin=12, echo_pin=14, echo_timeout_us=10000)

oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
temperature = 12.34
temperature_string = str(temperature)

while True:
  try:
    oled.fill(0)
    #oled.show()
    sensor.measure()
    distance = sensor1.distance_cm()
    temp = sensor.temperature()
    hum = sensor.humidity()
    temp_f = temp * (9/5) + 32.0
    print("distance=  ",distance)
    if (distance > 40):
      oled.text("Teplota %3.1f C" %temp, 0, 0);
      oled.show()
    else:
      oled.fill(0)
      oled.show()
  except OSError as e:
      print('Failed to read sensor.')
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND