Compare commits
10 commits
main
...
rust-firmw
| Author | SHA1 | Date | |
|---|---|---|---|
| fab4ac1af7 | |||
| 48d8ef95d6 | |||
| ff2a2e2440 | |||
| c3768283d3 | |||
| 17dec99395 | |||
| bfdfdc01c5 | |||
| 6068827dfb | |||
| dac87a1696 | |||
| 5507d11fe8 | |||
| 52c1ffdb5d |
34 changed files with 2111 additions and 550 deletions
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
|
|
@ -1,8 +0,0 @@
|
||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# Editor-based HTTP Client requests
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
12
.idea/misc.xml
generated
12
.idea/misc.xml
generated
|
|
@ -1,12 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/firmware">
|
|
||||||
<contentRoot DIR="$PROJECT_DIR$" />
|
|
||||||
</component>
|
|
||||||
<component name="CidrRootsConfiguration">
|
|
||||||
<sourceRoots>
|
|
||||||
<file path="$PROJECT_DIR$/firmware/src" />
|
|
||||||
<file path="$PROJECT_DIR$/src" />
|
|
||||||
</sourceRoots>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/untitled.iml" filepath="$PROJECT_DIR$/.idea/untitled.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
2
.idea/untitled.iml
generated
2
.idea/untitled.iml
generated
|
|
@ -1,2 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
|
||||||
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"cmake.sourceDirectory": "/home/clara/repositorys/climate-go/firmware"
|
"git.ignoreLimitWarning": true
|
||||||
}
|
}
|
||||||
16
firmware/.cargo/config.toml
Normal file
16
firmware/.cargo/config.toml
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
[build]
|
||||||
|
target = "xtensa-esp32-espidf"
|
||||||
|
|
||||||
|
[target.xtensa-esp32-espidf]
|
||||||
|
linker = "ldproxy"
|
||||||
|
runner = "espflash flash --monitor"
|
||||||
|
rustflags = [ "--cfg", "espidf_time64"]
|
||||||
|
|
||||||
|
[unstable]
|
||||||
|
build-std = ["std", "panic_abort"]
|
||||||
|
|
||||||
|
[env]
|
||||||
|
MCU="esp32"
|
||||||
|
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
|
||||||
|
ESP_IDF_VERSION = "v5.2.3"
|
||||||
|
|
||||||
9
firmware/.gitignore
vendored
9
firmware/.gitignore
vendored
|
|
@ -1,5 +1,4 @@
|
||||||
.pio
|
/.vscode
|
||||||
.vscode/.browse.c_cpp.db*
|
/.embuild
|
||||||
.vscode/c_cpp_properties.json
|
/target
|
||||||
.vscode/launch.json
|
/Cargo.lock
|
||||||
.vscode/ipch
|
|
||||||
|
|
|
||||||
8
firmware/.idea/.gitignore
generated
vendored
8
firmware/.idea/.gitignore
generated
vendored
|
|
@ -1,8 +0,0 @@
|
||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# Editor-based HTTP Client requests
|
|
||||||
/httpRequests/
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
2
firmware/.idea/firmware.iml
generated
2
firmware/.idea/firmware.iml
generated
|
|
@ -1,2 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
|
||||||
9
firmware/.idea/misc.xml
generated
9
firmware/.idea/misc.xml
generated
|
|
@ -1,9 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
|
||||||
<component name="CidrRootsConfiguration">
|
|
||||||
<sourceRoots>
|
|
||||||
<file path="$PROJECT_DIR$/src" />
|
|
||||||
</sourceRoots>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
8
firmware/.idea/modules.xml
generated
8
firmware/.idea/modules.xml
generated
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/firmware.iml" filepath="$PROJECT_DIR$/.idea/firmware.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
6
firmware/.idea/vcs.xml
generated
6
firmware/.idea/vcs.xml
generated
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
10
firmware/.vscode/extensions.json
vendored
10
firmware/.vscode/extensions.json
vendored
|
|
@ -1,10 +0,0 @@
|
||||||
{
|
|
||||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
|
||||||
// for the documentation about the extensions.json format
|
|
||||||
"recommendations": [
|
|
||||||
"platformio.platformio-ide"
|
|
||||||
],
|
|
||||||
"unwantedRecommendations": [
|
|
||||||
"ms-vscode.cpptools-extension-pack"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
# !!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE
|
|
||||||
# https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags
|
|
||||||
#
|
|
||||||
# If you need to override existing CMake configuration or add extra,
|
|
||||||
# please create `CMakeListsUser.txt` in the root of project.
|
|
||||||
# The `CMakeListsUser.txt` will not be overwritten by PlatformIO.
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.13)
|
|
||||||
set(CMAKE_SYSTEM_NAME Generic)
|
|
||||||
set(CMAKE_C_COMPILER_WORKS 1)
|
|
||||||
set(CMAKE_CXX_COMPILER_WORKS 1)
|
|
||||||
|
|
||||||
project("firmware" C CXX)
|
|
||||||
|
|
||||||
include(CMakeListsPrivate.txt)
|
|
||||||
|
|
||||||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/CMakeListsUser.txt)
|
|
||||||
include(CMakeListsUser.txt)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_custom_target(
|
|
||||||
Production ALL
|
|
||||||
COMMAND platformio -c clion run "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
|
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
add_custom_target(
|
|
||||||
Debug ALL
|
|
||||||
COMMAND platformio -c clion debug "$<$<NOT:$<CONFIG:All>>:-e${CMAKE_BUILD_TYPE}>"
|
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(Z_DUMMY_TARGET ${SRC_LIST})
|
|
||||||
1811
firmware/Cargo.lock
generated
Normal file
1811
firmware/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
51
firmware/Cargo.toml
Normal file
51
firmware/Cargo.toml
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
[package]
|
||||||
|
name = "firmware"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Clara Dautermann <git@cdaut.de>"]
|
||||||
|
edition = "2021"
|
||||||
|
resolver = "2"
|
||||||
|
rust-version = "1.77"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "firmware"
|
||||||
|
harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
opt-level = "s"
|
||||||
|
|
||||||
|
[profile.dev]
|
||||||
|
debug = true # Symbols are nice and they don't increase the size on Flash
|
||||||
|
opt-level = "z"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
|
||||||
|
experimental = ["esp-idf-svc/experimental"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
log = "0.4"
|
||||||
|
esp-idf-svc = "0.51"
|
||||||
|
bme280 = "0.5.1"
|
||||||
|
esp-idf-hal = "0.45.2"
|
||||||
|
esp-idf-sys = { version = "0.36.1", features = ["binstart"] }
|
||||||
|
mh-z19 = "0.3.3"
|
||||||
|
|
||||||
|
# --- Optional Embassy Integration ---
|
||||||
|
# esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] }
|
||||||
|
|
||||||
|
# If you enable embassy-time-driver, you MUST also add one of:
|
||||||
|
|
||||||
|
# a) Standalone Embassy libs ( embassy-time, embassy-sync etc) with a foreign async runtime:
|
||||||
|
# embassy-time = { version = "0.4.0", features = ["generic-queue-8"] } # NOTE: any generic-queue variant will work
|
||||||
|
|
||||||
|
# b) With embassy-executor:
|
||||||
|
# embassy-executor = { version = "0.7", features = ["executor-thread", "arch-std"] }
|
||||||
|
|
||||||
|
# NOTE: if you use embassy-time with embassy-executor you don't need the generic-queue-8 feature
|
||||||
|
|
||||||
|
# --- Temporary workaround for embassy-executor < 0.8 ---
|
||||||
|
# esp-idf-svc = { version = "0.51", features = ["embassy-time-driver", "embassy-sync"] }
|
||||||
|
# critical-section = { version = "1.1", features = ["std"], default-features = false }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
embuild = "0.33"
|
||||||
3
firmware/build.rs
Normal file
3
firmware/build.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
embuild::espidf::sysenv::output();
|
||||||
|
}
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
|
|
||||||
This directory is intended for project header files.
|
|
||||||
|
|
||||||
A header file is a file containing C declarations and macro definitions
|
|
||||||
to be shared between several project source files. You request the use of a
|
|
||||||
header file in your project source file (C, C++, etc) located in `src` folder
|
|
||||||
by including it, with the C preprocessing directive `#include'.
|
|
||||||
|
|
||||||
```src/main.c
|
|
||||||
|
|
||||||
#include "header.h"
|
|
||||||
|
|
||||||
int main (void)
|
|
||||||
{
|
|
||||||
...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Including a header file produces the same results as copying the header file
|
|
||||||
into each source file that needs it. Such copying would be time-consuming
|
|
||||||
and error-prone. With a header file, the related declarations appear
|
|
||||||
in only one place. If they need to be changed, they can be changed in one
|
|
||||||
place, and programs that include the header file will automatically use the
|
|
||||||
new version when next recompiled. The header file eliminates the labor of
|
|
||||||
finding and changing all the copies as well as the risk that a failure to
|
|
||||||
find one copy will result in inconsistencies within a program.
|
|
||||||
|
|
||||||
In C, the usual convention is to give header files names that end with `.h'.
|
|
||||||
It is most portable to use only letters, digits, dashes, and underscores in
|
|
||||||
header file names, and at most one dot.
|
|
||||||
|
|
||||||
Read more about using header files in official GCC documentation:
|
|
||||||
|
|
||||||
* Include Syntax
|
|
||||||
* Include Operation
|
|
||||||
* Once-Only Headers
|
|
||||||
* Computed Includes
|
|
||||||
|
|
||||||
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
||||||
|
|
||||||
This directory is intended for project specific (private) libraries.
|
|
||||||
PlatformIO will compile them to static libraries and link into executable file.
|
|
||||||
|
|
||||||
The source code of each library should be placed in a an own separate directory
|
|
||||||
("lib/your_library_name/[here are source files]").
|
|
||||||
|
|
||||||
For example, see a structure of the following two libraries `Foo` and `Bar`:
|
|
||||||
|
|
||||||
|--lib
|
|
||||||
| |
|
|
||||||
| |--Bar
|
|
||||||
| | |--docs
|
|
||||||
| | |--examples
|
|
||||||
| | |--src
|
|
||||||
| | |- Bar.c
|
|
||||||
| | |- Bar.h
|
|
||||||
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
|
|
||||||
| |
|
|
||||||
| |--Foo
|
|
||||||
| | |- Foo.c
|
|
||||||
| | |- Foo.h
|
|
||||||
| |
|
|
||||||
| |- README --> THIS FILE
|
|
||||||
|
|
|
||||||
|- platformio.ini
|
|
||||||
|--src
|
|
||||||
|- main.c
|
|
||||||
|
|
||||||
and a contents of `src/main.c`:
|
|
||||||
```
|
|
||||||
#include <Foo.h>
|
|
||||||
#include <Bar.h>
|
|
||||||
|
|
||||||
int main (void)
|
|
||||||
{
|
|
||||||
...
|
|
||||||
}
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
PlatformIO Library Dependency Finder will find automatically dependent
|
|
||||||
libraries scanning project source files.
|
|
||||||
|
|
||||||
More information about PlatformIO Library Dependency Finder
|
|
||||||
- https://docs.platformio.org/page/librarymanager/ldf.html
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
; PlatformIO Project Configuration File
|
|
||||||
;
|
|
||||||
; Build options: build flags, source filter
|
|
||||||
; Upload options: custom upload port, speed and extra flags
|
|
||||||
; Library options: dependencies, extra library storages
|
|
||||||
; Advanced options: extra scripting
|
|
||||||
;
|
|
||||||
; Please visit documentation for the other options and examples
|
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
|
||||||
|
|
||||||
[env:esp32dev]
|
|
||||||
platform = espressif32
|
|
||||||
board = esp32dev
|
|
||||||
framework = arduino
|
|
||||||
lib_deps = adafruit/Adafruit BMP280 Library@^2.6.1
|
|
||||||
2
firmware/rust-toolchain.toml
Normal file
2
firmware/rust-toolchain.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "esp"
|
||||||
10
firmware/sdkconfig.defaults
Normal file
10
firmware/sdkconfig.defaults
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
|
||||||
|
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
|
||||||
|
|
||||||
|
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
|
||||||
|
# This allows to use 1 ms granularity for thread sleeps (10 ms by default).
|
||||||
|
#CONFIG_FREERTOS_HZ=1000
|
||||||
|
|
||||||
|
# Workaround for https://github.com/espressif/esp-idf/issues/7631
|
||||||
|
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
|
||||||
|
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
|
||||||
|
|
@ -1,92 +0,0 @@
|
||||||
#include <BLEDevice.h>
|
|
||||||
#include <BLEServer.h>
|
|
||||||
#include <BLE2902.h>
|
|
||||||
#include "BluetoothServer.h"
|
|
||||||
|
|
||||||
#define INTERNAL_LED_PIN 2
|
|
||||||
#define BLE_SERVER_NAME "ClimateGO"
|
|
||||||
#define SERVICE_UUID "2150123c-af53-4038-bc92-ba3d0870a9e4"
|
|
||||||
#define TEMPERATURE_CHARACTERISTIC_UUID "cba1d466-344c-4be3-ab3f-189f80dd7518"
|
|
||||||
#define PRESSURE_CHARACTERISTIC_UUID "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"
|
|
||||||
|
|
||||||
//Setup callbacks onConnect and onDisconnect
|
|
||||||
class ServerCallbacks : public BLEServerCallbacks {
|
|
||||||
void onConnect(BLEServer *pServer) override {
|
|
||||||
Serial.println("New device connected.");
|
|
||||||
digitalWrite(INTERNAL_LED_PIN, HIGH);
|
|
||||||
};
|
|
||||||
|
|
||||||
void onDisconnect(BLEServer *pServer) override {
|
|
||||||
Serial.println("Device disconnected.");
|
|
||||||
digitalWrite(INTERNAL_LED_PIN, LOW);
|
|
||||||
//restart advertising on device disconnect
|
|
||||||
pServer->getAdvertising()->start();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents the Bluetooth server that handles all interactions
|
|
||||||
*/
|
|
||||||
BluetoothServer::BluetoothServer() {
|
|
||||||
// Create the BLE Device
|
|
||||||
BLEDevice::init(BLE_SERVER_NAME);
|
|
||||||
|
|
||||||
// Create the BLE Server
|
|
||||||
this->bleServer = BLEDevice::createServer();
|
|
||||||
bleServer->setCallbacks(new ServerCallbacks());
|
|
||||||
|
|
||||||
// Create the BLE Service
|
|
||||||
this->sensorService = bleServer->createService(SERVICE_UUID);
|
|
||||||
|
|
||||||
//create characteristics that will enable data sharing
|
|
||||||
BLECharacteristic *temperatureCharacteristic = sensorService->createCharacteristic(
|
|
||||||
TEMPERATURE_CHARACTERISTIC_UUID,
|
|
||||||
BLECharacteristic::PROPERTY_READ
|
|
||||||
);
|
|
||||||
BLECharacteristic *pressureCharacteristic = sensorService->createCharacteristic(
|
|
||||||
PRESSURE_CHARACTERISTIC_UUID,
|
|
||||||
BLECharacteristic::PROPERTY_READ
|
|
||||||
);
|
|
||||||
|
|
||||||
//set initial values
|
|
||||||
temperatureCharacteristic->setValue("NA");
|
|
||||||
pressureCharacteristic->setValue("NA");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to start the Bluetooth server and enable advertising to allow other devices to connect
|
|
||||||
*/
|
|
||||||
void BluetoothServer::startServer() {
|
|
||||||
// Start the service
|
|
||||||
this->sensorService->start();
|
|
||||||
|
|
||||||
// Start advertising
|
|
||||||
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
||||||
pAdvertising->addServiceUUID(SERVICE_UUID);
|
|
||||||
this->bleServer->getAdvertising()->start();
|
|
||||||
Serial.println("Started BLE Server.");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: Convert temperature to string somehow
|
|
||||||
* Publish a new temperature to the appropriate characteristic
|
|
||||||
* @param temperature the new temperature
|
|
||||||
*/
|
|
||||||
void BluetoothServer::setTemperature(float temperature) {
|
|
||||||
sensorService
|
|
||||||
->getCharacteristic(TEMPERATURE_CHARACTERISTIC_UUID)
|
|
||||||
->setValue(temperature);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: Convert pressure to string somehow
|
|
||||||
* Publish a new pressure to the appropriate characteristic
|
|
||||||
* @param pressure the new pressure
|
|
||||||
*/
|
|
||||||
void BluetoothServer::setPressure(float pressure) {
|
|
||||||
sensorService
|
|
||||||
->getCharacteristic(PRESSURE_CHARACTERISTIC_UUID)
|
|
||||||
->setValue(pressure);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
#ifndef NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
|
||||||
#define NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
|
||||||
|
|
||||||
#include <BLEDevice.h>
|
|
||||||
#include <BLEServer.h>
|
|
||||||
#include <BLEUtils.h>
|
|
||||||
#include <BLE2902.h>
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
#define temperatureCelsius
|
|
||||||
|
|
||||||
class BluetoothServer {
|
|
||||||
private:
|
|
||||||
BLEService *sensorService;
|
|
||||||
BLEServer *bleServer;
|
|
||||||
public:
|
|
||||||
BluetoothServer();
|
|
||||||
|
|
||||||
void startServer();
|
|
||||||
void setTemperature(float temperature);
|
|
||||||
void setPressure(float pressure);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //NEW_CLIMTE_GO_BLUETOOTHSERVER_H
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
#include <Arduino.h>
|
|
||||||
#include <sensors/BmpSensor.h>
|
|
||||||
#include <ble/BluetoothServer.h>
|
|
||||||
|
|
||||||
#define SLEEP_TIME 2
|
|
||||||
#define BAUD_RATE 112500
|
|
||||||
#define INTERNAL_LED_PIN 2
|
|
||||||
|
|
||||||
|
|
||||||
Sensor *bmpSensor;
|
|
||||||
BluetoothServer *server;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(BAUD_RATE);
|
|
||||||
pinMode(INTERNAL_LED_PIN, OUTPUT);
|
|
||||||
|
|
||||||
bmpSensor = new BmpSensor();
|
|
||||||
|
|
||||||
server = new BluetoothServer();
|
|
||||||
server->startServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
sensor_data_t sample = ((BmpSensor *) bmpSensor)->sampleLowEnergy();
|
|
||||||
server->setPressure(sample.pressure);
|
|
||||||
server->setTemperature(sample.temperature);
|
|
||||||
delay(SLEEP_TIME * 1000);
|
|
||||||
}
|
|
||||||
155
firmware/src/main.rs
Normal file
155
firmware/src/main.rs
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
use bme280::{i2c::BME280, Error};
|
||||||
|
use esp_idf_hal::{
|
||||||
|
adc::{
|
||||||
|
attenuation,
|
||||||
|
oneshot::{config::AdcChannelConfig, AdcChannelDriver, AdcDriver},
|
||||||
|
ADC2,
|
||||||
|
},
|
||||||
|
delay::Delay,
|
||||||
|
gpio,
|
||||||
|
i2c::{I2cConfig, I2cDriver, I2cError},
|
||||||
|
prelude::Peripherals,
|
||||||
|
uart::{config, UartDriver},
|
||||||
|
units::Hertz,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct InitResult<'s, I2C> {
|
||||||
|
bme280: BME280<I2C>,
|
||||||
|
no2_adc: AdcChannelDriver<'s, esp_idf_hal::gpio::Gpio4, AdcDriver<'s, ADC2>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut peripherals = init();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let measurement = peripherals
|
||||||
|
.bme280
|
||||||
|
.measure(&mut Delay::new_default())
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"temperature:{0}°C, pressure:{1}",
|
||||||
|
measurement.temperature, measurement.pressure
|
||||||
|
);
|
||||||
|
|
||||||
|
match peripherals.no2_adc.read() {
|
||||||
|
Ok(v) => {
|
||||||
|
println!("NO2 ADC voltage: {v}");
|
||||||
|
}
|
||||||
|
Err(err) => log::error!("Failed to read ADC for NO2 value with error {err}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
Delay::default().delay_ms(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init<'a>() -> InitResult<'a, I2cDriver<'a>> {
|
||||||
|
//set up logger
|
||||||
|
esp_idf_sys::link_patches(); // Important!
|
||||||
|
esp_idf_svc::log::EspLogger::initialize_default();
|
||||||
|
log::info!("Meow :3");
|
||||||
|
|
||||||
|
// Initialize peripherals
|
||||||
|
let peripherals = Peripherals::take().unwrap();
|
||||||
|
|
||||||
|
// Define SCL and SDA pins
|
||||||
|
let sda = peripherals.pins.gpio21;
|
||||||
|
let scl = peripherals.pins.gpio22;
|
||||||
|
|
||||||
|
// Create I2C config
|
||||||
|
let config = I2cConfig::new().baudrate(100000.into());
|
||||||
|
|
||||||
|
log::info!("Initializing I2C bus…");
|
||||||
|
|
||||||
|
// Initialize I2C driver
|
||||||
|
let i2c_bus = match I2cDriver::new(
|
||||||
|
peripherals.i2c0, // or i2c1/i2c2 based on board
|
||||||
|
sda,
|
||||||
|
scl,
|
||||||
|
&config,
|
||||||
|
) {
|
||||||
|
Ok(i2c_connection) => i2c_connection,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Initializing I2C connection failed with {err}!");
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
log::info!("I2C up!");
|
||||||
|
log::info!("Initializing bme280…");
|
||||||
|
|
||||||
|
let mut bme280 = BME280::new_primary(i2c_bus);
|
||||||
|
|
||||||
|
//initialize Sensor
|
||||||
|
match bme280.init(&mut Delay::new(100000)) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Error initializing bme280 sensor:");
|
||||||
|
handle_i2c_error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::info!("BME280 up!");
|
||||||
|
|
||||||
|
//initialize adc2 for MICS2714
|
||||||
|
let adc2 = match AdcDriver::new(peripherals.adc2) {
|
||||||
|
Ok(driver) => driver,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to initialize AdcDriver with error {err}");
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
log::info!("Initializing ADC2 on GPIO4 for NO2 sensor…");
|
||||||
|
|
||||||
|
// Define channel config with desired attenuation
|
||||||
|
let mut channel_config = AdcChannelConfig::new();
|
||||||
|
channel_config.attenuation = attenuation::DB_11;
|
||||||
|
|
||||||
|
let adc_channel_driver: AdcChannelDriver<'a, esp_idf_hal::gpio::Gpio4, AdcDriver<'_, ADC2>> =
|
||||||
|
match AdcChannelDriver::new(adc2, peripherals.pins.gpio4, &channel_config) {
|
||||||
|
Ok(channel) => channel,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Failed to initialize AdcChannelDriver with error {err}");
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
log::info!("ADC2 channel driver@GPIO4 initialized!");
|
||||||
|
|
||||||
|
//set up mh-Z19B CO2 sensor
|
||||||
|
let tx_pin = peripherals.pins.gpio5;
|
||||||
|
let rx_pin = peripherals.pins.gpio6;
|
||||||
|
|
||||||
|
//obtain UART connection
|
||||||
|
let uart_config = config::Config::new().baudrate(Hertz(115_200));
|
||||||
|
let uart = match UartDriver::new(
|
||||||
|
peripherals.uart1,
|
||||||
|
tx_pin,
|
||||||
|
rx_pin,
|
||||||
|
Option::<gpio::AnyIOPin>::None,
|
||||||
|
Option::<gpio::AnyIOPin>::None,
|
||||||
|
&uart_config,
|
||||||
|
) {
|
||||||
|
Ok(uart_drv) => uart_drv,
|
||||||
|
Err(error) => {
|
||||||
|
log::error!("Error initializing UART connection: {error}.");
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
InitResult {
|
||||||
|
bme280: bme280,
|
||||||
|
no2_adc: adc_channel_driver,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_i2c_error(error: Error<I2cError>) {
|
||||||
|
match error {
|
||||||
|
bme280::Error::CompensationFailed => log::error!("Failed to compensate a raw measurement"),
|
||||||
|
bme280::Error::Bus(buserr) => log::error!("Bus error: {}", buserr),
|
||||||
|
bme280::Error::InvalidData => log::error!("Failed to pare sensor data!"),
|
||||||
|
bme280::Error::NoCalibrationData => log::error!("No calibration data is available (probably forgot to call or check BME280::init for failure)"),
|
||||||
|
bme280::Error::UnsupportedChip => log::error!("Chip ID doesn't match expected value"),
|
||||||
|
bme280::Error::Delay => log::error!("Delay error"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,89 +0,0 @@
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Wire.h>
|
|
||||||
#include <Adafruit_BMP280.h>
|
|
||||||
#include "BmpSensor.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new BMP Sensor and initializes it
|
|
||||||
*/
|
|
||||||
BmpSensor::BmpSensor() {
|
|
||||||
this->BmpSensor::sensor_setup();
|
|
||||||
this->state = SensorState::ASLEEP;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Function responsible for setting up the sensor and the I2C connection
|
|
||||||
*/
|
|
||||||
void BmpSensor::sensor_setup() {
|
|
||||||
while (!Serial) delay(100); // wait for native usb
|
|
||||||
|
|
||||||
unsigned status = this->bmp.begin(0x76); //set the correct I2C port
|
|
||||||
|
|
||||||
//query status and reboot the board if no sensor is detected
|
|
||||||
if (!status)
|
|
||||||
ESP.restart();
|
|
||||||
|
|
||||||
//put sensor to standby mode
|
|
||||||
this->enableStandbyMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to wake sensor up from standby mode
|
|
||||||
*/
|
|
||||||
void BmpSensor::wakeUp() {
|
|
||||||
this->bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
|
|
||||||
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
|
|
||||||
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
|
|
||||||
Adafruit_BMP280::FILTER_X16, /* Filtering. */
|
|
||||||
Adafruit_BMP280::STANDBY_MS_500 /* Standby time. */
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to put sensor to standby mode
|
|
||||||
*/
|
|
||||||
void BmpSensor::enableStandbyMode() {
|
|
||||||
this->bmp.setSampling(Adafruit_BMP280::MODE_SLEEP, /* Operating Mode. */
|
|
||||||
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
|
|
||||||
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
|
|
||||||
Adafruit_BMP280::FILTER_X16, /* Filtering. */
|
|
||||||
Adafruit_BMP280::STANDBY_MS_500 /* Standby time. */
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* read a sample from the sensor
|
|
||||||
* @return a struct containing all necessary sensor data
|
|
||||||
*/
|
|
||||||
sensor_data_t BmpSensor::sample() {
|
|
||||||
//wake sensor up if it is in standby mode
|
|
||||||
if (this->state == SensorState::ASLEEP)
|
|
||||||
this->wakeUp();
|
|
||||||
|
|
||||||
//sample pressure and temperature
|
|
||||||
sensors_event_t temp_event, pressure_event;
|
|
||||||
this->bmp_temp->getEvent(&temp_event);
|
|
||||||
this->bmp_pressure->getEvent(&pressure_event);
|
|
||||||
|
|
||||||
|
|
||||||
return sensor_data_t
|
|
||||||
{
|
|
||||||
temp_event.temperature,
|
|
||||||
pressure_event.pressure
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read a sample and put sensor to standby mode.
|
|
||||||
* !!! Do not use this if sampling time is <= 500ms !!!
|
|
||||||
* @return the sample read
|
|
||||||
*/
|
|
||||||
sensor_data_t BmpSensor::sampleLowEnergy() {
|
|
||||||
//read sample
|
|
||||||
sensor_data_t sampledData = this->sample();
|
|
||||||
//put sensor to standby
|
|
||||||
this->enableStandbyMode();
|
|
||||||
return sampledData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
#ifndef NEW_CLIMTE_GO_BMPSENSOR_H
|
|
||||||
#define NEW_CLIMTE_GO_BMPSENSOR_H
|
|
||||||
|
|
||||||
#include "Sensor.h"
|
|
||||||
#include "Adafruit_BMP280.h"
|
|
||||||
|
|
||||||
class BmpSensor : public Sensor {
|
|
||||||
private:
|
|
||||||
Adafruit_BMP280 bmp; // use I2C interface
|
|
||||||
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
|
|
||||||
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
|
|
||||||
|
|
||||||
public:
|
|
||||||
BmpSensor();
|
|
||||||
|
|
||||||
sensor_data_t sample() override;
|
|
||||||
sensor_data_t sampleLowEnergy();
|
|
||||||
|
|
||||||
void enableStandbyMode() override;
|
|
||||||
|
|
||||||
void wakeUp() override;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void sensor_setup() override;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //NEW_CLIMTE_GO_BMPSENSOR_H
|
|
||||||
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
#ifndef NEW_CLIMTE_GO_SENSOR_H
|
|
||||||
#define NEW_CLIMTE_GO_SENSOR_H
|
|
||||||
|
|
||||||
#include <sensors/sensor_data_t.h>
|
|
||||||
|
|
||||||
enum class SensorState : unsigned short {
|
|
||||||
AWAKE, ASLEEP
|
|
||||||
};
|
|
||||||
|
|
||||||
class Sensor {
|
|
||||||
public:
|
|
||||||
virtual sensor_data_t sample() = 0;
|
|
||||||
|
|
||||||
virtual void wakeUp() = 0;
|
|
||||||
|
|
||||||
virtual void enableStandbyMode() = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
SensorState state = SensorState::ASLEEP;
|
|
||||||
|
|
||||||
virtual void sensor_setup() = 0;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif //NEW_CLIMTE_GO_SENSOR_H
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
#ifndef NEW_CLIMTE_GO_SENSOR_DATA_T_H
|
|
||||||
#define NEW_CLIMTE_GO_SENSOR_DATA_T_H
|
|
||||||
|
|
||||||
#include <types.h>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This struct represents data read from any sensor.
|
|
||||||
* This implies, reading temperature from e.g. the CO2 Sensor
|
|
||||||
* does not make any sense
|
|
||||||
*/
|
|
||||||
struct sensor_data_t {
|
|
||||||
float temperature;
|
|
||||||
float pressure;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
|
|
||||||
This directory is intended for PlatformIO Unit Testing and project tests.
|
|
||||||
|
|
||||||
Unit Testing is a software testing method by which individual units of
|
|
||||||
source code, sets of one or more MCU program modules together with associated
|
|
||||||
control data, usage procedures, and operating procedures, are tested to
|
|
||||||
determine whether they are fit for use. Unit testing finds problems early
|
|
||||||
in the development cycle.
|
|
||||||
|
|
||||||
More information about PlatformIO Unit Testing:
|
|
||||||
- https://docs.platformio.org/page/plus/unit-testing.html
|
|
||||||
6
flake.lock
generated
6
flake.lock
generated
|
|
@ -2,11 +2,11 @@
|
||||||
"nodes": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1728241625,
|
"lastModified": 1751271578,
|
||||||
"narHash": "sha256-yumd4fBc/hi8a9QgA9IT8vlQuLZ2oqhkJXHPKxH/tRw=",
|
"narHash": "sha256-P/SQmKDu06x8yv7i0s8bvnnuJYkxVGBWLWHaU+tt4YY=",
|
||||||
"owner": "nixos",
|
"owner": "nixos",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "c31898adf5a8ed202ce5bea9f347b1c6871f32d1",
|
"rev": "3016b4b15d13f3089db8a41ef937b13a9e33a8df",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|
|
||||||
69
flake.nix
69
flake.nix
|
|
@ -1,25 +1,66 @@
|
||||||
{
|
{
|
||||||
description = "A Nix-flake-based PlatformIO development environment";
|
description = "A Nix-flake-based Rust ESP IDF development environment";
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||||
|
|
||||||
outputs = { self, nixpkgs }:
|
outputs = { self, nixpkgs }:
|
||||||
let
|
let
|
||||||
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
|
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
|
||||||
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
|
|
||||||
pkgs = import nixpkgs { inherit system; };
|
nixpkgs-esp-dev = builtins.fetchGit {
|
||||||
});
|
url = "https://github.com/mirrexagon/nixpkgs-esp-dev.git";
|
||||||
|
|
||||||
|
# Optionally pin to a specific commit of `nixpkgs-esp-dev`.
|
||||||
|
rev = "f37890edc9b327fcbc6d48c4cf174e65e4c0d148";
|
||||||
|
};
|
||||||
|
|
||||||
|
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs { inherit system; overlays = [ (import "${nixpkgs-esp-dev}/overlay.nix") ]; };
|
||||||
|
|
||||||
|
libxml2Shared = pkgs.libxml2.overrideAttrs (old: {
|
||||||
|
configureFlags = (old.configureFlags or [ ]) ++ [ "--enable-shared" ];
|
||||||
|
|
||||||
|
# cursed patch necessary to fix linking errors
|
||||||
|
postInstall = ''
|
||||||
|
ln -sf libxml2.so $out/lib/libxml2.so.2
|
||||||
|
${old.postInstall or ""}
|
||||||
|
'';
|
||||||
|
});
|
||||||
|
in
|
||||||
|
f { inherit pkgs libxml2Shared; }
|
||||||
|
);
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
devShells = forEachSupportedSystem ({ pkgs }: {
|
devShells = forEachSupportedSystem
|
||||||
default = pkgs.mkShell {
|
({ pkgs, libxml2Shared }: {
|
||||||
packages = with pkgs; [
|
default = pkgs.mkShell
|
||||||
platformio-core
|
{
|
||||||
openocd
|
packages = with pkgs; [
|
||||||
python3
|
esp-idf-full
|
||||||
platformio
|
espflash
|
||||||
];
|
cargo-espflash
|
||||||
};
|
espup
|
||||||
});
|
|
||||||
|
llvmPackages_16.libclang # Explicitly add libclang
|
||||||
|
llvmPackages_16.clang # Include clang/headers
|
||||||
|
pkg-config # Often needed for bindgen to find clang
|
||||||
|
glibc.dev # Needed for C headers
|
||||||
|
libxml2Shared.out
|
||||||
|
ldproxy
|
||||||
|
];
|
||||||
|
|
||||||
|
env = {
|
||||||
|
# Set LD_LIBRARY_PATH so libstdc++.so.6 zlib and libxml2 can be found
|
||||||
|
LD_LIBRARY_PATH = with pkgs; pkgs.lib.makeLibraryPath [
|
||||||
|
gcc.cc.lib
|
||||||
|
zlib
|
||||||
|
libxml2Shared.out
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue