-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegaSensorPublic.ino
112 lines (90 loc) · 2.45 KB
/
MegaSensorPublic.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
//Multi-Sensor Ardiuno Mega 2560
//MQTT Definitions stored in secrets.h
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include "DHT.h"
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <HeartBeat.h>
EthernetClient ethClient;
PubSubClient client(ethClient);
#include "definitions.h"
#include "secrets.h"
void initHardware() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(53);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
getMAC();
}
void setup() {
pinMode(hbPin, OUTPUT);
initHardware();
initDHT();
initPIR();
initButton();
sensors.begin();
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
delay(200);
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the controller
Serial.print("Controller 1 is online at ");
Serial.println(Ethernet.localIP());
delay(2000);
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("Controller1", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("controller/1", "Controller 1 is Online");
client.subscribe("controller/1/update");
readDHT();
readDS();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
if ((char)payload[0] == '1'){
readDHT();
readDS();
}
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
timer.run();
// Update the Bounce instance :
checkButton();
timer.setInterval(300000, readDHT );
timer.setInterval(300000, readDS );
readPIR();
hbTimerFunc();
client.loop();
}