-
Notifications
You must be signed in to change notification settings - Fork 0
/
ardEnv_OPC.ino
108 lines (77 loc) · 2.23 KB
/
ardEnv_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <OPC.h>
#include <Ethernet2.h>
#include <Bridge.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
/*
OPC
*/
OPCEthernet aOPCEthernet;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xAD, 0x8D };
IPAddress ip(10,0, 0, 10);
IPAddress subnet(255,255,255,0);
const int listen_port = 80;
/*
BME280
*/
//#define BME_SCK 5
//#define BME_MISO 4
//#define BME_MOSI 3
//#define BME_CS 2
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; //I2C
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
/*
* create a callback function for the OPCItem
*/
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();
}
const int LED=53;
void setup() {
pinMode(LED,OUTPUT);
bool status;
bme.begin();
if (! bme.begin()) {
while (1);
}
/*
* OPCNet Object initialization
*/
aOPCEthernet.setup(listen_port,mac,ip);
aOPCEthernet.addItem("BME280.Temperature",opc_read, opc_float, callbackTemperature);
aOPCEthernet.addItem("BME280.Pressure",opc_read, opc_float, callbackPressure);
aOPCEthernet.addItem("BME280.Humidity",opc_read, opc_float, callbackHumidity);
aOPCEthernet.addItem("BME280.ArduinoOperation",opc_read, opc_float, OPCoperation);
}
void loop() {
digitalWrite(LED,millis()/1024%2);
aOPCEthernet.processOPCCommands();
printValues();
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}