element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Temp Sensor question..
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 8 replies
  • Subscribers 392 subscribers
  • Views 814 views
  • Users 0 members are here
Related

Temp Sensor question..

medictrode
medictrode over 8 years ago

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 ) */

  • Sign in to reply
  • Cancel

Top Replies

  • medictrode
    medictrode over 8 years ago +2
    Thank you all for the replies, super appreciated. I suppose I wasn't clear, and I apologize for that. I built the circuit first to work off of a web interface, and it worked well. I also decided the LCD…
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to medictrode +2
    That's perfectly possible. To do that, move the LCD code out of these loops while (client.connected()) { if (client.available()) { } }
  • Jan Cumps
    Jan Cumps over 8 years ago +1
    This example requires that your Arduino is connected to a network, with an ethernet shield. It seems that this example also requires that a server is running on address 192.168.15.69 that listens to commands…
Parents
  • medictrode
    medictrode over 8 years ago

    Thank you all for the replies, super appreciated.

     

    I suppose I wasn't clear, and I apologize for that. I built the circuit first to work off of a web interface, and it worked well. I also decided the LCD screen was a good idea and I wish to use both methods of temperature display. It's nice to remotely check the temp in my garage for utility purposes, but also have the display while I'm physically in there. If this is possible of course...

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to medictrode

    That's perfectly possible.

    To do that, move the LCD code out of these loops

    while (client.connected()) {
          if (client.available()) {
          }
    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to medictrode

    That's perfectly possible.

    To do that, move the LCD code out of these loops

    while (client.connected()) {
          if (client.available()) {
          }
    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
Children
  • medictrode
    medictrode over 8 years ago in reply to Jan Cumps

    So I tried moving the LCD code out of those loops, both above and below with no luck.  If I put it before I'd get errors while compiling and after it would display the data but not refresh until a browser was connected, and the screen would flicker as well. I think additional code is needed to make it a separate operation, but I could be wrong. My lack of coding experience is inhibiting me and this silly little project, but I am gonna keep at it and see what I can come up with. Any help given has been and is appreciated. Thank you..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • rachaelp
    rachaelp over 8 years ago in reply to medictrode

    Hi George,

     

    I'll preface this with, I haven't done much with Arduino (yet) so others with more Arduino experience may be able to offer better advice but I think if you want the LCD and web interface to be completely independent then there are a couple of things you could do.

     

    1) Put the LCD update into an interrupt handler and set up a timer to trigger an interrupt at a sensible rate. The web interface needs to be in your main execution path because once a client is connected I think it stays in a loop to service the client so if that were in an interrupt it would freeze out the LCD while the client was connected (I think).

     

    2) Use protothreads. The Atmel devices on the Arduino are great little devices but you don't have an OS or multi-core so you can't do proper threading, but protothreads is a reasonable approximation to threading. See here: https://create.arduino.cc/projecthub/reanimationxp/how-to-multithread-an-arduino-protothreading-tutorial-dd2c37

     

    Best Regards,

     

    Rachael

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • beacon_dave
    beacon_dave over 8 years ago in reply to medictrode

    As well as moving the LCD display code outside of the Ethernet client loop, you will also need to read the sensor outside of the Ethernet client loop.

     

    Take a look at the location of this line in your code:

     

    int chk = DHT.read11(DHT11_PIN);

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to beacon_dave

    Yes, it would have been better if I had said:

    Put only the Ethernet relevant code in the Ethernet Client loop.

    Move any calculations and any LCD related code in front of that loop.

    That's less ambiguous.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube