0

members

Easy Basics: Project 070v ESP32 Weather station with BME280, LDR, SD card module, DHT 11 21 22 Webserver

of Acoptex.com in ESP8266 ESP-32

Basics: Project 070v

Project name: ESP32 Weather station with BME280, LDR, SD card module, DHT 11 21 22 - Webserver

Tags: ESP32 Dev Module, ESP32 development board, ESP32 Development board with WiFi and Bluetooth, ESP32-DevKitC V4 development board, ESP-WROOM-32 module with ESP32‑D0WDQ6 chip, Espressif Systems, ESP32-based development board, ESP32 modules, ESP32-WROOM-32, ESP32-WROOM-32U, ESP32-WROOM-32D, ESP32-SOLO-1, USB-UART bridge, IOT, ESP-WROOM-32 Dev Module, ESP32 DEVKITV1, Installing the ESP32 Board in Arduino IDE, Uploading sketch, BME280, GY-BME280, Digital Sensor, SPI I2C Humidity Temperature and Barometric Pressure Sensor Module, GY-BME280-3.3, Web Server, DHT 11 21 22, LDR, SD card module, Micro SD card module, DHT11, DHT21, DHT22 module, temperature, humidity, pressure, altitude, luminosity

Attachments: I2Cscannersketch, library1, library2library4library3, ESP32devshieldsketch

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

In this project, you need these parts :

1. ESP32 development board with WiFi and Bluetooth and USB A / micro USB B cable 1 pc

2.Arduino IDE ( you can download it from here  )

3.Jumper cables F-F, F-M, M-M

4. BME280 sensor module 1 pc 

5. Micro SD card with adapter 1 pc

6. SD card module or Micro SD card module 1 pc

 or 

7. Breadboard 1 pc

8. Resistor 4 pcs (3pcs - 10 KOhm, 2pcs - 220 Ohm)

9. DHT11 21 22 Humidity and temperature module 1pc

10. Photoresistor (Photocell or LDR) 1pc

11. Momentary switch (Pushbutton) 1pc
12. Potentiometer 1pc (10 kOhm)
13. LED 2 pcs (red and green)

14. 5V Relay module 1 pc 

15.Light bulb 60 W 220 V with connector 1pc

General

We will learn how to create a web server with the ESP32 development board to display readings from different sensors. The web server displays data from all the sensors and automatically updates the readings every ten seconds, without the need to refresh the web page.

Sensors and available readings:

  • BME280 sensor module  - temperature, humidity, pressure, altitude. BMP280 sensor module - temperature, pressure, altitude.
  • DHT 11 or DHT 21 or DHT22 sensor module - temperature, humidity
  • Photocell (LDR) - luminosity.

There are a lot of different development boards made. You can find more information about them here

Understanding the relay module

You can read more about it here.

Understanding the DHT 11 21 22 module

You can read more about it here.

Understanding the potentiometer

You can read more about it here.

Understanding Photocell (Photoresistor or Light dependent resistor)

You can read more about it here.

Understanding the BME280 module

You can read more about it here.

Understanding the SD card module

You can read more about it here.

The SD card or Micro SD card module can be used to store readings if you want to build a data logger or it can store an HTML file to serve a web page. It is the best solution to store more complex web pages.

Understanding the ESP32 Development board with WiFi and Bluetooth

We will discuss here an Espressif Systems products. Our development board is using ESP-WROOM-32 module from Espressif Systems.

Espressif offers a wide range of fully-certified Wi-Fi & BT modules powered by their own advanced SoCs.

1. Dual-core Modules with Wi-Fi & Dual-mode Bluetooth

Features

  • Two independently-controlled CPU cores with adjustable clock frequency, ranging from 80 MHz to 240 MHz
  • +19.5 dBm output at the antenna ensures a good physical range
  • Classic Bluetooth for legacy connections, also supporting L2CAP, SDP, GAP, SMP, AVDTP, AVCTP, A2DP (SNK) and AVRCP (CT)
  • Support for Bluetooth Low Energy (BLE) profiles including L2CAP, GAP, GATT, SMP, and GATT-based profiles like BluFi, SPP-like, etc
  • Bluetooth Low Energy (BLE) connects to smart phones, broadcasting low-energy beacons for easy detection
  • Sleep current is less than 5 μA, making it suitable for battery-powered and wearable-electronics applications
  • Integrates 4 MB flash
  • Peripherals include capacitive touch sensors, Hall sensor, low-noise sense amplifiers, SD card interface, Ethernet, high-speed SPI, UART, I2S and I2C
  • Fully certified with integrated antenna and software stacks

2. Single-core Modules with Wi-Fi & Dual-mode Bluetooth

Features

  • High-performance 160 MHz single-core CPU
  • +19.5 dBm output at the antenna ensures a good physical range
  • Classic Bluetooth for legacy connections, also supporting L2CAP, SDP, GAP, SMP, AVDTP, AVCTP, A2DP (SNK) and AVRCP (CT)
  • Support for Bluetooth Low Energy (BLE) profiles including L2CAP, GAP, GATT, SMP, and GATT-based profiles like BluFi, SPP-like, etc
  • Bluetooth Low Energy (BLE) connects to smart phones, broadcasting low-energy beacons for easy detection
  • Sleep current is less than 5 μA, making it suitable for battery-powered and wearable-electronics applications
  • Peripherals include capacitive touch sensors, Hall sensor, low-noise sense amplifiers, SD card interface, Ethernet, high-speed SPI, UART, I2S and I2C
  • Fully certified with integrated antenna and software stacks

3. Single-core Modules with 802.11b/g/n 2.4 GHz Wi-Fi

Features

  • High-performance 160 MHz single-core CPU
  • +19.5 dBm output at the antenna ensures a good physical range
  • Sleep current is less than 20 μA, making it suitable for battery-powered and wearable-electronics applications
  • Peripherals include UART, GPIO, I2C, I2S, SDIO, PWM, ADC and SPI
  • Fully certified with integrated antenna and software stacks

There are different development Boards made by Espressif Systems and other manufacturers. We will publish some information about Espressif Systems boards but you can also find out more information about other development boards here.

1. 2.4 GHz Wi-Fi & BT/BLE Development Boards

Features

  • PC connectivity: USB
  • Power supply options: USB (by default), or 5V/GND header pins, or 3V3/GND header pins
  • SDK: ESP-IDF source code and example applications

2. 2.4 GHz Wi-Fi Development Boards

Features

  • PC connectivity: USB
  • SDK: ESP8266 SDK source code and example applications

3. 2.4 GHz Wi-Fi + BT/BLE + Sensor Development Boards

Features

  • PC connectivity: USB
  • SDK: ESP-IOT-SOLUTION source code and example applications

You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board by pressing Getting started link close to each board here.

ESP32 chip

ESP32 is a series of low cost, low power system on a chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations and includes in-built antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules. ESP32 is created and developed by Espressif Systems, a Shanghai-based Chinese company, and is manufactured by TSMC using their 40 nm process. It is a successor to the ESP8266 microcontroller.

ESP32 can perform as a complete standalone system or as a slave device to a host MCU, reducing communication stack overhead on the main application processor. ESP32 can interface with other systems to provide Wi-Fi and Bluetooth functionality through its SPI / SDIO or I2C / UART interfaces.

ESP32 is highly-integrated with in-built antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules. ESP32 adds priceless functionality and versatility to your applications with minimal Printed Circuit Board (PCB) requirements.

ESP32 is capable of functioning reliably in industrial environments, with an operating temperature ranging from –40°C to +125°C. Powered by advanced calibration circuitries, ESP32 can dynamically remove external circuit imperfections and adapt to changes in external conditions.

Engineered for mobile devices, wearable electronics and IoT applications, ESP32 achieves ultra-low power consumption with a combination of several types of proprietary software. ESP32 also includes state-of-the-art features, such as fine-grained clock gating, various power modes and dynamic power scaling.

Functional Block Diagram:

Features of the ESP32 include the following:

Processors:

  • CPU: Xtensa dual-core (or single-core) 32-bit LX6 microprocessor, operating at 160 or 240 MHz and performing at up to 600 DMIPS
  • Ultra low power (ULP) co-processor
  • Memory: 520 KiB SRAM

Wireless connectivity:

  • Wi-Fi: 802.11 b/g/n
  • Bluetooth: v4.2 BR/EDR and BLE

Peripheral interfaces:

  • 12-bit SAR ADC up to 18 channels
  • 2 × 8-bit DACs
  • 10 × touch sensors (capacitive sensing GPIOs)
  • Temperature sensor
  • 4 × SPI
  • 2 × I²S interfaces
  • 2 × I²C interfaces
  • 3 × UART
  • SD/SDIO/CE-ATA/MMC/eMMC host controller
  • SDIO/SPI slave controller
  • Ethernet MAC interface with dedicated DMA and IEEE 1588 Precision Time Protocol support
  • CAN bus 2.0
  • Infrared remote controller (TX/RX, up to 8 channels)
  • Motor PWM
  • LED PWM (up to 16 channels)
  • Hall effect sensor
  • Ultra low power analog pre-amplifier

Security:

  • IEEE 802.11 standard security features all supported, including WFA, WPA/WPA2 and WAPI
  • Secure boot
  • Flash encryption
  • 1024-bit OTP, up to 768-bit for customers
  • Cryptographic hardware acceleration: AES, SHA-2, RSA, elliptic curve cryptography (ECC), random number generator (RNG)

Power management:

  • Internal low-dropout regulator
  • Individual power domain for RTC
  • 5uA deep sleep current
  • Wake up from GPIO interrupt, timer, ADC measurements, capacitive touch sensor interrupt

You can find ESP32 chip datasheet here, hardware design here, technical reference manual here.

Signals and connections of the 5V relay module

The SRD-05 VDC-SL-C relay has three high voltage terminals (NC, C, and NO) which connect to the device you want to control. The other side has three low voltage pins (Ground, Vcc, and Signal) which connect to the Arduino.
NC: Normally closed 120-240V terminal
NO: Normally open 120-240V terminal
C: Common terminal

Signals and connections of potentiometer

Signals and connections of photocell

Attach one end of photoresistor to power. On the otherside, attach a 10-kilohm resistor to ground. This resistor is in series with the photoresistor, and together they form a voltage divider. The voltage at the point where they meet is proportionalto the ratio of their resistances, according to Ohm’s Law. 

Signals and connections of LED

The operating voltage of the LED is 1.8V and the operating current is 10mA-20mA. The Arduino Uno board can supply 5V or 3.3V power. We will use 5V for this project, so the minimum resistance of the current limiting resistor should be (5 V to 1.8 V)/20 = 160 Om. The 220 Om offered in the kit is suitable and you can also choose other resistors that meet the condition. The larger the resistance is, the dimmer the LED will get.

Signals and connections of the SD card module

or 

Note: depending on the module you’re using, the pins may be in a different order.

VCC (5V)  - connect to 5V pin Arduino Uno. If it is just VCC pin and no 3V3 pin connect VCC to 3.3V pin of Arduino Board

3V3 (or 3.3V) - connect to 3.3V pin Arduino Uno

CS (or SS or D3) (Chip Select or Slave Select) - the pin on each device that the master can use to enable and disable specific devices

MOSI (or DI or SI or CMD) (Master Out Slave In) - The Master line for sending data to the peripherals

CLK (or SCK) (Serial Clock) - The clock pulses which synchronize data transmission generated by the master

MISO (or DO or SO) (Master In Slave Out) - The Slave line for sending data to the master

GND (or G) - ground

CD - this is the Card Detect pin. It shorts to ground when a card is inserted. You should connect a pull up resistor (10K or so) and wire this to another pin if you want to detect when a card is inserted. 

Signals and connections of the DHT 11 21 22 module

There are two different versions of the DHT11 you might come across. One type has four pins, and the other type has three pins and is mounted to a small PCB. The PCB mounted version is nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are the pin outs for both versions:

Signals and connections of the BME280 sensor module

To use SPI communication protocol, you must have the following pins:

VIN -  power supply 3.3V DC

GND - ground

CLK (or SCK or SCL) (Serial Clock) - The clock pulses which synchronize data transmission generated by the master

MISO (or DO or SO or SDO) (Master In Slave Out) - The Slave line for sending data to the master

MOSI (or DI or SI or CMD or SDI or SDA) (Master Out Slave In) - The Master line for sending data to the peripherals

CS (or SS or D3 or CSB) (Chip Select or Slave Select) - the pin on each device that the master can use to enable and disable specific devices

If your BME280 sensor module has 4 pins it can use I2C communication protocol only: 

VIN -  power supply  3.3V DC

GND - ground

SDA - Serial Data Line

SCL  - Serial Clock Line 

Signals and connections of the ESP32 Development board with WiFi and Bluetooth

You can find more information (datasheets, schematics, pins descriptions, functional desgn descriptions) about each board (made by Espresiff Systems) by pressing Getting started link close to each board here.

Let's check our development board - ESP32 DEVKITV1 with ESP-WROOM-32 module from Espressif Systems:

Pinout diagram for the ESP Wroom 32 breakout:

ESP32-WROOM-32 - ESP32-WROOM-32 module soldered to the development board. Optionally ESP32-WROOM-32D, ESP32-WROOM-32U or ESP32-SOLO-1 module may be soldered instead of the ESP32-WROOM-32.

USB-UART Bridge - A single chip USB-UART bridge provides up to 3 Mbps transfers rates.

BOOT button - Download button: holding down the Boot button and pressing the EN button initiates the firmware download mode. Then user can download firmware through the serial port.

EN button - Reset button: pressing this button resets the system.

Micro USB Port - USB interface. It functions as the power supply for the board and the communication interface between PC and the ESP module.

TX0, TX2 - transmit pin. GPIO pin

RX0, RX2  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND - ground pin.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

Wiring

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT. WE ARE NOT RESPONSIBLE FOR ANY DAMAGE, INJURY, DEATH AND OTHER THINGS CAUSED BY THIS PROJECT IMPLEMENTATION. 

We suggest you to simulate device with additional led powered from 3.3V DC before connecting high load device powered with 220 VAC.

The relay has two different types of electrical contacts inside – normally open (NO) and normally closed (NC). The one you use will depend on whether you want the 5V signal to turn the switch on or turn the switch off. The 120-240V supply current enters the relay at the common (C) terminal in both configurations. To use the normally open contacts, use the NO terminal. To use the normally closed contacts, use the NC terminal. We are using NO configuration. In the normally open configuration, when the relay receives a HIGH signal the 120-240V switch closes and allows current to flow from the C terminal to the NO terminal. A LOW signal deactivates the relay and stops the current. 

Make sure that the high voltage connections to the 5V relay module are very well secured. Identify the hot power wire (red wire in the diagram above) in the cord leading to the DEVICE and make a cut. Connect the side leading to the DEVICE to the NO terminal of the 5V relay, and the side leading to the plug to the C terminal. This way the relay is on the hot side, and current is switched before it reaches the DEVICE. It’s dangerous to put the relay on the neutral wire, since if the device fails current can still fault to ground when the relay is off.

Pushbutton         GPIO 33

Potentiometer     GPIO 32

Photoresistor      GPIO 35

DHT11               GPIO 15

LED green          GPIO 27

LED red             GPIO 26

BME280             SDA(GPIO 21); SCL(GPIO 22)

SD card module   MOSI(GPIO 23); MISO(GPIO 19); CLK(GPIO 18); CS(GPIO 5)

5V Relay module      GPIO 25

Free GPIOs          GPIO14, GPIO13, GPIO12

Step by Step instruction

The ESP32 is currently being integrated with the Arduino IDE like it was done for the ESP8266. There’s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language.

1. Installing ESP32 add-on in the Arduino IDE (Windows 10 OS)

  1. Download and install the latest Arduino IDE Windows Installer from arduino.cc
  2. Download and install Git and Git GUI from git-scm.com
  3. Search for Git GUI, right-click the icon and select “Run as administrator
  4. Select the Clone Existing Repository option.
  5. Select source and destination. Source Location: https://github.com/espressif/arduino-esp32.git
  6. Target Directory:C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32
  7. Do not create the espressif/esp32 folders, because they will be created automatically.
  8. Click Clone to start cloning the repository.Wait a few seconds while the repository is being cloned.
  9. Open the folder: C:/Users/[YOUR_USER_NAME]/Documents/Arduino/hardware/espressif/esp32/tools
  10. Right-click the get.exe file and select “Run as administrator“.
  11. You will see that necessary files will be downloaded and upzipped. It will take some time.
  12. When get.exe finishes, you should see the following files in the directory.

2. Uploading sketch to the ESP32 development board

  1. Most SD cards work right out of the box, but it's possible you have one that was used in a computer or camera and it cannot be read by the SD library. Formatting the card will create a file system that the Arduino can read and write to. It's not desirable to format SD cards frequently, as it shortens their life span. You’ll need a SD reader and computer to format your card. The library supports the FAT16 and FAT32 filesystems, but use FAT16 when possible. See additional info here.
  2. Format the SD card as FAT16 or FAT32. Insert the SD card in your computer. Go to My Computer and right click on the SD card. Select Format...  
  3. A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.
  4. You will need a HTML file to build the web page (index.html). In this file you can change how your web page looks, the headings, the table and so on… The ESP32 will send this HTML text to your browser when you make an HTTP request on the ESP32 IP address. Copy it from here and save on the SD card or Micro SD card.
  5. Insert the formatted SD card in the SD card module.
  6. Do wiring.
  7. Plug the ESP32 development board to your PC and wait for the drivers to install (or install manually any that might be required).
  8. Open Arduino IDE. 
  9. Open Boards manager. Go to Tools -> Board -> Boards Manager… (in our case it’s the DOIT ESP32 DEVKIT V1)
  10. Select COM port that the board is attached to (if you don’t see the COM Port in your Arduino IDE, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers)
  11. Find your BME280 I2C address. Each device has an I2C address that it uses to  accept commands or send messages. Open, compile and upload the I2Cscannersketch to your ESP32 development board. If everything went as expected, you should see a “Done uploading” message. (You need to hold the ESP32 on-board Boot button while uploading).
  12. Press the ESP32 on-board EN button to reboot it. 
  13. Open the Serial Monitor at a baud rate of 115200. Arduino will scan the address range looking for a reply.  Even though the documentation said it was 0x77, this scanner can detect different (in our case 0x76). Adafruit_BME280 library has default I2C address 0x77 and if you are getting 0x76 you need to modify line of code in ESP32devshieldsketchstatus = bme.begin(); to status = bme.begin(0x76);
  14. Open and modify ESP32devshieldsketch with your SSID and password data.
  15. Compile and upload the ESP32devshieldsketch to your ESP32 development board. If everything went as expected, you should see a “Done uploading” message. (You need to hold the ESP32 on-board Boot button while uploading).
  16. Press the ESP32 on-board EN button to reboot it.
  17. Open the Serial Monitor at a baud rate of 115200. You should see the ESP32 IP address displayed on the Serial Monitor (we have 192.168.0.116).
  18. You can access your web server, if you type your ESP32 development board IP address in your browser. In our case it is http://192.168.0.116. You will see the latest sensor readings.The readings updated every 10 seconds - do not need to refresh the web page.
  19. Let this project run for a few hours to gather a decent amount of data, and when you’re happy with the data logging period, remove the SD from the SD card module.
  20. Insert the SD card to a SD card reader connected to your computer (PC), open it, and you should have a data.txt file with the collected data.
  21. You can open the data with a text editor, or use a spreadsheet to analyse and process your data.

Code

To update the readings without refreshing the web page, we use AJAX. AJAX is a developer’s dream, because it can update the web page without reloading the page, request and receive data from a server, after the page has loaded, and send data to a server in the background.

Troubleshooting

If you are getting getting readings of 0.00 or "nan". Check the wiring first. If it doesn't help use tips below:

1. You probably have wrong I2C address - find your BME280 I2C address. Each device has an I2C address that it uses to  accept commands or send messages. Compile and upload the I2Cscannersketch to your ESP32 development board. The scanner will detect your BME280 I2C address (in our case it was 0x76). Adafruit_BME280 library has default I2C address 0x77 and if you are getting 0x76 you need to modify line of code: status = bme.begin(); to status = bme.begin(0x76);

2. If you are getting message "Could not find a valid BME280 sensor, check wiring!" it means that you have another sensor module - BMP280.

3. If you do not have any response from sensor module it means that it is dead (broken).

4. If your are getting "nan" more often than not the problem can be in power supply - it should be 3.3V DC. Adafruit's BME280 board has an onboard I2C level shifter. The chinese clone has not. The clone therefore must be used with a 3.3volt Arduino.

Summary

We have learnt how to create a web server with the ESP32 development board to display readings from different sensors. Of course you still have a room for improvements - you can modify the code provided to use the shield in a way that meets your specific needs or add extra sensors.

Libraries

  • All libraries attached on the begining of this project description.
  • Wire library included in your Arduino IDE.
  • WiFi library included in your Arduino IDE.
  • Adafruit_BME280 library included.Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here.
  • Adafruit_Sensor library included.Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here.
  • NTP client library included.Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about library here.
  • DHT-sensor library included. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.  OR You can install it in Arduino IDE too: select Sketch-> Include library->Manage your libraries->type DHT in Filter your search line and you will see DHT sensor library by Adafruit->More info->select version->install. If it will be problem with compilation - remove 2 files from the library - DHT_U.cpp and DHT_U.h.

Sketch

  • See attachments on the begining of this project


Other projects of Acoptex.com
Easy Basics: Project 083d Sipeed Maixduino Kit for RISC-V AI and IoT of Acoptex.com in ESP8266 ESP-32 16-06-2019
Easy Basics: Project 076f ESP32 Development board - How to use GPIO pins of Acoptex.com in ESP8266 ESP-32 03-06-2019
Easy Basics: Project 082a Lithum battery charger TP4056 of Acoptex.com in UNO 01-06-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 17-09-2018
Viewed: 951 times