Advertisement
Guest User

Web Thermostat Arduino Code

a guest
Jan 19th, 2012
1,630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.81 KB | None | 0 0
  1. /*
  2. 1/15/2012
  3. */
  4. // include the library code:
  5. #include <LiquidCrystal.h>
  6. #include <Ethernet.h>
  7. #include <SPI.h>
  8. #include <WebServer.h>
  9. #include <OneWire.h>
  10. #include <DallasTemperature.h>
  11.  
  12.  
  13. // no-cost stream operator as described at
  14. // http://sundial.org/arduino/?page_id=119
  15. template<class T>
  16. inline Print &operator <<(Print &obj, T arg)
  17. { obj.print(arg); return obj; }
  18.  
  19.  
  20. // CHANGE THIS TO YOUR OWN UNIQUE VALUE
  21. static uint8_t mac[] = { 0xDE, 0xDE, 0xBE, 0xEF, 0xFE, 0xED };
  22.  
  23. // CHANGE THIS TO MATCH YOUR HOST NETWORK
  24. static uint8_t ip[] = { 192, 168, 1, 20 };
  25.  
  26. #define PREFIX ""
  27.  
  28. WebServer webserver(PREFIX, 80);
  29.  
  30.  
  31. //My Stuff
  32. //int sensorPin = 0;
  33. int DesiredAuto = 32;
  34. int DesiredTemp = DesiredAuto;
  35. float temperatureC;
  36. float temperatureF;
  37.  
  38. int hit_counter = 0;
  39. int loop_counter = 9999;
  40.  
  41. long max_manual_millis = 360000000;
  42. long current_manual_millis = 0;
  43. long start_manual_millis = 0;
  44.  
  45. int current_state = 1;
  46.  
  47. const int ledPin =  13;      // the number of the LED pin
  48. const int heaterPin =  A4;      // the number of the LED pin
  49. const int buttonPinUp = 6;     // the number of the pushbutton pin
  50. const int buttonPinDown = 5;     // the number of the pushbutton pin
  51. // Data wire is plugged into port 2 on the Arduino
  52. #define ONE_WIRE_BUS 2
  53.  
  54. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  55. OneWire oneWire(ONE_WIRE_BUS);
  56.  
  57. // Pass our oneWire reference to Dallas Temperature.
  58. DallasTemperature sensors(&oneWire);
  59.  
  60. // arrays to hold device address
  61. DeviceAddress insideThermometer;
  62.  
  63. // variables will change:
  64. int UpbuttonState = 0;         // variable for reading the pushbutton status
  65. int DownbuttonState = 0;         // variable for reading the pushbutton status
  66. int heater_on = 0;
  67. int system_on = 0;
  68. String status_msg = "";
  69.  
  70. // initialize the library with the numbers of the interface pins
  71. LiquidCrystal lcd(7, 8, 9, A1, A2, A3);
  72.  
  73. ///////////////////////////////////////////
  74. //Begin webduino Code
  75. ///////////////////////////////////////////
  76. // commands are functions that get called by the webserver framework
  77. // they can read any posted data from client, and they output to server
  78.  
  79.  
  80. void outputPins(WebServer &server, WebServer::ConnectionType type, bool addControls = false)
  81. {
  82.   ++hit_counter;
  83.   P(htmlHead) =
  84.     "<html>"
  85.     "<head>"
  86.     "<title>Stew's Arduino Thermostat Web Server</title>"
  87.     "<style type=\"text/css\">"
  88.     "BODY { font-family: sans-serif }"
  89.     "H1 { font-size: 14pt; text-decoration: underline }"
  90.     "H2 { font-size: 12pt; }"
  91.     "P  { font-size: 10pt; }"
  92.     ".info, .success, .warning, .error, .validation { border: 1px solid;margin: 10px 0px;padding:15px 10px 15px 50px;width:340px;background-repeat: no-repeat;background-position: 10px center; }"
  93.     ".success { color: #4F8A10;background-color: #DFF2BF;background-image:url('http://gurutech-sl.com/voting/success.png'); }"
  94.     ".warning { color: #9F6000;background-color: #FEEFB3;background-image: url('http://gurutech-sl.com/voting/warning.png');}"
  95.     ".error { color: #D8000C; background-color: #FFBABA; background-image: url('http://gurutech-sl.com/voting/error.png');}"
  96.     "body { margin: 0px 20px 0px 20px; }"
  97.     ".box { width: 400px; margin: 0px auto -1px auto; border: 1px solid; padding: 5px;}"
  98.     "</style>"
  99.     "</head>"
  100.     "<body>";
  101.    
  102.    
  103.  
  104.   int i;
  105.   server.httpSuccess();
  106.   server.printP(htmlHead);
  107.  
  108.   server << "<div class='box'>";
  109.    
  110.   if(status_msg != "")
  111.     {
  112.       server << status_msg;
  113.       status_msg = "";
  114.      
  115.     }
  116.    
  117.   server << "<center><h1>Stew's web-based thermostat</h1></center>";
  118.  
  119.   server << "<form action='" PREFIX "/form' method='post'>";
  120.  
  121.  
  122.   server << "<p>System";
  123.   server.radioButton("system_on", "1", "On", system_on);  
  124.   server << " ";
  125.   server.radioButton("system_on", "0", "Off", !system_on);
  126.   server << "<p>";
  127.  
  128.   server << "<p>Furnace:";
  129.  
  130.   if(heater_on)
  131.     server << " ON";
  132.   else
  133.     server << " OFF";
  134.  
  135.   server << "<p>Mode:" << current_state;
  136.  
  137.   if(current_state == 3)
  138.     server << " Count Down:" << current_manual_millis;
  139.  
  140.   server << "</p>";
  141.  
  142.   server << "<h2>Current Temp:</h1><p>" << (int)temperatureF << "F" ;
  143.  
  144.   if(system_on)
  145.   {
  146.     server << "<h2>Manual Setting:</h1><p>" << DesiredTemp << "F";
  147.     server << " New: <input type='text' size = '2' name='desiredtemp'/>";
  148.     server << "<h2>Automatic Target Temp:</h1><p>" << DesiredAuto << "F";
  149.     server << " New: <input type='text' size = '2' name='desiredauto'/>";
  150.   }
  151.  
  152.     server << "<br><input type='submit' value='Submit'/></form></div>";
  153.  
  154.   server << "</body></html>";
  155. }
  156.  
  157. void formCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
  158. {
  159.  
  160.   if (type == WebServer::POST)
  161.   {
  162.     bool repeat;
  163.     char name[16], value[16];
  164.     int test_temp;
  165.     do
  166.     {
  167.       repeat = server.readPOSTparam(name, 16, value, 16);
  168.      
  169.       Serial.print("|");Serial.print(name); Serial.print("|"); Serial.println(value);
  170.      
  171.       if((String)name == "desiredtemp" && system_on)
  172.       {
  173.         test_temp = strtoul(value,NULL,10);
  174.        
  175.         if(test_temp > 0 && test_temp < 100)
  176.         {
  177.           start_manual(test_temp);
  178.           status_msg = "<div class='success'> Target temperature has been changed</div>";
  179.          
  180.         }
  181.         else
  182.         {
  183.           status_msg = "<div class='error'> Target temperature must be between 0F and 75F</div>";
  184.         }
  185.         Serial.print("DT:"); Serial.print(DesiredTemp);
  186.       }
  187.       else if((String)name == "desiredauto" && system_on)
  188.       {
  189.         test_temp = strtoul(value,NULL,10);
  190.        
  191.         if(test_temp > 0 && test_temp < 100)
  192.         {
  193.           DesiredAuto = test_temp;
  194.           //status_msg = "<div class='success'> Automatic Target temperature has been changed</div>";
  195.         }
  196.         else
  197.         {
  198.           //status_msg = "<div class='error'> Automatic Target temperature must be between 0F and 75F</div>";
  199.         }
  200.        
  201.       }      
  202.      
  203.       else if((String)name == "system_on")
  204.       {
  205.         system_on = strtoul(value,NULL,10);
  206.    
  207.         if(system_on)
  208.           current_state = 2;
  209.         else
  210.           current_state = 1;
  211.          
  212.      
  213.       }
  214.      
  215.      
  216.     } while (repeat);
  217.  
  218.     server.httpSeeOther(PREFIX "/form");
  219.   }
  220.   else
  221.     outputPins(server, type, true);
  222. }
  223.  
  224.  
  225. //////////////////////////////////
  226. //End Webduino Code
  227. //////////////////////////////////
  228.  
  229. // function to print the temperature for a device
  230. void printTemperature(DeviceAddress deviceAddress)
  231. {
  232.   // method 1 - slower
  233.   //Serial.print("Temp C: ");
  234.   //Serial.print(sensors.getTempC(deviceAddress));
  235.   //Serial.print(" Temp F: ");
  236.   //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  237.  
  238.   // method 2 - faster
  239.   float tempC = sensors.getTempC(deviceAddress);
  240.   Serial.print("Temp C: ");
  241.   Serial.print(tempC);
  242.   Serial.print(" Temp F: ");
  243.   Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  244. }
  245.  
  246.  
  247. void setup() {
  248.  
  249.   //Webserver start
  250.   Ethernet.begin(mac, ip);
  251.   webserver.begin();
  252.  
  253.   webserver.addCommand("form", &formCmd);
  254.  
  255.   //Temperature Sense
  256.   if (sensors.isParasitePowerMode()) Serial.println("ON");
  257.   else Serial.println("OFF");
  258.  
  259.   if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
  260.   Serial.print("Device 0 Address: ");
  261.   printAddress(insideThermometer);
  262.   Serial.println();
  263.  
  264.   //Pin change
  265.  
  266.   pinMode(A1, OUTPUT);
  267.   pinMode(A2, OUTPUT);
  268.   pinMode(A3, OUTPUT);
  269.   pinMode(A4, OUTPUT);
  270.  
  271.   Serial.begin(9600);
  272.  
  273.   // set up the LCD's number of columns and rows:
  274.   lcd.begin(16, 2);
  275.   // Print a message to the LCD.
  276.   lcd.print("~~~~~booting~~~~");
  277.   lcd.setCursor(0, 1);
  278.   lcd.print("192.168.1.20");
  279.   // initialize the LED pin as an output:
  280.   pinMode(ledPin, OUTPUT);      
  281.   pinMode(heaterPin, OUTPUT);    
  282.   // initialize the pushbutton pin as an input:
  283.   pinMode(buttonPinUp, INPUT);  
  284.   pinMode(buttonPinDown, INPUT);
  285.  
  286.   delay(1200);
  287.  
  288. }
  289.  
  290. void loop() {
  291.  
  292.   /* process incoming connections one at a time forever */
  293.   webserver.processConnection();
  294.  
  295.   if(current_state == 3)
  296.     current_manual_millis = millis() - start_manual_millis;
  297.  
  298.   if(current_manual_millis > max_manual_millis)
  299.   {
  300.     DesiredTemp = DesiredAuto;
  301.     current_state = 2;
  302.   }
  303.    
  304.  
  305.   ++loop_counter;
  306.  
  307.   if(loop_counter > 50)
  308.   {
  309.      read_temp();
  310.   }
  311.  
  312.   read_buttons();
  313.   display_data();
  314.    
  315.   delay(500);  
  316. }
  317. char
  318. void start_manual(int new_temp)
  319. {
  320.     DesiredTemp = new_temp;
  321.     start_manual_millis = millis();
  322.     current_state = 3;
  323.     system_on = 1;
  324. }
  325. void read_buttons()
  326. {
  327.   UpbuttonState = digitalRead(buttonPinUp);
  328.   DownbuttonState = digitalRead(buttonPinDown);
  329.  
  330.   if (UpbuttonState != HIGH) {    
  331.     start_manual(DesiredTemp + 1);  
  332.        
  333.   }
  334.    
  335.   if (DownbuttonState != HIGH) {    
  336.     start_manual(DesiredTemp - 1);  
  337.   }  
  338. }
  339. void read_temp()
  340. {
  341.   Serial.print("Requesting temperatures...");
  342.   sensors.requestTemperatures(); // Send the command to get temperatures
  343.  
  344.  
  345.   // It responds almost immediately. Let's print out the data
  346.   //printTemperature(insideThermometer); // Use a simple function to print out the data
  347.  
  348.    loop_counter = 0;
  349.    float temperatureC = sensors.getTempC(insideThermometer);
  350.    Serial.println("Temp C:");
  351.    Serial.println(temperatureC);
  352. //   int reading = analogRead(sensorPin);      
  353. //   float voltage = reading * 5.0;
  354. //   voltage /= 1024.0;
  355.    
  356.    //temperatureC = (voltage - 0.5) * 100 ;    
  357.    temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  358. }
  359. void display_data()
  360. {
  361.    lcd.setCursor(0, 0);    
  362.    lcd.print((int)temperatureF);   lcd.print("F/");    
  363.    lcd.print(DesiredTemp); lcd.print("F ");
  364.    lcd.print(" Hits: ");lcd.print(hit_counter);
  365.    
  366.    lcd.setCursor(0, 1);
  367.    
  368.    if(DesiredTemp > temperatureF)
  369.    {
  370.      heater_on = 1;
  371.    }
  372.    else
  373.    {
  374.      heater_on = 0;
  375.    }
  376.    
  377.    if(heater_on == 1 && system_on)
  378.    {
  379.      lcd.print("Heat ON / ");
  380.      lcd.print(current_state); lcd.print("   ");
  381.      digitalWrite(ledPin, HIGH);
  382.      digitalWrite(heaterPin, HIGH);
  383.    }
  384.    else if(system_on)
  385.    {
  386.      lcd.print("Heat OFF / ");
  387.      lcd.print(current_state); lcd.print("   ");
  388.      digitalWrite(ledPin, LOW);
  389.      digitalWrite(heaterPin, LOW);
  390.    }
  391.    else
  392.    {
  393.      if(heater_on)
  394.      {
  395.        heater_on = 0;
  396.      }
  397.      
  398.      lcd.print("System is OFF   ");
  399.      digitalWrite(ledPin, LOW);
  400.      digitalWrite(heaterPin, LOW);
  401.    }
  402. }
  403.  
  404. // function to print a device address
  405. void printAddress(DeviceAddress deviceAddress)
  406. {
  407.   for (uint8_t i = 0; i < 8; i++)
  408.   {
  409.     if (deviceAddress[i] < 16) Serial.print("0");
  410.     Serial.print(deviceAddress[i], HEX);
  411.   }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement