Skip to content

Commit

Permalink
Update BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
hung-eoh committed Sep 26, 2024
1 parent 574ac57 commit 1353e73
Show file tree
Hide file tree
Showing 11 changed files with 1,741 additions and 457 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ jobs:
- source-path: ./
- name: TinyGSM
- name: "Espressif32-S3"
fqbn: "esp32:esp32:esp32s3:PartitionScheme=min_spiffs"
fqbn: "esp32:esp32:esp32s3:PartitionScheme=default_8MB,FlashSize=8M"
platform-name: esp32:esp32
sketch-paths: |
- examples/ESP32/ESP32_WiFi_Basic
- examples/ESP32/ESP32_WiFi_SSL
- examples/ESP32/ESP32_WiFi_BLE
- examples/ESP32/ESP32_PlugNPlay
- examples/ESP32/ESP32_Ethernet_SPI
- examples/ESP32/ESP32_Ethernet_SSL_SPI
Expand Down
143 changes: 143 additions & 0 deletions examples/ESP32/ESP32_WiFi_BLE/ESP32_WiFi_BLE.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*************************************************************
Download latest ERa library here:
https://github.com/eoh-jsc/era-lib/releases/latest
https://www.arduino.cc/reference/en/libraries/era
https://registry.platformio.org/libraries/eoh-ltd/ERa/installation
ERa website: https://e-ra.io
ERa blog: https://iotasia.org
ERa forum: https://forum.eoh.io
Follow us: https://www.fb.com/EoHPlatform
*************************************************************/

// Enable debug console
// Set CORE_DEBUG_LEVEL = 3 first
// #define ERA_DEBUG
// #define ERA_SERIAL Serial

/* Select ERa host location (VN: Viet Nam, SG: Singapore) */
#define ERA_LOCATION_VN
// #define ERA_LOCATION_SG

// You should get Auth Token in the ERa App or ERa Dashboard
#define ERA_AUTH_TOKEN "ERA2706"

/* Define setting button */
// #define BUTTON_PIN 0

#if defined(BUTTON_PIN)
// Active low (false), Active high (true)
#define BUTTON_INVERT false
#define BUTTON_HOLD_TIMEOUT 5000UL

// This directive is used to specify whether the configuration should be erased.
// If it's set to true, the configuration will be erased.
#define ERA_ERASE_CONFIG false
#endif

#include <Arduino.h>
#include <ERa.hpp>
#if defined(BUTTON_PIN)
#include <pthread.h>
#include <ERa/ERaButton.hpp>
#endif
#include <BLE/ERaBLE.hpp>

const char ssid[] = "YOUR_SSID";
const char pass[] = "YOUR_PASSWORD";

const char bluetoothAddress[] = "YOUR_BLUETOOTH_ADDRESS";
const char bluetoothPassword[] = "YOUR_BLUETOOTH_PASSWORD";

WiFiClient mbTcpClient;
ERaBLETransp ERaBLE(ERa, false, false);

#if defined(BUTTON_PIN)
ERaButton button;
pthread_t pthreadButton;

static void* handlerButton(void* args) {
for (;;) {
button.run();
ERaDelay(10);
}
pthread_exit(NULL);
}

#if ERA_VERSION_NUMBER >= ERA_VERSION_VAL(1, 2, 0)
static void eventButton(uint8_t pin, ButtonEventT event) {
if (event != ButtonEventT::BUTTON_ON_HOLD) {
return;
}
ERa.switchToConfig(ERA_ERASE_CONFIG);
(void)pin;
}
#else
static void eventButton(ButtonEventT event) {
if (event != ButtonEventT::BUTTON_ON_HOLD) {
return;
}
ERa.switchToConfig(ERA_ERASE_CONFIG);
}
#endif

void initButton() {
pinMode(BUTTON_PIN, INPUT);
button.setButton(BUTTON_PIN, digitalRead, eventButton,
BUTTON_INVERT).onHold(BUTTON_HOLD_TIMEOUT);
pthread_create(&pthreadButton, NULL, handlerButton, NULL);
}
#endif

/* This function will run every time ERa is connected */
ERA_CONNECTED() {
ERA_LOG("ERa", "ERa connected!");
}

/* This function will run every time ERa is disconnected */
ERA_DISCONNECTED() {
ERA_LOG("ERa", "ERa disconnected!");
}

/* This function print uptime every second */
void timerEvent() {
ERA_LOG("Timer", "Uptime: %d", ERaMillis() / 1000L);
}

void setup() {
/* Setup debug console */
#if defined(ERA_DEBUG)
Serial.begin(115200);
#endif

#if defined(BUTTON_PIN)
/* Initializing button. */
initButton();
/* Enable read/write WiFi credentials */
ERa.setPersistent(true);
#endif

/* Set board id */
// ERa.setBoardID("Board_1");

/* Setup Client for Modbus TCP/IP */
ERa.setModbusClient(mbTcpClient);

/* Set scan WiFi. If activated, the board will scan
and connect to the best quality WiFi. */
ERa.setScanWiFi(true);

/* Initializing the ERa BLE library. */
ERaBLE.setAPI(ERa);
ERaBLE.begin(bluetoothAddress, bluetoothPassword);

/* Initializing the ERa library. */
ERa.begin(ssid, pass);

/* Setup timer called function every second */
ERa.addInterval(1000L, timerEvent);
}

void loop() {
ERa.run();
}
Loading

0 comments on commit 1353e73

Please sign in to comment.