Neil Roberts Brewhouse

Neil Roberts Brewhouse

This is the source code for the temperature monitor that's being trended on the main page. Yes this is my first time coding up an Arduino and it shows. But it is functional on an UNO, although I'm upgrading to a MEGA since I've ran out of memory and want to add more functionality.


/*
relaytest
 
 This routine monitors a digital refernce(current switch) and switches a relay after a defined dropout delay.
 In addition it monitors multiple 1-wire temperature sensors and provides that data to a 16X2 LCD which
 uses a 5 pushbutton panel to scroll through the device temperatures and relay status. This information is 
 posted to a PHP file that saves the data in an MySQL db.
 */
#include 
#include 
#include 
//#include 
#include 
//#include 
#include 
#include 
//#include 
//DS3231 clock;
// comment out next line to write to SD from FTP server

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
//1023= no button , sw1=0 , sw2=140 , sw3=324 , sw4=498 , sw5=736
// Data wire is plugged into pin 8 on the Arduino
#define ONE_WIRE_BUS 8
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer = { 
  0x28, 0xFB, 0xF2, 0xBB, 0x05, 0x00, 0x00, 0x81 };

DeviceAddress pipeThermometer = {
  0x28, 0xE0, 0xDA, 0xBB, 0x05, 0x00, 0x00, 0xE4 };
#define FTPWRITE  
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,98);
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change to your server
//IPAddress server("xxx.xxx.xxx.xxx");
char server[] = "xxx.xxx.xxx.xxx";
EthernetClient client;
EthernetClient dclient;  
char fileName[] = "xxx.xxx";
int done = 0;
int loop1=0;
//File myFile;
char timebuf2[3];
char timebuf[4];
//char timebuf3[6];
char outCount;
//int tempcount;
int timebuftemp;
int pushButton = 2;
int holder=0;
int holder2=0;
int relay1 = 5;
int relay2 = 6;
int pwrpin = 3;
int pwrpin2 = 4;
int pwrpin3 = 7;
int pwrpin9 = 9;
int indsz = 0;
int sensorValue;
int button;
int buttonout;
int bchange;
int x2;
int txttot;
int txttot2=1;
int timeout1 = 0;
int unitrun = 0;
int active;
String data;
//int inittempcount = 0;
boolean buttonState;
boolean init3=1;
boolean b5 = 1;
boolean b5old = 0;
boolean x3[12] = {
  0,0,0,0,0,0,0,0,0,0,0,0};
unsigned long time1a=millis();
unsigned long time2a=400;
boolean x4;
int bg = 0;
int en = 12;
int offset = 4;
int offset2 = 2;
int zz = 1;
int init1 = 0;
int positionCounter = 0;
uint8_t n1[8] = {  
  B11000,  B11100,  B11100,  B11110,  B11110,  B11011,  B11011,  B11001};
uint8_t n2[8] = {  
  B00011,  B00011,  B00011,  B00011,  B00011,  B00011,  B00011,  B00011};
uint8_t n3[8] = {  
  B11000,  B11000,  B11000,  B11000,  B11000,  B11000,  B11000,  B11000,};
uint8_t n4[8] = {  
  B10011,  B11011,  B11011,  B01111,  B01111,  B00111,  B00111,  B00011};
unsigned long time1;
unsigned long time2 = 1000; //Relay delay time in ms - 604800000 ms for 1 week
unsigned long time3 = 60000; //LCD screen timeout
unsigned long time4 = 5000; //temperature sample time (msec)
unsigned long time5 = time4 +millis(); 
unsigned long time6;
unsigned long time7=millis() + time3;
unsigned long time8= 60000; //FTP refresh
unsigned long time9=millis() + time8;

unsigned long timeleft;
unsigned long btime1;
//unsigned long btime2 = 100;
int sd2=0;
float tempf = 0;
float tempfpipe = 0;
float tempfpipeold = 0;
float tempfold = 1;
char* txt1[]={  
  "Fridge          ","Timer          ","Bock Temp       ","Lager Temp      "};
char* txt2[4];
char* txt2old[4];
int bindex = 0;
int bindex2 = 1;
int bindexold;
int bindexold2;
// the setup routine runs once when you press reset:
void setup(void) {
  sensors.begin();
//  clock.begin();
  // set the resolution to 12 bit
  sensors.setResolution(insideThermometer, 12);
  sensors.setResolution(pipeThermometer, 12);
  pinMode(pushButton, INPUT_PULLUP);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(pwrpin, OUTPUT);
  pinMode(pwrpin2, OUTPUT);
  pinMode(pwrpin3, OUTPUT);
  pinMode(pwrpin9, OUTPUT);
  digitalWrite(pwrpin9, HIGH);
  lcd.begin(16,2);
  lcd.createChar(0, n1);
  lcd.createChar(1, n2);
  lcd.createChar(2, n3);
  lcd.createChar(3, n4);
  lcd.clear();

  Serial.begin(9600);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);


  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);
 delay(2000);
// data = "";
}

//Function to post data to SQL
byte dowebpost(){
Serial.println("Starting Post");  
        
        data="";
        data+="temp1=";
        data+=(int)(tempf*100);
        data+="&temp2=";
        data+=(int)(tempfpipe*100);
        data+="&unitrun=";
        data+=(unitrun);
        data+="&active=";
        data+=(active);

        
Serial.println(data);
	if (client.connect("xxx.xxx.xxx",80)) { // REPLACE WITH YOUR SERVER ADDRESS

Serial.println("connected");
		client.println("POST /add.php HTTP/1.1"); 
		client.println("Host: xxx.xxx.xxx"); // SERVER ADDRESS HERE TOO
		client.println("Content-Type: application/x-www-form-urlencoded"); 
		client.print("Content-Length: "); 
		client.println(data.length()); 
		client.println(); 
		client.print(data); 
               delay(1000);
	} 

	if (client.connected()) { 
		client.stop();	// DISCONNECT FROM THE SERVER
	}
}


//Function - Get 1 wire temperatures
float writetemp(DeviceAddress deviceAddress)
{
  float tempf = sensors.getTempF(deviceAddress);
  return tempf;
}

//Function -Get Button State from analog 5 PB panel
int buttonpress(int analogpin)
{
  int buttonraw;
  int buttonval;

  buttonraw = analogRead(analogpin);
  if (buttonraw > 989)
  {
    buttonval = 0;
  }
  else if (buttonraw > 711)
  {
    buttonval = 5;
  }
  else if (buttonraw > 473)
  {
    buttonval = 4;
  }
  else if (buttonraw > 299)
  {
    buttonval = 3;
  }
  else if (buttonraw > 115)
  {
    buttonval = 2;
  }
  else if(buttonraw < 25)
  {
    buttonval = 1;
  }

  return buttonval;
}

//Function - Check if all values in boolean array = 1. Used for LCD splagh screen animation
boolean boolminarray(boolean *arrayname){
  for (int qw=0; qw <= sizeof(arrayname)-1; qw++){
    boolean minval = min(arrayname[qw], 1);
    if(minval==0){
      return minval;
      qw=sizeof(arrayname)+5;
    }
    else if (qw == sizeof(arrayname)-1){
      return minval;
    }
  }
}



void loop(void) {


Serial.println(F("1"));

  // Run after initialization / lcd splash screen animation complete
  if (x4==1 & zz>4 & positionCounter>=18){
    //Set digital pins to provide power to devices, record current switch, and read pushbutton panel
    digitalWrite(pwrpin, HIGH);
    digitalWrite(pwrpin2, HIGH);
    digitalWrite(pwrpin3, HIGH);
    digitalWrite(pwrpin9, b5);
    button = buttonpress(A3);
    buttonState = digitalRead(pushButton);
    delay(5);
Serial.println(F("2"));
    //Read temperature data from 1 wire sensors
    if ((millis() > time5)){
      sensors.requestTemperatures();
      tempf = (writetemp(insideThermometer) - 1.6);
      tempfpipe = (writetemp(pipeThermometer) - 1);
      
     
      if (tempf != tempfold || tempfpipe != tempfpipeold)
      {
        tempfold = tempf;
        tempfpipeold = tempfpipe;
        txttot2 = 1;
      }
      time5 = (time4 + millis());
    } 

Serial.println(F("3"));
    //Set UV light relay based on current switch status and delay timer
    if (buttonState == 1 )
    {
      if (relay2 != LOW){
        digitalWrite(relay2,LOW);
      }
      if(holder != buttonState)
      {
        holder = buttonState;
        holder2 = buttonState;
        timeleft = (time2/1000);
        unitrun++;
        delay(1);
      }
    }
    else
    {
      //Timer start time
      if (buttonState == 0 & holder != buttonState)
      {
        time1=millis();
        time6=millis();
        holder = buttonState;
        delay (1);
      }
      //Timer expired, dropout relay
      else if (buttonState == 0 & millis() >= (time1+time2) )
      {
        digitalWrite(relay2,HIGH);
        timeleft = 0;
        if  (holder2 != buttonState){
          holder2 = buttonState;
        }
      }
      else if (buttonState == 0 & millis() < (time1+time2) )
      {
        //Time remaining prior to relay dropout
        timeleft = ((time1+time2-millis())/1000); 
        if(time6 < millis()  & (holder2 != buttonState))
        {
          btime1 = millis();
          time6 = time6 + 1000;
          txttot2=1;
        }
      }
    }
    Serial.println(F("4"));
    //Write LCD text array current relay values
    if (buttonState == 1 )
    {
      txt2[0] = "Off ";
      active = 0;
      char lcdbuf[15];
      dtostrf(timeleft,8, 0, lcdbuf);
      txt2[1]=lcdbuf;
    }
    if (buttonState == 0 & holder2 ==1)
    {
      txt2[0] = "Off ";
      active = 0;
      char lcdbuf[15];
      dtostrf(timeleft,8, 0, lcdbuf);
      txt2[1]=lcdbuf;   

    }
    if (holder2 == 0 & buttonState ==0)
    {
      txt2[0] = "On  ";
      active = 1;
      char lcdbuf[15];
      dtostrf(timeleft,8, 0, lcdbuf);
      txt2[1]=lcdbuf;

    }

    //Scroll logic based on PB panel input
    if (button != bchange)
    {
      buttonout = button;
      time7 = millis() + time3;

      if (buttonout == 3)
      {
        bindex=bindex+1;
        //delay(5);
      }
      if (bindex == 4)
      {
        bindex = 0;

      }
      if (buttonout == 2)
      {
        bindex=bindex-1;
        //delay(5);
      }
      if (bindex == -1)
      {
        bindex=3;
      }
      if (buttonout == 5)
      {
        // Screen on/off toggle based on PB #5
        if (b5 < 1){
          b5 = HIGH;
          digitalWrite(pwrpin9, b5);
          lcd.begin(16,2);
          lcd.clear();
          txttot2 = 1;
          init3 = 1;
          time7 = millis() + time3;
        }
        else {
          b5 = LOW;
        }
      }
      bchange = button;
      if (bindex < 3)
      {
        bindex2 = bindex + 1;
      }
      if (bindex >= 3)
      {
        bindex2 = 0;
      }
    }
Serial.println(F("5"));
    //Screen timeout
    if (time7 < millis())
    {
      b5 = LOW;
    }

    //Write current temperature reading to LCD text array
    char lcdbuf2[15];
    dtostrf(tempf,4, 1, lcdbuf2);
    txt2[2]=lcdbuf2;
    char lcdbuf3[15];
    dtostrf(tempfpipe,4, 1, lcdbuf3);
    txt2[3]=lcdbuf3;

    //Update LCD display only when scrolling or data changes value
    if (bindexold != bindex || init3 == 1){
      lcd.setCursor(0, 0);
      lcd.print(txt1[bindex]);
      if (bindex == 1){
        lcd.setCursor(8, 0);
      }
      else {
        lcd.setCursor(8, 0);
        lcd.setCursor(12, 0);
      }
      lcd.print(txt2[bindex]);
      bindexold=bindex;
      lcd.setCursor(0, 1);
      lcd.print(txt1[bindex2]);
      if (bindex2 == 1){
        lcd.setCursor(8, 1);
      }
      else {      
        lcd.setCursor(8, 0);
        lcd.setCursor(12, 1);
      }
      lcd.print(txt2[bindex2]);
      bindexold2=bindex2;
    }
Serial.println(F("6"));
    //Update LCD display when data changes value
    if (txttot != holder2 + buttonState || txttot2==1){
      if (bindex == 1){
        lcd.setCursor(8, 0);
      }
      else {
        lcd.setCursor(12, 0);
      }
      lcd.print(txt2[bindex]);
      if (bindex2 == 1){
        lcd.setCursor(8, 1);
      }
      else { 
        lcd.setCursor(12, 1);
      }
      lcd.print(txt2[bindex2]);

      Serial.println(F("7"));
      if (millis() > time9){

        dowebpost();
        done = 1;
        time9 = millis()+ time8;
      }  
    txttot = holder2 + buttonState;
    txttot2=0;
    init3=0;
   }
  }
Serial.println(F("8"));

  //LCD splash screen routine
  if (x4!=1 || zz<=4 || positionCounter<16){
    char line1[]= "Initializing";  
    char line2[]= "Call Support @";
    char line3[]= "845-424-5066  ";
    lcd.setCursor(0, 0) ;
    lcd.print((char)0); 
    lcd.setCursor(1, 0) ;
    lcd.print((char)1);
    lcd.setCursor(0, 1) ;
    lcd.print((char)2); 
    lcd.setCursor(1, 1) ;
    lcd.print((char)3);


    //WOPR-esque animation (random numbers generated until actual text "found")
    if(millis()>time1a+time2a & bg<=en+offset)
    {
      int ran[12] = {
        5,1,11,7,4,6,0,3,9,8,2,10                  };  
      int r=ran[bg];
      lcd.setCursor(r+offset, 0) ;
      lcd.print(line1[r]);
      delay(5);
      x3[r]=1;
      delay(5);
      bg++;
      time1a=millis();
    }
    else{

      for (int q=offset; q4 & positionCounter<18){
      //scroll out and clear LCD display
      for ( positionCounter = 0; positionCounter < 18; positionCounter++) {
        lcd.clear();
        lcd.setCursor(positionCounter, 0) ;
        lcd.print((char)0); 
        lcd.setCursor(positionCounter+1, 0) ;
        lcd.print((char)1);
        lcd.setCursor(positionCounter, 1) ;
        lcd.print((char)2); 
        lcd.setCursor(positionCounter+1, 1) ;
        lcd.print((char)3);
        delay(80);
        time7 = millis() + time3;
      }
    }

  }
  //debug tools
  //  int buttonraw2 = analogRead(A3);
  //  lcd.setCursor(0, 0) ;
  //    lcd.print(buttonraw2); 
  //   lcd.setCursor(0, 1) ;
  //    lcd.print(x4); 
  //    lcd.setCursor(1, 1) ;
  //    lcd.print(zz); 
  //    lcd.setCursor(2, 1) ;
  //    lcd.print(positionCounter); 
}


Main Page Data Table Arduino Source Code Brewing Tools