-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManIoTbox_v3.ino
332 lines (234 loc) · 5.62 KB
/
ManIoTbox_v3.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#define DHTTYPE DHT22
#define Acel_X A3
#define Acel_Y A4
#define Acel_Z A5
#define ProxPin 34
#define ProxPinTwo 35
#define OneWirePin 18
#define PIRPIN 5
#define DHTTOPPIN 26
#define BATTLEVEL A18
#define ORANGE 27
#define GREEN 14
#define WIFI_BLUE 12
#define MQTT_BLUE 13
#define BATT_LOW_LEVEL 3000
#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <PubSubClient.h>
#include "DHT.h"
// CHANGE WIFI HERE
const char* ssid = "XRX";
const char* password = "52265226!!";
const char* mqtt_server = "mqtt.1worx.co";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int minVal = 265;
int maxVal = 402;
float battery_val = 0;
float temp_val = 0;
float dht_h = 0;
float dht_t = 0;
double x;
double y;
double z;
volatile float prox1_val = 0;
volatile float prox2_val = 0;
volatile float pir_val = 0;
DHT dht(DHTTOPPIN, DHTTYPE);
OneWire oneWire(OneWirePin);
DallasTemperature sensors(&oneWire);
String BatteryLevel() {
battery_val = analogRead(BATTLEVEL);
if (battery_val < BATT_LOW_LEVEL) {
digitalWrite(ORANGE, HIGH);
digitalWrite(GREEN, LOW);
}
else {
digitalWrite(ORANGE, LOW);
digitalWrite(GREEN, HIGH);
}
String buf;
buf += F(",\"batt\":");
buf += String((battery_val / 4095.0) * 100.0, 0);
return buf;
}
void ProxHandle1() {
prox1_val += 1;
}
void ProxHandle2() {
prox2_val += 1;
}
void PirHandle() {
if (digitalRead(PIRPIN) == LOW) {
pir_val = 1;
} else {
pir_val = 0;
}
}
void setup() {
Serial.begin(9600);
pinMode(ORANGE, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(WIFI_BLUE, OUTPUT);
pinMode(MQTT_BLUE, OUTPUT);
pinMode(OneWirePin, INPUT);
pinMode(ProxPin, INPUT);
pinMode(ProxPinTwo, INPUT);
pinMode(PIRPIN, INPUT);
pinMode(BATTLEVEL, INPUT);
pinMode(Acel_X, INPUT);
pinMode(Acel_Y, INPUT);
pinMode(Acel_Z, INPUT);
attachInterrupt(digitalPinToInterrupt(ProxPin), ProxHandle1, CHANGE);
attachInterrupt(digitalPinToInterrupt(ProxPinTwo), ProxHandle2, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIRPIN), PirHandle, CHANGE);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(WIFI_BLUE, HIGH);
delay(500);
digitalWrite(WIFI_BLUE, LOW);
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(WIFI_BLUE, HIGH);
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.subscribe("esp32/output");
digitalWrite(MQTT_BLUE, HIGH);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
digitalWrite(MQTT_BLUE, LOW);
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 100) {
lastMsg = now;
String Result = "";
Result ="{"+prox()+prox2()+pir()+acc()+dhti()+temp()+BatteryLevel()+"}";
Serial.println(Result);
char charBuf[200];
Result.toCharArray(charBuf, 200);
uint64_t chipid = ESP.getEfuseMac();
String chipid_s = String((unsigned long)((chipid & 0xFFFF0000) >> 16), HEX) + String((unsigned long)((chipid & 0x0000FFFF)), HEX);
String res = String("esp32/" + chipid_s);
char buff2[200];
res.toCharArray(buff2, 200);
client.publish(buff2, charBuf);
}
}
String acc()
{
x = analogRead(Acel_X);
y = analogRead(Acel_Y);
z = analogRead(Acel_Z);
String buf;
buf += F(",\"x\":");
buf += String(x, 0);
buf += F(",\"y\":");
buf += String(y, 0);
buf += F(",\"z\":");
buf += String(z, 0);
return buf;
}
String dhti()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if ((!isnan(h)) && (!isnan(t))) {
dht_t = t;
dht_h = h;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
String buf;
buf += F(",\"DUTTemp\":");
buf += String(dht_t, 2);
buf += F(",\"DHTHum\":");
buf += String(dht_h, 2);
return buf;
}
String temp()
{
sensors.requestTemperatures();
float Current_Temp = sensors.getTempCByIndex(0);
if (Current_Temp != -127.00) {
temp_val = Current_Temp;
}
Serial.print("Temp: ");
Serial.println(Current_Temp);
String buf;
buf += F(",\"Temp\":");
buf += String(temp_val, 2);
return buf;
}
String prox()
{
String buf;
buf += F("\"Prox\":");
buf += String(prox1_val, 0);
prox1_val = 0;
return buf;
}
String prox2()
{
String buf;
buf += F(",\"Prox2\":");
buf += String(prox2_val, 0);
prox2_val = 0;
return buf;
}
String pir(){
String buf;
buf += F(",\"Pir\":");
buf += String(pir_val, 0);
return buf;
}