-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
667 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
set(COMPONENT_SRCDIRS | ||
"src" "src/Packets" "src/Transport" | ||
) | ||
|
||
set(COMPONENT_ADD_INCLUDEDIRS | ||
"src" "src/Packets" "src/Transport" | ||
) | ||
|
||
set(COMPONENT_REQUIRES | ||
"arduino-esp32" | ||
"AsyncTCP" | ||
) | ||
|
||
register_component() | ||
|
||
target_compile_definitions(${COMPONENT_TARGET} PUBLIC -DESP32) | ||
target_compile_options(${COMPONENT_TARGET} PRIVATE -fno-rtti) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
COMPONENT_ADD_INCLUDEDIRS := src | ||
COMPONENT_SRCDIRS := src | ||
CXXFLAGS += -fno-rtti |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
lib/espMqttClient/examples/notask-esp32/notask-esp32.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include <WiFi.h> | ||
|
||
#include <espMqttClient.h> | ||
|
||
#define WIFI_SSID "yourSSID" | ||
#define WIFI_PASSWORD "yourpass" | ||
|
||
#define MQTT_HOST IPAddress(192, 168, 1, 10) | ||
#define MQTT_PORT 1883 | ||
|
||
espMqttClient mqttClient(espMqttClientTypes::UseInternalTask::NO); | ||
bool reconnectMqtt = false; | ||
uint32_t lastReconnect = 0; | ||
|
||
void connectToWiFi() { | ||
Serial.println("Connecting to Wi-Fi..."); | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
} | ||
|
||
void connectToMqtt() { | ||
Serial.println("Connecting to MQTT..."); | ||
if (!mqttClient.connect()) { | ||
reconnectMqtt = true; | ||
lastReconnect = millis(); | ||
Serial.println("Connecting failed."); | ||
} else { | ||
reconnectMqtt = false; | ||
} | ||
} | ||
|
||
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"); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void onMqttConnect(bool sessionPresent) { | ||
Serial.println("Connected to MQTT."); | ||
Serial.print("Session present: "); | ||
Serial.println(sessionPresent); | ||
uint16_t packetIdSub = mqttClient.subscribe("foo/bar", 2); | ||
Serial.print("Subscribing at QoS 2, packetId: "); | ||
Serial.println(packetIdSub); | ||
mqttClient.publish("foo/bar", 0, true, "test 1"); | ||
Serial.println("Publishing at QoS 0"); | ||
uint16_t packetIdPub1 = mqttClient.publish("foo/bar", 1, true, "test 2"); | ||
Serial.print("Publishing at QoS 1, packetId: "); | ||
Serial.println(packetIdPub1); | ||
uint16_t packetIdPub2 = mqttClient.publish("foo/bar", 2, true, "test 3"); | ||
Serial.print("Publishing at QoS 2, packetId: "); | ||
Serial.println(packetIdPub2); | ||
} | ||
|
||
void onMqttDisconnect(espMqttClientTypes::DisconnectReason reason) { | ||
Serial.printf("Disconnected from MQTT: %u.\n", static_cast<uint8_t>(reason)); | ||
|
||
if (WiFi.isConnected()) { | ||
reconnectMqtt = true; | ||
lastReconnect = millis(); | ||
} | ||
} | ||
|
||
void onMqttSubscribe(uint16_t packetId, const espMqttClientTypes::SubscribeReturncode* codes, size_t len) { | ||
Serial.println("Subscribe acknowledged."); | ||
Serial.print(" packetId: "); | ||
Serial.println(packetId); | ||
for (size_t i = 0; i < len; ++i) { | ||
Serial.print(" qos: "); | ||
Serial.println(static_cast<uint8_t>(codes[i])); | ||
} | ||
} | ||
|
||
void onMqttUnsubscribe(uint16_t packetId) { | ||
Serial.println("Unsubscribe acknowledged."); | ||
Serial.print(" packetId: "); | ||
Serial.println(packetId); | ||
} | ||
|
||
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total) { | ||
(void) payload; | ||
Serial.println("Publish received."); | ||
Serial.print(" topic: "); | ||
Serial.println(topic); | ||
Serial.print(" qos: "); | ||
Serial.println(properties.qos); | ||
Serial.print(" dup: "); | ||
Serial.println(properties.dup); | ||
Serial.print(" retain: "); | ||
Serial.println(properties.retain); | ||
Serial.print(" len: "); | ||
Serial.println(len); | ||
Serial.print(" index: "); | ||
Serial.println(index); | ||
Serial.print(" total: "); | ||
Serial.println(total); | ||
} | ||
|
||
void onMqttPublish(uint16_t packetId) { | ||
Serial.println("Publish acknowledged."); | ||
Serial.print(" packetId: "); | ||
Serial.println(packetId); | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println(); | ||
Serial.println(); | ||
|
||
WiFi.setAutoConnect(false); | ||
WiFi.setAutoReconnect(true); | ||
WiFi.onEvent(WiFiEvent); | ||
|
||
mqttClient.onConnect(onMqttConnect); | ||
mqttClient.onDisconnect(onMqttDisconnect); | ||
mqttClient.onSubscribe(onMqttSubscribe); | ||
mqttClient.onUnsubscribe(onMqttUnsubscribe); | ||
mqttClient.onMessage(onMqttMessage); | ||
mqttClient.onPublish(onMqttPublish); | ||
mqttClient.setServer(MQTT_HOST, MQTT_PORT); | ||
|
||
connectToWiFi(); | ||
} | ||
|
||
void loop() { | ||
static uint32_t currentMillis = millis(); | ||
|
||
if (reconnectMqtt && currentMillis - lastReconnect > 5000) { | ||
connectToMqtt(); | ||
} | ||
|
||
// We used to option not to use the internal task | ||
// so we need to call the loop-method ourselves. | ||
// During connecting it may block. | ||
// Creating a separate task yourself is obviously | ||
// also a possibility. | ||
mqttClient.loop(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# The following lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
SET(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(simple-esp32-idf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This example is for use with [Arduino as a component](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html) in the ESP-IDF framework. | ||
|
||
Be sure to follow [this section](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html#adding-local-library) about adding libraries to your project. |
3 changes: 3 additions & 0 deletions
3
lib/espMqttClient/examples/simple-esp32-idf/main/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
idf_component_register( | ||
SRCS "main.cpp" | ||
INCLUDE_DIRS "") |
Oops, something went wrong.