From e8a3fde146fdc34a10896d4c384004bcd4107332 Mon Sep 17 00:00:00 2001 From: Evan Morgan <e.morgan@ed.ac.uk> Date: Thu, 18 Feb 2021 15:23:36 +0000 Subject: [PATCH] WIP adding deep sleep --- src/NameDictionary.h | 3 +- src/ServoBehaviours.h | 4 +- src/VizBlocks.cpp | 90 ++++++++++++++++++++++++++----------------- src/VizBlocks.h | 7 ++++ 4 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/NameDictionary.h b/src/NameDictionary.h index ea83dd8..52e33f9 100644 --- a/src/NameDictionary.h +++ b/src/NameDictionary.h @@ -40,7 +40,7 @@ class NameDictionary {"da516d", "aya"}, {"c2a597", "bea"}, {"da5331", "bev"}, - {"8e07b", "bob"}, + {"8e07b" , "bob"}, {"c318a1", "bud"}, {"da5a56", "deb"}, {"da4a70", "cal"}, @@ -56,6 +56,7 @@ class NameDictionary {"31054b", "kev"}, {"30ce48", "sam"}, {"30db8b", "tim"}, + {"8e1ac" , "eve"}, }; int _arraySize; diff --git a/src/ServoBehaviours.h b/src/ServoBehaviours.h index 288ffc7..8616e52 100644 --- a/src/ServoBehaviours.h +++ b/src/ServoBehaviours.h @@ -38,8 +38,8 @@ class ServoWiggle : public Behaviour int _wiggle_factor = 5; public: - ServoWiggle(Servo servo, String name = "Wiggle", int slowness=3) - char* args() + ServoWiggle(Servo servo, String name = "Wiggle", int slowness=3); + char* args(); String start(String args); void update(); diff --git a/src/VizBlocks.cpp b/src/VizBlocks.cpp index 76eec6a..ce9d6bf 100644 --- a/src/VizBlocks.cpp +++ b/src/VizBlocks.cpp @@ -49,6 +49,19 @@ void VizBlock::MQTT_connect() } Serial.print("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; @@ -71,6 +84,46 @@ void VizBlock::MQTT_connect() Serial.println("MQTT Connected!"); }; +void VizBlock::wifi_connect() +{ + WiFi.mode(WIFI_STA); + WiFi.setSleepMode(WIFI_NONE_SLEEP); + + // Connect to WiFi access point. + Serial.println(); + Serial.print("Connecting to "); + Serial.println(_ssid); + + WiFi.begin(_ssid, _wifi_pass); + _wifi_timeout_initialMillis = millis(); + _wifi_timeout = 30000; + Serial.println("Checking for WiFi..."); + + while (WiFi.status() != WL_CONNECTED) + { + unsigned long currentMillis = millis(); + + if (currentMillis - _wifi_timeout_initialMillis < _wifi_timeout) + { + Serial.print("."); + delay(500); + } + else + { + Serial.println("No WiFi found. Going to sleep for a bit..."); + ESP.deepSleep(10e6); + } + + + } + + Serial.println(); + + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.print(WiFi.localIP()); // Done Wifi +}; + void VizBlock::run() { int loop_start_time = millis(); @@ -121,42 +174,7 @@ void VizBlock::init() if( _wifi ) { - - WiFi.mode(WIFI_STA); - WiFi.setSleepMode(WIFI_NONE_SLEEP); - - // Connect to WiFi access point. - Serial.println(); - Serial.print("Connecting to "); - Serial.println(_ssid); - - WiFi.begin(_ssid, _wifi_pass); - - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print("."); - } - - Serial.println(); - - Serial.println("WiFi connected"); - Serial.println("IP address: "); Serial.println(WiFi.localIP()); - // Done Wifi - - // 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); - + wifi_connect(); MQTT_connect(); } diff --git a/src/VizBlocks.h b/src/VizBlocks.h index a3f717f..02562f2 100644 --- a/src/VizBlocks.h +++ b/src/VizBlocks.h @@ -38,6 +38,8 @@ class VizBlock Behaviour* _background[NUM_BACKGROUND_BEHAVIOURS]; int _loop_time = 5; + unsigned long _wifi_timeout; // How long to wait for wifi until we go into deep sleep + unsigned long _wifi_timeout_initialMillis; Adafruit_MQTT_Client* _mqtt; Adafruit_MQTT_Subscribe* _device_subscription; @@ -108,6 +110,11 @@ public: */ void MQTT_connect(); + /** + * Function to connect and reconnect as necessary to the WIFI. + */ + void wifi_connect(); + /** Generates an array json formatted string containing information about each of the behaviours the block can perform. -- GitLab