Stap 3: Trek het programma samen
De eerste stap in het maken van het programma eigenlijk werken was om de temp sensoren werken. Ze vormen de kern van de thermostaat, en zonder hen, de oven niet weet wanneer op of uit te schakelen.
Tijd om een paar bibliotheken.
De temperatuursensor is vrij koel, omdat een enkele draad wordt gebruikt om gegevens te verzenden naar de Arduino. Wat meer is, heeft elke sensor een adres, een uniek nummer waarmee de sensor. Dit is echt spannend, omdat het betekent dat je kunt meerdere sensoren die het verzenden van hun ingangen in de dezelfde pin op de Arduino (een groot probleem wanneer je begint te kijken naar wat nodig is om de stekker in de 16 x 2 LCD-scherm. PIN ruimte is op een premie!).
Om dit te doen, noemen we op een aantal bibliotheken die al bestaan - geen noodzaak om het wiel opnieuw uitvinden. De twee bibliotheken zijn privacy en DallasTemperature. Beide zijn vrij verkrijgbaar en handig zal komen als we vooruit. Wordt niet vertrouwd zijn met één van beide één van deze bibliotheken, ik weer waarnaar de interwebs en vond een vrij interessante tutorial op slechts dat ding. Alles wat ik moest doen was aanpassen aan mijn behoeften.
In de hacktronics leerprogramma doorlopen, is het dat de Temp van de DS18B20 sensoren adresseerbare - dat wil zeggen, dat zij worden vertegenwoordigd door een specifiek adres vermeld. Dit is wat laat voor meerdere sensoren om te rijden in de Arduino op dezelfde pincode. Dus als we vooruit gaan, moeten we deze temperatuur sensor adressen vinden. Hacktronics biedt een andere tutorial die Hoe vindt u de adressen, ook schetst.
Privacy-bibliotheek
DallasTemperature bibliotheek
Code tot nu toe: (alot van spullen is uitgecommentarieerd want er nog een heleboel veronderstellingen en onbekenden zijn, maar ik denk dat dit is hoe de code zal uiteindelijk uit te spelen. We zullen zien)
/* This is the application that I will be using to control the furnace. The concept is this: I have 3 zones I will be monitoring. Each zone will have at least one zone damper which will be controlled by the arduino application. Each zone will have a single thermometer which will report back to the controller. Depending on the thermometer reading and the thermostat setting, the dampers will open or shut and the central furnace will be turned on. Once an acceptable temperature has been reached in all zones, the furnace will turn off. In addition to general heating cycles, the system will be programmable. At this time, however, the programming cycle will be handled here, not via the thermostat. IMPORTANT TEST CASES: (to be added as I think of them/come across them * Need to ensure that the furnace is always off if all 3 dampers are closed. */ #include //This is a library needed for the LCD display #include //This is a library needed for the thermometer readings #include //This is a temperature library #include // Connections: // rs (LCD pin 4) to Arduino pin 12 // rw (LCD pin 5) to Arduino pin 11 // enable (LCD pin 6) to Arduino pin 10 // LCD pin 15 to Arduino pin 13 // LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2 LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); const int backlight = 13; //controls backlight #define THERM_BUS 6 // all temperature readings come in via this single pin. OneWire oneWire(THERM_BUS); DallasTemperature sensors(&oneWire); DeviceAddress zone1Therm = {0x28, 0x48, 0x39, 0x11, 0x04, 0x00, 0x00, 0x94}; // START PROGRAMMING // Times are 6 AM, 8 AM, 3PM, and 9PM int* PROGRAMMED_VALUES[7]; int SUNDAY_VALUES[4] = {67, 67, 67, 60}; int MONDAY_VALUES[4] = {67, 67, 67, 60}; int TUESDAY_VALUES[4] = {67, 67, 67, 60}; int WEDNESDAY_VALUES[4] = {67, 67, 67, 60}; int THURSDAY_VALUES[4] = {67, 67, 67, 60}; int FRIDAY_VALUES[4] = {67, 67, 67, 60}; int SATURDAY_VALUES[4] = {67, 67, 67, 60}; //END PROGRAMMING float zone1Temp = 0; float zone2Temp = 0; float zone3Temp = 0; boolean isOverrideHeld = false; boolean isOverridden = false; boolean furnaceState = false; //with furnaceState, false means it's currently off, true means its currently on. int overrideValue = 0; const int furnacePin = 1; void setup() { Serial.begin(9600); // This connects the arduino to the RPi sensors.begin(); sensors.setResolution(zone1Therm, 10); //SETUP OUTPUTS pinMode(furnacePin, OUTPUT); //SETTING UP THE WEEKLY PROGRAM PROGRAMMED_VALUES[0] = SUNDAY_VALUES; PROGRAMMED_VALUES[1] = MONDAY_VALUES; PROGRAMMED_VALUES[2] = TUESDAY_VALUES; PROGRAMMED_VALUES[3] = WEDNESDAY_VALUES; PROGRAMMED_VALUES[4] = THURSDAY_VALUES; PROGRAMMED_VALUES[5] = FRIDAY_VALUES; PROGRAMMED_VALUES[6] = SATURDAY_VALUES; } // this runs the continual loop void loop() { delay(1000); log("000", "Getting Temperature"); sensors.requestTemperatures(); zone1Temp = getTemperature(zone1Therm); log("001", (String)(int)zone1Temp); // if(c1 || c2 || c3) { // if(!furnaceState) { // startFurnace(); // } // TODO - DETERMINE THE CORRECT PINS FOR THE DAMPERS. WILL THE DAMPERS BE DIGITAL OR ANALOG? // if(c1) { // openDamper(1); // } // if(c2) { // openDamper(2); // } // if(c3) { // openDamper(3); // } // } // else { // if(furnaceState){ // stopFurnace(); // } // } // if(!c1) { // closeDamper(damper1Pin); // } // if(!c2) { // closeDamper(damper2Pin); // } // if(!c3) { // closeDamper(damper3Pin); // } } float getTemperature(DeviceAddress deviceAddress) { float tempC = sensors.getTempC(deviceAddress); //Serial.println(tempC); if(tempC == -127){ Serial.println("ERROR getting temperature."); } return DallasTemperature::toFahrenheit(tempC); } //convenience function. Handles all the boilerplate for writing to the LCDScreen void toScreen(String line1Value, String line2Value) { lcd.begin(16,2); lcd.clear(); lcd.setCursor(0,0); lcd.print(line1Value); lcd.setCursor(0,1); lcd.print(line2Value); } int getTime() { return -1; } void openDamper(int damper) { // if(damper == damper1Pin || damper == damper2Pin || damper == damper3Pin) { // digitalWrite(damper, HIGH); // } else { // log("Problem opening damper. Damper not found. Was expecting 1, 2, or 3. Got " + damper); // } } void closeDamper(int damper) { // if(damper == damper1Pin || damper == damper2Pin || damper == damper3Pin) { // digitalWrite(damper, LOW); // } else { // log("Problem closing damper. Damper not found. Was expecting 1, 2, or 3. Got " + damper); // } } //function for logging messages to the console. //Eventually plan to write to logfile on RPi void log(String code, String message) { Serial.println(code + ": " + message); } void recordData(String type, String object, String value) { // TODO - output to RPi database } void startFurnace(){ //Send appropriate signal to the furnace to start up if(furnaceState != 0) { digitalWrite(furnacePin, HIGH); } } void stopFurnace(){ //Send appropriate signal to the furnace to stop if(furnaceState == 1){ digitalWrite(furnacePin, LOW); } }