Skip to content
Snippets Groups Projects
Commit 376c024c authored by emorgan's avatar emorgan
Browse files

Refactoring and add deep sleep when no wifi detected

parent 6b1d80c9
No related branches found
No related tags found
No related merge requests found
......@@ -48,21 +48,8 @@ void VizBlock::MQTT_connect()
return;
}
Serial.print("Connecting to MQTT... ");
Serial.println("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 = 300000; // 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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment