From 3c24cea706aa50eaa0139f5abde41b192b7e4990 Mon Sep 17 00:00:00 2001 From: jptrsn Date: Fri, 12 Oct 2018 21:37:56 -0400 Subject: [PATCH 1/7] platformio functionality - moved libraries into folder --- .gitignore | 3 +- .idea/ESP32-mqtt-room.iml | 12 ++ .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 177 ++++++++++++++++++ ESP32-mqtt-room.ino | 45 ++--- README.md | 5 +- .../BLEEddystoneTLM/BLEEddystoneTLM.cpp | 0 .../BLEEddystoneTLM/BLEEddystoneTLM.h | 0 .../BLEEddystoneURL/BLEEddystoneURL.cpp | 0 .../BLEEddystoneURL/BLEEddystoneURL.h | 0 lib/readme.txt | 41 ++++ platformio.ini | 3 +- 13 files changed, 275 insertions(+), 25 deletions(-) create mode 100644 .idea/ESP32-mqtt-room.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml rename BLEEddystoneTLM.cpp => lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp (100%) rename BLEEddystoneTLM.h => lib/BLEEddystoneTLM/BLEEddystoneTLM.h (100%) rename BLEEddystoneURL.cpp => lib/BLEEddystoneURL/BLEEddystoneURL.cpp (100%) rename BLEEddystoneURL.h => lib/BLEEddystoneURL/BLEEddystoneURL.h (100%) create mode 100644 lib/readme.txt diff --git a/.gitignore b/.gitignore index 79d222a..f3a1799 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ Settings_local.h .pioenvs .piolibdeps -lib +.clang_complete +.gcc-flags.json diff --git a/.idea/ESP32-mqtt-room.iml b/.idea/ESP32-mqtt-room.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/ESP32-mqtt-room.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..69d816c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..348a740 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + General + + + XPath + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - 1538770075726 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 5919cf79c5dc38585681934c83a82711076e6955 Mon Sep 17 00:00:00 2001 From: jptrsn Date: Fri, 12 Oct 2018 22:11:18 -0400 Subject: [PATCH 4/7] Initial copy/paste from async mqtt example --- ESP32-mqtt-room.ino | 94 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/ESP32-mqtt-room.ino b/ESP32-mqtt-room.ino index 2522b5d..0e3e784 100644 --- a/ESP32-mqtt-room.ino +++ b/ESP32-mqtt-room.ino @@ -11,7 +11,7 @@ */ #include #include -#include +#include #include #include #include @@ -29,7 +29,9 @@ uint16_t beconUUID = 0xFEAA; #define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) WiFiClient espClient; -PubSubClient client(espClient); +AsyncMqttClient mqttClient; +TimerHandle_t mqttReconnectTimer; +TimerHandle_t wifiReconnectTimer; TaskHandle_t CoreZeroTask; @@ -82,6 +84,64 @@ float calculateDistance(int rssi, int txPower) { } +void connectToWifi() { + Serial.println("Connecting to WiFi..."); + WiFi.begin(ssid, password); + WiFi.setHostname(hostname); +} + +void connectToMqtt() { + Serial.println("Connecting to MQTT"); + mqttClient.setClientId(uint64_to_string(ESP.getEfuseMac())); + mqttClient.setCredentials(mqttUser, mqttPassword); + mqttClient.connect(); +} + +void WiFiEvent(WiFiEvent_t event) { + Serial.printf("[WiFi-event] event: %d\n", event); + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + connectToMqtt(); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.println("WiFi lost connection"); + xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi + xTimerStart(wifiReconnectTimer, 0); + break; + } +} + +void onMqttConnect(bool sessionPresent) { + Serial.println("Connected to MQTT."); + Serial.print("Session present: "); + Serial.println(sessionPresent); + + String publishTopic = String(channel) + "/" + room; + if (client.publish((char *)publishTopic.c_str(), "Hello from ESP32") == true) { + Serial.println("Success sending message to topic"); + } else { + Serial.println("Error sending message"); + } + +} + +void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { + Serial.println("Disconnected from MQTT."); + + if (WiFi.isConnected()) { + xTimerStart(mqttReconnectTimer, 0); + } +} + +void onMqttPublish(uint16_t packetId) { + Serial.println("Publish acknowledged."); + Serial.print(" packetId: "); + Serial.println(packetId); +} + void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); @@ -256,36 +316,28 @@ void createTaskOnCoreZero() { void setup() { + Serial.begin(115200); - createTaskOnCoreZero(); - WiFi.begin(ssid, password); - WiFi.setHostname(hostname); - Serial.print("Connecting to WiFi.."); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } + mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast(connectToMqtt)); + wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast(connectToWifi)); - Serial.println(); - Serial.print("Connected to the WiFi network as "); - Serial.println(hostname); + WiFi.onEvent(WiFiEvent); - client.setServer(mqttServer, mqttPort); - reconnect(); + mqttClient.onConnect(onMqttConnect); + mqttClient.onDisconnect(onMqttDisconnect); - String publishTopic = String(channel) + "/" + room; - if (client.publish((char *)publishTopic.c_str(), "Hello from ESP32") == true) { - Serial.println("Success sending message to topic"); - } else { - Serial.println("Error sending message"); - } + mqttClient.onPublish(onMqttPublish); + + connectToWifi(); + createTaskOnCoreZero(); BLEDevice::init(""); pBLEScan = BLEDevice::getScan(); //create new scan pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster + } unsigned long last = 0; From c1ec26b9decef3fd32c1c91296f9bd8ef4f577c7 Mon Sep 17 00:00:00 2001 From: jptrsn Date: Fri, 12 Oct 2018 22:25:00 -0400 Subject: [PATCH 5/7] successful compile and upload --- ESP32-mqtt-room.ino | 36 +++++++++--------------------------- platformio.ini | 2 +- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/ESP32-mqtt-room.ino b/ESP32-mqtt-room.ino index 0e3e784..14a9f8f 100644 --- a/ESP32-mqtt-room.ino +++ b/ESP32-mqtt-room.ino @@ -10,12 +10,17 @@ Ported to Arduino ESP32 by Evandro Copercini */ #include -#include +extern "C" { + #include "freertos/FreeRTOS.h" + #include "freertos/timers.h" +} #include #include #include #include #include +#include +#include #include "BLEBeacon.h" #include "BLEEddystoneTLM.h" #include "BLEEddystoneURL.h" @@ -92,7 +97,6 @@ void connectToWifi() { void connectToMqtt() { Serial.println("Connecting to MQTT"); - mqttClient.setClientId(uint64_to_string(ESP.getEfuseMac())); mqttClient.setCredentials(mqttUser, mqttPassword); mqttClient.connect(); } @@ -120,7 +124,7 @@ void onMqttConnect(bool sessionPresent) { Serial.println(sessionPresent); String publishTopic = String(channel) + "/" + room; - if (client.publish((char *)publishTopic.c_str(), "Hello from ESP32") == true) { + if (mqttClient.publish((char *)publishTopic.c_str(), 0, 0, "Hello from ESP32") == true) { Serial.println("Success sending message to topic"); } else { Serial.println("Error sending message"); @@ -142,23 +146,6 @@ void onMqttPublish(uint16_t packetId) { Serial.println(packetId); } -void reconnect() { - while (!client.connected()) { - Serial.print("Attempting MQTT connection..."); - - if (client.connect(uint64_to_string(ESP.getEfuseMac()), mqttUser, mqttPassword )) { - Serial.print("connected with client id "); - Serial.println(uint64_to_string(ESP.getEfuseMac())); - } else { - Serial.print("failed, rc="); - Serial.print(client.state()); - Serial.println(" try again in 5 seconds"); - // Wait 5 seconds before retrying - delay(5000); - } - } -} - class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { @@ -287,7 +274,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { String publishTopic = String(channel) + "/" + room; - if (client.publish((char *)publishTopic.c_str(), JSONmessageBuffer) == true) { + if (mqttClient.publish((char *)publishTopic.c_str(), 0, 0, JSONmessageBuffer) == true) { Serial.print("Success sending message to topic: "); Serial.println(publishTopic); // Serial.print("Message: "); Serial.println(JSONmessageBuffer); @@ -343,11 +330,8 @@ void setup() { unsigned long last = 0; void loop() { - if (!client.connected()) { - reconnect(); - } - // vTaskDelay(10); // watchdog timer + vTaskDelay(10); // watchdog timer if (millis() - last > (waitTime * 1000) || last == 0) { Serial.println("Scanning..."); @@ -356,8 +340,6 @@ void loop() { last = millis(); } - - } char *uint64_to_string(uint64_t input) { diff --git a/platformio.ini b/platformio.ini index 6d80167..16b478e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,6 @@ src_dir = . platform = espressif32 framework = arduino board = esp32dev -lib_deps = PubSubClient@2.6.0, ArduinoJSON@5.13.2, ESP32 BLE Arduino, AsyncMqttClient@0.8.2, AsyncTCP@1.0.1 +lib_deps = ArduinoJSON@5.13.2, ESP32 BLE Arduino, AsyncMqttClient@0.8.2, AsyncTCP@1.0.1 board_build.partitions = partitions_singleapp.csv upload_port = COM13 From f0a15b632d07f45c54a3eb9877e61679e50a4ed1 Mon Sep 17 00:00:00 2001 From: jptrsn Date: Fri, 12 Oct 2018 22:39:31 -0400 Subject: [PATCH 6/7] connected to mqtt server. modified config --- ESP32-mqtt-room.ino | 1 + Settings.h | 4 ++-- Settings_CI.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ESP32-mqtt-room.ino b/ESP32-mqtt-room.ino index 14a9f8f..a47d49f 100644 --- a/ESP32-mqtt-room.ino +++ b/ESP32-mqtt-room.ino @@ -315,6 +315,7 @@ void setup() { mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onPublish(onMqttPublish); + mqttClient.setServer(mqttHost, mqttPort); connectToWifi(); diff --git a/Settings.h b/Settings.h index 7b74977..8f4c68f 100644 --- a/Settings.h +++ b/Settings.h @@ -7,8 +7,8 @@ //Replace with a human-friendly host name. #define hostname "esp32_room_presence" -//Replace with your MQTT Broker address; example: #define mqttServer "192.168.0.112" -#define mqttServer "$MQTT_BROKER_ADDRESS$" +//Replace with your MQTT Broker address; example: #define mqttHost IPAddress(192, 168, 1, 195) +#define mqttHost IPAddress(192, 168, 1, 195) //Replace with your MQTT Broker port; example: #define mqttPort 1883 #define mqttPort $MQTT_PORT$ diff --git a/Settings_CI.h b/Settings_CI.h index e7084af..e847eb4 100644 --- a/Settings_CI.h +++ b/Settings_CI.h @@ -1,7 +1,7 @@ #define ssid "Travis" #define password "123456789" #define hostname "esp32_room_presence" -#define mqttServer "192.168.1.1" +#define mqttHost IPAddress(192, 168, 1, 195) #define mqttPort 1883 #define mqttUser "homeassistant" #define mqttPassword "123456789" From 43bce879e0066310938bc3128b4952426f187215 Mon Sep 17 00:00:00 2001 From: jptrsn Date: Fri, 12 Oct 2018 22:44:43 -0400 Subject: [PATCH 7/7] added attribution link to forked files --- lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp | 11 ++++++----- lib/BLEEddystoneTLM/BLEEddystoneTLM.h | 1 + lib/BLEEddystoneURL/BLEEddystoneURL.cpp | 1 + lib/BLEEddystoneURL/BLEEddystoneURL.h | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp b/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp index 2fa1e03..c944a29 100644 --- a/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp +++ b/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp @@ -3,6 +3,7 @@ * * Created on: Mar 12, 2018 * Author: pcbreflux + * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan */ #include "Arduino.h" #include "sdkconfig.h" @@ -57,27 +58,27 @@ std::string BLEEddystoneTLM::toString() { std::string out = ""; String buff; uint32_t rawsec; - + out += "Version "; buff = String(m_eddystoneData.version, DEC); out += buff.c_str(); out += "\n"; - + out += "Battery Voltage "; buff = String(ENDIAN_CHANGE_U16(m_eddystoneData.volt), DEC); out += buff.c_str(); out += " mV\n"; - + out += "Temperature "; buff = String((float)m_eddystoneData.temp, 1); out += buff.c_str(); out += " °C\n"; - + out += "Adv. Count "; buff = String(ENDIAN_CHANGE_U32(m_eddystoneData.advCount), DEC); out += buff.c_str(); out += "\n"; - + out += "Time "; rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); buff = "0000"+String(rawsec/864000, DEC); diff --git a/lib/BLEEddystoneTLM/BLEEddystoneTLM.h b/lib/BLEEddystoneTLM/BLEEddystoneTLM.h index de65d6f..94327a3 100644 --- a/lib/BLEEddystoneTLM/BLEEddystoneTLM.h +++ b/lib/BLEEddystoneTLM/BLEEddystoneTLM.h @@ -3,6 +3,7 @@ * * Created on: Mar 12, 2018 * Author: pcbreflux + * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan */ #ifndef _BLEEddystoneTLM_H_ diff --git a/lib/BLEEddystoneURL/BLEEddystoneURL.cpp b/lib/BLEEddystoneURL/BLEEddystoneURL.cpp index ceb4f7a..19f02fa 100644 --- a/lib/BLEEddystoneURL/BLEEddystoneURL.cpp +++ b/lib/BLEEddystoneURL/BLEEddystoneURL.cpp @@ -3,6 +3,7 @@ * * Created on: Mar 12, 2018 * Author: pcbreflux + * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan */ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) diff --git a/lib/BLEEddystoneURL/BLEEddystoneURL.h b/lib/BLEEddystoneURL/BLEEddystoneURL.h index 764fdac..3ce4632 100644 --- a/lib/BLEEddystoneURL/BLEEddystoneURL.h +++ b/lib/BLEEddystoneURL/BLEEddystoneURL.h @@ -3,6 +3,7 @@ * * Created on: Mar 12, 2018 * Author: pcbreflux + * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan */ #ifndef _BLEEddystoneURL_H_