Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * PINOUT ULTRASONIC 9 (TRIGGER)
- 10 (ECHO)
- TEMP SENSORS PIN 11
- RTC A3
- A4
- 2 rx
- 3 tx
- 4 driver (tr,rx)
- */
- #include <OPC.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 11
- #include <Wire.h>
- #include "RTClib.h"
- RTC_PCF8523 rtc;
- #include <RS485_non_blocking.h>
- #include <SoftwareSerial.h>
- const byte ENABLE_PIN = 4;
- const byte LED_PIN = 13;
- int Distance;
- SoftwareSerial mySerial(2, 3); // RX, TX
- byte buf [20];
- int fAvailable () {
- return mySerial.available ();
- }
- int fRead() {
- return mySerial.read ();
- }
- RS485 myChannel (fRead, fAvailable, NULL, 20);
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- DeviceAddress tempDeviceAddress= { 0x28, 0xCB, 0x30, 0x56, 0x05, 0x00, 0x00, 0x19 };
- DeviceAddress tempDeviceAddress1= { 0x28, 0xFF, 0x76, 0x1C, 0x3E, 0x04, 0x00, 0x82 };
- DeviceAddress tempDeviceAddress2= { 0x28, 0xFF, 0x29, 0x05, 0x3A, 0x04, 0x00, 0x4D };
- int Hour;
- int Minute;
- int Second;
- int distance =0;
- int precision = 12;
- float T1 = 0.0;
- float T2Out = 0.0;
- float TFridge = 0.0;
- bool automate=0;
- bool tempfault=0;
- bool relaystatus = 0;
- unsigned long lastTempRequest = 0;
- int delayInMillis = 0;
- const int pResistor = A0; // Photoresistor at Arduino analog pin A0
- int idle = 0;
- int timercheck=0;
- long int timer;
- int relayPin = 12;
- int lightvalue;
- int timervalue;
- const char servo1[] = "servo1";
- OPCSerial aOPCSerial;
- int Photoresistor1(const char *itemID, const opcOperation opcOP, const int value){ //Photoresistor OPC
- return analogRead(pResistor);
- }
- float t_1(const char *itemID, const opcOperation opcOP, const float value) { //Temperature OPC
- return T1;
- }
- float t_2(const char *itemID, const opcOperation opcOP, const float value) { //Temperature OPC
- return T2Out;
- }
- float t_3(const char *itemID, const opcOperation opcOP, const float value) { //Temperature OPC
- return TFridge;
- }
- float tempError(const char *itemID, const opcOperation opcOP, const float value) { //Temperature error OPC
- return tempfault;
- }
- int RTC_Hour(const char *itemID, const opcOperation opcOP, const int value) { //OPC TIME HOUR
- return Hour;
- }
- int RTC_Minute(const char *itemID, const opcOperation opcOP, const int value) { //OPC TIME MINUTE
- return Minute;
- }
- int RTC_Second(const char *itemID, const opcOperation opcOP, const int value) { //OPC TIME SECOND
- return Second;
- }
- int Ultrasonic(const char *itemID, const opcOperation opcOP, const int value){ //ULTRASONIC
- return Distance;
- }
- int Timerset(const char *itemID, const opcOperation opcOP, const int value){ //Set Timer OPC
- if (opcOP == opc_opwrite) {
- timervalue =60*value;
- }
- return timervalue/60;
- }
- bool Relay(const char *itemID, const opcOperation opcOP, const bool value){ //Relay on/off OPC
- if (opcOP == opc_opwrite) {
- relaystatus = value;
- if (relaystatus)
- digitalWrite(relayPin, HIGH);
- else
- digitalWrite(relayPin, LOW);
- }
- else
- return relaystatus;
- }
- bool Automation(const char *itemID, const opcOperation opcOP, const bool value){ //Automation OPC
- if (opcOP == opc_opwrite) {
- automate = value;
- }
- return automate;
- }
- void setup(void) //SETUP
- {
- Serial.begin(9600);
- mySerial.begin(9600);
- myChannel.begin();
- pinMode (ENABLE_PIN, OUTPUT); // driver output enable
- pinMode (LED_PIN, OUTPUT); // built-in LED
- aOPCSerial.setup();
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (! rtc.initialized()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2018, 30, 07, 16, 58, 30));
- }
- pinMode(relayPin, OUTPUT);
- pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
- sensors.begin();
- sensors.setResolution(tempDeviceAddress, precision);
- sensors.setResolution(tempDeviceAddress1, precision);
- sensors.setResolution(tempDeviceAddress2, precision);
- sensors.setWaitForConversion(false);
- sensors.requestTemperatures();
- delayInMillis = 1000 / (1 << (12 - precision)); //scary? :)
- lastTempRequest = millis();
- aOPCSerial.addItem("ds_temp1",opc_read, opc_float, t_1);
- aOPCSerial.addItem("ds_temp2Out",opc_read, opc_float, t_2);
- aOPCSerial.addItem("ds_tempFridge",opc_read, opc_float, t_3);
- aOPCSerial.addItem("TemperatureFailure",opc_readwrite, opc_bool, tempError);
- aOPCSerial.addItem("automationValue",opc_readwrite, opc_bool, Automation);
- aOPCSerial.addItem("relayControl",opc_readwrite, opc_bool, Relay);
- aOPCSerial.addItem("LightSensor",opc_read, opc_int, Photoresistor1);
- aOPCSerial.addItem("TimerSet",opc_readwrite, opc_int, Timerset);
- aOPCSerial.addItem("Ultrasonic",opc_read, opc_int, Ultrasonic);
- aOPCSerial.addItem("RTC.Hour",opc_read, opc_int, RTC_Hour);
- aOPCSerial.addItem("RTC.Minute",opc_read, opc_int, RTC_Minute);
- aOPCSerial.addItem("RTC.Second",opc_read, opc_int, RTC_Second);
- }
- void loop(void)
- {
- aOPCSerial.processOPCCommands();
- DateTime now = rtc.now();
- Hour= now.hour();
- Minute=now.minute();
- Second=now.second();
- lightvalue = analogRead(pResistor);
- if (automate==1)
- {
- automate=1;
- if (timercheck==0)
- {
- DateTime now = rtc.now();
- timer= now.unixtime() + timervalue;
- Serial.println(now.unixtime());
- timercheck=1; //Timed boiler
- }
- if ((now.unixtime()<=timer))
- {
- digitalWrite(relayPin, HIGH);
- relaystatus=1;
- }
- if ((now.unixtime()>=timer) || automate==0){
- digitalWrite (relayPin,LOW);
- relaystatus=0;
- timercheck=0;
- automate=0;
- }
- }
- if (automate==0 && timercheck==1)
- {
- relaystatus=0;
- digitalWrite (relayPin,LOW);
- timercheck=0;
- }
- if (millis() - lastTempRequest > delayInMillis) // waited long enough to read sample??
- {
- T1 = sensors.getTempC(tempDeviceAddress);
- T2Out = sensors.getTempC(tempDeviceAddress1);
- TFridge = sensors.getTempC(tempDeviceAddress2);
- sensors.requestTemperatures();
- lastTempRequest = millis();
- if (T1==-127){ //Checking for Temperature Sensor error
- tempfault=1;
- }
- if (T1>-127 && T1<80)
- {
- tempfault=0;
- }
- }
- if (myChannel.update ())
- {
- digitalWrite (LED_PIN, LOW); // turn on LED if error
- memcpy (buf, myChannel.getData (), myChannel.getLength ()); // make a copy of the data
- if (buf [1] == 1)
- {
- Distance = buf[2];
- // Serial.println ("Hi");
- // Serial.println (ultrasonic);
- }
- }
- if (!(myChannel.update ())){
- digitalWrite (LED_PIN, HIGH); // turn on LED if error
- }
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement