Hello all,
I am trying to teach my 9 y/o how to do basic coding, and I figured the Arduino UNO was a good place to start. The problem is I am no coder, but I wish I had someone just to show me something like this when I was this age so I wouldn't be so dumb to it. So along with blinking LEDs, and showing cause and effect from changing Parameters I decided a Temp/Humidity sensor would be fun and easy for us. Obviously I got my code from around the net and made mild changes,so any credit goes to the original coder.
We added an LCD screen in and managed to make it work. The problem is the LCD screen won't update temp/Hum unless a client is connected. I think I've figured out why with the "if(client.available())" line. I get that it would read if no browser is connected, however I am not smart enough to code my way around it. I've tried moving the sensor reads to other areas and all I've gotten is various errors.. Any help would be super appreciated, and sorry for the Newb question as I am learning, as is the little one..
/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <Ethernet.h>
#include <dht.h>
#include <Wire.h>
#include <LiquidCrystal.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define DHT11_PIN 9 // The Temperature/Humidity sensor
LiquidCrystal lcd(3, 4, 5, 6, 7, 8);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xBA, 0xDB, 0x00, 0xBF, 0xFE, 0xED };
/*-----( Declare objects )-----*/
IPAddress ip(192,168,15,69);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
dht DHT; //The Sensor Object
/*-----( Declare Variables )-----*/
void setup() /****** SETUP: RUNS ONCE ******/
{
lcd.begin(16, 2);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}//--(end setup )---
void loop() /*----( LOOP: RUNS OVER AND OVER AGAIN )----*/
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.print("<TITLE> Temp Sensor</TITLE>");
client.print("<center><H1 style=color:blue>TEMPERATURE SENSOR :-) </H1>");
client.println("<br />");
client.println("<br />");
client.println("<br />");
/*----(Get sensor reading, calculate and print results )-----------------*/
int chk = DHT.read11(DHT11_PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case 0:
Serial.println("OK");
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
client.print("Temperature (C): ");
client.println((float)DHT.temperature, 1);
client.println("<br />");
client.println("<br />");
client.print("Temperature (F): ");
client.println(Fahrenheit(DHT.temperature), 1);
client.println("<br />");
client.println("<br />");
client.print("Humidity (%): ");
client.println((float)DHT.humidity, 0);
client.println("<br />");
client.println("<br />");
client.print("Temperature (K): ");
client.println(Kelvin(DHT.temperature), 1);
client.println("<br />");
client.println("<br />");
client.print("Dew Point (F): ");
client.println(dewPoint(DHT.temperature, DHT.humidity));
client.println("<br />");
client.println("<br />");
client.print("Dew PointFast (C): ");
client.println(dewPointFast(DHT.temperature, DHT.humidity));
client.println("<br />");
client.print("<br />");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(Fahrenheit( DHT. temperature));
lcd.print(" F");
lcd.setCursor(0,1);
lcd.print("Hum: ");
lcd.print((float)DHT.humidity);
lcd.print(" %");
/*--------( End Sensor Read )--------------------------------*/
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
} // END Loop
/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return celsius * 1.8 + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T) * 1.8 + 32;
}
// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
/* ( THE END ) */