Arduino and GY-21P readings on a web page

In this example we connect an Ethernet shield to an Arduino, we then connect a GY-21P sensor to this and we will display the readings on a webpage

The GY-21P is an interesting module in that it combines a BMP280 sensor and an SI7021 sensor. The on-board BMP280+SI7021 sensor measures atmospheric pressure from 30kPa to 110kPa as well as relative humidity and temperature.

BMP280
Pressure range: 300-1100 hPa (9000 meters above sea level at -500m)
Relative accuracy (at 950 – 1050 hPa at 25 ° C): ± 0.12 hPa, equiv. to ± 1 m
Absolute accuracy (at (950 – 1050 hPa, 0 – +40 ° C): ± 0.12 hPa, equiv. To ± 1 m
Mains voltage: 1.8V – 3.6V
Power consumption: 2.7µA at 1Hz readout rate
Temperature range: -40 to + 85 ° C

SI7021
HVAC/R
Thermostats/humidistats
Respiratory therapy
White goods
Indoor weather stations
Micro-environments/data centers
Automotive climate control and defogging
Asset and goods tracking
Mobile phones and tablets
Size: 1.3*1cm/0.51*0.39″

Features:

Operation Voltage: 3.3V
I2C & SPI Communications Interface
Temp Range: -40C to 85C
Humidity Range: 0 – 100% RH, =-3% from 20-80%
Pressure Range: 30,000Pa to 110,000Pa, relative accuracy of 12Pa, absolute accuracy of 100Pa
Altitude Range: 0 to 30,000 ft (9.2 km), relative accuracy of 3.3 ft (1 m) at sea level, 6.6 (2 m) at 30,000 ft.

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
GY-21P GY-21P Atmospheric Humidity Temperature Sensor Breakout Barometric Pressure BMP280 SI7021 For Arduino
Connecting cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
Arduino Ethernet shield Ethernet Shield W5100 R3 UNO and Mega 2560

 

Schematics/Layout

Connect the Ethernet shield and the connect the sensor to the shield connector, like this.

Ethernet shield and GY-21p
Ethernet shield and GY-21p

Code

I use a variety of Adafruit libraries, took the default examples and made the following out of them

https://github.com/adafruit/Adafruit_Sensor

https://github.com/adafruit/Adafruit_BMP280_Library

https://github.com/adafruit/Adafruit_Si7021

I got the sea level pressure value from this link

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
 
 
Adafruit_BMP280 bmp; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
 
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
IPAddress ip(192,168,1, 177);
 
// Initialize the Ethernet server library
EthernetServer server(80);
 
void setup()
{
  // Open serial communications
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  if (!bmp.begin()) 
  { 
  Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  while (1);
  }
 
  if (!sensor.begin()) 
  {
  Serial.println("Did not find Si7021 sensor!");
  while (true);
  }
}
 
 
void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) 
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<br />");     
          //bmp280 part
          client.println("<h3>BMP280 readings</h3>");
          client.print("Pressure (Pa): ");
          client.println((float)bmp.readPressure(), 1);  
          client.println("<br />");          
          client.print("Temperature (C): ");
          client.println((float)bmp.readTemperature(), 1);  
          client.println("<br />");
          client.print("Altitude (m): ");
          client.println((float)bmp.readAltitude(1024), 1);  // this should be adjusted to your local forcase
          client.println("<br />");
          //SI7021 part
          client.println("<h3>SI7021 readings</h3>");
          client.print("Humidity (%): ");
          client.println((float)sensor.readHumidity(), 1);  
          client.println("<br />");          
          client.print("Temperature (C): ");
          client.println((float)sensor.readTemperature(), 1);  
          client.println("<br />");   
 
          client.println("</html>");
          break;
        }
        if (c == '\n') 
        {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') 
        {
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

Output

Open your favourite web browser and type in the IP address, you should see something like this

gy-21p output
gy-21p output

 

Links