-
Notifications
You must be signed in to change notification settings - Fork 0
/
Env_Serial_OPC.ino
81 lines (57 loc) · 1.93 KB
/
Env_Serial_OPC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <OPC.h>
#include <Bridge.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
OPCSerial myArduinoMEGA;
opcAccessRights analog_status_input[1];
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; //I2C
float callbackTemperature(const char *itemID, const opcOperation opcOP, const float value){
return bme.readTemperature();
}
float callbackPressure(const char *itemID, const opcOperation opcOP, const float value){
return (bme.readPressure()/ 100.0F);
}
float OPCoperation(const char *itemID, const opcOperation opcOP, const float value){
return 1;
}
float callbackHumidity(const char *itemID, const opcOperation opcOP, const float value){
return bme.readHumidity();
}
int readwrite_analog(const char *itemID, const opcOperation opcOP, const int value)
{
byte port;
OPCItemType aOPCItem = myArduinoMEGA.getOPCItem(itemID);
port = atoi(&itemID[1]);
if (opcOP == opc_opread) {
if ((aOPCItem.opcAccessRight == opc_read) || (aOPCItem.opcAccessRight == opc_readwrite)) {
if (analog_status_input[port] != opc_read) {
pinMode(port, INPUT);
analog_status_input[port] = opc_read;
}
return analogRead(port);
}
}
}
const int LED=53;
void setup() {
pinMode(LED,OUTPUT);
bool status;
bme.begin();
if (! bme.begin()) {
while (1);
}
Serial.begin(9600);
myArduinoMEGA.setup();
myArduinoMEGA.addItem("BME280.Temperature",opc_read, opc_float, callbackTemperature);
myArduinoMEGA.addItem("BME280.Pressure",opc_read, opc_float, callbackPressure);
myArduinoMEGA.addItem("BME280.Humidity",opc_read, opc_float, callbackHumidity);
myArduinoMEGA.addItem("BME280.ArduinoOperation",opc_read, opc_float, OPCoperation);
myArduinoMEGA.addItem("A0",opc_read, opc_int, readwrite_analog);
}
void loop() {
digitalWrite(LED,millis()/1024%2);
myArduinoMEGA.processOPCCommands();
}