Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Rim Puks
/
its8050-2019
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
1
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
abfa8efb
authored
6 years ago
by
ripuks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added hallNode
parent
a7e3f354
master
…
patch-1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
157 additions
and
0 deletions
hallNode.ino
hallNode.ino
0 → 100644
View file @
abfa8efb
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <Wire.h>
// Replace the next variables with your SSID/Password combination
const
char
*
ssid
=
"Intz"
;
const
char
*
password
=
"intzintz"
;
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "mqtt.thingspeak.com";
const
char
*
mqtt_server
=
"85.119.83.194"
;
WiFiClient
espClient
;
PubSubClient
client
(
espClient
);
long
lastMsg
=
0
;
char
msg
[
50
];
int
value
=
0
;
WiFiUDP
ntpUDP
;
NTPClient
timeClient
(
ntpUDP
);
String
formattedDate
;
String
dayStamp
;
String
timeStamp
;
void
setup
()
{
Serial
.
begin
(
115200
);
setup_wifi
();
client
.
setServer
(
mqtt_server
,
1883
);
client
.
setCallback
(
callback
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
timeClient
.
begin
();
timeClient
.
setTimeOffset
(
10800
);
}
void
setup_wifi
()
{
delay
(
10
);
// We start by connecting to a WiFi network
Serial
.
println
();
Serial
.
print
(
"Connecting to "
);
Serial
.
println
(
ssid
);
WiFi
.
begin
(
ssid
,
password
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
println
(
""
);
Serial
.
println
(
"WiFi connected"
);
Serial
.
println
(
"IP address: "
);
Serial
.
println
(
WiFi
.
localIP
());
}
void
callback
(
char
*
topic
,
byte
*
message
,
unsigned
int
length
)
{
Serial
.
print
(
"Message arrived on topic: "
);
Serial
.
print
(
topic
);
Serial
.
print
(
". Message: "
);
String
messageTemp
;
int
hallReading
;
for
(
int
i
=
0
;
i
<
length
;
i
++
)
{
Serial
.
print
((
char
)
message
[
i
]);
messageTemp
+=
(
char
)
message
[
i
];
}
hallReading
=
messageTemp
.
toInt
();
Serial
.
println
();
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
//if (String(topic) == "channels/695112/subscribe/fields/field2") {
if
(
String
(
topic
)
==
"embedded/hall"
)
{
if
(
hallReading
<=
0
){
Serial
.
println
(
"negative"
);
digitalWrite
(
LED_BUILTIN
,
HIGH
);
}
else
if
(
hallReading
>
0
){
Serial
.
println
(
"positive"
);
digitalWrite
(
LED_BUILTIN
,
LOW
);
}
}
}
void
reconnect
()
{
// Loop until we're reconnected
while
(
!
client
.
connected
())
{
Serial
.
print
(
"Attempting MQTT connection..."
);
// Attempt to connect
if
(
client
.
connect
(
"ESP8266Client"
))
{
Serial
.
println
(
"connected"
);
// Subscribe
//client.subscribe("channels/695112/subscribe/fields/field2");
client
.
subscribe
(
"embedded/hall"
);
}
else
{
Serial
.
print
(
"failed, rc="
);
Serial
.
print
(
client
.
state
());
Serial
.
println
(
" try again in 5 seconds"
);
// Wait 5 seconds before retrying
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
setup_wifi
();
}
delay
(
5000
);
}
}
}
void
get_time
(){
while
(
!
timeClient
.
update
())
{
timeClient
.
forceUpdate
();
}
formattedDate
=
timeClient
.
getFormattedDate
();
int
splitT
=
formattedDate
.
indexOf
(
"T"
);
dayStamp
=
formattedDate
.
substring
(
0
,
splitT
);
Serial
.
print
(
"DATE: "
);
Serial
.
println
(
dayStamp
);
// Extract time
timeStamp
=
formattedDate
.
substring
(
splitT
+
1
,
formattedDate
.
length
()
-
1
);
Serial
.
print
(
"HOUR: "
);
Serial
.
println
(
timeStamp
);
}
void
loop
()
{
if
(
!
client
.
connected
())
{
reconnect
();
}
client
.
loop
();
long
now
=
millis
();
if
(
now
-
lastMsg
>
1000
)
{
lastMsg
=
now
;
int
val
=
hallRead
();
get_time
();
char
hallString
[
8
];
char
string_to_send
[
25
];
char
time_to_send
[
10
];
dtostrf
(
val
,
1
,
2
,
hallString
);
//timeStamp.toCharArray(time_to_send, 10);
//strcpy(string_to_send, hallString);
//strcat(string_to_send, ";");
//strcat(string_to_send, time_to_send);
//client.publish("channels/695112/publish/fields/field2/V8DD4LPOL4XZGM8V", hallString);
client
.
publish
(
"embedded/hall"
,
hallString
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment