Initial commit of Arduino libraries
This commit is contained in:
@@ -0,0 +1,262 @@
|
||||
/****************************************************************************************************************************
|
||||
FullyFeatureSSL_ESP32.ino
|
||||
|
||||
AsyncMQTT_ESP32 is a library for ESP32 boards using WiFi or LwIP W5500, LAN8720 or ENC28J60
|
||||
|
||||
Based on and modified from :
|
||||
|
||||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
|
||||
2) async-mqtt-client (https://github.com/khoih-prog/AsyncMQTT_Generic)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMQTT_ESP32
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
// Example project which can be built with SSL enabled or disabled.
|
||||
// The espressif8266_stage platform must be installed.
|
||||
// Refer to platformio.ini for the build configuration and platform installation.
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
}
|
||||
|
||||
#define ASYNC_TCP_SSL_ENABLED true
|
||||
//#define ASYNC_TCP_SSL_ENABLED false
|
||||
|
||||
#include <AsyncMQTT_ESP32.h>
|
||||
|
||||
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
|
||||
#define MQTT_HOST "broker.emqx.io" // Broker address
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
|
||||
#define MQTT_SECURE true
|
||||
|
||||
const uint8_t MQTT_SERVER_FINGERPRINT[] = {0x7e, 0x36, 0x22, 0x01, 0xf9, 0x7e, 0x99, 0x2f, 0xc5, 0xdb, 0x3d, 0xbe, 0xac, 0x48, 0x67, 0x5b, 0x5d, 0x47, 0x94, 0xd2};
|
||||
const char *PubTopic = "async-mqtt/ESP32_SSL_Pub"; // Topic to publish
|
||||
|
||||
#define MQTT_PORT 8883
|
||||
|
||||
#else
|
||||
|
||||
const char *PubTopic = "async-mqtt/ESP32_Pub"; // Topic to publish
|
||||
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#endif
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
TimerHandle_t wifiReconnectTimer;
|
||||
|
||||
void connectToWifi()
|
||||
{
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
}
|
||||
|
||||
void connectToMqtt()
|
||||
{
|
||||
Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
#if USING_CORE_ESP32_CORE_V200_PLUS
|
||||
|
||||
case ARDUINO_EVENT_WIFI_READY:
|
||||
Serial.println("WiFi ready");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_START:
|
||||
Serial.println("WiFi STA starting");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
Serial.println("WiFi STA connected");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.println("WiFi connected");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||
Serial.println("WiFi lost IP");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_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;
|
||||
#else
|
||||
|
||||
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;
|
||||
#endif
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void printSeparationLine()
|
||||
{
|
||||
Serial.println("************************************************");
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent)
|
||||
{
|
||||
Serial.print("Connected to MQTT broker: ");
|
||||
Serial.print(MQTT_HOST);
|
||||
Serial.print(", port: ");
|
||||
Serial.println(MQTT_PORT);
|
||||
Serial.print("PubTopic: ");
|
||||
Serial.println(PubTopic);
|
||||
|
||||
printSeparationLine();
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
|
||||
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
|
||||
Serial.print("Subscribing at QoS 2, packetId: ");
|
||||
Serial.println(packetIdSub);
|
||||
|
||||
mqttClient.publish(PubTopic, 0, true, "ESP32 Test");
|
||||
Serial.println("Publishing at QoS 0");
|
||||
|
||||
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
|
||||
Serial.print("Publishing at QoS 1, packetId: ");
|
||||
Serial.println(packetIdPub1);
|
||||
|
||||
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
|
||||
Serial.print("Publishing at QoS 2, packetId: ");
|
||||
Serial.println(packetIdPub2);
|
||||
|
||||
printSeparationLine();
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected())
|
||||
{
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
|
||||
{
|
||||
Serial.println("Subscribe acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
Serial.print(" qos: ");
|
||||
Serial.println(qos);
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(const uint16_t& packetId)
|
||||
{
|
||||
Serial.println("Unsubscribe acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
|
||||
const size_t& len, const size_t& index, const 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(const uint16_t& packetId)
|
||||
{
|
||||
Serial.println("Publish acknowledged");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && millis() < 5000);
|
||||
|
||||
delay(500);
|
||||
|
||||
Serial.print("\nStarting FullyFeatureSSL_ESP32 on ");
|
||||
Serial.println(ARDUINO_BOARD);
|
||||
Serial.println(ASYNC_MQTT_ESP32_VERSION);
|
||||
|
||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
|
||||
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
||||
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
|
||||
reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
||||
|
||||
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);
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
mqttClient.setSecure(MQTT_SECURE);
|
||||
|
||||
if (MQTT_SECURE)
|
||||
{
|
||||
//mqttClient.addServerFingerprint((const uint8_t[])MQTT_SERVER_FINGERPRINT);
|
||||
mqttClient.addServerFingerprint((const uint8_t *)MQTT_SERVER_FINGERPRINT);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
connectToWifi();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/****************************************************************************************************************************
|
||||
defines.h
|
||||
AsyncMQTT_ESP32 is a library for ESP32 boards using WiFi or LwIP W5500, LAN8720 or ENC28J60
|
||||
|
||||
Based on and modified from :
|
||||
|
||||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
|
||||
2) async-mqtt-client (https://github.com/khoih-prog/AsyncMQTT_Generic)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMQTT_ESP32
|
||||
***************************************************************************************************************************************/
|
||||
|
||||
#ifndef defines_h
|
||||
#define defines_h
|
||||
|
||||
#define _ASYNC_MQTT_LOGLEVEL_ 1
|
||||
|
||||
#define WIFI_SSID "yourSSID"
|
||||
#define WIFI_PASSWORD "yourpass"
|
||||
|
||||
#endif //defines_h
|
||||
@@ -0,0 +1,232 @@
|
||||
/****************************************************************************************************************************
|
||||
FullyFeature_ESP32.ino
|
||||
|
||||
AsyncMQTT_ESP32 is a library for ESP32 boards using WiFi or LwIP W5500, LAN8720 or ENC28J60
|
||||
|
||||
Based on and modified from :
|
||||
|
||||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
|
||||
2) async-mqtt-client (https://github.com/khoih-prog/AsyncMQTT_Generic)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMQTT_ESP32
|
||||
*****************************************************************************************************************************/
|
||||
/*
|
||||
This example uses FreeRTOS softwaretimers as there is no built-in Ticker library
|
||||
*/
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/timers.h"
|
||||
}
|
||||
#include <AsyncMQTT_ESP32.h>
|
||||
|
||||
//#define MQTT_HOST IPAddress(192, 168, 2, 110)
|
||||
#define MQTT_HOST "broker.emqx.io" // Broker address
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
const char *PubTopic = "async-mqtt/ESP32_Pub"; // Topic to publish
|
||||
|
||||
AsyncMqttClient mqttClient;
|
||||
TimerHandle_t mqttReconnectTimer;
|
||||
TimerHandle_t wifiReconnectTimer;
|
||||
|
||||
void connectToWifi()
|
||||
{
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
}
|
||||
|
||||
void connectToMqtt()
|
||||
{
|
||||
Serial.println("Connecting to MQTT...");
|
||||
mqttClient.connect();
|
||||
}
|
||||
|
||||
void WiFiEvent(WiFiEvent_t event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
#if USING_CORE_ESP32_CORE_V200_PLUS
|
||||
|
||||
case ARDUINO_EVENT_WIFI_READY:
|
||||
Serial.println("WiFi ready");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_START:
|
||||
Serial.println("WiFi STA starting");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
Serial.println("WiFi STA connected");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||
Serial.println("WiFi connected");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
connectToMqtt();
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||
Serial.println("WiFi lost IP");
|
||||
break;
|
||||
|
||||
case ARDUINO_EVENT_WIFI_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;
|
||||
#else
|
||||
|
||||
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;
|
||||
#endif
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void printSeparationLine()
|
||||
{
|
||||
Serial.println("************************************************");
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent)
|
||||
{
|
||||
Serial.print("Connected to MQTT broker: ");
|
||||
Serial.print(MQTT_HOST);
|
||||
Serial.print(", port: ");
|
||||
Serial.println(MQTT_PORT);
|
||||
Serial.print("PubTopic: ");
|
||||
Serial.println(PubTopic);
|
||||
|
||||
printSeparationLine();
|
||||
Serial.print("Session present: ");
|
||||
Serial.println(sessionPresent);
|
||||
|
||||
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
|
||||
Serial.print("Subscribing at QoS 2, packetId: ");
|
||||
Serial.println(packetIdSub);
|
||||
|
||||
mqttClient.publish(PubTopic, 0, true, "ESP32 Test");
|
||||
Serial.println("Publishing at QoS 0");
|
||||
|
||||
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
|
||||
Serial.print("Publishing at QoS 1, packetId: ");
|
||||
Serial.println(packetIdPub1);
|
||||
|
||||
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
|
||||
Serial.print("Publishing at QoS 2, packetId: ");
|
||||
Serial.println(packetIdPub2);
|
||||
|
||||
printSeparationLine();
|
||||
}
|
||||
|
||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
Serial.println("Disconnected from MQTT.");
|
||||
|
||||
if (WiFi.isConnected())
|
||||
{
|
||||
xTimerStart(mqttReconnectTimer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void onMqttSubscribe(const uint16_t& packetId, const uint8_t& qos)
|
||||
{
|
||||
Serial.println("Subscribe acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
Serial.print(" qos: ");
|
||||
Serial.println(qos);
|
||||
}
|
||||
|
||||
void onMqttUnsubscribe(const uint16_t& packetId)
|
||||
{
|
||||
Serial.println("Unsubscribe acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, const AsyncMqttClientMessageProperties& properties,
|
||||
const size_t& len, const size_t& index, const 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(const uint16_t& packetId)
|
||||
{
|
||||
Serial.println("Publish acknowledged.");
|
||||
Serial.print(" packetId: ");
|
||||
Serial.println(packetId);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && millis() < 5000);
|
||||
|
||||
delay(500);
|
||||
|
||||
Serial.print("\nStarting FullyFeature_ESP32 on ");
|
||||
Serial.println(ARDUINO_BOARD);
|
||||
Serial.println(ASYNC_MQTT_ESP32_VERSION);
|
||||
|
||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
|
||||
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
||||
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0,
|
||||
reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
||||
|
||||
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()
|
||||
{
|
||||
}
|
||||
21
AsyncMQTT_ESP32/examples/ESP32/FullyFeatured_ESP32/defines.h
Normal file
21
AsyncMQTT_ESP32/examples/ESP32/FullyFeatured_ESP32/defines.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/****************************************************************************************************************************
|
||||
defines.h
|
||||
AsyncMQTT_ESP32 is a library for ESP32 boards using WiFi or LwIP W5500, LAN8720 or ENC28J60
|
||||
|
||||
Based on and modified from :
|
||||
|
||||
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
|
||||
2) async-mqtt-client (https://github.com/khoih-prog/AsyncMQTT_Generic)
|
||||
|
||||
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMQTT_ESP32
|
||||
***************************************************************************************************************************************/
|
||||
|
||||
#ifndef defines_h
|
||||
#define defines_h
|
||||
|
||||
#define _ASYNC_MQTT_LOGLEVEL_ 1
|
||||
|
||||
#define WIFI_SSID "yourSSID"
|
||||
#define WIFI_PASSWORD "yourpass"
|
||||
|
||||
#endif //defines_h
|
||||
Reference in New Issue
Block a user