microrobotics icon

GY-21P Code

microrobotics | PRO | 04/05/24 09:14:32 AM UTC | 0 ⭐ | 12893 👁️ | Never ⏰ | []
Arduino |

3.07 KB

|

None

|

0 👍

/

0 👎

#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");
  }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎