Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
VizBlocksFramework
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Design Informatics
VizBlocks
VizBlocksFramework
Commits
376c024c
Commit
376c024c
authored
4 years ago
by
emorgan
Browse files
Options
Downloads
Patches
Plain Diff
Refactoring and add deep sleep when no wifi detected
parent
6b1d80c9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/VizBlocks.cpp
+68
-18
68 additions, 18 deletions
src/VizBlocks.cpp
with
68 additions
and
18 deletions
src/VizBlocks.cpp
+
68
−
18
View file @
376c024c
...
...
@@ -48,21 +48,8 @@ void VizBlock::MQTT_connect()
return
;
}
Serial
.
print
(
"Connecting to MQTT... "
);
Serial
.
print
ln
(
"Connecting to MQTT... "
);
// Setup MQTT
_client
=
new
WiFiClient
();
_mqtt
=
new
Adafruit_MQTT_Client
(
_client
,
_server
,
_port
,
""
/* mqttt username */
,
""
/* mqtt pass*/
);
_device_subscription
=
new
Adafruit_MQTT_Subscribe
(
_mqtt
,
_id
);
_announce
=
new
Adafruit_MQTT_Publish
(
_mqtt
,
"announce"
);
_my_announce_channel
=
String
(
"announce/"
)
+
String
(
_id
);
_my_announce
=
new
Adafruit_MQTT_Publish
(
_mqtt
,
_my_announce_channel
.
c_str
());
// Setup MQTT subscription for this device
_mqtt
->
subscribe
(
_device_subscription
);
// This *would* setup a callback, but we're not doing this right now...
//_device_subscription->setCallback(test_callback);
uint8_t
retries
=
3
;
while
((
ret
=
_mqtt
->
connect
())
!=
0
)
// connect will return 0 for connected
...
...
@@ -86,6 +73,11 @@ void VizBlock::MQTT_connect()
void
VizBlock
::
wifi_connect
()
{
if
(
WiFi
.
status
()
==
WL_CONNECTED
)
{
return
;
}
WiFi
.
mode
(
WIFI_STA
);
WiFi
.
setSleepMode
(
WIFI_NONE_SLEEP
);
...
...
@@ -96,7 +88,7 @@ void VizBlock::wifi_connect()
WiFi
.
begin
(
_ssid
,
_wifi_pass
);
_wifi_timeout_initialMillis
=
millis
();
_wifi_timeout
=
30000
;
_wifi_timeout
=
30000
0
;
// Wait 5 minutes for wifi
Serial
.
println
(
"Checking for WiFi..."
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
...
...
@@ -110,8 +102,8 @@ void VizBlock::wifi_connect()
}
else
{
Serial
.
println
(
"No WiFi found. Going to sleep
for a bit...
"
);
ESP
.
deepSleep
(
10e6
);
Serial
.
println
(
"No WiFi found. Going to sleep
. Bye!
"
);
ESP
.
deepSleep
(
0
);
// deep sleep until reset. Could use D0 to wake after a specified interval, but D0 is already in use.
}
...
...
@@ -124,14 +116,57 @@ void VizBlock::wifi_connect()
Serial
.
print
(
WiFi
.
localIP
());
// Done Wifi
};
void
onStationModeDisconnectedEvent
(
const
WiFiEventStationModeDisconnected
&
evt
)
{
if
(
WiFi
.
status
()
==
WL_CONNECTED
)
{
WiFi
.
disconnect
();
}
Serial
.
println
(
"WiFi disconnected..."
);
// set the LED to be on, rather than flashing
digitalWrite
(
D4
,
LOW
);
}
void
VizBlock
::
run
()
{
int
loop_start_time
=
millis
();
serial_command
();
// The battery charging and power module will switch off automatically unless a certain amount of
// current is drawn. We can keep it on by regularly trigerring a reset input.
_rst_time
=
millis
();
if
(
_rst_time
-
_last_rst_time
>
20000
){
digitalWrite
(
D0
,
LOW
);
if
(
!
_resetting
)
{
_resetting
=
true
;
_rst_start_time
=
_rst_time
;
}
if
(
_rst_time
-
_rst_start_time
>
300
)
{
digitalWrite
(
D0
,
HIGH
);
_resetting
=
false
;
_last_rst_time
=
millis
();
}
}
if
(
_wifi
)
{
{
wifi_connect
();
mqtt_command
();
unsigned
long
current_flashMillis
=
millis
();
if
(
current_flashMillis
-
_previous_flashMillis
>=
_flash_interval
)
{
// save the last time you blinked the LED
_previous_flashMillis
=
current_flashMillis
;
// if the LED is off turn it on and vice-versa:
if
(
_ledState
==
LOW
)
{
_ledState
=
HIGH
;
}
else
{
_ledState
=
LOW
;
}
// set the LED with the ledState of the variable:
digitalWrite
(
D4
,
_ledState
);
}
}
if
(
_active
)
...
...
@@ -171,10 +206,25 @@ void VizBlock::init()
Serial
.
println
();
Serial
.
println
(
F
(
"VizBlock Node starting up"
));
Serial
.
println
(
"Initialising "
+
String
(
_id
));
pinMode
(
D4
,
OUTPUT
);
pinMode
(
D0
,
OUTPUT
);
digitalWrite
(
D4
,
LOW
);
if
(
_wifi
)
{
wifi_connect
();
// Setup MQTT
_client
=
new
WiFiClient
();
_mqtt
=
new
Adafruit_MQTT_Client
(
_client
,
_server
,
_port
,
""
/* mqttt username */
,
""
/* mqtt pass*/
);
_device_subscription
=
new
Adafruit_MQTT_Subscribe
(
_mqtt
,
_id
);
_announce
=
new
Adafruit_MQTT_Publish
(
_mqtt
,
"announce"
);
_my_announce_channel
=
String
(
"announce/"
)
+
String
(
_id
);
_my_announce
=
new
Adafruit_MQTT_Publish
(
_mqtt
,
_my_announce_channel
.
c_str
());
// Setup MQTT subscription for this device
_mqtt
->
subscribe
(
_device_subscription
);
// This *would* setup a callback, but we're not doing this right now...
//_device_subscription->setCallback(test_callback);
MQTT_connect
();
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment