In this session I take a network shield and hook it to my Arduinio mega. I then take a temp sensor and a tilt switch and produce a webpage when a browser is pointed at the IP address of the Arduino.
Parts Needed |
|
Make sure you set a correct IP address to match the network you are plugging into. Also it is recommended to make sure you are not in the DHCP address range. If you are not sure of the range of your network. Usually the highest numbers are not taken. 200 and above.
#include <SPI.h> #include <Ethernet.h> #include <math.h> double Thermister(int RawADC) { //Function to perform the fancy math of the Steinhart-Hart equation double Temp; Temp = log(((10240000/RawADC) - 10000)); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; // Convert Kelvin to Celsius Temp = (Temp * 9.0)/ 5.0 + 32.0; // Celsius to Fahrenheit - comment out this line if you need Celsius return Temp; } // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(10,0,1,177); // Change this to your IP address on your network // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } pinMode(A11, INPUT);//Only needed for temp sensor pinMode(A13, INPUT);//Only needed for tilt switch sensor // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // 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("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 5"); // refresh the page automatically every 5 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); // output the value of each analog input pin with tempurature client.println("<div>"); client.println("<span>Current Temperature is</span>"); client.println(Thermister(analogRead(A11))); client.println("</div>"); client.println("<div>"); client.println("<span>Switch position</span>"); client.println("</div>"); if(analogRead(A13) > 500) { client.println("Down"); }else { client.println("Up"); } client.println("</div>"); 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"); } }