|
| 1 | +/* |
| 2 | + Web Server |
| 3 | + |
| 4 | + A simple web server that shows the value of the analog input pins. |
| 5 | + using an Arduino Wiznet Ethernet shield. |
| 6 | + |
| 7 | + Circuit: |
| 8 | + * Ethernet shield attached to pins 10, 11, 12, 13 |
| 9 | + * Analog inputs attached to pins A0 through A5 (optional) |
| 10 | + |
| 11 | + created 18 Dec 2009 |
| 12 | + by David A. Mellis |
| 13 | + modified 9 Apr 2012 |
| 14 | + by Tom Igoe |
| 15 | + |
| 16 | + */ |
| 17 | + |
| 18 | +#include <SPI.h> |
| 19 | +#include <EthernetV2_0.h> |
| 20 | +#include <Wire.h> |
| 21 | + |
| 22 | +// Enter a MAC address and IP address for your controller below. |
| 23 | +// The IP address will be dependent on your local network: |
| 24 | +byte mac[] = { |
| 25 | + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
| 26 | +IPAddress ip(10,1,1, 15); |
| 27 | + |
| 28 | +int tmp102Address = 0x48; |
| 29 | +float oldfahrenheit = 0; |
| 30 | + |
| 31 | +// Initialize the Ethernet server library |
| 32 | +// with the IP address and port you want to use |
| 33 | +// (port 80 is default for HTTP): |
| 34 | +EthernetServer server(80); |
| 35 | +#define W5200_CS 10 |
| 36 | +#define SDCARD_CS 4 |
| 37 | + |
| 38 | +void setup() { |
| 39 | + // Open serial communications and wait for port to open: |
| 40 | + Serial.begin(9600); |
| 41 | + pinMode(SDCARD_CS,OUTPUT); |
| 42 | + digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card |
| 43 | + // start the Ethernet connection and the server: |
| 44 | + Ethernet.begin(mac, ip); |
| 45 | + server.begin(); |
| 46 | + Serial.print("server is at "); |
| 47 | + Serial.println(Ethernet.localIP()); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +void loop() { |
| 52 | + // listen for incoming clients |
| 53 | + EthernetClient client = server.available(); |
| 54 | + if (client) { |
| 55 | + Serial.println("new client"); |
| 56 | + // an http request ends with a blank line |
| 57 | + boolean currentLineIsBlank = true; |
| 58 | + while (client.connected()) { |
| 59 | + if (client.available()) { |
| 60 | + char c = client.read(); |
| 61 | + Serial.write(c); |
| 62 | + // if you've gotten to the end of the line (received a newline |
| 63 | + // character) and the line is blank, the http request has ended, |
| 64 | + // so you can send a reply |
| 65 | + if (c == '\n' && currentLineIsBlank) { |
| 66 | + // send a standard http response header |
| 67 | + client.println("HTTP/1.1 200 OK"); |
| 68 | + client.println("Content-Type: text/html"); |
| 69 | + client.println("Connnection: close"); |
| 70 | + client.println(); |
| 71 | + client.println("<!DOCTYPE HTML>"); |
| 72 | + client.println("<html>"); |
| 73 | + // add a meta refresh tag, so the browser pulls again every 5 seconds: |
| 74 | + client.println("<meta http-equiv=\"refresh\" content=\"5\">"); |
| 75 | + // output the value of each analog input pin |
| 76 | + |
| 77 | + |
| 78 | + client.print("Temperature is "); |
| 79 | + client.print(oldfahrenheit); |
| 80 | + client.print(" F"); |
| 81 | + client.println("<br />"); |
| 82 | + |
| 83 | + client.println("</html>"); |
| 84 | + break; |
| 85 | + } |
| 86 | + if (c == '\n') { |
| 87 | + // you're starting a new line |
| 88 | + currentLineIsBlank = true; |
| 89 | + } |
| 90 | + else if (c != '\r') { |
| 91 | + // you've gotten a character on the current line |
| 92 | + currentLineIsBlank = false; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + // give the web browser time to receive the data |
| 97 | + delay(1); |
| 98 | + // close the connection: |
| 99 | + client.stop(); |
| 100 | + Serial.println("client disonnected"); |
| 101 | + } |
| 102 | + |
| 103 | + float celsius = getTemperature(); |
| 104 | + //Serial.print("Celsius: "); |
| 105 | + //Serial.println(celsius); |
| 106 | + |
| 107 | + |
| 108 | + float fahrenheit = (1.8 * celsius) + 32; |
| 109 | + |
| 110 | + if(abs(oldfahrenheit-fahrenheit)>.05) |
| 111 | + { |
| 112 | + oldfahrenheit=fahrenheit; |
| 113 | + Serial.print("Fahrenheit: "); |
| 114 | + Serial.println(fahrenheit); |
| 115 | +} |
| 116 | +} |
| 117 | + |
| 118 | +float getTemperature(){ |
| 119 | + Wire.requestFrom(tmp102Address,2); |
| 120 | + |
| 121 | + byte MSB = Wire.read(); |
| 122 | + byte LSB = Wire.read(); |
| 123 | + |
| 124 | + //it's a 12bit int, using two's compliment for negative |
| 125 | + int TemperatureSum = ((MSB << 8) | LSB) >> 4; |
| 126 | + |
| 127 | + float celsius = TemperatureSum*0.0625; |
| 128 | + return celsius; |
| 129 | +} |
| 130 | + |
0 commit comments