update espMqttclient
This commit is contained in:
@@ -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)
|
||||
3
lib/espMqttClient/examples/simple-esp32-idf/README.md
Normal file
3
lib/espMqttClient/examples/simple-esp32-idf/README.md
Normal file
@@ -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.
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(
|
||||
SRCS "main.cpp"
|
||||
INCLUDE_DIRS "")
|
||||
142
lib/espMqttClient/examples/simple-esp32-idf/main/main.cpp
Normal file
142
lib/espMqttClient/examples/simple-esp32-idf/main/main.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <Arduino.h>
|
||||
#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;
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#
|
||||
# Bootloader config
|
||||
#
|
||||
CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL_NONE=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL=0
|
||||
|
||||
#
|
||||
# Serial flasher config
|
||||
#
|
||||
CONFIG_ESPTOOLPY_FLASHMODE_DIO=y
|
||||
CONFIG_ESPTOOLPY_FLASHMODE="dio"
|
||||
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
|
||||
CONFIG_ESPTOOLPY_FLASHFREQ="40m"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||
#
|
||||
# Partition Table
|
||||
#
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=n
|
||||
|
||||
#
|
||||
# Arduino Configuration
|
||||
#
|
||||
CONFIG_ARDUINO_VARIANT="esp32"
|
||||
CONFIG_ENABLE_ARDUINO_DEPENDS=y
|
||||
CONFIG_AUTOSTART_ARDUINO=y
|
||||
|
||||
#
|
||||
# FreeRTOS
|
||||
#
|
||||
# 1000 require for Arduino
|
||||
CONFIG_FREERTOS_HZ=1000
|
||||
|
||||
#ASYNC_TCP
|
||||
CONFIG_ASYNC_TCP_RUN_NO_AFFINITY=y
|
||||
|
||||
#MBEDTLS
|
||||
CONFIG_MBEDTLS_PSK_MODES=y
|
||||
Reference in New Issue
Block a user