Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 21st, 2018 76 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. /*
  2.  * PINOUT             ULTRASONIC      9 (TRIGGER)
  3.                                       10 (ECHO)
  4.                       TEMP SENSORS    PIN 11
  5.  
  6.                       RTC             A3
  7.                                       A4
  8.                                       2 rx
  9.                                       3 tx
  10.                                       4 driver (tr,rx)
  11.                                      
  12. */
  13.  
  14. #include <OPC.h>
  15. #include <OneWire.h>
  16. #include <DallasTemperature.h>
  17. #define ONE_WIRE_BUS 11
  18. #include <Wire.h>
  19. #include "RTClib.h"
  20. RTC_PCF8523 rtc;
  21.  
  22. #include <RS485_non_blocking.h>
  23. #include <SoftwareSerial.h>
  24. const byte ENABLE_PIN = 4;
  25. const byte LED_PIN = 13;
  26. int Distance;
  27.  
  28. SoftwareSerial mySerial(2, 3); // RX, TX
  29.  
  30. byte buf [20];
  31. int fAvailable () {
  32.   return mySerial.available ();  
  33. }
  34.  
  35. int fRead() {
  36.   return mySerial.read ();
  37. }
  38.  
  39. RS485 myChannel (fRead, fAvailable, NULL, 20);
  40.  
  41.  
  42.  
  43. OneWire oneWire(ONE_WIRE_BUS);
  44. DallasTemperature sensors(&oneWire);
  45. DeviceAddress tempDeviceAddress= { 0x28, 0xCB, 0x30, 0x56, 0x05, 0x00, 0x00, 0x19 };
  46. DeviceAddress tempDeviceAddress1= { 0x28, 0xFF, 0x76, 0x1C, 0x3E, 0x04, 0x00, 0x82 };
  47. DeviceAddress tempDeviceAddress2= { 0x28, 0xFF, 0x29, 0x05, 0x3A, 0x04, 0x00, 0x4D };
  48.  
  49.  
  50.  
  51. int Hour;
  52. int Minute;
  53. int Second;
  54. int distance =0;
  55. int  precision = 12;
  56. float T1 = 0.0;
  57. float T2Out = 0.0;
  58. float TFridge = 0.0;
  59. bool automate=0;
  60. bool tempfault=0;
  61. bool relaystatus = 0;
  62. unsigned long lastTempRequest = 0;
  63. int  delayInMillis = 0;
  64. const int pResistor = A0; // Photoresistor at Arduino analog pin A0
  65. int  idle = 0;
  66. int timercheck=0;
  67. long int timer;
  68. int relayPin = 12;
  69. int lightvalue;
  70. int timervalue;
  71. const char servo1[] = "servo1";            
  72.  
  73. OPCSerial aOPCSerial;
  74.  
  75.  
  76. int Photoresistor1(const char *itemID, const opcOperation opcOP, const int value){                             //Photoresistor OPC
  77.   return analogRead(pResistor);
  78.   }
  79.  
  80. float t_1(const char *itemID, const opcOperation opcOP, const float value) {                             //Temperature OPC
  81.   return T1;
  82.   }
  83.  
  84. float t_2(const char *itemID, const opcOperation opcOP, const float value) {                             //Temperature OPC
  85.   return T2Out;
  86.   }  
  87.  
  88. float t_3(const char *itemID, const opcOperation opcOP, const float value) {                             //Temperature OPC
  89.   return TFridge;
  90.   }  
  91.  
  92. float tempError(const char *itemID, const opcOperation opcOP, const float value) {                       //Temperature error OPC
  93.   return tempfault;
  94.   }
  95.  
  96. int RTC_Hour(const char *itemID, const opcOperation opcOP, const int value) {                       //OPC TIME HOUR
  97.   return Hour;
  98.   }
  99.  
  100. int RTC_Minute(const char *itemID, const opcOperation opcOP, const int value) {                       //OPC TIME MINUTE
  101.   return Minute;
  102.   }
  103.  
  104. int RTC_Second(const char *itemID, const opcOperation opcOP, const int value) {                       //OPC TIME SECOND
  105.   return Second;
  106.   }
  107.  
  108. int Ultrasonic(const char *itemID, const opcOperation opcOP, const int value){                             //ULTRASONIC
  109.   return Distance;
  110.   }
  111.  
  112.  
  113. int Timerset(const char *itemID, const opcOperation opcOP, const int value){                             //Set Timer OPC
  114.   if (opcOP == opc_opwrite) {
  115.    timervalue =60*value;
  116.   }
  117. return timervalue/60;
  118. }
  119.  
  120. bool Relay(const char *itemID, const opcOperation opcOP, const bool value){                          //Relay on/off OPC
  121.     if (opcOP == opc_opwrite) {
  122.     relaystatus = value;
  123.    
  124.     if (relaystatus)
  125.      
  126.       digitalWrite(relayPin, HIGH);
  127.        
  128.     else
  129.       digitalWrite(relayPin, LOW);
  130.   }
  131.   else
  132.      return relaystatus;
  133. }  
  134.  
  135. bool Automation(const char *itemID, const opcOperation opcOP, const bool value){                          //Automation OPC
  136.   if (opcOP == opc_opwrite) {
  137.    automate = value;
  138.    }
  139. return automate;  
  140. }
  141.  
  142.  
  143. void setup(void)                                                                //SETUP
  144. {
  145.  
  146. Serial.begin(9600);
  147. mySerial.begin(9600);
  148. myChannel.begin();
  149. pinMode (ENABLE_PIN, OUTPUT);  // driver output enable
  150. pinMode (LED_PIN, OUTPUT);  // built-in LED
  151. aOPCSerial.setup();
  152.  
  153. if (! rtc.begin()) {
  154.     Serial.println("Couldn't find RTC");
  155.     while (1);
  156.   }
  157.  
  158.   if (! rtc.initialized()) {
  159.     Serial.println("RTC is NOT running!");
  160.     // following line sets the RTC to the date & time this sketch was compiled
  161.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  162.     // This line sets the RTC with an explicit date & time, for example to set
  163.     // January 21, 2014 at 3am you would call:
  164.     // rtc.adjust(DateTime(2018, 30, 07, 16, 58, 30));
  165.  
  166.   }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. pinMode(relayPin, OUTPUT);
  173. pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
  174.  
  175.  
  176.  sensors.begin();
  177.  
  178.  sensors.setResolution(tempDeviceAddress, precision);
  179.  sensors.setResolution(tempDeviceAddress1, precision);
  180.  sensors.setResolution(tempDeviceAddress2, precision);
  181.  sensors.setWaitForConversion(false);
  182.  sensors.requestTemperatures();
  183.  delayInMillis = 1000 / (1 << (12 - precision));        //scary? :)
  184.  
  185.  
  186.  lastTempRequest = millis();
  187.  
  188.  
  189. aOPCSerial.addItem("ds_temp1",opc_read, opc_float, t_1);
  190. aOPCSerial.addItem("ds_temp2Out",opc_read, opc_float, t_2);
  191. aOPCSerial.addItem("ds_tempFridge",opc_read, opc_float, t_3);
  192. aOPCSerial.addItem("TemperatureFailure",opc_readwrite, opc_bool, tempError);
  193. aOPCSerial.addItem("automationValue",opc_readwrite, opc_bool, Automation);
  194. aOPCSerial.addItem("relayControl",opc_readwrite, opc_bool, Relay);
  195. aOPCSerial.addItem("LightSensor",opc_read, opc_int, Photoresistor1);
  196. aOPCSerial.addItem("TimerSet",opc_readwrite, opc_int, Timerset);
  197. aOPCSerial.addItem("Ultrasonic",opc_read, opc_int, Ultrasonic);
  198. aOPCSerial.addItem("RTC.Hour",opc_read, opc_int, RTC_Hour);
  199. aOPCSerial.addItem("RTC.Minute",opc_read, opc_int, RTC_Minute);
  200. aOPCSerial.addItem("RTC.Second",opc_read, opc_int, RTC_Second);
  201. }
  202.  
  203. void loop(void)
  204.  
  205. {
  206. aOPCSerial.processOPCCommands();
  207.  
  208. DateTime now = rtc.now();
  209.  
  210. Hour= now.hour();
  211. Minute=now.minute();
  212. Second=now.second();
  213.  
  214. lightvalue = analogRead(pResistor);
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. if (automate==1)
  224. {
  225.   automate=1;
  226.   if (timercheck==0)
  227.   {
  228.     DateTime now = rtc.now();
  229.     timer= now.unixtime() + timervalue;
  230.     Serial.println(now.unixtime());
  231.   timercheck=1;                                               //Timed boiler
  232.   }
  233. if ((now.unixtime()<=timer))
  234.  {
  235.    digitalWrite(relayPin, HIGH);
  236.    relaystatus=1;
  237.  }
  238.  
  239. if ((now.unixtime()>=timer) || automate==0){
  240.  
  241.   digitalWrite (relayPin,LOW);
  242.   relaystatus=0;
  243.   timercheck=0;
  244.   automate=0;
  245.  }
  246. }
  247.  
  248.  
  249.  
  250. if (automate==0 && timercheck==1)
  251. {
  252.  
  253. relaystatus=0;
  254.  digitalWrite (relayPin,LOW);
  255.  timercheck=0;
  256.  
  257.  }
  258.  
  259.  
  260.  
  261. if (millis() - lastTempRequest > delayInMillis) // waited long enough to read sample??
  262.  {
  263.  
  264.    T1 = sensors.getTempC(tempDeviceAddress);
  265.    T2Out = sensors.getTempC(tempDeviceAddress1);
  266.    TFridge = sensors.getTempC(tempDeviceAddress2);
  267.    sensors.requestTemperatures();
  268.    
  269.  
  270.    lastTempRequest = millis();
  271.    
  272.    
  273.  
  274.  
  275.     if (T1==-127){       //Checking for Temperature Sensor error
  276.  
  277.       tempfault=1;
  278.     }
  279.     if (T1>-127 && T1<80)
  280.    {
  281.     tempfault=0;
  282.    }
  283.    
  284.  
  285.  
  286.  }
  287.  
  288. if (myChannel.update ())
  289.     {
  290.    
  291.      digitalWrite (LED_PIN, LOW);  // turn on LED if error
  292.    
  293.     memcpy (buf, myChannel.getData (), myChannel.getLength ());  // make a copy of the data
  294.    if (buf [1] == 1)
  295.     {
  296.      Distance = buf[2];
  297.      //  Serial.println ("Hi");
  298.      //  Serial.println (ultrasonic);
  299.    
  300.     }
  301.    
  302.    }
  303.  
  304.  
  305.    if (!(myChannel.update ())){
  306.  
  307. digitalWrite (LED_PIN, HIGH);  // turn on LED if error
  308.    
  309.    }
  310.  
  311.    
  312.   // }
  313.  
  314.  
  315.  
  316. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top