Arduino WiFi Temperature Web Page Alert

This project displays the temperature on a web page that auto refreshes. The color of the temperature readout changes based on the temperature.

Functional Parts in the Project:

#include <SPI.h>
#include <WiFiNINA.h>

#define sensorPin A0

char ssid[] = "test";
char pass[] = "";
int keyIndex = 0;

int status = WL_IDLE_STATUS;
WiFiServer server(80);

int highTemp = 82;
int goodTemp = 76;

void setup() {
  Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }
  server.begin();

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {

  int reading = analogRead(sensorPin);
  float voltage = reading * 5.0;
  voltage /= 1024.0;
  float temperatureC = (voltage - 0.5) * 100 ;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  WiFiClient client = server.available();   
  if (client) {                             
    Serial.println("new client");           
    String currentLine = "";                
    while (client.connected()) {            
      if (client.available()) {             
        char c = client.read();             
        Serial.write(c);                    
        if (c == '\n') {                    

          if (currentLine.length() == 0) {


           client.println("<head><meta http-equiv=\"refresh\" content=\"5\"></head>");

            if (temperatureF >= highTemp){

              client.print("<h1 style=\"color:red; font-size:400px\">");
              client.print(temperatureF);
              client.println("</h1>");
            }
            else if(temperatureF < highTemp && temperatureF >= goodTemp){
              client.print("<h1 style=\"color:yellow; font-size:400px\">");
              client.print(temperatureF);
              client.println("</h1>");
            }
            else {
              client.print("<h1 style=\"color:green; font-size:400px\">");
              client.print(temperatureF);
              client.println("</h1>");
            }
            break;

            
          }else {
            currentLine = "";
          }
      
        } else if (c != '\r') {
          currentLine += c;
        }
      }
    }

    client.stop();
    Serial.println("client disconnected");
  }
}

Be the first to comment

Leave a Reply