0

members

Easy Basics: Project 086d ESP-12E module, BME280 sensor - Web server

of Acoptex.com in ESP8266 ESP-12

Basics: Project 086d

Project name: ESP8266 ESP-12E module, BME280 sensor - Web server

Tags: Arduino, Arduino IDE, ESP8266 ESP-12E module, ESP, ESP8266, WI FI module, ESP-12E, LoLin NODEMCU V3, NODEMCU, BME280, BME280 sensor, humidity, temperature, pressure, altitude, GY-BME280, Digital Sensor, SPI I2C Humidity Temperature and Barometric Pressure Sensor Module, GY-BME280-3.3, web server

Attachments: I2Cscannersketch, sketch1library1 and library2

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1.ESP8266 ESP-12E module with micro USB cable 1pc

2. Jumper cables F-M

3. Breadboard 1 pc

4. BME280 sensor module 1 pc

General

We will learn how to use ESP8266 ESP-12E module with BME280 sensor module and create the web server to display temperature, humidity, pressure and altitude. 

Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

You can read more about it here.

Understanding the BME280 module

The BME280 is the next-generation of sensors from Bosch, and is the upgrade to the BMP085/BMP180/BMP183 - with a low altitude noise of 0.25m and the same fast conversion time. 

The BME280 sensor module reads temperature, humidity, and pressure. You can also estimate altitude as the pressure changes with altitude. 

The sensor can communicate using either SPI (supports 3-, 4-wire SPI) or I2C communication protocols (there are modules of this sensor that just communicate with I2C, these just come with four pins).

The sensor comes with an on-board LM6206 3.3V regulator and I2C Voltage Level Translator, so you can use it with a 3.3V or 5V logic microcontroller like Arduino without worry.

The BME280 consumes less than 1mA during measurements and only 5μA during idle. This low power consumption allow the implementation in battery driven devices such as handsets, GPS modules or watches.

Specifications:

  • Supply Voltage: 1.8 - 5V DC
  • Interface: I2C (up to 3.4MHz), SPI (up to 10 MHz)
  • Operational Range: Temperature: -40 to +85° C
  • Humidity: 0-100%
  • Pressure: 300-1100 hPa
  • Resolution:Temperature: 0.01°C Humidity: 0.008% Pressure: 0.18Pa
  • Accuracy:Temperature: +-1°C Humidity: +-3% Pressure: +-1Pa
  • I2C address SDO LOW : 0x76 SDO HIGH: 0x77 

The module features a simple two-wire I2C interface which can be easily interfaced with any microcontroller of your choice. The default I2C address of the BME280 module is 0x76 and can be changed to 0x77 easily with the solder jumper besides chip.

How to change I2C Address?

  1. Locate the solder jumper besides chip. By default the middle copper pad is connected to the left pad.
  2. Scratch the connection between the middle and the left copper pad to disconnect those using a sharp knife.
  3. Add a solder blob between the middle and the right copper pad to join them. It allows you to set the I2C address 0x77.

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 ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

TX - transmit pin. GPIO pin

RX  - receive pin.  GPIO pin

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

GND ( or G) - ground pin.

RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.

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

Vin - External power supply 5VDC.

D0-D8 - GPIO (General Purpose Input Output) pins 

D5-D8 - SPI interface

D1-D2– I²C/TWI Interface

SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin

SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin

SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin

S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin

S2 (or SD2) - GPIO pin

S3 (or SD3) - GPIO pin

VU (or VUSB) - external power 5VDC.

A0 - ADC output.

RSV - reserved

Wiring

BME280 sensor  Nodemcu 1.0

GND                 G (GND)

VIN                  3V3

SCL                  D1 (GPIO 5)

SDA                 D2 (GPIO 4)

Step by Step instruction

1. Adding ESP8266 platform to Arduino IDE

The Arduino environment has to be set up to make it compatible with the ESP8266 ESP-12E module. We are using PC with Windows 7 64 bit OS.

  1. Download and install the Arduino IDE. You can download it here 
  2. Open Arduino IDE. 
  3. Open the Preferences window from the Arduino IDE. Go to File -> Preferences.
  4. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field and click the “OK” button. If you already have a URL in there, and want to keep it, you can separate multiple URLs by placing a comma between them. (Arduino 1.6.5 added an expanded text box, separate links in here by line.)
  5. Open Boards manager. Go to Tools -> Board -> Boards Manager…

  6. There should be a couple new entries in addition to the standard Arduino boards. Look for esp8266. or scroll down to the ESP8266 entry (usually at the bottom). Select the ESP8266 entry. When you click it an install option will appear. Select the latest version and click install
  7. The board definitions and tools for the ESP8266 include a whole new set of gcc, g++, and other reasonably large, compiled binaries, so it may take a few minutes to download and install (the archived file is ~110MB). Once the installation has completed, an Arduino-blue “INSTALLED” will appear next to the entry.

2. Uploading sketch to ESP8266 ESP-12E development board

If you’re using an ESP-12E NodeMCU Kit, uploading the sketch is very simple, since it has built-in programmer. 

Before use ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3), you need to download the manufacture's driver (CH340)  for this chip and install it in your PC. Here is the link. See the description of driver installation package below: CH340 / CH341 USB to serial WINDOWS driver installation package that supports 32/64 bit Windows 10 / 8.1 / 8/7 / VISTA / XP, SERVER 2016/2012/2008/2003, 2000 / ME / 98, through Microsoft digital signature authentication, support USB to 3-wire and 9-wire serial port, with the product release To the end user. Applicable scope: CH340G, CH340C, CH340B, CH340E, CH340T, CH340R, CH341A, CH341T, CH341H chips.

If you have CP2102 chip then  you need to download the manufacture's driver for this chip and install it in your PC. Driver for MacWindowsLinux or more.. 

  1. Do wiring.
  2. Open Arduino IDE.
  3. Plug your ESP8266 ESP-12E module into your PC USB port.
  4. Choose your NodeMCU board. Go to Tools -> Board -> NodeMCU 1.0 (ESP-12E Module)
  5. Select the correct com port.
  6. 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 ESP8266 ESP-12E module.
  7. 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  bme280testsketch1status = bme.begin(); to status = bme.begin(0x76);

  8. Modify, compile and upload sketch1 to your ESP8266 ESP-12E module. You also need to set your local WiFi network SSID and password in the sketch.
  9. Open the Serial Monitor at a baud rate of 115200. You should see the ESP8266 ESP-12E module IP address displayed on the Serial Monitor (we have 192.168.0.121).
  10. You can access your web server, if you type your ESP8266 ESP-12E module IP address in your browser. In our case it is http://http://192.168.0.121. You will see the latest sensor readings. To update the readings, you just need to refresh the web page.

Code

First we include the required libraries to read from the BME280 sensor. Then define a variable to save the pressure at the sea level. For more accurate altitude estimation, replace the value with the current sea level pressure at your location. Next you create an Adafruit_BME280 object called bme that by default establishes a communication with the sensor using I2C: Adafruit_BME280 bme; // I2C

You need to insert your ssid and password in the following lines inside the double quotes: const char* ssid     = ""; const char* password = "";

Set your web server to port 80.The following line creates a variable to store the header of the HTTP request: String header;

In the setup(),  we start a serial communication at a baud rate of 115200 for debugging purposes. You also check that the BME280 sensor was successfully initialized. Next lines begin the Wi-Fi connection with WiFi.begin(ssid, password), wait for a successful connection and print the ESP32 IP address in the Serial Monitor.

In the loop() we program what happens when a new client establishes a connection with the web server. The ESP is always listening for incoming clients. When a request is received from a client, we’ll save the incoming data. The while loop that follows will be running as long as the client stays connected. The next thing you need to do is to send a response to the client with the HTML text to build the web page. The web page is sent to the client using this expression client.println(). You should enter what you want to send to the client as an argument.

To display the sensor readings on the table, we just need to send them between the corresponding and tags. Thetag is useful to style a particular part of a text. In this case, we’re using the tag to include the sensor reading in a class called "sensor".

By default the table is displaying the temperature readings in both Celsius degrees and Fahrenheit. Of course you can comment unnec essary lines to display the temperature in Fahrenheit degrees only.

Finally, when the response ends, we clear the header variable, and stop the connection with the client with client.stop().

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 in sketch1status = bme.begin(); to status = bme.begin(0x76);

2. If the  I2Cscannersketch shows you your BME280 I2C address and you modified sketch1 but 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.

Summary

We have learnt how to use ESP8266 ESP-12E module with BME280 sensor module and created the web server to present its readings - temperature, humidity, pressure and altitude.

Libraries

  • All libraries attached on the begining of this project description.
  • ESP8266WiFi included in your Arduino IDE.
  • Wire 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.

Sketch

  • See on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 18-07-2019
Viewed: 130 times