This article recommends using the esp8266 to read temperature and humidity from the DHT11 sensor, the voltage from the LDR sensor, received from the switch, and display via OLED with MicroPython’s Python language, this feature is the dCore-espWST board we are using (Which in the previous article we used the dCore-esp32WST with the same design, but using esp32, but the program code can still be used with the model board) and is a board for use in teaching IoT subjects. The prototype structure of the board is as shown in Figure 1, which is normally used with a battery power supply unit with a solar panel charging circuit.
Circuit diagram
From the dCore-espWST prototype board, there is a circuit as shown in Figure 2. The equipments that are required are:
- esp8266
- DHT11 or DHT22 (4 pins) connect with R-Pull up size 10K
- I2C OLED
- LDR module or LDR with R
- Push button and 10 R for R-pull up
Example Code
An example program is to test the board’s functionality. The working principle is as follows.
- Configure the default settings.
- Display data from sensor to OLED display.
- If the time interval from pressing is within 2 s, the temperature and humidity are displayed in n/a.
- If more than 2 seconds, read the value from DHT11 and display the temperature and humidity reading.
- If the user has pressed SW which has not been pressed before will call step 2.
Code
##################################################################################
# dCore-espWST-test
# (C) 2021, JarutEx
##################################################################################
import gc
import os
import sys
import time
import math
import machine as mc
from machine import Pin,I2C,ADC
import ssd1306
import dht
##################################################################################
gc.enable()
gc.collect()
mc.freq(160000000)
sclPin = Pin(5)
sdaPin = Pin(4)
dValue = ADC(0)
dht11 = dht.DHT11(Pin(13))
sw = Pin(12, Pin.IN, Pin.PULL_UP)
i2c = I2C(scl=sclPin, sda=sdaPin, freq=1000000)
oled_addr = const(0x3c)
oled = ssd1306.SSD1306_I2C(128,64,i2c,oled_addr)
oled.poweron()
t0 = time.ticks_ms()
t1 = time.ticks_ms()
##################################################################################
def showData():
global t0,t1
oled.fill(0)
oled.fill_rect(0,0,128,18,1)
oled.text("dCore",8,4,0)
oled.text("dCore",9,4,0)
oled.text("espWST1",64,4,0)
oled.text("LDR :{}".format(dValue.read()),12,28,1)
t1 = time.ticks_ms()
if (math.fabs(t0-t1)>2000):
dht11.measure()
t0 = time.ticks_ms()
oled.text("Tem.:{}C".format(dht11.temperature()),12,36,1)
oled.text("Hum.:{}%".format(dht11.humidity()),12,44,1)
else:
oled.text("Tem.:n/a",12,36,1)
oled.text("Hum.:n/a",12,44,1)
oled.show()
##################################################################################
# main program
##################################################################################
try:
oldSwValue = 1
showData()
while True:
swValue = sw.value()
if ((swValue==0) and (oldSwValue != swValue)):
showData()
oldSwValue = swValue
time.sleep_ms(25) # ป้องกันการกระเพื่อมของสัญญาณจากสวิตช์ (debounce)
except KeyboardInterrupt:
pass
oled.poweroff()
print("end of program")
The result is show in Figure 3.
Conclusion
From the example in the article, it can be modified to store the data in the same file as the previous article (The MicroPython Internal File System.). In addition, it can be applied to use as a web to provide services to clients like the article ESP8266/ESP32 WiFi or used as a service provider that waits for clients to request and send data back like a client/server programming article for a weather station over a wireless network, etc. It can be seen that written in Python still has the benefit of moving code between platforms, so have fun programming.
If you want to talk with us, feel free to leave comments below!!
(C) 2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-12-10