-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_app.ino
171 lines (148 loc) · 3.72 KB
/
mqtt_app.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
byte mac[] = {0xFA, 0x2B, 0x4E, 0x0E, 0x27, 0xED};
IPAddress ip(192, 168, 1, 64);
IPAddress server(192, 168, 1, 63);
EthernetClient ethClient;
PubSubClient client(ethClient);
typedef String (*GeneralFunction)(int);
struct Device
{
String deviceName;
unsigned int pinID;
bool analog;
GeneralFunction computer;
unsigned int millisToWait;
unsigned int threshold;
unsigned long lastTime;
int lastValue;
bool initialized;
};
String currentIP = "";
void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient", "alex", "anarchy51"))
{
IPAddress address = Ethernet.localIP();
currentIP = String(address[0]) + "." +
String(address[1]) + "." +
String(address[2]) + "." +
String(address[3]);
Serial.println("connected");
delay(1000);
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
bool publishMessage(const char *topic, const String message)
{
if (!client.connected())
{
reconnect();
}
client.loop();
char msgChar[message.length() + 1];
message.toCharArray(msgChar, message.length() + 1);
bool result = client.publish(topic, msgChar);
if (!result)
{
Serial.print("ERROR publishing :");
Serial.println(msgChar);
}
delay(20);
return result;
}
bool isValidThreshold(Device &device, int rawVal) {
if (device.threshold) {
return (device.lastValue - device.threshold > rawVal) || (device.lastValue + device.threshold < rawVal);
} else {
return true;
}
}
void readAndPublish(Device &device)
{
unsigned long currentTime = millis();
if (!device.initialized || device.lastTime + device.millisToWait < currentTime)
{
int rawVal;
if (device.analog)
{
rawVal = analogRead(device.pinID);
}
else
{
rawVal = digitalRead(device.pinID);
}
if (!device.initialized || (device.lastValue != rawVal && isValidThreshold(device, rawVal)))
{
device.lastValue = rawVal;
device.lastTime = currentTime;
device.initialized = true;
String val;
if (device.computer != NULL)
{
val = device.computer(rawVal);
}
else
{
val = String(rawVal);
}
String message = "{\"identifier\":\"";
message += currentIP;
message += "\",\"service\":\"mqtt\",\"type\":\"";
message += device.deviceName;
message += "\",\"state\":{\"value\":\"";
message += val;
message += "\"}}";
Serial.println(device.deviceName + " sending value=" + val);
publishMessage("gladys/master/devicestate/create", message);
}
}
}
String computeTemperature(int defaultValue)
{
long Resistance = 1000.0 * ((1024.0 / defaultValue) - 1);
double temp = log(Resistance);
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15;
return String(temp);
}
String computeLux(int defaultValue)
{
float lux = (250.0 / (0.0048828125 * defaultValue)) - 50.0;
return String(lux);
}
void setup()
{
Serial.begin(57600);
client.setServer(server, 1883);
Ethernet.begin(mac);
// Allow the hardware to sort itself out
delay(1500);
}
int nbDevices = 3;
Device devices[] = {
{"lum", 0, true, computeLux, 0, 25},
{"motion", 7, false, NULL, 0},
{"temp", 1, true, computeTemperature, 60000}};
void loop()
{
for (int i = 0; i < nbDevices; i++)
{
readAndPublish(devices[i]);
delay(50);
}
}