Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Paste
P277
(An Untitled Masterwork)
Active
Public
Actions
Authored by
stwalkerster
on Aug 29 2020, 4:13 PM.
Edit Paste
Archive Paste
View Raw File
Subscribe
Mute Notifications
Tags
None
Referenced Files
F327170: raw-paste-data.txt
Aug 29 2020, 4:13 PM
2020-08-29 16:13:29 (UTC+0)
Subscribers
None
#include
<Arduino.h>
#include
<Wire.h>
#include
<Ethernet.h>
#include
<MQTT.h>
#include
"credentials.h"
#define VERSION "1.0"
#define MQTT_MONTOPIC "admin/monitoring/client/%s/state"
#define MQTT_VERTOPIC "admin/monitoring/client/%s/version"
#define MQTT_IPTOPIC "admin/monitoring/client/%s/ipaddr"
#define MQTT_BROKER "mqttbroker.scimonshouse.net"
#define MQTT_PORT 1883
#define MQTT_BASETOPIC "sandbox/automation/attic/sensor/%s"
void
(
*
resetFunc
)
(
void
)
=
0
;
EthernetClient
net
;
byte
mac
[]
=
{
0x0E
,
0xBB
,
0x00
,
0x00
,
0x00
,
0x03
};
MQTTClient
client
;
String
data
=
""
;
bool
dataAvailable
=
false
;
void
mqttInit
()
{
client
.
begin
(
MQTT_BROKER
,
MQTT_PORT
,
net
);
if
(
mqttConnect
()
!=
0
)
{
resetFunc
();
}
int
verTopicLen
=
strlen
(
MQTT_VERTOPIC
)
-
1
+
strlen
(
MQTT_USERNAME
);
char
verTopic
[
verTopicLen
];
sprintf
(
verTopic
,
MQTT_VERTOPIC
,
MQTT_USERNAME
);
verTopic
[
verTopicLen
-
1
]
=
0
;
client
.
publish
(
verTopic
,
VERSION
,
true
,
1
);
}
int
mqttConnect
()
{
digitalWrite
(
2
,
HIGH
);
int
monTopicLen
=
strlen
(
MQTT_MONTOPIC
)
-
1
+
strlen
(
MQTT_USERNAME
);
char
monTopic
[
monTopicLen
];
sprintf
(
monTopic
,
MQTT_MONTOPIC
,
MQTT_USERNAME
);
monTopic
[
monTopicLen
-
1
]
=
0
;
Serial
.
println
(
monTopic
);
client
.
setWill
(
monTopic
,
"missing"
,
true
,
1
);
int
attempts
=
10
;
while
(
!
client
.
connect
(
MQTT_USERNAME
,
MQTT_USERNAME
,
MQTT_PASSWORD
)
&&
attempts
>
0
)
{
delay
(
1500
);
attempts
--
;
}
if
(
attempts
==
0
&&
!
client
.
connected
())
{
delay
(
500
);
resetFunc
();
}
client
.
publish
(
monTopic
,
"connected"
,
true
,
1
);
digitalWrite
(
2
,
LOW
);
int
ipTopicLen
=
strlen
(
MQTT_IPTOPIC
)
-
1
+
strlen
(
MQTT_USERNAME
);
char
ipTopic
[
ipTopicLen
];
sprintf
(
ipTopic
,
MQTT_IPTOPIC
,
MQTT_USERNAME
);
ipTopic
[
ipTopicLen
-
1
]
=
0
;
IPAddress
ip
=
Ethernet
.
localIP
();
client
.
publish
(
ipTopic
,
String
(
ip
[
0
])
+
"."
+
String
(
ip
[
1
])
+
"."
+
String
(
ip
[
2
])
+
"."
+
String
(
ip
[
3
]),
true
,
1
);
}
String
getToken
(
String
data
,
char
separator
,
int
index
)
{
int
found
=
0
;
int
strIndex
[]
=
{
0
,
-
1
};
int
maxIndex
=
data
.
length
()
-
1
;
for
(
int
i
=
0
;
i
<=
maxIndex
&&
found
<=
index
;
i
++
){
if
(
data
.
charAt
(
i
)
==
separator
||
i
==
maxIndex
)
{
found
++
;
strIndex
[
0
]
=
strIndex
[
1
]
+
1
;
strIndex
[
1
]
=
(
i
==
maxIndex
)
?
i
+
1
:
i
;
}
}
return
found
>
index
?
data
.
substring
(
strIndex
[
0
],
strIndex
[
1
])
:
""
;
}
void
receiveEvent
(
int
dataLength
)
{
char
localData
[
dataLength
];
int
i
;
for
(
i
=
0
;
Wire
.
available
()
>
0
;
i
++
)
{
localData
[
i
]
=
Wire
.
read
();
}
data
=
String
(
localData
).
substring
(
0
,
i
);
dataAvailable
=
true
;
}
void
setup
()
{
pinMode
(
2
,
OUTPUT
);
digitalWrite
(
2
,
HIGH
);
// connect to ethernet
delay
(
250
);
if
(
Ethernet
.
begin
(
mac
)
==
0
)
{
delay
(
5000
);
resetFunc
();
}
// connect to MQTT broker
mqttInit
();
Wire
.
begin
(
8
);
Wire
.
onReceive
(
receiveEvent
);
}
void
loop
()
{
if
(
!
client
.
connected
())
{
mqttConnect
();
}
Ethernet
.
maintain
();
client
.
loop
();
if
(
dataAvailable
)
{
String
localData
=
data
;
dataAvailable
=
false
;
String
temp
=
getToken
(
localData
,
'/'
,
0
);
String
humid
=
getToken
(
localData
,
'/'
,
1
);
String
pressure
=
getToken
(
localData
,
'/'
,
2
);
int
topicLen
=
strlen
(
MQTT_BASETOPIC
)
-
1
+
strlen
(
"temperature"
);
char
topicTemp
[
topicLen
];
sprintf
(
topicTemp
,
MQTT_BASETOPIC
,
"temperature"
);
topicTemp
[
topicLen
-
1
]
=
0
;
client
.
publish
(
topicTemp
,
temp
,
false
,
1
);
topicLen
=
strlen
(
MQTT_BASETOPIC
)
-
1
+
strlen
(
"humidity"
);
char
topicHumid
[
topicLen
];
sprintf
(
topicHumid
,
MQTT_BASETOPIC
,
"humidity"
);
topicHumid
[
topicLen
-
1
]
=
0
;
client
.
publish
(
topicHumid
,
humid
,
false
,
1
);
topicLen
=
strlen
(
MQTT_BASETOPIC
)
-
1
+
strlen
(
"pressure"
);
char
topicPressure
[
topicLen
];
sprintf
(
topicPressure
,
MQTT_BASETOPIC
,
"pressure"
);
client
.
publish
(
topicPressure
,
pressure
,
false
,
1
);
}
}
Event Timeline
stwalkerster
created this paste.
Aug 29 2020, 4:13 PM
2020-08-29 16:13:29 (UTC+0)
stwalkerster
created this object with visibility "Public (No Login Required)".
Log In to Comment