-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple-esp32 ESP IDF example project.
- Loading branch information
Showing
10 changed files
with
227 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Build simple-esp32 with ESP IDF | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: 'recursive' | ||
path: ${{ github.workspace }}/app | ||
|
||
- name: esp-idf build | ||
uses: espressif/esp-idf-ci-action@v1 | ||
with: | ||
esp_idf_version: v4.4.6 | ||
target: esp32 | ||
path: app/examples/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,10 @@ | ||
[submodule "examples/simple-esp32-idf/components/arduino-esp32"] | ||
path = examples/simple-esp32-idf/components/arduino-esp32 | ||
url = https://github.com/espressif/arduino-esp32 | ||
branch = idf-release/v4.4 | ||
[submodule "examples/simple-esp32-idf/components/AsyncTCP"] | ||
path = examples/simple-esp32-idf/components/AsyncTCP | ||
url = https://github.com/me-no-dev/AsyncTCP | ||
[submodule "examples/simple-esp32-idf/components/espMqttClient"] | ||
path = examples/simple-esp32-idf/components/espMqttClient | ||
url = https://github.com/bertmelis/espMqttClient |
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,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) |
Submodule arduino-esp32
added at
b1500b
Submodule espMqttClient
added at
ee8adb
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 "") |
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,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(); | ||
} | ||
} |
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,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 |