-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardEnv_Humidity_Ethernet.ino
154 lines (109 loc) · 3.34 KB
/
ardEnv_Humidity_Ethernet.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <OPC.h>
#include <Ethernet2.h>
#include <Bridge.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
/*
OPC
*/
//ArdEnvStation
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;
//AnalogDevices
opcOperation analog_status_input[6];
/*
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
*/
//ArdEnvStation
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();
}
//Analog Devices
int readwrite_analog(const char *itemID, const opcOperation opcOP, const int value) {
byte port;
port = atoi(&itemID[1]);
if (opcOP == opc_opwrite) {
if (analog_status_input[port] == opc_opread) {
analog_status_input[port] = opc_opwrite;
pinMode(port,OUTPUT);
}
analogWrite(port,value);
}
else
{
if (analog_status_input[port] == opc_opwrite) {
analog_status_input[port] = opc_opread;
//pinMode(port,INPUT);
}
return analogRead(port);
}
}
const int LED=53;
void setup() {
byte k;
for (k=0;k<5;k++) analog_status_input[k] = opc_opread;
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);
aOPCEthernet.addItem("A0",opc_readwrite, opc_int, readwrite_analog);
aOPCEthernet.addItem("A1",opc_readwrite, opc_int, readwrite_analog);
aOPCEthernet.addItem("A2",opc_readwrite, opc_int, readwrite_analog);
aOPCEthernet.addItem("A3",opc_readwrite, opc_int, readwrite_analog);
aOPCEthernet.addItem("A4",opc_readwrite, opc_int, readwrite_analog);
aOPCEthernet.addItem("A5",opc_readwrite, opc_int, readwrite_analog);
}
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();
}