diff --git a/src/NameDictionary.h b/src/NameDictionary.h index ea83dd881080a1180caa5985e6978c8af837f864..52e33f927e9baa0659e556e44d622c5715a39b58 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 288ffc7b11e88e867c25071dcb236c90505d7f8a..8616e5261ab295d38f365426347972b89b3f5618 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 76eec6aa1ff16b7fb67485db896e957379dc8ec3..ce9d6bfcba5579601c25e2d837210372f0ebb2a1 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 a3f717f3c0a16d14f22b02ee7a5686a455ca9fef..02562f20eab4fd2c7ab57a23a6fd6b05267209ac 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.