diff --git a/include/network.h b/include/network.h index 79b99a8..df4e873 100644 --- a/include/network.h +++ b/include/network.h @@ -34,8 +34,11 @@ class Network { /** * @brief Establish a connection to the WiFi. + * + * @return true Wifi is connected + * @return false Timeout */ - static void connectWifi(); + static bool connectWifi(); /** * @brief Set all general settings to connect to a broker. diff --git a/include/networkConfig.h b/include/networkConfig.h index 31fb423..9cb7562 100644 --- a/include/networkConfig.h +++ b/include/networkConfig.h @@ -14,7 +14,22 @@ // General config #define MQTT_TIME_RECONNECT 2500 -#define HW1 + +/** + * @brief Wlan connect timeout + * + * This number multiplied by WLAN_CONNECT_LOOP_TIME is the + * timeout in milliseconds. + */ +#define WLAN_CONNECT_TIMEOUT 20 + +/** + * @brief Wlan connect loop time + * + * The time in milliseconds until the next connection attempt. + */ +#define WLAN_CONNECT_LOOP_TIME 500 +#define ESP32BROKER //Network config ESP32 diff --git a/src/main.cpp b/src/main.cpp index 7917572..e1fd95c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,13 +18,14 @@ #include #include #include +#include #include "config.h" #include "driveModi/driveManager.h" +#include "OutputBuf/outputBuf.h" #include "moveControl.h" #include "network.h" -#include "outputBufMqtt.h" #include "debugMqtt.h" #include "menu.h" @@ -40,8 +41,11 @@ MoveControl moveController; DriveManager driveManager(&moveController); Menu* main_m; LiquidCrystal_I2C* lcd; -OutputBufMqtt* bufMqtt; -DebugMqtt* debugMqtt; +OutputBuf* outputBuf; +DebugMqtt* debugMqtt = nullptr; +BluetoothSerial* serialBT = nullptr; + +bool wifiIsActive; void callbackControllerAction(void); void callbackControllerConnect(void); @@ -51,17 +55,21 @@ void makeMenu(void); void setup() { Serial.begin(115200); + char wifiIndicator = 'X'; Network::setIps(); - Network::connectWifi(); Network::setupMQTT(); - Network::checkMQTT(); - DebugMqtt::init(Network::getMqttClient(), Loglevel::debug); - - std::cout << "Activate additional output via MQTT..." << std::endl; - debugMqtt = new DebugMqtt("Console"); - bufMqtt = new OutputBufMqtt(debugMqtt); - std::cout.rdbuf(bufMqtt); + wifiIsActive = Network::connectWifi(); + if (wifiIsActive) { + Network::checkMQTT(); + DebugMqtt::init(Network::getMqttClient(), Loglevel::debug); + std::cout << "Activate additional output via MQTT..." << std::endl; + debugMqtt = new DebugMqtt("Console"); + outputBuf = new OutputBuf(debugMqtt); + wifiIndicator = '-'; + } else + outputBuf = new OutputBuf(); + std::cout.rdbuf(outputBuf); std::cout << "Welcome to Kleiax-Rover" << std::endl; std::cout << "All actions from the main program run on Core -> " << xPortGetCoreID() << std::endl; @@ -69,14 +77,22 @@ void setup() { Ps3.attach(callbackControllerAction); Ps3.attachOnConnect(callbackControllerConnect); std::cout << "\nReady to connect a PS3 Controller... \n"; + + // Need to be connected befor an other device connect Ps3.begin(CONTROLLER_MAC); + std::cout << "Activate additional output via Bluetooth..." << std::endl; + serialBT = new BluetoothSerial; + serialBT->begin("Kleiax-Rover"); + outputBuf->setSerialBT(serialBT); + outputBuf->activateMqtt(false); + Wire.begin(21, 19); // i2cScanner(); lcd = new LiquidCrystal_I2C(0x3F,16,2); lcd->init(); lcd->backlight(); - lcd->print("- Kleiax-Rover -"); + lcd->printf("%c Kleiax-Rover %c", wifiIndicator, wifiIndicator); lcd->setCursor(0, 1); lcd->print("Press PS-Button"); @@ -84,9 +100,18 @@ void setup() { } void loop() { - Network::checkWiFi(); - Network::checkMQTT(); + if (wifiIsActive) { + Network::checkWiFi(); + Network::checkMQTT(); + } + driveManager.loop(); + + // static uint32_t lastMillis = 0; + // if (millis() - lastMillis > 2000) { + // std::cout << "Is BT-Consol connected: " << serialBT->connected() << std::endl; + // lastMillis = millis(); + // } } void callbackControllerAction() { diff --git a/src/network.cpp b/src/network.cpp index 5f746c3..c81cb25 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -31,7 +31,7 @@ void Network::setupMQTT() { // mqtt_client->setServer(mqtt_server, MQTT_PORT); Network::mqtt_client->setServer(MQTT_SERVER, MQTT_PORT); Network::mqtt_client->setSocketTimeout(1); - Network::connectMQTT(); + // Network::connectMQTT(); } bool Network::connectMQTT() { @@ -52,7 +52,7 @@ bool Network::connectMQTT() { } return mqtt_client->connected(); } -void Network::connectWifi() { +bool Network::connectWifi() { // Configures static IP address if (!WiFi.config(local_IP, gateway, subnet)) std::cout << "STA Failed to configure" << std::endl; @@ -60,9 +60,16 @@ void Network::connectWifi() { // Connect to Wi-Fi network with SSID and password std::cout << "Connecting to " << WLAN_SSID << std::endl; WiFi.begin(WLAN_SSID, WLAN_PASSWORD); + uint8_t timeout = WLAN_CONNECT_TIMEOUT; while (WiFi.status() != WL_CONNECTED) { - delay(500); + delay(WLAN_CONNECT_LOOP_TIME); std::cout << "." << std::flush; + timeout--; + if (timeout == 0) { + std::cout << std::endl; + std::cout << "WiFi NOT connected." << std::endl; + return false; + } } // Print local IP address and start web server @@ -70,6 +77,7 @@ void Network::connectWifi() { std::cout << "WiFi connected." << std::endl; std::cout << "IP address: " << std::endl; std::cout << WLAN_IP << std::endl; + return true; } void Network::checkMQTT() {