Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1/15/2012
- */
- // include the library code:
- #include <LiquidCrystal.h>
- #include <Ethernet.h>
- #include <SPI.h>
- #include <WebServer.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // no-cost stream operator as described at
- // http://sundial.org/arduino/?page_id=119
- template<class T>
- inline Print &operator <<(Print &obj, T arg)
- { obj.print(arg); return obj; }
- // CHANGE THIS TO YOUR OWN UNIQUE VALUE
- static uint8_t mac[] = { 0xDE, 0xDE, 0xBE, 0xEF, 0xFE, 0xED };
- // CHANGE THIS TO MATCH YOUR HOST NETWORK
- static uint8_t ip[] = { 192, 168, 1, 20 };
- #define PREFIX ""
- WebServer webserver(PREFIX, 80);
- //My Stuff
- //int sensorPin = 0;
- int DesiredAuto = 32;
- int DesiredTemp = DesiredAuto;
- float temperatureC;
- float temperatureF;
- int hit_counter = 0;
- int loop_counter = 9999;
- long max_manual_millis = 360000000;
- long current_manual_millis = 0;
- long start_manual_millis = 0;
- int current_state = 1;
- const int ledPin = 13; // the number of the LED pin
- const int heaterPin = A4; // the number of the LED pin
- const int buttonPinUp = 6; // the number of the pushbutton pin
- const int buttonPinDown = 5; // the number of the pushbutton pin
- // Data wire is plugged into port 2 on the Arduino
- #define ONE_WIRE_BUS 2
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // arrays to hold device address
- DeviceAddress insideThermometer;
- // variables will change:
- int UpbuttonState = 0; // variable for reading the pushbutton status
- int DownbuttonState = 0; // variable for reading the pushbutton status
- int heater_on = 0;
- int system_on = 0;
- String status_msg = "";
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(7, 8, 9, A1, A2, A3);
- ///////////////////////////////////////////
- //Begin webduino Code
- ///////////////////////////////////////////
- // commands are functions that get called by the webserver framework
- // they can read any posted data from client, and they output to server
- void outputPins(WebServer &server, WebServer::ConnectionType type, bool addControls = false)
- {
- ++hit_counter;
- P(htmlHead) =
- "<html>"
- "<head>"
- "<title>Stew's Arduino Thermostat Web Server</title>"
- "<style type=\"text/css\">"
- "BODY { font-family: sans-serif }"
- "H1 { font-size: 14pt; text-decoration: underline }"
- "H2 { font-size: 12pt; }"
- "P { font-size: 10pt; }"
- ".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; }"
- ".success { color: #4F8A10;background-color: #DFF2BF;background-image:url('http://gurutech-sl.com/voting/success.png'); }"
- ".warning { color: #9F6000;background-color: #FEEFB3;background-image: url('http://gurutech-sl.com/voting/warning.png');}"
- ".error { color: #D8000C; background-color: #FFBABA; background-image: url('http://gurutech-sl.com/voting/error.png');}"
- "body { margin: 0px 20px 0px 20px; }"
- ".box { width: 400px; margin: 0px auto -1px auto; border: 1px solid; padding: 5px;}"
- "</style>"
- "</head>"
- "<body>";
- int i;
- server.httpSuccess();
- server.printP(htmlHead);
- server << "<div class='box'>";
- if(status_msg != "")
- {
- server << status_msg;
- status_msg = "";
- }
- server << "<center><h1>Stew's web-based thermostat</h1></center>";
- server << "<form action='" PREFIX "/form' method='post'>";
- server << "<p>System";
- server.radioButton("system_on", "1", "On", system_on);
- server << " ";
- server.radioButton("system_on", "0", "Off", !system_on);
- server << "<p>";
- server << "<p>Furnace:";
- if(heater_on)
- server << " ON";
- else
- server << " OFF";
- server << "<p>Mode:" << current_state;
- if(current_state == 3)
- server << " Count Down:" << current_manual_millis;
- server << "</p>";
- server << "<h2>Current Temp:</h1><p>" << (int)temperatureF << "F" ;
- if(system_on)
- {
- server << "<h2>Manual Setting:</h1><p>" << DesiredTemp << "F";
- server << " New: <input type='text' size = '2' name='desiredtemp'/>";
- server << "<h2>Automatic Target Temp:</h1><p>" << DesiredAuto << "F";
- server << " New: <input type='text' size = '2' name='desiredauto'/>";
- }
- server << "<br><input type='submit' value='Submit'/></form></div>";
- server << "</body></html>";
- }
- void formCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
- {
- if (type == WebServer::POST)
- {
- bool repeat;
- char name[16], value[16];
- int test_temp;
- do
- {
- repeat = server.readPOSTparam(name, 16, value, 16);
- Serial.print("|");Serial.print(name); Serial.print("|"); Serial.println(value);
- if((String)name == "desiredtemp" && system_on)
- {
- test_temp = strtoul(value,NULL,10);
- if(test_temp > 0 && test_temp < 100)
- {
- start_manual(test_temp);
- status_msg = "<div class='success'> Target temperature has been changed</div>";
- }
- else
- {
- status_msg = "<div class='error'> Target temperature must be between 0F and 75F</div>";
- }
- Serial.print("DT:"); Serial.print(DesiredTemp);
- }
- else if((String)name == "desiredauto" && system_on)
- {
- test_temp = strtoul(value,NULL,10);
- if(test_temp > 0 && test_temp < 100)
- {
- DesiredAuto = test_temp;
- //status_msg = "<div class='success'> Automatic Target temperature has been changed</div>";
- }
- else
- {
- //status_msg = "<div class='error'> Automatic Target temperature must be between 0F and 75F</div>";
- }
- }
- else if((String)name == "system_on")
- {
- system_on = strtoul(value,NULL,10);
- if(system_on)
- current_state = 2;
- else
- current_state = 1;
- }
- } while (repeat);
- server.httpSeeOther(PREFIX "/form");
- }
- else
- outputPins(server, type, true);
- }
- //////////////////////////////////
- //End Webduino Code
- //////////////////////////////////
- // function to print the temperature for a device
- void printTemperature(DeviceAddress deviceAddress)
- {
- // method 1 - slower
- //Serial.print("Temp C: ");
- //Serial.print(sensors.getTempC(deviceAddress));
- //Serial.print(" Temp F: ");
- //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
- // method 2 - faster
- float tempC = sensors.getTempC(deviceAddress);
- Serial.print("Temp C: ");
- Serial.print(tempC);
- Serial.print(" Temp F: ");
- Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
- }
- void setup() {
- //Webserver start
- Ethernet.begin(mac, ip);
- webserver.begin();
- webserver.addCommand("form", &formCmd);
- //Temperature Sense
- if (sensors.isParasitePowerMode()) Serial.println("ON");
- else Serial.println("OFF");
- if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
- Serial.print("Device 0 Address: ");
- printAddress(insideThermometer);
- Serial.println();
- //Pin change
- pinMode(A1, OUTPUT);
- pinMode(A2, OUTPUT);
- pinMode(A3, OUTPUT);
- pinMode(A4, OUTPUT);
- Serial.begin(9600);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- lcd.print("~~~~~booting~~~~");
- lcd.setCursor(0, 1);
- lcd.print("192.168.1.20");
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- pinMode(heaterPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPinUp, INPUT);
- pinMode(buttonPinDown, INPUT);
- delay(1200);
- }
- void loop() {
- /* process incoming connections one at a time forever */
- webserver.processConnection();
- if(current_state == 3)
- current_manual_millis = millis() - start_manual_millis;
- if(current_manual_millis > max_manual_millis)
- {
- DesiredTemp = DesiredAuto;
- current_state = 2;
- }
- ++loop_counter;
- if(loop_counter > 50)
- {
- read_temp();
- }
- read_buttons();
- display_data();
- delay(500);
- }
- char
- void start_manual(int new_temp)
- {
- DesiredTemp = new_temp;
- start_manual_millis = millis();
- current_state = 3;
- system_on = 1;
- }
- void read_buttons()
- {
- UpbuttonState = digitalRead(buttonPinUp);
- DownbuttonState = digitalRead(buttonPinDown);
- if (UpbuttonState != HIGH) {
- start_manual(DesiredTemp + 1);
- }
- if (DownbuttonState != HIGH) {
- start_manual(DesiredTemp - 1);
- }
- }
- void read_temp()
- {
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures(); // Send the command to get temperatures
- // It responds almost immediately. Let's print out the data
- //printTemperature(insideThermometer); // Use a simple function to print out the data
- loop_counter = 0;
- float temperatureC = sensors.getTempC(insideThermometer);
- Serial.println("Temp C:");
- Serial.println(temperatureC);
- // int reading = analogRead(sensorPin);
- // float voltage = reading * 5.0;
- // voltage /= 1024.0;
- //temperatureC = (voltage - 0.5) * 100 ;
- temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
- }
- void display_data()
- {
- lcd.setCursor(0, 0);
- lcd.print((int)temperatureF); lcd.print("F/");
- lcd.print(DesiredTemp); lcd.print("F ");
- lcd.print(" Hits: ");lcd.print(hit_counter);
- lcd.setCursor(0, 1);
- if(DesiredTemp > temperatureF)
- {
- heater_on = 1;
- }
- else
- {
- heater_on = 0;
- }
- if(heater_on == 1 && system_on)
- {
- lcd.print("Heat ON / ");
- lcd.print(current_state); lcd.print(" ");
- digitalWrite(ledPin, HIGH);
- digitalWrite(heaterPin, HIGH);
- }
- else if(system_on)
- {
- lcd.print("Heat OFF / ");
- lcd.print(current_state); lcd.print(" ");
- digitalWrite(ledPin, LOW);
- digitalWrite(heaterPin, LOW);
- }
- else
- {
- if(heater_on)
- {
- heater_on = 0;
- }
- lcd.print("System is OFF ");
- digitalWrite(ledPin, LOW);
- digitalWrite(heaterPin, LOW);
- }
- }
- // function to print a device address
- void printAddress(DeviceAddress deviceAddress)
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- if (deviceAddress[i] < 16) Serial.print("0");
- Serial.print(deviceAddress[i], HEX);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement