-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYSENSOR_VSR
137 lines (118 loc) · 3.53 KB
/
MYSENSOR_VSR
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
#include <MySigningNone.h>
#include <MyTransportNRF24.h>
#include <MyTransportRFM69.h>
#include <MyHwATMega328.h>
#include <MySensor.h>
#include <SPI.h>
#include <DHT.h>
#include <math.h>
#include <Wire.h>
#define RELAY_1 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define NUMBER_OF_RELAYS 4 // Total number of attached relays
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay
#define MY_LEDS_BLINKING_FEATURE
#define CHILD_ID_HUM 5
#define CHILD_ID_TEMP 6
#define CHILD_ID_MQ 7
#define HUMIDITY_SENSOR_DIGITAL_PIN 2
const int MQ_Pin = A4;
DHT dht;
float lastTemp;
float lastHum;
float hum_floa;
float last_mq_reading;
long MQ2_Millis = 0;
long MQ2_interval = 30000;
long DHT_Millis = 0;
long DHT_interval = 30000;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMQ(CHILD_ID_MQ, V_LEVEL);
MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
MyHwATMega328 hw;
MySensor gw(radio, hw);
void setup()
{
gw.begin(incomingMessage, 55, false);
gw.sendSketchInfo("VSR", "1.0");
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_MQ, S_AIR_QUALITY);
// Fetch relay status
for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
gw.present(sensor, S_LIGHT);
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
// Set relay to last known state (using eeprom storage)
digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
}
}
void loop()
{
// Alway process incoming messages whenever possible
gw.process();
unsigned long DHT_Current_Millis = millis();
if(DHT_Current_Millis - DHT_Millis > DHT_interval)
{
DHT_Millis = DHT_Current_Millis;
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
if (isnan(temperature))
{
Serial.println("Failed reading temperature from DHT");
}
else if (temperature != lastTemp)
{
lastTemp = temperature;
gw.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}
float humidity = dht.getHumidity();
if (isnan(humidity))
{
Serial.println("Failed reading humidity from DHT");
}
else if (humidity != lastHum)
{
lastHum = humidity;
gw.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}
unsigned long MQ5_Current_Millis = millis();
if(MQ5_Current_Millis - MQ2_Millis > MQ2_interval)
{
MQ2_Millis = MQ5_Current_Millis;
float mq_reading = analogRead(MQ_Pin);
if (isnan(mq_reading))
{
Serial.println("Failed mq_reading");
}
else if (mq_reading != last_mq_reading)
{
last_mq_reading = mq_reading;
gw.send(msgMQ.set(mq_reading, 1));
Serial.print("MQ: ");
Serial.println(mq_reading);
}
}
}
void incomingMessage(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type==V_LIGHT) {
// Change relay state
digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
// Store state in eeprom
gw.saveState(message.sensor, message.getBool());
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}