from machine import I2C, Pin, ADC
import time
from i2c_lcd import I2cLcd
import dht
from machine import Pin
import ujson
from umqtt.simple import MQTTClient
import network
import math
from time import sleep
# Δηλώνουμε τον αισθητήρα DHT
sensor = dht.DHT22(Pin(15))
# Initialize I2C for the LCD
AddressOfLcd = 0x27
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) # connect scl to GPIO 22, sda to GPIO 21
lcd = I2cLcd(i2c, AddressOfLcd, 2, 16)
# MQTT Server Parameters
MQTT_CLIENT_ID = "Wokwi-GUEST"
MQTT_BROKER = "broker.emqx.io"
MQTT_USER = "Wokwi-GUEST"
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-weather"
BETA = 3950
sensor = ADC(Pin(33))
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
def publish_temperature(temp):
mqtt_client.publish(topic, str(temp))
prev_weather = ""
def blink_led(topic, msg):
if msg.decode() == 'on':
print(led.value)
led.value(1)
elif msg.decode() == 'off':
led.value(0)
print("Hello, ESP32!")
while True:
sensor_ADC_value = sensor.read() # value from the ADC
sensor_value = 5000 * sensor_ADC_value / 4095 # convert ADC value to V
temperature = 94.23 - sensor_value * 25.42 # convert mV to oC according to the LTM70 datasheet (pages 12,13)
temperature = 1 / (math.log(1 / (4095 /sensor_value -1 ))/ BETA + 1/ 298.15) - 273.15
celsius = 1 / (math.log(1 / (4095 /sensor_value -1 ))/ BETA + 1/ 298.15) - 273.15
print("sensor_ADC_value ", sensor_ADC_value)
print("sensor_value ", sensor_value)
print("temperature ", temperature)
lcd.move_to(0,0)
lcd.putstr("Temp: " + str(temperature) + " °C")
time.sleep_ms(1000)