Go Down

Topic: Heating controller with LCD (Read 279 times) previous topic - next topic

svetnik

Feb 11, 2018, 02:03 pm Last Edit: Mar 11, 2018, 10:41 pm by svetnik
Hello. I'm new to the arduino programing and stuff. I was trying to make a heating controller for my tap water boiler. I was able to put some codes together and it works very fine. But, since i am beginner in arduino coding, i am asking for help. Below is the code that i created, but it is VERY messy. Could you please help me to organize it. And if you have any suggestions about code, please tell me.
Thank you very much.
 Code will be in two pieces.

Code: [Select]


#define SDCARDPin 4
#define DHTPIN_1 8 // what pin we're connected to
#define ONE_WIRE_BUS 9
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht1(DHTPIN_1, DHTTYPE);  //definicion of dht22
// Set up which Arduino pin will be used for the 1-wire interface to the sensor
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27,20,4);  // Set the LCD I2C address  <<<<< IDE complains of NEGATIVE
/*-----( Declare Variables )-----*/
float maxi = 0,mini = 100; // max/min temperature variables with initial values. LM35 in simple setup only measures Temp above 0.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress TEMPERATURA1 = {
  0x28, 0x0D, 0x7C, 0xA8, 0x04, 0x00, 0x00, 0x33 };
DeviceAddress TEMPERATURA2 = {
  0x28, 0x9E, 0x79, 0xD0, 0x04, 0x00, 0x00, 0xBD };
DeviceAddress TEMPERATURA3 = {
  0x28, 0xB9, 0x37, 0xD1, 0x04, 0x00, 0x00, 0x4A };
DeviceAddress TEMPERATURA4 = {
  0x28, 0xD7, 0x29, 0xD1, 0x04, 0x00, 0x00, 0x44 };
DeviceAddress TEMPERATURA5 = {
  0x28, 0x23, 0xD9, 0xD0, 0x04, 0x00, 0x00, 0xC3 };
DeviceAddress TEMPERATURA6 = {
  0x28, 0x2D, 0xF1, 0xD0, 0x04, 0x00, 0x00, 0x16 };
DeviceAddress TEMPERATURA7 = {
  0x28, 0xB5, 0xD1, 0x27, 0x06, 0x00, 0x00, 0x46 };

byte comaO[8] = {
  0b00111,
  0b00101,
  0b00111,
  0b00000,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

byte sumnikC[8] = {
  0b01010,
  0b00100,
  0b01110,
  0b10001,
  0b10000,
  0b10000,
  0b10001,
  0b01110
};

String cikData = "10cc8c57ac1a1847369423ec64ebc5ce5537d3f5"; // <-- FILL IN YOUR CIK HERE! (https://portals.exosite.com -> Add Device)
byte macData[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDB, 0x02}; // <-- FILL IN YOUR Ethernet shield's MAC address here.
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(80); //server is using port 84
// User defined variables for Exosite reporting period and averaging samples
#define REPORT_TIMEOUT 30000 //milliseconds period for reporting to Exosite.com
#define SENSOR_READ_TIMEOUT 10000 //milliseconds period for reading sensors in loop
class EthernetClient client;
Exosite exosite(cikData, &client);
const int analogInPin0 = A0;
const int analogInPin1 = A1;






void setup()
{
 
  pinMode(SDCARDPin, OUTPUT);
  digitalWrite(SDCARDPin, HIGH);
  Serial.begin(9600);
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines
  lcd.createChar(1, comaO);
  lcd.createChar(2, sumnikC);
  Serial.println("Boot");
  // Start up the OneWire Sensors library
  Serial.println("Starting Exosite Temp Monitor");
  Serial.print("OneWire Digital Pin Specified: ");
  Ethernet.begin(macData, ip);
  server.begin();
  dht1.begin();
  sensors.begin();
  // Nastavenie rozlisenia na 10 bitov
  sensors.setResolution(TEMPERATURA1, 10);
  sensors.setResolution(TEMPERATURA2, 10);
  sensors.setResolution(TEMPERATURA3, 10);
  sensors.setResolution(TEMPERATURA4, 10);
  sensors.setResolution(TEMPERATURA5, 10);
  sensors.setResolution(TEMPERATURA6, 10);
  sensors.setResolution(TEMPERATURA7, 10);
  lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Z.T.:");
  lcd.setCursor(0,1);
  lcd.print("T.PE");
  lcd.write(2);
  lcd.print("I: ");
  lcd.setCursor(0,2);
  lcd.print("T.BOJL:");
  lcd.setCursor(0,3);
  lcd.print("DELOVANJE:");
  lcd.setCursor(18,3);
  lcd.print("UR");
  // wait 3 seconds for connection
  delay(1000);
}



svetnik

Code: [Select]
void loop() {
 
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  sensors.requestTemperatures();          // Send the command to get temperatures
  float temp1 = sensors.getTempC(TEMPERATURA1);
  if(temp1 > maxi) {
    maxi = temp1;
  } // set max temperature
  if(temp1 < mini) {
    mini = temp1;
  } // set min temperature
  float temp2 = sensors.getTempC(TEMPERATURA2);
  float temp3 = sensors.getTempC(TEMPERATURA3);
  float temp4 = sensors.getTempC(TEMPERATURA4);
  float temp5 = sensors.getTempC(TEMPERATURA5);
  float temp6 = sensors.getTempC(TEMPERATURA6);
  float temp7 = sensors.getTempC(TEMPERATURA7);

  lcd.setCursor(6,0);
  lcd.print(t1);
  lcd.write(1);
  lcd.print(" ");
  lcd.print( h1);
  lcd.setCursor(8,1);
  lcd.print(temp1);
  lcd.write(1);
  lcd.setCursor(8,2);
  lcd.print(temp7);
  lcd.write(1);
  lcd.setCursor(12,3);
  lcd.print(millis()/1000/60/60);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.print ("<body style=background-color:#33CC99>");
          client.print("<h1 align=center><font color=red>TEMPERATURNA TIPALA!</font></h1>");   
          client.println("<br />");   
          client.println("<br />");
          /*----(Get sensor reading, calculate and print results )-----------------*/

          client.print("<p>ZUNANJA TEMPERATURA: ");
          client.print(t1);
          client.println("</p>");
          client.print("<p>ZUNANJA VLAGA: ");
          client.print(h1);
          client.println("</p>");
          client.print("<p>Temperatura peci: ");
          client.print(temp1);
          client.println("</p>");
          client.print("<p>Temperatura peci MAX: ");
          client.print(maxi);
          client.println("</p>");
          client.print("<p>Temperatura BOJLERJA: ");
          client.print(temp7);
          client.println("</p>");
          client.print("<p>Temperatura - dvizni vod - Ogrevanje: ");
          client.print(temp2);
          client.println("</p>");
          client.print("<p>Temperatura - povratni vod - Ogrevanje: ");
          client.print(temp3);
          client.println("</p>");
          client.print("<p>Temperatura - dvizni vod - bojler: ");
          client.print(temp4);
          client.println("</p>");
          client.print("<p>Temperatura - povratni vod - bojler: ");
          client.print(temp5);
          client.println("</p>");

          client.print("<p>Neprekinjeno delovanje ze: ");
          client.print(millis()/1000/60/60);
          client.println(" ur</p>");

          /*--------( End Sensor Read )--------------------------------*/
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    } // END Loop
  }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
static unsigned long sendPrevTime = 0;
static unsigned long sensorPrevTime = 0;
char buffer[7];
String readParam = "";
String writeParam = "";
String returnString = "";
Serial.print("."); // print to show running
// Read sensor every defined timeout period
if (millis() - sensorPrevTime > SENSOR_READ_TIMEOUT) {
Serial.println();
Serial.println("Requesting temperature...");
Serial.print("Humidity: ");
Serial.print(h1);
Serial.println(" % ..........DONE");
Serial.print("Celsius2: ");
Serial.print(t1);
Serial.println(" C ..........DONE");
Serial.print("Celsius3: ");
Serial.print(temp1);
Serial.println(" C ..........DONE");
Serial.print("Celsius4: ");
Serial.print(temp2);
Serial.println(" C ..........DONE");
Serial.print("Celsius5: ");
Serial.print(temp3);
Serial.println(" C ..........DONE");
Serial.print("Celsius6: ");
Serial.print(temp4);
Serial.println(" C ..........DONE");
sensorPrevTime = millis();
}
// Send to Exosite every defined timeout period
if (millis() - sendPrevTime > REPORT_TIMEOUT) {
Serial.println(); //start fresh debug line
Serial.println("Sending data to Exosite...");
readParam = ""; //nothing to read back at this time e.g. 'control&status' if you wanted to read those data sources
writeParam = "t="; //parameters to write e.g. 'temp=65.54' or 'temp=65.54&status=on'
//String tempValue = dtostrf(tC, 1, 2, buffer); // convert float to String, minimum size = 1, decimal places = 2
writeParam += h1; //add converted temperature String value
//writeParam += "&message=hello"; //add another piece of data to send
//--
writeParam +="&t2=";
//tempValue = dtostrf(tC2, 1, 2, buffer);
String tempValue = dtostrf(t1, 1, 2, buffer);
writeParam += tempValue;
//--
writeParam +="&t3=";
writeParam += temp1;
//--
writeParam +="&t4=";
writeParam += temp2;
//--
writeParam +="&t5=";
writeParam += temp3;
//--
writeParam +="&t6=";
writeParam += temp4;
//--
writeParam +="&t7=";
writeParam += temp5;
//--
writeParam +="&t8=";
writeParam += temp6;
//--
writeParam +="&t9=";
writeParam += temp7;
Serial.println("Exosite String");
Serial.println(writeParam);
//--
if ( exosite.writeRead(writeParam, readParam, returnString)) {
Serial.println("Exosite OK");
if (returnString != "") {
Serial.println("Response:");
Serial.println(returnString);
}
}
else {
Serial.println("Exosite Error");
}
sendPrevTime = millis(); //reset report period timer
Serial.println("done sending.");
}
delay(1000); //slow down loop
}

wildbill

Just getting rid of unnecessary blank lines would make it look neater.

Anytime you have variables called x1, x2, x3 etc, is a strong hint that you should look into arrays.

Your loop function is far too long for my taste. As a start, I'd refactor it into calls to three functions. One to read & display the temps on the LCD (which I'd subdivide again once it was working), one to handle serving web pages and one to deal with serial input.

Go Up