/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Load Wi-Fi library
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "Adafruit_GFX.h"
#include "Fonts/FreeSansBold9pt7b.h"
#include "Fonts/Org_01.h"
#include "Adafruit_SSD1306.h"
#include "Wire.h"
#include "FS.h"
#include "Adafruit_SI1145.h"
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_SSD1306 display(128, 32, &Wire, 8); //Declaring the display name (display)
Adafruit_BME680 bme; // I2C
Adafruit_SI1145 uv = Adafruit_SI1145();
// Replace with your network credentials
const char *ssid = "Fishing24";
const char *password = "Fishing$602";
float uvindex;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
#define AnalogPin A0
#define LED 5
float sensorValue1 = 0;
float sensorValue2 = 0;
float sensorValue3 = 0;
int limit = 10;
void setup()
{
Serial.begin(9600);
Wire.begin();
server.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
WiFi.begin(ssid, password);
while (WiFi.status() == WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, HIGH);
}
while (!Serial)
;
Serial.println();
Serial.println("BME680 Test");
Serial.println("SI1145 Test");
if (!uv.begin())
{
Serial.println("No Si1145 found");
while (1)
;
}
if (!bme.begin())
{
Serial.println("No BME680 found");
while (1)
;
}
}
void displayData(){
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setFont(&Org_01);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(1, 5);
display.print("IP: ");
display.setCursor(25, 25);
display.setFont(&FreeSansBold9pt7b);
display.setTextSize(1);
display.println(WiFi.localIP());
display.display();
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void loop()
{
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150);
digitalWrite(LED, LOW);
// display.print("Temperature: ");
//display.print(bme.temperature, 1);
//display.println(" C");
Serial.print("Temperature: ");
Serial.print(bme.temperature, 1);
Serial.println(" C");
//display.print("Humidity: ");
//display.print(bme.humidity, 0);
//display.println(" %");
Serial.print("Humidity: ");
Serial.print(bme.humidity, 0);
Serial.println(" %");
//display.print("Mositure: ");
//display.print(sensorValue1, 0);
//display.println("%");
Serial.print("Mositure: ");
Serial.print(sensorValue1, 0);
Serial.println("%");
if (sensorValue1 < limit)
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
//display.print(F("Pressure: "));
//display.print((bme.pressure / 100.0) * 0.03, 0);
//display.println(F(" in Hg"));
Serial.print(F("Pressure: "));
Serial.print((bme.pressure / 100.0) * 0.03, 0);
Serial.println(F(" in Hg"));
//display.print("Altitude: ");
//display.print(bme.readAltitude(SEALEVELPRESSURE_HPA), 0);
//display.println(" m");
Serial.print("Altitude: ");
Serial.print((2.75*bme.readAltitude(SEALEVELPRESSURE_HPA)), 0);
Serial.println(" m");
//display.print("Gas: ");
//display.print(bme.gas_resistance / 1000.0, 0);
//display.println(" KOhm");
Serial.print("Gas: ");
Serial.print(bme.gas_resistance / 1000.0, 0);
Serial.println(" KOhm");
float UVindex = uv.readUV();
// the index is multiplied by 100 so to get the
// integer index, divide by 100!
UVindex /= 100.0;
//display.print("UV Index: "); //250Serial.print("UV Index: ");
//display.print(UVindex, 2); //250
//display.display();
Serial.print("UV Index: "); //250Serial.print("UV Index: ");
Serial.print(UVindex, 2); //250
if (uv.readUV() >= 0 && uv.readUV() < 689)
{
Serial.println(" LOW");
//display.println(" LOW");
//display.display();
}
else
if (uv.readUV() >= 690 && uv.readUV() < 1379)
{
Serial.println(" Moderate");
//display.println(" Moderate");
//display.display();
}
else
if (uv.readUV() >= 1380 && uv.readUV() < 1839)
{
Serial.println(" High");
//display.println(" High");
//display.display();
}
else
if (uv.readUV() >= 1839 && uv.readUV() < 2529)
{
Serial.println(" HIGH!");
//display.println(" HIGH!");
//display.display();
}
if (uv.readUV() >= 2530 && uv.readUV() < 2760)
{
Serial.println(" Get shade!");
//display.println(" Get shade!");
//display.display();
}
Serial.println();
WiFiClient client = server.available(); // Listen for incoming clients
if (client)
{ // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime)
{ // loop while the client's connected
currentTime = millis();
if (client.available())
{ // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n')
{ // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:450; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("</style></head><body><h1>Monster Laboratories Weather Station</h1>");
client.println("<img src=/Ben_Grin_glasses.JPG width=180>");
client.println("<img src=/Ben_Grin_glasses.jpg width=180>");
client.println("<img src=Ben_Grin_glasses.JPG width=180>");
client.println("<img src=Ben_Grin_glasses.jpg width=180>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Centigrade</td><td><span class=\"sensor\">");
client.println(bme.temperature, 1);
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
client.println((1.8 * bme.readTemperature() + 32), 0);
client.println(" *F</span></td></tr>");
client.println("<tr><td>Soil Moisture:</td><td><span class=\"sensor\">");
client.println(sensorValue1, 0);
client.println("%</span></td></tr>");
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
client.println(bme.humidity, 0);
client.println(" %</span></td></tr>");
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println((bme.pressure / 100.0) * 0.03, 0);
client.println(" in Hg</span></td></tr>");
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
client.println((5.5 * bme.readAltitude(SEALEVELPRESSURE_HPA)), 0);
client.println(" m</span></td></tr>");
client.println("<tr><td>Uv Index</td><td><span class=\"sensor\">");
client.println(UVindex);
client.println(" </span></td></tr>");
client.println("<tr><td>Uv </td><td><span class=\"sensor\">");
client.println(uv.readUV() / 230);
client.println(" </span></td></tr>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
}
else
{ // if you got a newline, then clear currentLine
currentLine = "";
}
}
else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}