Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Florian Klien
iot-smoke-detector
Commits
904cc807
Commit
904cc807
authored
Aug 23, 2018
by
Florian Klien
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parents
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
0 deletions
+102
-0
LICENSE.txt
LICENSE.txt
+0
-0
main.py
main.py
+102
-0
schematic.png
schematic.png
+0
-0
No files found.
LICENSE.txt
0 → 100644
View file @
904cc807
This diff is collapsed.
Click to expand it.
main.py
0 → 100644
View file @
904cc807
# This script connects to WIFI and sends a message via MQTT.
#
# Copyright (C) 2018 Florian Klien 2018 <flowolf@klienux.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import
machine
import
network
import
socket
import
ubinascii
import
time
from
umqtt.robust
import
MQTTClient
debug
=
True
NETWORK_CONNECT_TIMEOUT
=
60
#seconds
INTERVAL
=
10
DEV_ID
=
"SmokeDetector-{0}"
.
format
(
ubinascii
.
hexlify
(
machine
.
unique_id
()).
decode
(
'utf-8'
))
MY_NETS
=
{
#"your_net": "password",
}
mqtt_server
=
"192.168.1.1"
topic
=
"/home/smoke/livingroom"
mqtt_user
=
"MQTTuser"
mqtt_pw
=
"MQTTpassword"
led
=
machine
.
Pin
(
2
,
machine
.
Pin
.
OUT
)
led
.
value
(
0
)
start_time
=
time
.
ticks_ms
()
wlan
=
network
.
WLAN
(
network
.
STA_IF
)
def
do_connect
():
print
(
"getting network"
)
network_connect_start_time
=
time
.
ticks_ms
()
import
network
if
not
wlan
.
active
():
wlan
.
active
(
True
)
nets
=
wlan
.
scan
()
for
net
in
nets
:
net
=
net
[
0
].
decode
(
'utf-8'
)
if
net
in
MY_NETS
and
not
wlan
.
isconnected
():
print
(
'connecting to network: {}'
.
format
(
net
))
wlan
.
connect
(
net
,
MY_NETS
[
net
])
#, timeout=5000)
while
not
wlan
.
isconnected
():
if
debug
:
led
.
value
(
1
)
# off
if
wlan
.
status
()
==
network
.
STAT_IDLE
:
print
(
"ERROR: nothing going on"
)
break
# if wlan.status() == network.STAT_CONNECTING:
# print("INFO: connecting to network")
if
wlan
.
status
()
==
network
.
STAT_GOT_IP
:
print
(
"INFO: what are you doing here, shouldn't be here..."
)
if
wlan
.
status
()
==
network
.
STAT_WRONG_PASSWORD
or
\
wlan
.
status
()
==
network
.
STAT_NO_AP_FOUND
or
\
wlan
.
status
()
==
network
.
STAT_CONNECT_FAIL
:
print
(
"ERROR: wifi has issues ({})"
.
format
(
wlan
.
status
()))
break
if
(
network_connect_start_time
+
NETWORK_CONNECT_TIMEOUT
*
1000
)
<
time
.
ticks_ms
():
print
(
"ERROR: network timeout. trying other network, or sleeping"
)
break
machine
.
idle
()
if
debug
:
led
.
value
(
0
)
# on
print
(
'network config:'
,
wlan
.
ifconfig
())
c
=
MQTTClient
(
DEV_ID
,
mqtt_server
,
user
=
mqtt_user
,
password
=
mqtt_pw
)
#ap.active(False)
# run notification forever, notify every INTERVAL seconds
while
True
:
current_time
=
time
.
ticks_ms
()
do_connect
()
print
(
"sending smoke alarm to MQTT"
)
led
.
value
(
0
)
try
:
c
.
connect
()
print
(
"publishing: "
)
c
.
publish
(
topic
,
"smoke"
)
c
.
disconnect
()
except
:
print
(
"ERROR: connecting or sending data to MQTT server!"
)
led
.
value
(
1
)
time
.
sleep
(
INTERVAL
)
schematic.png
0 → 100644
View file @
904cc807
64.5 KB
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