Advertisement
MortenBogetvedt

Ethernet problems

Feb 1st, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.53 KB | None | 0 0
  1. //Arduino setup
  2.  
  3. /************************************************************************************/
  4. /*  The Arduino software is to control the main regulation of the brewery.          */
  5. /*  Software gets all its variables from a Google spreadsheet and are stored in     */
  6. /*  different variables in the Arduino main program.                                */
  7. /*  The main program keeps track of the different brewingsteps and calls other      */
  8. /*  code-snippets for executing each step of the brewing process                    */
  9. /*                                                                                  */
  10. /*  BREWING STEPS                                                                   */
  11. /*  1. Fill HLT tank to a given level. The level is set in the Arduino config       */
  12. /*     spreadsheet.                                                                 */
  13. /*  2. While filling the HLT tank the snippet also heats the water to a given temp. */
  14. /*  3. When correct temp is reached, start pumping water into the mash-tun.         */
  15. /*  4. Once the water is all transfered to the mash-tun, start resirculating the    */
  16. /*     mash and also fill the HLT tank to new level and heat water.                 */
  17. /*     Resirculation is controlled by number of minutes in config spreadsheet       */
  18. /*  5. Once initial mashing is complete, transfer mash to boiling kettle and        */
  19. /*     start emptying the HLT tank to wash the sparge.                              */
  20. /*  6. Start boiling the worth. Once boiling, let it boil for the amount of time    */
  21. /*     defined in the config spreadsheet.                                           */
  22. /*  7. When done boiling, start chilling the worth to temperature given in the      */
  23. /*     config spreadsheet.                                                          */
  24. /*  8. Empty boiler.                                                                */
  25. /************************************************************************************/
  26.  
  27.  
  28. /************************************************************************************/
  29. /*                      ARDUINO PIN OVERVIEW/ASSIGNMENTS                            */
  30. /*                                                                                  */
  31. /*  ANALOG PINS                                                                     */
  32. /* HLTTempRead      =0                                                              */
  33. /* HLTLevelRead     =1                                                              */
  34. /*                                                                                  */
  35. /*  DIGITAL PINS                                                                    */
  36. /* HLTFillValvePin  =2                                                              */
  37. /* HLTHeater1Pin    =3                                                              */
  38. /* HLTHeater2Pin    =4                                                              */
  39. /* HLTEmptyValvePin =5                                                              */
  40. /* MashPumpPin      =6                                                              */
  41.  
  42.  
  43. // Main program
  44. // Include files
  45. //#include "HLTTemp.h"
  46. //Define constants
  47. #define j 50
  48.  
  49. // Declaration of variables
  50. // Integers
  51. int BrewingProcess = 0; // Possible values: 0=Not started, 1=Startup, 2=Heating HLT, 3=Mashing
  52. int sleep = 10000; // Variable for sleeping for 10 sec
  53. int MashToBePreheated = 0;  //Variable for holding if the mashtun is to be preheated. 0: No preheat. 1:Preheat
  54.                             //See PreheatMash() function for more details.
  55. bool connected;//Is the connection to brygging.bogetvedt.com up and running?
  56. float TempToWrite = 0.0; //Holds temperature to be written to database
  57.  
  58.                            
  59. //HLT tank variables
  60. int HLTFillValvePin = 2; //Set valve for filling HLT tank to digital out 5.
  61. int HLTHeater1Pin = 3; //Set the heater for HLT tank to digital out 6.
  62. int HLTEmptyValvePin = 5; //Set valve for emptying HLT tank to digital out 7.
  63. int HLTTempRead = 2; //Analog pin for temperature sensor
  64. int HLTLevelRead = 3; //Analog pin for level sensor
  65.  
  66.  
  67. int HLTFilled = 0; //Holds variable to tell if HLT tank is full or not.
  68. int HLTTempered = 0; //Holds variable to tell if HLT water is hot enough.
  69. int HLTEmpty = 0; //Holds variable to tell if HLT tank is emptied.
  70. int HLTLevelBit=0;
  71.  
  72.  
  73. int i;
  74. byte kbd;
  75.  
  76. //Mash tank variables
  77. int MashPumpPin = 8; //Sets MashPump to digital 8.
  78. int MashResirculateValvePin = 9; //Sets valve for resirculating worh to digital 9.
  79. int MashHeaterPin = 10; //Sets the heater for mash to digital out 10.
  80. int MashMinutes = 90;   //The number of minutes for the mashing process.
  81. // Floats
  82. float HLTTemp = 0.0; //Holds each temp measurement
  83. float HLTTempTot = 0.0; //Holds the sum of serveral measurements
  84. float HLTTempAvg = 0.0; //Holds the average temperature of serveral readings
  85. float HLTTempMean = 0.0; //Holds the mean of serveral temperaturemeasurements
  86. float HLTLevel = 0.0;
  87. float HLTTempSetpoint=0.0;
  88. float HLTTempDeviation = 0.0;
  89. float HLTLevelSetpoint=750.00;
  90. float HLTLevelEmpty = 125.0;
  91. // Strings
  92.  
  93. // Arrays
  94. float HLTTempArray[j];
  95. float HLTTempArray2[j-20]; //Holds temp-array with 20 removed values. Removed values are 10 lowest and 10 highest in HLTTempArray
  96.  
  97. // Ethernet declarations
  98. #include <SPI.h>
  99. #include <Ethernet.h>
  100.  
  101. // the media access control (ethernet hardware) address for the shield:
  102. // MAC For my ethernet shield BC:B1:F3:4A:B4:EA
  103. byte mac[] = { 0xBC, 0xB1, 0xF3, 0x4A, 0xB4, 0xEA };  
  104. //the IP address for the shield: 192.168.1.5
  105. byte ip[] = { 192, 168, 1, 25 };    
  106. // Default gateway
  107. byte gateway[] = { 192,168,1,1 };
  108.  
  109.  //What server to connect to?
  110. byte server[] = { 83, 143, 81, 42 };//
  111.  
  112. EthernetClient Client;
  113.  
  114. void setup()
  115. {
  116.     Serial.begin(9600);
  117.     Serial.println("Starting up.....");
  118.     EthernetConnect();
  119.     //Initialize all nessecary ports and variables
  120.     pinMode(HLTFillValvePin, OUTPUT);
  121.     pinMode(HLTHeater1Pin, OUTPUT);
  122.     pinMode(HLTEmptyValvePin, OUTPUT);
  123.     digitalWrite(HLTEmptyValvePin, LOW);   
  124.     Ethernet.begin(mac,ip);
  125.     Serial.println(Ethernet.localIP());
  126.  
  127. }
  128.  
  129. void EthernetConnect()
  130. {
  131.     Serial.println("Initializing ethernet module");
  132.     Ethernet.begin(mac,ip);
  133.     //Try to connect to brygging.bogetvedt.com
  134.     connected=Client.connect(server, 80);
  135.     if(connected==1)
  136.     {
  137.         Serial.println("CONNECTED");
  138.         //Client.stop();
  139.         Serial.println(Ethernet.localIP());
  140.     }
  141.     return;
  142. }
  143. void loop()
  144. {
  145.     if (BrewingProcess=0);
  146.     {
  147.         digitalWrite(HLTFillValvePin, LOW);
  148.         digitalWrite(HLTHeater1Pin,LOW);
  149.         //Set all variables - This is to be read from a google spreadsheet
  150.         HLTTempSetpoint = 29.0; // To be replaced by a call to GetGoogleData();
  151.         Serial.print(HLTTempSetpoint); Serial.println(" C er satt som setpunkt for temperatur");
  152.         Serial.println("Venter på at bryggeprosessen skal starte");
  153.         BrewingProcess=-1;
  154.     }
  155.     while(BrewingProcess==-1)
  156.     {
  157.         kbd=Serial.read();
  158.         if(kbd==115)
  159.         {
  160.             Serial.println("Brewingprocess initiated.........");
  161.             BrewingProcess=1;
  162.         }
  163.     }
  164.     while(BrewingProcess==1)
  165.     //Starting up the brewery.
  166.     //First connect to brygging.bogetvedt.com to write to database.
  167.     //Then start filling water and start heating.
  168.     {
  169.         if(MashToBePreheated == 1)
  170.         {
  171.             Serial.println("ENTERING PREHEAT MODE!!!!!!");
  172.             PreheatMash();
  173.         }
  174.         Serial.println("1. Bryggeprosess startet. Fyller og varmer vann");
  175.         digitalWrite(HLTFillValvePin, HIGH);
  176.         delay(sleep); //Wait for water to cover heat elements
  177.         digitalWrite(HLTHeater1Pin,HIGH);
  178.         delay(sleep);
  179.         Serial.println("2. Ventil for vann åpnet samt varmeelement på.");
  180.         BrewingProcess=2;  
  181.         Serial.print("3. Bryggeprosessvariabel er nå: "); Serial.println(BrewingProcess);
  182.     }
  183.     while(BrewingProcess==2)
  184.     //This mode is for checking temperature&level of HLT water
  185.     {
  186.         //Check if level is OK
  187.         if(HLTLevelBit==0) //Set to 0... Now overriding
  188.         {
  189.             Serial.println("4. Sjekker om tank er full");
  190.             HLTLevelBit=CheckHLTLevel(0); //Call function telling it is in filling mode
  191.             //HLTLevelBit=1; //Tvinge gjennom full tank...
  192.         }
  193.         Serial.print("5. Status for fylling er:"); Serial.println(HLTLevelBit);
  194.         Serial.println("6. Sjekker temperatur i meskevann");
  195.         HLTTempered=CheckHLTTemp();
  196.         TempToWrite=HLTTempered;
  197.         WriteHLTTempToDB(); //Write HLTTempered to database
  198.         Serial.print("7. Status for temperatur er: "); Serial.println(HLTTempered);
  199.         if(HLTLevelBit==1 && HLTTempered==1)
  200.         {
  201.             //Both water and temp OK. Start mashing
  202.             Serial.println("8. Vann ok. Kan starte meskingen.");
  203.             digitalWrite(HLTEmptyValvePin, HIGH);
  204.             BrewingProcess=3;
  205.         }
  206.     }
  207.     while(BrewingProcess==3) //Emptying HLT tank
  208.     {
  209.         //Open valve
  210.         if(HLTEmpty==0 || HLTEmpty==2)
  211.         {
  212.             digitalWrite(MashPumpPin,HIGH);
  213.             HLTEmpty=CheckHLTLevel(1);
  214.            
  215.         }
  216.         else
  217.         {
  218.             //Tank is empty.
  219.             digitalWrite(MashPumpPin,LOW);
  220.         }
  221.        
  222.         Serial.println("9. Mesker!!!!");
  223.         delay(sleep);
  224.     }
  225. }
  226.  
  227. /********************************************************************/
  228. /* FUNCTIONS FOR HLT REGULATIONS ARE LOCATED HERE!!!!               */
  229. /********************************************************************/
  230.  
  231. /********************************************************************/
  232. /* Function for pre-heating the mashtun                             */
  233. /********************************************************************/
  234. void PreheatMash()
  235. {
  236.     //Defining internal variables for preheat
  237.     float PreheatLevel = 0.0; // How many liters is in the HLT for preheating
  238.     float PreheatVolume = 25.0; //How many liters to preheat?
  239.     float HLTTempLimit = 25.0; //How hot should the preheated water be?
  240.     int Preheating = 0; //Variable for holding the preheating status.
  241.     Serial.println("Preheating the mash equipment."); Serial.println("Filling the preheat liquid.");
  242.     digitalWrite(HLTFillValvePin, HIGH);
  243.     delay(10000);
  244.     Serial.print("Preheating start:");Serial.println(Preheating);
  245.     digitalWrite(HLTHeater1Pin,HIGH);
  246.     while(Preheating<=1)
  247.     {
  248.         Preheating =0;
  249.         PreheatLevel = analogRead(HLTLevelRead);
  250.         Serial.print("PreheatLevel:"); Serial.println(PreheatLevel);
  251.         delay(5000);
  252.         if (PreheatLevel >= PreheatVolume)
  253.         {
  254.             digitalWrite(HLTFillValvePin, LOW);
  255.             Preheating = Preheating+1;
  256.             Serial.println("Preheat volume reached.");
  257.         }
  258.         HLTTempered = CheckHLTTemp();
  259.         Serial.print("HLTTemp:"); Serial.println(HLTTemp);
  260.         delay(5000);
  261.         if (HLTTemp >=HLTTempLimit)
  262.         {
  263.             digitalWrite(HLTHeater1Pin, LOW);
  264.             Preheating = Preheating+1;
  265.             Serial.println("Preheat temperature reached.");
  266.         }
  267.         Serial.print("Preheating midt:"); Serial.println(Preheating);
  268.     }
  269.     while(Preheating ==2) //Waterlevel and temperature is at correct level. Transfer to mash
  270.     {
  271.         Serial.println("Both preheat volume and preheat temperature is reached. Emptying HLT.");
  272.         digitalWrite(HLTEmptyValvePin, HIGH);
  273.         PreheatLevel = analogRead(HLTLevelRead);
  274.         if (PreheatLevel <=HLTLevelEmpty)
  275.         {
  276.             digitalWrite(HLTEmptyValvePin, LOW);
  277.             return;
  278.         }
  279.        
  280.     }
  281.     Serial.print("Preheating end:");Serial.println(Preheating);
  282.     delay(50000);
  283.     return;
  284. }
  285. /********************************************************************/
  286. /* Function for checking if the level of the HLT tank is reached.   */
  287. /********************************************************************/
  288.  
  289. int CheckHLTLevel(int type)
  290. {
  291.     //RETURNVALUES
  292.     //0: HLT tank is filling.
  293.     //1: HLT Tank is full.
  294.     //2: HLT tank is emptying
  295.     //3: HLT tank has been emptied.
  296.    
  297.     int returnvalue=0;
  298.   //Getting HLTLevel
  299.   Serial.println("4.1 Leser vannivå");
  300.   HLTLevel=analogRead(HLTLevelRead);
  301.     Serial.print(HLTLevel);
  302.     if(type==0)
  303.     {
  304.         if(HLTLevel>=HLTLevelSetpoint)
  305.         {
  306.             digitalWrite(HLTFillValvePin, LOW); //Turn off valve for filling. Level reached
  307.             returnvalue=1; //Tank is full
  308.        
  309.         }
  310.         else
  311.         {
  312.             returnvalue=0;
  313.         }
  314.     }
  315.     else if (type == 1) //The process is in emptying HLT mode. Mashing started.
  316.     {
  317.         if(HLTLevel<=HLTLevelEmpty) // The HLT tank is emprty. Close HLT valve and open mash sirculation valve.
  318.         {
  319.             Serial.print("------------------------------------HLT TANK EMPTY----------------------------------------------------");
  320.             returnvalue=3;
  321.         }
  322.         else
  323.         {
  324.             Serial.print("------------------------------------EMPTYING HLT TANK----------------------------------------------------");
  325.             returnvalue=2; //Tank is still emptying
  326.         }
  327.     }
  328.     return(returnvalue);
  329. }
  330.  
  331. /********************************************************************/
  332. /*  Function for checking the temperature level of the HLT tank.    */
  333. /*  Turn on&off element                                             */
  334. /********************************************************************/
  335.  
  336. int CheckHLTTemp()
  337.  {
  338.     //RETURNVALUES
  339.     //1. Water hot enough
  340.     //2. Water not hot enough
  341.     int returnvalue=0;
  342.     HLTTempTot=0;
  343.     //Getting the HLTTemp
  344.     Serial.println("6.1 Reading temperature sensor");  
  345.     for(i=0;i<j;i++)
  346.     {   //Get readings from analog port
  347.  
  348.         HLTTemp = analogRead(HLTTempRead);
  349.         HLTTemp = ((5.0 * HLTTemp * 100.0)/1024.0);
  350.         HLTTempArray[i]=HLTTemp;
  351.         HLTTempTot = HLTTempTot + HLTTemp;
  352.         delay(250);
  353.         //Serial.print("Reading tempnr."); Serial.print(i); Serial.print(": "); Serial.println(HLTTemp);
  354.     }
  355.     //Calculating average and mean
  356.     HLTTempAvg=HLTTempTot/j;
  357.     //Serial.print("6.1.1 Sittberegning av temp viser: "); Serial.println(HLTTempAvg);
  358.     Serial.println("---------------------------------Sorterer-------------------------------");
  359.     HLTTempMean=bubbleSort();
  360.     //Serial.print("6.1.2 Median av temp viser: "); Serial.println(HLTTempMean);
  361.     HLTTemp=(HLTTempMean + HLTTempAvg)/2;
  362.     Serial.print("6.2 Målt temperatur er: "); Serial.println(HLTTemp);
  363.     Serial.print("6.3 Referansetemperatur er: "); Serial.println(HLTTempSetpoint+HLTTempDeviation);
  364.     delay(sleep);
  365.     if(HLTTemp >=(HLTTempSetpoint+HLTTempDeviation))
  366.     {
  367.         digitalWrite(HLTHeater1Pin, LOW); //Turn off element. Within Setpoint limit
  368.         returnvalue= 1;
  369.     }
  370.     else if (HLTTemp >=(HLTTempSetpoint - HLTTempDeviation))
  371.     {
  372.         digitalWrite(HLTHeater1Pin, LOW); //Turn off element. Within Setpoint limit
  373.         returnvalue=1;
  374.     }
  375.     else
  376.     {
  377.         digitalWrite(HLTHeater1Pin, HIGH); //Make shure the heater is on. Not reached setpoint or gone below.
  378.         returnvalue=0;
  379.     }
  380.     return returnvalue;
  381.  }
  382.  
  383.  float func_avg()
  384. {
  385.     float calcavg;
  386.     HLTTempAvg=HLTTempTot/i;
  387.     //print the array
  388.     return calcavg;
  389. }
  390.  
  391.  float bubbleSort()
  392. {
  393.   int out=0;
  394.   int in=0;
  395.   float swapper;
  396.   //Sort the array Temp[]
  397.   for(out=0 ; out < j; out++)
  398.   {  // outer loop
  399.     for(in=out; in<j; in++)  
  400.     {  // inner loop
  401.     //Serial.print("Comparing stringnr: "); Serial.print(in); Serial.print(" with stringnr: "); Serial.println(in+1);
  402.     //Serial.print("Comparing value: "); Serial.print(HLTTempArray[in]); Serial.print(" with value: "); Serial.println(HLTTempArray[in+1]);
  403.       if( (HLTTempArray[in] > HLTTempArray[in+1]) && (in+1<j))
  404.       {   // out of order?
  405.         // swap them:
  406.         //Serial.println("Swapping");
  407.         swapper = HLTTempArray[in];
  408.         HLTTempArray[in] = HLTTempArray[in+1];
  409.         HLTTempArray[in+1] = swapper;
  410.         //delay(500);
  411.       }
  412.     }
  413.   }
  414.  
  415.      //Remove the lowest and highest values to remove most of the noice
  416.     //Serial.print("Liste: ");
  417.     in=0;
  418.     for (out=0; out < j; out++)
  419.     {   //Outer loop to scroll trough all values in the array... Remove bottom 10 and top 10
  420.         //Serial.print(out);Serial.print(":");Serial.println(HLTTempArray[out]);
  421.         //delay(1000);
  422.         //in=0;
  423.         if(out>9 && out<(j-10))
  424.         {
  425.             HLTTempArray2[in]=HLTTempArray[out];
  426.             in++;
  427.         }
  428.     }
  429.     //Get median value
  430.   HLTTempMean = HLTTempArray2[(j-20)/2];
  431.   return HLTTempMean;
  432. }
  433.  
  434. void WriteHLTTempToDB()
  435. {
  436.     //This function will connect to a php script at brygging.bogetvedt.com and log temperature to database bogetve_brygging
  437.     //Create class for Ethernet Client
  438.     Serial.print(".1 Ethernet skriving");
  439.     //EthernetClient Client;
  440.     //connected = Client.connect(server, 80); //Connect to server
  441.     if(connected>0)
  442.     {
  443.         Serial.println("connected");
  444.         Serial.println("Inserting data");
  445.         /*
  446.         //Try to insert data into table Test
  447.         Client.print("GET /Scripts/insert.php?");
  448.         Client.print("Felt1=");
  449.         Client.print(TempToWrite);
  450.         Client.println(" HTTP/1.1");
  451.         Client.println("Host: brygging.bogetvedt.com");
  452.         Client.println();
  453.         Client.stop();
  454.         delay(1000);
  455.         */
  456.     }
  457.     else
  458.     {
  459.         Serial.println("NOT CONNECTED");
  460.     }
  461.     return;
  462. }
  463.  
  464.  
  465. /********************************************************************/
  466. /* FUNCTIONS FOR MASH REGULATIONS ARE LOCATED HERE!!!!              */
  467. /********************************************************************/
  468.  
  469. /********************************************************************/
  470. /* This function is for controlling the sirculation in the mash     */
  471. /* and checking of the temperature is ok.                           */
  472. /********************************************************************/
  473. /*
  474. void SirculateMash()
  475.  {
  476.     int SirculationTime = 90; //total mash sirculation time
  477.     //First check if the temperature is as it should. Call CheckMashTemp()
  478.     MashTemp=CheckMashTemp()
  479.     if(MashTemp=1) //Temperature too low
  480.     {
  481.         digitalWrite(MashHeaterPin,HIGH); //Turn on heatingelement for the mash
  482.     }
  483.     else    //Temperature OK or too high
  484.     {
  485.         digitalWrite(MashHeaterPin,LOW); //Turn off heatingelement for the mash
  486.     }
  487.  }
  488.  */
  489. /********************************************************************/
  490. /* This function is checking if the mash temperature is ok.         */
  491. /********************************************************************/
  492. /*
  493. int CheckMashTemp()
  494.  {
  495.  }
  496.  */
  497.  
  498. /********************************************************************/
  499. /* MISC FUNCTIONS ARE LOCATED HERE!!!!                              */
  500. /********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement