<commit_before>

/*
 Arduino Powered Thermostat
 
 The project involves the abstraction of how a real thermostat works by using the thermostat library.
 
 You can declare one or more sensors attached to analog pins to retrieve the temperature.
 
 Each sensor can be calibrated during any time by putting the current temperature measured with a thermometer and
 the program will calculate a coefficent used to convert the raw value coming from analog input to the correct
 temperature float value. 
 
 You must define a reference sensor that is used to calculate the delta t with the other sensors and to activate the thermostat
 accordingly.

 The program provides a web interface accessible by network that shows in real time the values of each sensors and the state of all analog pins 
 and digital pins. All data is updated by ajax without refreshing the page, you can else calibrate each sensors, set the minimum delta t necessary to 
 activate the thermostat and save all settings to a simple text file on the SD card.

 The web pages are stored into the SD card and I wrote a simple editor to change the content of the files via web without removing everytime the SD card.
 You can else write new files to the SD card.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13 with SD card slot
 * Analog inputs attached to pins A0 through A5 
 
 
 created 18 Dec 2009
 by <NAME> 
 ideozone.it
 
 The web page is loaded from the SD card thanks to:
 http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-web-server/
 
 Sensors values in the page are updating via ajax thanks to:
 http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-read-switch-automatically-using-AJAX/
 
 REMEMBER (for Arduino one)
 do not use digital pin 0,1 as they are reserved for serial communication 
 do not use digital pin 10,11,12,13 as they are reserved for SPI bus 
 do not use digital pin 4 as it's used to select SD card
 */



#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <thermostat.h>

#define ANALOG_PIN_COUNT 6 // total number of analog pins
#define DIGITAL_PIN_COUNT 14 // total number of digital pins

// analog input pin for sensors
#define SOLAR_PANEL_SENSOR 0
#define ROCKET_STOVE_SENSOR 1
#define BOILER_SENSOR 2

// digital output pin for activate various devices
#define CIRCULATOR 5
#define SOLAR_PANEL_SOLENOID 6
#define ROCKET_STOVE_SOLENOID 7

// how many sensors the thermostat object can handle. it's used to dimensionate the sensors array
#define MAX_SENSORS_COUNT 5

// Uncomment to use byte values for the temperature although this doesn't seems to eat less memory
//#define TEMPERATURE_BYTE 1 

//uncomment this for dev mode
//#define DEVMODE 1

EthernetServer server(80);
char stringParam[15]; // it's used to store parsed string parameters from http requests. I declared global to avoid all series of problems caused by returning a string from a function in C
EthernetClient client;

// fires when the sensor overcome delta t
void onOvercome(TempSensor *sensor, Thermostat* thermostat) { 
  
  #if defined(DEVMODE)
    Serial.println();  
    Serial.print(F("sensor "));
    Serial.print(sensor->name);
    Serial.println(F(" overcome delta t"));
  #endif

   // TURN ON CIRCULATOR: remember that the relay modules works when the input pin have a LOW state
  digitalWrite(CIRCULATOR, LOW); 
  
  if (sensor->linkedOutputPin) { 
    digitalWrite(sensor->linkedOutputPin, LOW); 
  }
}

// fires when the sensor temperature return to values within delta t
void onNormal(TempSensor *sensor, Thermostat* thermostat) { 
  
  #if defined(DEVMODE)
    Serial.println();  
    Serial.print(F("sensor "));
    Serial.print(sensor->name);
    Serial.println(F(" return on normal values"));
  #endif
   
  if (sensor->linkedOutputPin) { 
    digitalWrite(sensor->linkedOutputPin, HIGH); 
  }
}

// TURN OFF CIRCULATOR only if all sensors are under delta t  
void onAllNormal(Thermostat* thermostat) { 
   digitalWrite(CIRCULATOR, HIGH);
}

// SENSORS DECLARATION
TempSensor boiler("boiler", BOILER_SENSOR); // feel the temperature of water inside the boiler
TempSensor solarPanel("solar panel", SOLAR_PANEL_SENSOR);
TempSensor stove("stove", ROCKET_STOVE_SENSOR); 

Thermostat thermostat;

//unsigned long loopTimer


void setup() {
    
  // Open serial communications 
  #if defined(DEVMODE)  
    Serial.begin(9600);
  #endif
  
  // Enter a MAC address and IP address for your controller below.
  byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  IPAddress ip(192,168,1,177);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  
  server.begin();
  
  #if defined(DEVMODE)
    Serial.print(F("server is at "));
    Serial.println(Ethernet.localIP());  
  
    // initialize SD card
    Serial.println(F("Initializing SD card..."));
  #endif
  
  if (!SD.begin(4)) {
    #if defined(DEVMODE) 
      Serial.println(F("ERROR - SD card initialization failed!"));
    #endif 
    return;    // init failed
  }
  #if defined(DEVMODE) 
    Serial.println(F("SUCCESS - SD card initialized."));
  #endif 
  
  // set the digital pins output to HIGH to not activate the relays module on startup. Remember that relay modules works when the input state is LOW
  pinMode(CIRCULATOR, OUTPUT);    
  digitalWrite(CIRCULATOR, HIGH);    
 
  // link output pins to the sensors 
  solarPanel.linkOutput(SOLAR_PANEL_SOLENOID, HIGH);
  stove.linkOutput(ROCKET_STOVE_SOLENOID, HIGH);
 
  analogReference (INTERNAL);   // 1.1V reference: improve the precision especially if simple diodes are used as temperature sensors
  delay(300);     // let voltages settle
  
  thermostat.addReference(&boiler); // reference sensor: the thermostat will act upon calculating the delta t between this sensor and the others
  
  // other sensors:
  thermostat.addSensor(&solarPanel);
  thermostat.addSensor(&stove);
  
  thermostat.onOvercome = &onOvercome; // callback function that fires when a sensor temperature overcome the minimum delta t
  thermostat.onNormal = &onNormal; // callback function that fires when the sensor value return within the minimum delta t
  
  loadSettings("set.txt"); 
}


void loop() {
  
  // check the sensors temperature against the reference temperature and call appropriate callbacks <commit_msg>bug fixed<commit_after>

/*
 Arduino Powered Thermostat
 
 The project involves the abstraction of how a real thermostat works by using the thermostat library.
 
 You can declare one or more sensors attached to analog pins to retrieve the temperature.
 
 Each sensor can be calibrated during any time by putting the current temperature measured with a thermometer and
 the program will calculate a coefficent used to convert the raw value coming from analog input to the correct
 temperature float value. 
 
 You must define a reference sensor that is used to calculate the delta t with the other sensors and to activate the thermostat
 accordingly.

 The program provides a web interface accessible by network that shows in real time the values of each sensors and the state of all analog pins 
 and digital pins. All data is updated by ajax without refreshing the page, you can else calibrate each sensors, set the minimum delta t necessary to 
 activate the thermostat and save all settings to a simple text file on the SD card.

 The web pages are stored into the SD card and I wrote a simple editor to change the content of the files via web without removing everytime the SD card.
 You can else write new files to the SD card.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13 with SD card slot
 * Analog inputs attached to pins A0 through A5 
 
 
 created 18 Dec 2014
 by <NAME> 
 ideozone.it
 
 The web page is loaded from the SD card thanks to:
 http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-web-server/
 
 Sensors values in the page are updating via ajax thanks to:
 http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-read-switch-automatically-using-AJAX/
 
 REMEMBER (for Arduino one)
 do not use digital pin 0,1 as they are reserved for serial communication 
 do not use digital pin 10,11,12,13 as they are reserved for SPI bus 
 do not use digital pin 4 as it's used to select SD card
 */



#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <thermostat.h>

#define ANALOG_PIN_COUNT 6 // total number of analog pins
#define DIGITAL_PIN_COUNT 14 // total number of digital pins

// analog input pin for sensors
#define SOLAR_PANEL_SENSOR 0
#define ROCKET_STOVE_SENSOR 1
#define BOILER_SENSOR 2

// digital output pin for activate various devices
#define CIRCULATOR 5
#define SOLAR_PANEL_SOLENOID 6
#define ROCKET_STOVE_SOLENOID 7

// how many sensors the thermostat object can handle. it's used to dimensionate the sensors array
#define MAX_SENSORS_COUNT 5

// Uncomment to use byte values for the temperature although this doesn't seems to eat less memory
//#define TEMPERATURE_BYTE 1 

//uncomment this for dev mode
//#define DEVMODE 1

EthernetServer server(80);
char stringParam[15]; // it's used to store parsed string parameters from http requests. I declared global to avoid all series of problems caused by returning a string from a function in C
EthernetClient client;

// fires when the sensor overcome delta t
void onOvercome(TempSensor *sensor, Thermostat* thermostat) { 
  
  #if defined(DEVMODE)
    Serial.println();  
    Serial.print(F("sensor "));
    Serial.print(sensor->name);
    Serial.println(F(" overcome delta t"));
  #endif

   // TURN ON CIRCULATOR: remember that the relay modules works when the input pin have a LOW state
  digitalWrite(CIRCULATOR, LOW); 
  
  if (sensor->linkedOutputPin) { 
    digitalWrite(sensor->linkedOutputPin, LOW); 
  }
}

// fires when the sensor temperature return to values within delta t
void onNormal(TempSensor *sensor, Thermostat* thermostat) { 
  
  #if defined(DEVMODE)
    Serial.println();  
    Serial.print(F("sensor "));
    Serial.print(sensor->name);
    Serial.println(F(" return on normal values"));
  #endif
   
  if (sensor->linkedOutputPin) { 
    digitalWrite(sensor->linkedOutputPin, HIGH); 
  }
}

// TURN OFF CIRCULATOR only if all sensors are under delta t  
void onAllNormal(Thermostat* thermostat) { 
   digitalWrite(CIRCULATOR, HIGH);
}

// SENSORS DECLARATION
TempSensor boiler("boiler", BOILER_SENSOR); // feel the temperature of water inside the boiler
TempSensor solarPanel("solar panel", SOLAR_PANEL_SENSOR);
TempSensor stove("stove", ROCKET_STOVE_SENSOR); 

Thermostat thermostat;


void setup() {
    
  // Open serial communications 
  #if defined(DEVMODE)  
    Serial.begin(9600);
  #endif
  
  // Enter a MAC address and IP address for your controller below.
  byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  IPAddress ip(192,168,1,177);
  
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  
  server.begin();
  
  #if defined(DEVMODE)
    Serial.print(F("server is at "));
    Serial.println(Ethernet.localIP());  
  
    // initialize SD card
    Serial.println(F("Initializing SD card..."));
  #endif
  
  if (!SD.begin(4)) {
    #if defined(DEVMODE) 
      Serial.println(F("ERROR - SD card initialization failed!"));
    #endif 
    return;    // init failed
  }
  #if defined(DEVMODE) 
    Serial.println(F("SUCCESS - SD card initialized."));
  #endif 
  
  // set the digital pins output to HIGH to not activate the relays module on startup. Remember that relay modules works when the input state is LOW
  pinMode(CIRCULATOR, OUTPUT);    
  digitalWrite(CIRCULATOR, HIGH);    
 
  // link output pins to the sensors 
  solarPanel.linkOutput(SOLAR_PANEL_SOLENOID, HIGH);
  stove.linkOutput(ROCKET_STOVE_SOLENOID, HIGH);
 
  analogReference (INTERNAL);   // 1.1V reference: improve the precision especially if simple diodes are used as temperature sensors
  delay(300);     // let voltages settle
  
  thermostat.addReference(&boiler); // reference sensor: the thermostat will act upon calculating the delta t between this sensor and the others
  
  // other sensors:
  thermostat.addSensor(&solarPanel);
  thermostat.addSensor(&stove);
  
  thermostat.onOvercome = &onOvercome; // callback function that fires when a sensor temperature overcome the minimum delta t
  thermostat.onNormal = &onNormal; // callback function that fires when the sensor value return within the minimum delta t
  thermostat.onAllNormal = &onAllNormal; // callback function that fires when all sensors are within the minimum delta t
  
  loadSettings("set.txt"); 
}


void loop() {
  
  // check the sensors temperature against the reference temperature and call appropriate callbacks 
{'fraction_non_alphanumeric': 0.07386541471048513, 'fraction_numerical': 0.0107981220657277, 'mean_word_length': 3.8193815987933637, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556712', 'n_tokens_mistral': 3951, 'n_tokens_neox': 3626, 'n_words': 1523}