Initial commit
This commit is contained in:
27
.clang-format
Normal file
27
.clang-format
Normal file
@@ -0,0 +1,27 @@
|
||||
BasedOnStyle: LLVM
|
||||
ColumnLimit: 100
|
||||
|
||||
UseTab: Never
|
||||
IndentWidth: 2
|
||||
TabWidth: 2
|
||||
|
||||
AlignConsecutiveAssignments: true
|
||||
AlignConsecutiveDeclarations: true
|
||||
AlignConsecutiveMacros: true
|
||||
AlignEscapedNewlines: Left
|
||||
AlignTrailingComments: true
|
||||
AllowShortBlocksOnASingleLine: true
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
BreakBeforeBraces: Attach
|
||||
IndentCaseLabels: true
|
||||
MaxEmptyLinesToKeep: 1
|
||||
|
||||
PointerAlignment: Right
|
||||
SpaceAfterCStyleCast: true
|
||||
|
||||
SortIncludes: true
|
||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
3
CMakeLists.txt
Normal file
3
CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
cmake_minimum_required(VERSION 3.16.0)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(WeatherIDF)
|
||||
30
LICENSE
Normal file
30
LICENSE
Normal file
@@ -0,0 +1,30 @@
|
||||
Copyright (c) 2023-2024 Marisa
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted (subject to the limitations in the disclaimer
|
||||
below) provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the copyright holder nor the names of its
|
||||
contributors may be used to endorse or promote products derived from this
|
||||
software without specific prior written permission.
|
||||
|
||||
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
|
||||
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
||||
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||||
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
||||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
20
platformio.ini
Normal file
20
platformio.ini
Normal file
@@ -0,0 +1,20 @@
|
||||
; 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 = espidf
|
||||
|
||||
monitor_speed = 115200
|
||||
monitor_filters =
|
||||
direct
|
||||
esp32_exception_decoder
|
||||
upload_speed = 921600
|
||||
1903
sdkconfig.esp32dev
Normal file
1903
sdkconfig.esp32dev
Normal file
File diff suppressed because it is too large
Load Diff
6
src/CMakeLists.txt
Normal file
6
src/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# This file was automatically generated for projects
|
||||
# without default 'CMakeLists.txt' file.
|
||||
|
||||
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
|
||||
|
||||
idf_component_register(SRCS ${app_sources})
|
||||
281
src/bme280.c
Normal file
281
src/bme280.c
Normal file
@@ -0,0 +1,281 @@
|
||||
/**
|
||||
* @file bme280.c
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief BME280 implementation
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "bme280.h"
|
||||
#include "i2c.h"
|
||||
|
||||
#define TAG "BME280"
|
||||
|
||||
static bool m_initialized = false;
|
||||
|
||||
// Forward declaration
|
||||
double m_calcDelay(struct BME280 *bme);
|
||||
double m_calcTemp(struct BME280 *bme, uint32_t t);
|
||||
double m_calcHumidity(struct BME280 *bme, uint32_t h, uint32_t t);
|
||||
double m_calcPressure(struct BME280 *bme, uint32_t p, uint32_t t);
|
||||
|
||||
void BME280Init(struct BME280 *bme, enum BME280Oversampling oversampling) {
|
||||
// Initializes i2c device
|
||||
bme->i2cHandle = i2cInitDevice(0x76, 400 * 1000);
|
||||
bme->oversampling = oversampling;
|
||||
|
||||
uint8_t reg;
|
||||
|
||||
// Poweron reset
|
||||
ESP_LOGW(TAG, "Performing power-on reset");
|
||||
uint8_t data[2] = {0xE0, 0xB6};
|
||||
ESP_ERROR_CHECK(i2c_master_transmit(bme->i2cHandle, data, sizeof(data), -1));
|
||||
vTaskDelay(250 / portTICK_PERIOD_MS);
|
||||
|
||||
// Temperature calibration data
|
||||
|
||||
reg = REG_T1;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.t1,
|
||||
sizeof(bme->calibParams.t1),
|
||||
-1));
|
||||
|
||||
reg = REG_T2;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.t2,
|
||||
sizeof(bme->calibParams.t2),
|
||||
-1));
|
||||
|
||||
reg = REG_T3;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.t3,
|
||||
sizeof(bme->calibParams.t3),
|
||||
-1));
|
||||
|
||||
// Pressure calibration data
|
||||
|
||||
reg = REG_P1;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p1,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P2;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p2,
|
||||
sizeof(bme->calibParams.p2),
|
||||
-1));
|
||||
|
||||
reg = REG_P3;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p3,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P4;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p4,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P5;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p5,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P6;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p6,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P7;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p7,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P8;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p8,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
reg = REG_P9;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.p9,
|
||||
sizeof(bme->calibParams.p1),
|
||||
-1));
|
||||
|
||||
// Humidity calibration data
|
||||
|
||||
reg = REG_H1;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.h1,
|
||||
sizeof(bme->calibParams.h1),
|
||||
-1));
|
||||
|
||||
reg = REG_H2;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.h2,
|
||||
sizeof(bme->calibParams.h2),
|
||||
-1));
|
||||
|
||||
reg = REG_H3;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.h3,
|
||||
sizeof(bme->calibParams.h3),
|
||||
-1));
|
||||
|
||||
reg = REG_H6;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(bme->i2cHandle,
|
||||
®,
|
||||
sizeof(reg),
|
||||
(uint8_t *) &bme->calibParams.h3,
|
||||
sizeof(bme->calibParams.h3),
|
||||
-1));
|
||||
|
||||
int8_t e4, e5, e6;
|
||||
|
||||
reg = REG_E4;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(
|
||||
bme->i2cHandle, (uint8_t *) ®, sizeof(reg), (uint8_t *) &e4, sizeof(e4), -1));
|
||||
|
||||
reg = REG_E5;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(
|
||||
bme->i2cHandle, (uint8_t *) ®, sizeof(reg), (uint8_t *) &e5, sizeof(e5), -1));
|
||||
|
||||
reg = REG_E6;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(
|
||||
bme->i2cHandle, (uint8_t *) ®, sizeof(reg), (uint8_t *) &e6, sizeof(e6), -1));
|
||||
|
||||
bme->calibParams.h4 = (int8_t) ((e4 << 4) | (e5 & 0x0F));
|
||||
bme->calibParams.h5 = (int8_t) (((e5 >> 4) & 0x0F) | (e6 << 4));
|
||||
|
||||
m_initialized = true;
|
||||
|
||||
ESP_LOGI(TAG, "BME280 initialized successfully");
|
||||
}
|
||||
|
||||
struct BME280Reading BME280Read(struct BME280 *bme) {
|
||||
assert(m_initialized);
|
||||
|
||||
uint8_t data[2];
|
||||
|
||||
data[0] = 0xF2;
|
||||
data[1] = (uint8_t) bme->oversampling;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit(bme->i2cHandle, (uint8_t *) &data, sizeof(data), -1));
|
||||
|
||||
data[0] = 0xF4;
|
||||
data[1] = (uint8_t) ((bme->oversampling << 5) | (bme->oversampling << 2) | 1);
|
||||
ESP_ERROR_CHECK(i2c_master_transmit(bme->i2cHandle, (uint8_t *) &data, sizeof(data), -1));
|
||||
|
||||
vTaskDelay(m_calcDelay(bme) * 1000 / portTICK_PERIOD_MS);
|
||||
|
||||
uint8_t rawData[8] = {};
|
||||
uint8_t reg = 0xF7;
|
||||
ESP_ERROR_CHECK(i2c_master_transmit_receive(
|
||||
bme->i2cHandle, (uint8_t *) ®, sizeof(reg), rawData, sizeof(rawData), -1));
|
||||
|
||||
struct BME280Reading reading;
|
||||
|
||||
uint32_t uPres = ((rawData[0] << 16) | (rawData[1] << 8) | rawData[2]) >> 4;
|
||||
uint32_t uTemp = ((rawData[3] << 16) | (rawData[4] << 8) | rawData[5]) >> 4;
|
||||
uint32_t uHumi = (rawData[6] << 8) | rawData[7];
|
||||
|
||||
reading.tempC = m_calcTemp(bme, uTemp) / 5120.;
|
||||
reading.humidity = m_calcHumidity(bme, uHumi, uTemp);
|
||||
reading.pressurehPa = m_calcPressure(bme, uPres, uTemp) / 100.;
|
||||
|
||||
return reading;
|
||||
}
|
||||
|
||||
double m_calcDelay(struct BME280 *bme) {
|
||||
double tDelay = 0.000575 + 0.0024 * (1 << bme->oversampling);
|
||||
double hDelay = 0.000575 + 0.0024 * (1 << bme->oversampling);
|
||||
double pDelay = 0.001250 + 0.0024 * (1 << bme->oversampling);
|
||||
return tDelay + hDelay + pDelay;
|
||||
}
|
||||
|
||||
double m_calcTemp(struct BME280 *bme, uint32_t t) {
|
||||
double v1, v2;
|
||||
|
||||
v1 = (t / 16384. - bme->calibParams.t1 / 1024.) * bme->calibParams.t2;
|
||||
v2 = pow(t / 131072. - bme->calibParams.t1 / 8192., 2) * bme->calibParams.t3;
|
||||
|
||||
return v1 + v2;
|
||||
}
|
||||
|
||||
double m_calcHumidity(struct BME280 *bme, uint32_t h, uint32_t t) {
|
||||
double res;
|
||||
|
||||
res = m_calcTemp(bme, t) - 76800.;
|
||||
res = ((h - (bme->calibParams.h4 * 64. + bme->calibParams.h5 / 16384. * res)) *
|
||||
(bme->calibParams.h2 / 65536. *
|
||||
(1. +
|
||||
bme->calibParams.h6 / 67108864. * res * (1. + bme->calibParams.h3 / 67108864. * res))));
|
||||
res = res * (1. - bme->calibParams.h1 * res / 524288.);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
double m_calcPressure(struct BME280 *bme, uint32_t p, uint32_t t) {
|
||||
double v1, v2, res;
|
||||
|
||||
v1 = m_calcTemp(bme, t) / 2. - 64000.;
|
||||
v2 = v1 * v1 * bme->calibParams.p6 / 32768.;
|
||||
v2 = v2 + v1 * bme->calibParams.p5 * 2.;
|
||||
v2 = v2 / 4. + bme->calibParams.p4 * 65536.;
|
||||
v1 = (bme->calibParams.p3 * v1 * v1 / 524288. + bme->calibParams.p2 * v1) / 524288.;
|
||||
v1 = (1. + v1 / 32768.) * bme->calibParams.p1;
|
||||
|
||||
if (v1 == 0)
|
||||
return 0;
|
||||
|
||||
res = 1048576. - p;
|
||||
res = ((res - v2 / 4096.) * 6250.) / v1;
|
||||
v1 = bme->calibParams.p9 * res * res / 2147483648.;
|
||||
v2 = res * bme->calibParams.p8 / 32768.;
|
||||
res = res + (v1 + v2 + bme->calibParams.p7) / 16.;
|
||||
|
||||
return res;
|
||||
}
|
||||
109
src/bme280.h
Normal file
109
src/bme280.h
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* @file bme280.h
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief BME280 helper
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <driver/i2c_master.h>
|
||||
#include <stdint.h>
|
||||
|
||||
enum BME280Regs {
|
||||
// Start of first calibration data registers
|
||||
REG_CALIB00 = 0x88,
|
||||
|
||||
// Temperature calibration data registers
|
||||
REG_T1 = 0x88,
|
||||
REG_T2 = 0x8A,
|
||||
REG_T3 = 0x8C,
|
||||
|
||||
// Pressure calibration data registers
|
||||
REG_P1 = 0x8E,
|
||||
REG_P2 = 0x90,
|
||||
REG_P3 = 0x92,
|
||||
REG_P4 = 0x94,
|
||||
REG_P5 = 0x96,
|
||||
REG_P6 = 0x98,
|
||||
REG_P7 = 0x9A,
|
||||
REG_P8 = 0x9C,
|
||||
REG_P9 = 0x9E,
|
||||
|
||||
// Humidity calibration data registers
|
||||
REG_H1 = 0xA1,
|
||||
REG_H2 = 0xE1,
|
||||
REG_H3 = 0xE3,
|
||||
REG_E4 = 0xE4,
|
||||
REG_E5 = 0xE5,
|
||||
REG_E6 = 0xE6,
|
||||
REG_H6 = 0xE7,
|
||||
|
||||
// Start of second calibration data registers
|
||||
REG_CALIB26 = 0xE1,
|
||||
|
||||
// System registers
|
||||
REG_CHIP_ID = 0xD0,
|
||||
REG_RESET = 0xE0,
|
||||
REG_CTRL_HUM = 0xF2,
|
||||
REG_STATUS = 0xF3,
|
||||
REG_CTRL_MEAS = 0xF4,
|
||||
REG_CONFIG = 0xF5,
|
||||
|
||||
// Start of raw data registers
|
||||
REG_RAWDATA = 0xF7,
|
||||
REG_P_MSB = 0xF7,
|
||||
REG_P_LSB = 0xF8,
|
||||
REG_P_XLSB = 0xF9,
|
||||
REG_T_MSB = 0xFA,
|
||||
REG_T_LSB = 0xFB,
|
||||
REG_T_XLSB = 0xFC,
|
||||
REG_H_MSB = 0xFD,
|
||||
REG_H_LSB = 0xFE,
|
||||
};
|
||||
|
||||
struct BME280CalibParams {
|
||||
// Temperature trimming parameters
|
||||
uint16_t t1;
|
||||
int16_t t2, t3;
|
||||
|
||||
// Pressure trimming parameters
|
||||
uint16_t p1;
|
||||
int16_t p2, p3, p4, p5, p6, p7, p8, p9;
|
||||
|
||||
// Humidity trimming params
|
||||
uint8_t h1;
|
||||
int16_t h2;
|
||||
int8_t h3, h4, h5, h6;
|
||||
};
|
||||
|
||||
struct BME280Reading {
|
||||
double tempC, humidity, pressurehPa;
|
||||
};
|
||||
|
||||
enum BME280Oversampling {
|
||||
OVSP_X1 = 1,
|
||||
OVSP_X2,
|
||||
OVSP_X4,
|
||||
OVSP_X8,
|
||||
OVSP_X16,
|
||||
};
|
||||
|
||||
struct BME280 {
|
||||
struct BME280CalibParams calibParams;
|
||||
enum BME280Oversampling oversampling;
|
||||
i2c_master_dev_handle_t i2cHandle;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initializes BME280 struct. Creates i2c handle, sets oversampling and calibParams
|
||||
*
|
||||
* @param bme
|
||||
*/
|
||||
void BME280Init(struct BME280 *bme, enum BME280Oversampling oversampling);
|
||||
|
||||
struct BME280Reading BME280Read(struct BME280 *bme);
|
||||
20
src/events.c
Normal file
20
src/events.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @file events.c
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief System events implementation
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#include "events.h"
|
||||
|
||||
EventGroupHandle_t wifiEventGroup = NULL;
|
||||
EventGroupHandle_t ipEventGroup = NULL;
|
||||
|
||||
void setupEvents() {
|
||||
wifiEventGroup = xEventGroupCreate();
|
||||
ipEventGroup = xEventGroupCreate();
|
||||
}
|
||||
24
src/events.h
Normal file
24
src/events.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @file events.h
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief Global system events
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/event_groups.h>
|
||||
|
||||
extern EventGroupHandle_t wifiEventGroup;
|
||||
extern EventGroupHandle_t ipEventGroup;
|
||||
|
||||
/**
|
||||
* @brief Sets up event groups, must be called before any event is set
|
||||
*
|
||||
*/
|
||||
void setupEvents();
|
||||
46
src/i2c.c
Normal file
46
src/i2c.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @file i2c.c
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
i2c_master_bus_handle_t m_handle = NULL;
|
||||
|
||||
static bool busInitialized = false;
|
||||
|
||||
i2c_master_bus_handle_t i2cInitMasterBus(uint8_t sda, uint8_t scl) {
|
||||
i2c_master_bus_config_t i2cMasterConfig = {
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.scl_io_num = scl,
|
||||
.sda_io_num = sda,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = true,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&i2cMasterConfig, &m_handle));
|
||||
busInitialized = true;
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
i2c_master_dev_handle_t i2cInitDevice(uint8_t addr, uint32_t speed) {
|
||||
assert(busInitialized);
|
||||
|
||||
i2c_device_config_t deviceCfg = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = addr,
|
||||
.scl_speed_hz = speed,
|
||||
};
|
||||
|
||||
i2c_master_dev_handle_t devHandle;
|
||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(m_handle, &deviceCfg, &devHandle));
|
||||
|
||||
return devHandle;
|
||||
}
|
||||
30
src/i2c.h
Normal file
30
src/i2c.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @file i2c.h
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief I2C helper
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#include <driver/i2c_master.h>
|
||||
|
||||
/**
|
||||
* @brief Initializes I2C master bus
|
||||
*
|
||||
* @param sda
|
||||
* @param scl
|
||||
* @return i2c_master_bus_handle_t
|
||||
*/
|
||||
i2c_master_bus_handle_t i2cInitMasterBus(uint8_t sda, uint8_t scl);
|
||||
|
||||
/**
|
||||
* @brief Initializes i2c slave device in the bus
|
||||
*
|
||||
* @param addr address of slave
|
||||
* @param speed speed of the slave in Hz
|
||||
* @return i2c_master_dev_handle_t
|
||||
*/
|
||||
i2c_master_dev_handle_t i2cInitDevice(uint8_t addr, uint32_t speed);
|
||||
33
src/main.c
Normal file
33
src/main.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <esp_log.h>
|
||||
|
||||
#include "bme280.h"
|
||||
#include "events.h"
|
||||
#include "i2c.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#define TAG "main"
|
||||
|
||||
void app_main() {
|
||||
ESP_LOGW(TAG, "helloooooooo");
|
||||
|
||||
setupEvents();
|
||||
|
||||
// wifiInit("204", "31415926");
|
||||
// wifiStart();
|
||||
|
||||
i2cInitMasterBus(4, 15);
|
||||
|
||||
struct BME280 bme;
|
||||
|
||||
BME280Init(&bme, OVSP_X1);
|
||||
|
||||
for (;;) {
|
||||
struct BME280Reading reading = BME280Read(&bme);
|
||||
|
||||
ESP_LOGW(TAG, "HEY THE TEMPERATURE IS %f °C", reading.tempC);
|
||||
ESP_LOGW(TAG, "HEY THE HUMIDITY IS %f %%", reading.humidity);
|
||||
ESP_LOGW(TAG, "HEY THE PRESSURE IS %f hPa\n", reading.pressurehPa);
|
||||
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
112
src/wifi.c
Normal file
112
src/wifi.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* @file wifi.c
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief Wifi implementation
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "events.h"
|
||||
#include "wifi.h"
|
||||
|
||||
#define TAG "wifi"
|
||||
|
||||
// Private variables
|
||||
static esp_netif_t *m_netif;
|
||||
|
||||
// Forward declaration
|
||||
static void
|
||||
m_staEventHandler(void *arg, esp_event_base_t eventBase, int32_t eventId, void *eventData);
|
||||
|
||||
int wifiInit(const char *ssid, const char *psk) {
|
||||
esp_err_t err = nvs_flash_init();
|
||||
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
m_netif = esp_netif_create_default_wifi_sta();
|
||||
|
||||
ESP_LOGD(TAG, "Init Wifi mode STA; ssid=%s; psk=%s", ssid, psk);
|
||||
|
||||
wifi_config_t espCfg = {};
|
||||
wifi_init_config_t initCfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&initCfg));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
WIFI_EVENT, ESP_EVENT_ANY_ID, m_staEventHandler, NULL, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(
|
||||
IP_EVENT, IP_EVENT_STA_GOT_IP, m_staEventHandler, NULL, NULL));
|
||||
|
||||
// espCfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
strncpy((char *) espCfg.sta.ssid, ssid, sizeof(espCfg.sta.ssid));
|
||||
strncpy((char *) espCfg.sta.password, psk, sizeof(espCfg.sta.password));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &espCfg));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wifiStart() {
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
m_staEventHandler(void *arg, esp_event_base_t eventBase, int32_t eventId, void *eventData) {
|
||||
ESP_LOGD(TAG, "Received STA event base %s; eventId=%ld", eventBase, eventId);
|
||||
|
||||
if (eventBase != WIFI_EVENT && eventBase != IP_EVENT)
|
||||
return;
|
||||
|
||||
if (eventBase == WIFI_EVENT)
|
||||
switch (eventId) {
|
||||
case WIFI_EVENT_STA_START:
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
|
||||
case WIFI_EVENT_STA_DISCONNECTED: {
|
||||
wifi_event_sta_disconnected_t *disconnected = (wifi_event_sta_disconnected_t *) eventData;
|
||||
ESP_LOGW(TAG,
|
||||
"Disconnected from STA; reason=%d; ssid=%s; Retrying connection...",
|
||||
disconnected->reason,
|
||||
disconnected->ssid);
|
||||
xEventGroupSetBits(wifiEventGroup, WIFI_DISCONNECTED);
|
||||
xEventGroupSetBits(ipEventGroup, IP_NONE);
|
||||
esp_wifi_connect();
|
||||
break;
|
||||
}
|
||||
|
||||
case WIFI_EVENT_STA_CONNECTED: {
|
||||
wifi_event_sta_connected_t *connected = (wifi_event_sta_connected_t *) eventData;
|
||||
ESP_LOGI(TAG, "Connected to STA %s", connected->ssid);
|
||||
xEventGroupSetBits(wifiEventGroup, WIFI_CONNECTED);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
else if (eventBase == IP_EVENT)
|
||||
switch (eventId) {
|
||||
case IP_EVENT_STA_GOT_IP: {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t *) eventData;
|
||||
ESP_LOGI(TAG, "Got IP from STA: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||
xEventGroupSetBits(ipEventGroup, IP_DYNAMIC);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
src/wifi.h
Normal file
39
src/wifi.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @file wifi.h
|
||||
* @author marisa (marisa@fwmari.net)
|
||||
* @brief Wifi helper
|
||||
* @version 0.1
|
||||
* @date 2024-05-06
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
enum WifiEvent {
|
||||
WIFI_NONE,
|
||||
WIFI_CONNECTED,
|
||||
WIFI_DISCONNECTED,
|
||||
};
|
||||
|
||||
enum IpEvent {
|
||||
IP_NONE,
|
||||
IP_DYNAMIC,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Initializes wifi connection in STA mode
|
||||
*
|
||||
* @param ssid
|
||||
* @param psk
|
||||
* @return int
|
||||
*/
|
||||
int wifiInit(const char *ssid, const char *psk);
|
||||
|
||||
/**
|
||||
* @brief Starts wifi with configuration previously set by @wifiInit
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int wifiStart();
|
||||
Reference in New Issue
Block a user