Advertisement
microrobotics

GY-21P Code

Apr 5th, 2024
1,229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <Wire.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_BMP280.h>
  6. #include "Adafruit_Si7021.h"
  7.  
  8.  
  9. Adafruit_BMP280 bmp; // I2C
  10. Adafruit_Si7021 sensor = Adafruit_Si7021();
  11.  
  12. // Enter a MAC address and IP address for your controller below.
  13. // The IP address will be dependent on your local network:
  14. byte mac[] = {
  15.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  16.  
  17. IPAddress ip(192,168,1, 177);
  18.  
  19. // Initialize the Ethernet server library
  20. EthernetServer server(80);
  21.  
  22. void setup()
  23. {
  24.   // Open serial communications
  25.   Serial.begin(9600);
  26.   Ethernet.begin(mac, ip);
  27.   server.begin();
  28.   Serial.print("server is at ");
  29.   Serial.println(Ethernet.localIP());
  30.   if (!bmp.begin())
  31.   {
  32.   Serial.println("Could not find a valid BMP280 sensor, check wiring!");
  33.   while (1);
  34.   }
  35.  
  36.   if (!sensor.begin())
  37.   {
  38.   Serial.println("Did not find Si7021 sensor!");
  39.   while (true);
  40.   }
  41. }
  42.  
  43.  
  44. void loop()
  45. {
  46.   // listen for incoming clients
  47.   EthernetClient client = server.available();
  48.   if (client)
  49.   {
  50.     Serial.println("new client");
  51.     boolean currentLineIsBlank = true;
  52.     while (client.connected())
  53.     {
  54.       if (client.available())
  55.       {
  56.         char c = client.read();
  57.         Serial.write(c);
  58.         if (c == '\n' && currentLineIsBlank)
  59.         {
  60.           // send a standard http response header
  61.           client.println("HTTP/1.1 200 OK");
  62.           client.println("Content-Type: text/html");
  63.           client.println("Connnection: close");
  64.           client.println();
  65.           client.println("<!DOCTYPE HTML>");
  66.           client.println("<html>");
  67.           client.println("<meta http-equiv=\"refresh\" content=\"5\">");
  68.           client.println("<br />");    
  69.           //bmp280 part
  70.           client.println("<h3>BMP280 readings</h3>");
  71.           client.print("Pressure (Pa): ");
  72.           client.println((float)bmp.readPressure(), 1);  
  73.           client.println("<br />");          
  74.           client.print("Temperature (C): ");
  75.           client.println((float)bmp.readTemperature(), 1);  
  76.           client.println("<br />");
  77.           client.print("Altitude (m): ");
  78.           client.println((float)bmp.readAltitude(1024), 1);  // this should be adjusted to your local forcase
  79.           client.println("<br />");
  80.           //SI7021 part
  81.           client.println("<h3>SI7021 readings</h3>");
  82.           client.print("Humidity (%): ");
  83.           client.println((float)sensor.readHumidity(), 1);  
  84.           client.println("<br />");          
  85.           client.print("Temperature (C): ");
  86.           client.println((float)sensor.readTemperature(), 1);  
  87.           client.println("<br />");  
  88.  
  89.           client.println("</html>");
  90.           break;
  91.         }
  92.         if (c == '\n')
  93.         {
  94.           currentLineIsBlank = true;
  95.         }
  96.         else if (c != '\r')
  97.         {
  98.           currentLineIsBlank = false;
  99.         }
  100.       }
  101.     }
  102.     // give the web browser time to receive the data
  103.     delay(1);
  104.     // close the connection:
  105.     client.stop();
  106.     Serial.println("client disonnected");
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement