Guest User

TCP_IP - Arduino Power Controller

a guest
Nov 24th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. //magikh0e.ihtb.org
  2.  
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #include <OneWire.h>
  6. #include <DallasTemperature.h>
  7.  
  8. #define ONE_WIRE_BUS 2
  9. OneWire oneWire(ONE_WIRE_BUS);
  10. DallasTemperature sensors(&oneWire);
  11.  
  12. DeviceAddress outsideThermometer = { 0x28, 0xB2, 0xBB, 0x3A, 0x04, 0x00, 0x00, 0x85 };
  13. DeviceAddress insideThermometer = { 0x28, 0xD8, 0x91, 0x3A, 0x04, 0x00, 0x00, 0xCC };
  14.  
  15.  
  16. byte mac[] = { 0xDA, 0xRP, 0xA0, 0xR0, 0xCK, 0xSS };
  17. byte ip[] = { 1, 2, 3, 4 };  // IP Address
  18. byte gateway[] = { 1, 2, 3, 1 }; // Default Gateway
  19. byte subnet[] = { 255, 255, 255, 0 }; // Netmask
  20. EthernetServer server(80);
  21.  
  22. String readString;
  23.  
  24. void setup(){
  25.  
  26.   pinMode(6, OUTPUT);
  27.  
  28.   Ethernet.begin(mac, ip, gateway, gateway, subnet);
  29.   server.begin();
  30.  
  31.   Serial.begin(9600);
  32.   Serial.println("TCP/IP Power Controller - v1.5");
  33.   Serial.println("By: magik");
  34.  
  35.    sensors.begin();
  36.   // set the resolution to 10 bit (good enough?)
  37.   sensors.setResolution(insideThermometer, 10);
  38.   sensors.setResolution(outsideThermometer, 10);
  39.  
  40. }
  41.  
  42. void printTemperature(DeviceAddress deviceAddress)
  43. {
  44.   float tempC = sensors.getTempC(deviceAddress);
  45.   if (tempC == -127.00) {
  46.     Serial.print("Error getting temperature");
  47.    } else {
  48.     Serial.print("C: ");
  49.     Serial.print(tempC);
  50.     Serial.print(" F: ");
  51.     Serial.print(DallasTemperature::toFahrenheit(tempC));
  52.   }
  53. }
  54.  
  55. void loop(){
  56.  
  57.   EthernetClient client = server.available();
  58.  
  59.   if (client) {
  60.     while (client.connected()) {
  61.       if (client.available()) {
  62.         char c = client.read();
  63.  
  64.         //read char by char HTTP request
  65.         if (readString.length() < 100) {
  66.  
  67.           //store characters to string
  68.           readString += c;
  69.           //Serial.print(c);
  70.         }
  71.  
  72.        
  73.         if (c == '\n') {
  74.           Serial.println(readString); //print to serial monitor for debuging
  75.  
  76.  
  77.           client.println("HTTP/1.1 200 OK");
  78.           client.println("Content-Type: text/html");
  79.           client.println();
  80.  
  81.           client.println("<HTML>");
  82.           client.println("<HEAD>");
  83.           client.println("<TITLE>Light Controller - By: magik</TITLE>");
  84.           client.println("</HEAD>");
  85.           client.println("<BODY>");
  86.  
  87.           client.println("<H1>TCP/IP Power Controller</H1>");
  88.      
  89.                
  90.           client.print("<b>Socket 1</b><BR>");
  91.                  
  92.           client.print("<input type=submit value='Light ON' style=width:100px;height:45px onClick=location.href='/?Status=Light1_ON;'>");
  93.           client.print("<input type=submit value='Light OFF' style=width:100px;height:45px onClick=location.href='/?Status=Light1_OFF;'>");
  94.         //  ALL OFF - client.print("&nbsp;<input type=submit value='ALL OFF' style=width:100px;height:45px onClick=location.href='/?off3579;'>");
  95.  
  96.           client.print("<BR><b>Socket 2</b><BR>");
  97.           client.print("<b>Socket 3</b><BR>");
  98.           client.print("<b>Socket 4</b><BR>");
  99.          
  100.           client.print("<BR><BR><b>Temperature(s): </b> <BR>");
  101.           client.print("Outside: ");
  102.          
  103.          client.print("<BR>Inside: ");
  104.        
  105.           client.print("<b>Weather Status: </b> <I>Not Connected</I><BR>");
  106.           client.print("<b>Humidity: </b> <I>Not Connected</I><BR>");
  107.           client.print("<b>GPRS Status: </b> <I>Not Connected</I><BR>");
  108.  
  109.           client.println("</BODY>");
  110.           client.println("</HTML>");
  111.  
  112.           delay(1);
  113.           client.stop();
  114.          
  115.           if(readString.indexOf('Light_1_ON') >0)
  116.           {
  117.             digitalWrite(6, HIGH);    
  118.             Serial.println("Light On");
  119.           }
  120.          
  121.           if(readString.indexOf('Light_1_OFF') >0)
  122.           {
  123.             digitalWrite(6, LOW);    
  124.              Serial.println("Light Off");
  125.           }
  126.          
  127.           readString="";
  128.  
  129.         }
  130.       }
  131.     }
  132.   }
  133.         //Pull stats, print to serial
  134.         delay(2000);
  135.         Serial.print("Getting Socket(s) Status\n\r");
  136.         Serial.print("Socket #1\n\r");
  137.         Serial.print("Getting temperatures...\n\r");
  138.         sensors.requestTemperatures();
  139.  
  140.         Serial.print("Inside temperature is: ");
  141.           printTemperature(insideThermometer);
  142.         Serial.print("\n\r");
  143.         Serial.print("Outside temperature is: ");
  144.           printTemperature(outsideThermometer);
  145.         Serial.print("\n\r");
  146.         Serial.print("\n\r\n\r");
  147. }
Advertisement
Add Comment
Please, Sign In to add comment