Skip to content

Commit

Permalink
fix: some code cleanup, improved WiFi reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikMeinders committed Nov 24, 2024
1 parent 384b677 commit 1b0d59f
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 218 deletions.
107 changes: 101 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,107 @@
# knx_platformio
KNX platform for Platform IO
# KNX PlatformIO Library

I use this as a starting point for KaeNX applications I develop
A comprehensive platform for building KNX applications for ESP8266/ESP32 devices. This library provides a robust foundation for developing KNX-enabled IoT devices with features like OTA updates, network management, and monitoring capabilities.

## Compiler options
## Features

- **Multi-Platform Support**: Compatible with ESP8266 and ESP32
- **Network Management**:
- WiFi configuration via WiFiManager
- MDNS support
- Telnet debugging
- HTTP server capabilities
- **KNX Integration**:
- Full KNX protocol support
- Programming mode handling
- Cyclic updates
- **System Features**:
- OTA (Over-The-Air) updates
- Heartbeat monitoring
- Time synchronization (NTP)
- Serial debugging
- Telnet monitoring

## Installation

## Diagram
1. Add this library to your PlatformIO project:
```ini
lib_deps =
https://github.com/ErikMeinders/knx_platformio.git
```

![Visualization of the codebase](./diagram.svg)
2. Include the main header in your code:
```cpp
#include "knxp_platformio.h"
```

## Configuration

### Build Flags

The library supports various build flags to customize functionality:

- `NO_WIFI`: Disable WiFi functionality
- `NO_NTP`: Disable NTP time synchronization
- `NO_TELNET`: Disable Telnet debugging
- `NO_KNX`: Disable KNX functionality
- `NO_MDNS`: Disable MDNS support
- `NO_OTA`: Disable OTA updates
- `NO_HTTP`: Disable HTTP server
- `NO_HEARTBEAT`: Disable heartbeat monitoring

### Platform-Specific Settings

#### ESP8266
```ini
[env:esp8266]
platform = espressif8266
board = d1
```

#### ESP32
```ini
[env:esp32]
platform = espressif32
board = wemos_d1_mini32
```

## Usage

1. Create a new KNX application class:
```cpp
class MyKnxApp : public KnxApp {
public:
void setup() override {
// Your setup code
}

void loop() override {
// Your loop code
}

void status() override {
// Your status update code
}
};
```
2. Initialize the application:
```cpp
MyKnxApp myApp;
```

## Dependencies

- [ArduinoLog](https://github.com/thijse/Arduino-Log)
- [Profiler](https://github.com/erikmeinders/profiler)
- [TelnetStream](https://github.com/jandrassy/TelnetStream)
- [KNX](https://github.com/thelsing/knx)
- [WiFiManager](https://github.com/tzapu/WiFiManager)

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.
4 changes: 2 additions & 2 deletions include/knxp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class _knxapp {
* Unlikely your application will override this
*
*/
virtual void cyclic();
void cyclic();
/**
* @brief Get the Hostname object
*
Expand All @@ -66,7 +66,7 @@ class _knxapp {
*
* @param interval - in seconds
*/
void setCyclicTimer(unsigned long interval) { _cyclicKnxTimer_interval = interval * 1000; }
void setCyclicTimer(unsigned long interval);
/**
* @brief Set the Group Object Count (how many group objects to consider to send cyclic values)
*
Expand Down
100 changes: 92 additions & 8 deletions include/knxp_network.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,103 @@
#ifndef NETWORK_H
#define NETWORK_H
#ifndef KNXP_NETWORK_H
#define KNXP_NETWORK_H

#include <Arduino.h>
#include <knxp_espcompat.h>
#include <WiFiManager.h>
#include <TelnetStream.h>

/**
* @file knxp_network.h
* @brief Network management functionality for KNX PlatformIO
*
* This module handles WiFi connectivity, Telnet debugging, and mDNS services.
* It supports both station and access point modes, with automatic fallback
* and connection monitoring.
*/

/**
* @brief Get current network ready status
* @return true if network is ready, false otherwise
*/
bool isNetworkReady();

/**
* @brief Initialize and start WiFi connection
*
* @param hostname Device hostname for network identification
*
* @details
* - In normal mode, attempts to connect to configured WiFi network
* - In NETWORK_ONDEMAND mode, only starts when PROG button is pressed
* - Supports fallback to AP mode for configuration
* - Automatically restarts device on connection timeout
*
* Usage example:
* @code
* startWiFi("my-device");
* @endcode
*/
void startWiFi(const char *hostname);

#include <TelnetStream.h>

void startWiFi(const char *Hostname);
#ifndef NO_TELNET
/**
* @brief Initialize and start Telnet server
*
* @details
* - Enables remote debugging and monitoring via Telnet
* - When STDIO_TELNET is defined, redirects standard I/O to Telnet
* - Requires active network connection
*
* Usage example:
* @code
* if (isNetworkReady()) startTelnet();
* @endcode
*/
void startTelnet();
#endif
void startMDNS(const char *Hostname);

/**
* @brief Initialize and start mDNS responder
*
* @param hostname Device hostname for mDNS resolution
*
* @details
* - Sets up mDNS for local network device discovery
* - Advertises HTTP service on port 80
* - Requires active network connection
* - Device will be accessible as hostname.local
*
* Usage example:
* @code
* if (isNetworkReady()) startMDNS("my-device");
* @endcode
*/
void startMDNS(const char *hostname);

/**
* @brief Process network-related tasks
*
* @details
* - Monitors WiFi connection status
* - Attempts automatic reconnection when needed
* - Should be called regularly from the main loop
*
* Usage example:
* @code
* void loop() {
* processNetwork();
* // other loop tasks
* }
* @endcode
*/
void processNetwork();

#ifndef NO_HTTP
extern WebServer httpServer;
/**
* @brief Global HTTP server instance
* @details Available when NO_HTTP is not defined
*/
extern WebServer httpServer;
#endif

#endif
#endif // KNXP_NETWORK_H
2 changes: 0 additions & 2 deletions include/knxp_platformio.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ extern Stream *stdOut;
#endif

#define DELAY delay(STD_DELAY)

extern bool networkReady;
27 changes: 22 additions & 5 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"name": "knx_platformio",
"version": "## Will be replaced by the build system based on the git tag ##",
"description": "A platform for building KNX applications for ESP",
"keywords": "knx, esp, platformio",
"description": "A comprehensive platform for building KNX applications for ESP8266/ESP32 devices with advanced networking, OTA updates, and monitoring capabilities",
"keywords": [
"knx",
"esp8266",
"esp32",
"platformio",
"home-automation",
"iot"
],
"repository": {
"type": "git",
"url": "https://github.com/ErikMeinders/knx_platformio.git"
Expand All @@ -25,17 +32,27 @@
"src/knxp_*",
"include/knxp_*",
"library.json",
"platformio.ini"
"platformio.ini",
"README.md",
"LICENSE"
],
"exclude": [
"tests",
"cicd",
".pio*"
]
},
"dependencies": {
"profiler": "erikmeinders/profiler@^0.7.4",
"ArduinoLog": "thijse/ArduinoLog@^1.1.1",
"TelnetStream": "jandrassy/TelnetStream @ ^1.2.2",
"TelnetStream": "jandrassy/TelnetStream@^1.2.4",
"knx": "https://github.com/thelsing/knx.git",
"WiFiManager": "https://github.com/tzapu/WiFiManager.git"
},
"build": {
"libArchive": false
}
},
"examples": [
"examples/*/*.ino"
]
}
45 changes: 32 additions & 13 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,51 @@ default_envs = example-esp8266, example-esp32
workspace_dir = .pio.nosync
description = Your platform for ESP-based KNX devices

; Common settings for all environments
[env]
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep+

; Consolidated dependencies with fixed versions
lib_deps =
thijse/ArduinoLog@^1.1.1
erikmeinders/profiler@^0.7.0
jandrassy/TelnetStream @ ^1.2.4
https://github.com/thelsing/knx.git
https://github.com/tzapu/WiFiManager.git
thijse/ArduinoLog @ ^1.1.1
erikmeinders/profiler @ ^0.7.4
jandrassy/TelnetStream @ ^1.2.4
https://github.com/thelsing/knx.git
https://github.com/tzapu/WiFiManager.git

; Common build flags
build_flags =
-DMASK_VERSION=0x57B0
-DNO_HEARTBEAT
-DHOSTNAME='knx-esp'
-DNETWORK_ONDEMAND
-Wno-unknown-pragmas
-Wno-unused-function
-Wno-register
-Wno-deprecated-declarations
; Version control
-DMASK_VERSION=0x57B0
; Feature flags
-DNO_HEARTBEAT
-DHOSTNAME='"knx-esp"'
-DNETWORK_ONDEMAND
; Warning suppressions
-Wno-unknown-pragmas
-Wno-unused-function
-Wno-register
-Wno-deprecated-declarations

; ESP8266 specific settings
[env:example-esp8266]
platform = espressif8266
board = d1
build_type = debug
check_skip_packages = true
build_flags =
${env.build_flags}
-DESP8266
-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH

; ESP32 specific settings
[env:example-esp32]
platform = espressif32
board = wemos_d1_mini32
build_type = debug
build_flags =
${env.build_flags}
-DESP32
-DCORE_DEBUG_LEVEL=5
4 changes: 3 additions & 1 deletion src/knxapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ void knxapp::setup()
{
Println("Your setup");

setCyclicTimer(15);
setGroupObjectCount(1);

knx.getGroupObject(1).dataPointType(DPT_Value_Temp);
knx.getGroupObject(1).value(19.8);

setCyclicTimer(15);

}

unsigned long xx=0;
Expand Down
9 changes: 1 addition & 8 deletions src/knxapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@
class knxapp : public _knxapp
{
public:
// void progress(int step, const char *msg); // callback for knxp_platformio

// void pinsetup(); // before anything else
// void conf(); // after Wifi, NTP and Telnet

void setup() ; // after KNX configuration before KNX start
void loop(); // application loop
// void cyclic(); // cyclic KNX feedback

// char* hostname(); // callback by knxp_platformio | default: HOSTNAME from platformio.ini
void status(); // callback for additonal status information Menu item 'S'
void status(); // callback for additonal status information Menu item 'S'

};

Expand Down
Loading

0 comments on commit 1b0d59f

Please sign in to comment.