ApfelStein lebt

This commit is contained in:
Conner 2024-10-14 01:19:50 +02:00
parent 55102027c1
commit ef3e253a12
6 changed files with 120 additions and 108 deletions

View file

@ -57,9 +57,12 @@ async fn main() -> std::io::Result<()> {
if let Ok((_, mut value)) = Data::from_bytes((&buffer, 0)) { if let Ok((_, mut value)) = Data::from_bytes((&buffer, 0)) {
println!("Received: {:#?}", value); println!("Received: {:#?}", value);
value.mac.rotate_right(2); let mut mac = value.mac;
let mac = i64::from_be_bytes(value.mac); mac.rotate_right(2);
let mac = i64::from_be_bytes(mac);
println!("MAC AS INT: {}", mac);
let sensor_data = entity::sensor_data::ActiveModel { let sensor_data = entity::sensor_data::ActiveModel {
id: ActiveValue::Set(mac), id: ActiveValue::Set(mac),
@ -72,7 +75,9 @@ async fn main() -> std::io::Result<()> {
let result = sensor_data.insert(&db).await; let result = sensor_data.insert(&db).await;
match result { match result {
Err(_) => println!("Failed to insert data"), Err(_) => println!(
"Failed to insert data (You probably didnt add the node)"
),
_ => (), _ => (),
} }
} else { } else {

View file

@ -3,7 +3,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_log.h" #include "esp_log.h"
static const char* TAG_DS18B20 = "DS18B20"; static const char *TAG_DS18B20 = "DS18B20";
static const uint16_t ds18b20_temp_conv_time[] = {94, 188, 375, 750}; // ms static const uint16_t ds18b20_temp_conv_time[] = {94, 188, 375, 750}; // ms
static const uint16_t ds18b20_resolution_val[] = {0x1F, 0x3F, 0x5F, 0x7F}; static const uint16_t ds18b20_resolution_val[] = {0x1F, 0x3F, 0x5F, 0x7F};

View file

@ -3,23 +3,26 @@
#include "onewire.h" #include "onewire.h"
typedef enum { typedef enum
TEMP_RES_9_BIT = 0, {
TEMP_RES_9_BIT = 0,
TEMP_RES_10_BIT = 1, TEMP_RES_10_BIT = 1,
TEMP_RES_11_BIT = 2, TEMP_RES_11_BIT = 2,
TEMP_RES_12_BIT = 3 TEMP_RES_12_BIT = 3
} ds18b20_temp_res_t; } ds18b20_temp_res_t;
typedef enum { typedef enum
{
_SCRATCH_WRITE = 0x4E, _SCRATCH_WRITE = 0x4E,
_SCRATCH_READ = 0xBE, _SCRATCH_READ = 0xBE,
_SCRATCH_COPY = 0x48, _SCRATCH_COPY = 0x48,
_CONVERT_T = 0x44 _CONVERT_T = 0x44
} ds18b20_commands_t; } ds18b20_commands_t;
typedef uint8_t ds18b20_scratchpad_t[9]; typedef uint8_t ds18b20_scratchpad_t[9];
typedef struct { typedef struct
{
onewire_bus_handle_t bus; onewire_bus_handle_t bus;
ds18b20_temp_res_t res; ds18b20_temp_res_t res;
ds18b20_scratchpad_t scratchpad; ds18b20_scratchpad_t scratchpad;
@ -27,11 +30,11 @@ typedef struct {
/** /**
* @brief Initialize DS18B20 * @brief Initialize DS18B20
* *
* @param device DS18B20 handler * @param device DS18B20 handler
* @param pin Data pin * @param pin Data pin
* @param resolution Temperature resolution * @param resolution Temperature resolution
* *
* @retval 1: Success * @retval 1: Success
* @retval 0: Incorrect pin or gpio configuration failed (Logs tells which happened) * @retval 0: Incorrect pin or gpio configuration failed (Logs tells which happened)
*/ */
@ -39,57 +42,57 @@ uint8_t ds18b20_init(ds18b20_handler_t *device, gpio_num_t pin, ds18b20_temp_res
/** /**
* @brief Send command to DS18B20 * @brief Send command to DS18B20
* *
* @param device DS18B20 handler * @param device DS18B20 handler
* @param command Function command * @param command Function command
*/ */
void ds18b20_send_command(ds18b20_handler_t *device, ds18b20_commands_t command); void ds18b20_send_command(ds18b20_handler_t *device, ds18b20_commands_t command);
/** /**
* @brief Write to scratchpad * @brief Write to scratchpad
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
void ds18b20_write_scratchpad(ds18b20_handler_t *device); void ds18b20_write_scratchpad(ds18b20_handler_t *device);
/** /**
* @brief Read from scratchpad * @brief Read from scratchpad
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
void ds18b20_read_scratchpad(ds18b20_handler_t *device); void ds18b20_read_scratchpad(ds18b20_handler_t *device);
/** /**
* @brief Copy to scratchpad * @brief Copy to scratchpad
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
void ds18b20_copy_scratchpad(ds18b20_handler_t *device); void ds18b20_copy_scratchpad(ds18b20_handler_t *device);
/** /**
* @brief Print scratchpad bytes * @brief Print scratchpad bytes
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
void ds18b20_print_scratchpad(ds18b20_handler_t *device); void ds18b20_print_scratchpad(ds18b20_handler_t *device);
/** /**
* @brief Initialize temperature conversion and wait for conversion * @brief Initialize temperature conversion and wait for conversion
* *
* Function sends CONV_T command and waits for X ms according to `ds18b20_temp_conv_time` static array * Function sends CONV_T command and waits for X ms according to `ds18b20_temp_conv_time` static array
* *
* @warning Should be called before `ds18b20_convert_temp()` function * @warning Should be called before `ds18b20_convert_temp()` function
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
void ds18b20_convert_temp(ds18b20_handler_t *device); void ds18b20_convert_temp(ds18b20_handler_t *device);
/** /**
* @brief Read temperature from scratchpad * @brief Read temperature from scratchpad
* *
* Function reads temperature from scratchpad and converts it to Celsius. * Function reads temperature from scratchpad and converts it to Celsius.
* @warning `ds18b20_convert_temp()` have to be called before for updated temperature. * @warning `ds18b20_convert_temp()` have to be called before for updated temperature.
* *
* @param device DS18B20 handler * @param device DS18B20 handler
*/ */
float ds18b20_read_temp(ds18b20_handler_t *device); float ds18b20_read_temp(ds18b20_handler_t *device);

View file

@ -19,10 +19,10 @@ uint8_t onewire_configure_gpio(gpio_num_t pin, gpio_config_t *custom_config)
{ {
config.intr_type = GPIO_INTR_DISABLE; config.intr_type = GPIO_INTR_DISABLE;
config.mode = GPIO_MODE_OUTPUT_OD; config.mode = GPIO_MODE_OUTPUT_OD;
config.pin_bit_mask = ((uint32_t) 1 << pin); config.pin_bit_mask = ((uint32_t)1 << pin);
config.pull_down_en = 0; config.pull_down_en = 0;
config.pull_up_en = 0; config.pull_up_en = 0;
} }
else else
{ {
config = *custom_config; config = *custom_config;
@ -32,16 +32,16 @@ uint8_t onewire_configure_gpio(gpio_num_t pin, gpio_config_t *custom_config)
{ {
return 0; return 0;
} }
return 1; return 1;
} }
uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_t *custom_config) uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_t *custom_config)
{ {
if (!bus) if (!bus)
{ {
ESP_LOGW(TAG_ONEWIRE, "bus is null! (onewire_init)"); ESP_LOGW(TAG_ONEWIRE, "bus is null! (onewire_init)");
return 0; return 0;
} }
@ -49,7 +49,7 @@ uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_
bus->mutex = xSemaphoreCreateMutex(); bus->mutex = xSemaphoreCreateMutex();
// configure GPIO // configure GPIO
if(!onewire_configure_gpio(bus_pin, custom_config)) if (!onewire_configure_gpio(bus_pin, custom_config))
{ {
return 0; return 0;
} }
@ -60,7 +60,7 @@ uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_
uint8_t onewire_reset(onewire_bus_handle_t *bus) uint8_t onewire_reset(onewire_bus_handle_t *bus)
{ {
uint8_t presence; uint8_t presence;
if (xSemaphoreTake(bus->mutex, _BLOCK_TIME)) if (xSemaphoreTake(bus->mutex, _BLOCK_TIME))
{ {
gpio_set_level(bus->pin, 0); // Send reset pulse gpio_set_level(bus->pin, 0); // Send reset pulse
@ -68,11 +68,11 @@ uint8_t onewire_reset(onewire_bus_handle_t *bus)
gpio_set_level(bus->pin, 1); // Leave floating gpio_set_level(bus->pin, 1); // Leave floating
ets_delay_us(_ONEWIRE_PRESENCE_WAIT); ets_delay_us(_ONEWIRE_PRESENCE_WAIT);
presence = !gpio_get_level(bus->pin); presence = !gpio_get_level(bus->pin);
xSemaphoreGive(bus->mutex); xSemaphoreGive(bus->mutex);
} }
else else
{ {
ESP_LOGE(TAG_ONEWIRE, _SEMFAIL_MSG, "onewire_reset"); ESP_LOGE(TAG_ONEWIRE, _SEMFAIL_MSG, "onewire_reset");
@ -129,7 +129,7 @@ uint8_t onewire_read_bit(onewire_bus_handle_t *bus)
ets_delay_us(_ONEWIRE_READ_WAIT); ets_delay_us(_ONEWIRE_READ_WAIT);
bit = !gpio_get_level(bus->pin); bit = !gpio_get_level(bus->pin);
xSemaphoreGive(bus->mutex); xSemaphoreGive(bus->mutex);
ets_delay_us(_ONEWIRE_READ_RECOVERY); ets_delay_us(_ONEWIRE_READ_RECOVERY);

View file

@ -7,41 +7,43 @@
#include "esp_types.h" #include "esp_types.h"
#include "esp_err.h" #include "esp_err.h"
#define _ONEWIRE_WRITE1_LOW 6 #define _ONEWIRE_WRITE1_LOW 6
#define _ONEWIRE_WRITE1_WAIT 64 #define _ONEWIRE_WRITE1_WAIT 64
#define _ONEWIRE_WRITE0_LOW 60 #define _ONEWIRE_WRITE0_LOW 60
#define _ONEWIRE_WRITE0_WAIT 10 #define _ONEWIRE_WRITE0_WAIT 10
#define _ONEWIRE_READ_WAIT 9 #define _ONEWIRE_READ_WAIT 9
#define _ONEWIRE_READ_RECOVERY 55 #define _ONEWIRE_READ_RECOVERY 55
#define _ONEWIRE_RESET_WAIT 480 #define _ONEWIRE_RESET_WAIT 480
#define _ONEWIRE_PRESENCE_WAIT 70 #define _ONEWIRE_PRESENCE_WAIT 70
#define _ONEWIRE_RESET_RECOVERY 410 #define _ONEWIRE_RESET_RECOVERY 410
#define _BLOCK_TIME pdMS_TO_TICKS(1000) #define _BLOCK_TIME pdMS_TO_TICKS(1000)
#define _SEMFAIL_MSG "Failed to obtain semaphore. (%s)" #define _SEMFAIL_MSG "Failed to obtain semaphore. (%s)"
static const char *TAG_ONEWIRE = "ONEWIRE"; static const char *TAG_ONEWIRE = "ONEWIRE";
typedef enum { typedef enum
_ROM_READ = 0x33, {
_ROM_READ = 0x33,
_ROM_SEARCH = 0xF0, _ROM_SEARCH = 0xF0,
_ROM_MATCH = 0x55, _ROM_MATCH = 0x55,
_ROM_SKIP = 0xCC _ROM_SKIP = 0xCC
} onewire_rom_commands_t; } onewire_rom_commands_t;
typedef struct { typedef struct
{
gpio_num_t pin; gpio_num_t pin;
SemaphoreHandle_t mutex; SemaphoreHandle_t mutex;
} onewire_bus_handle_t; } onewire_bus_handle_t;
/** /**
* @brief Configure gpio pins for onewire communication * @brief Configure gpio pins for onewire communication
* *
* Set `custom_config` to NULL for default config. * Set `custom_config` to NULL for default config.
* *
* @param pin Bus pin * @param pin Bus pin
* @param custom_config Custom gpio config * @param custom_config Custom gpio config
* *
* @retval 1: Success * @retval 1: Success
* @retval 0: Incorrect pin or gpio configuration failed (Logs tells which happened) * @retval 0: Incorrect pin or gpio configuration failed (Logs tells which happened)
*/ */
@ -49,14 +51,14 @@ uint8_t onewire_configure_gpio(gpio_num_t pin, gpio_config_t *custom_config);
/** /**
* @brief Initalize onewire bus * @brief Initalize onewire bus
* *
* Set `custom_config` to NULL for default config. * Set `custom_config` to NULL for default config.
* @warning MUST be called before any other library function! * @warning MUST be called before any other library function!
* *
* @param bus Bus handle * @param bus Bus handle
* @param pin Bus pin * @param pin Bus pin
* @param custom_config Custom gpio config * @param custom_config Custom gpio config
* *
* @retval 1: Success * @retval 1: Success
* @retval 0: `bus` is NULL or gpio configuration failed (Logs tells which happened) * @retval 0: `bus` is NULL or gpio configuration failed (Logs tells which happened)
*/ */
@ -64,9 +66,9 @@ uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_
/** /**
* @brief Send reset pulse * @brief Send reset pulse
* *
* @param bus Bus handle * @param bus Bus handle
* *
* @retval 1: Success (device sent presence pulse) * @retval 1: Success (device sent presence pulse)
* @retval -1: Failed to obtain semaphore for gpio handling * @retval -1: Failed to obtain semaphore for gpio handling
* @retval 0: Device failed to return presence pulse * @retval 0: Device failed to return presence pulse
@ -75,7 +77,7 @@ uint8_t onewire_reset(onewire_bus_handle_t *bus);
/** /**
* @brief Write bit * @brief Write bit
* *
* @param bus Bus handle * @param bus Bus handle
* @param bit Bit to send * @param bit Bit to send
*/ */
@ -83,7 +85,7 @@ void onewire_write_bit(onewire_bus_handle_t *bus, uint8_t bit);
/** /**
* @brief Write byte * @brief Write byte
* *
* @param bus Bus handle * @param bus Bus handle
* @param bit Byte to send * @param bit Byte to send
*/ */
@ -91,9 +93,9 @@ void onewire_write_byte(onewire_bus_handle_t *bus, uint8_t byte);
/** /**
* @brief Read bit * @brief Read bit
* *
* @param bus Bus handle * @param bus Bus handle
* *
* @retval 1: Device returned 1 * @retval 1: Device returned 1
* @retval 0: Device returned 0 * @retval 0: Device returned 0
* @retval -1: Failed to obtain semaphore for gpio handling * @retval -1: Failed to obtain semaphore for gpio handling
@ -102,19 +104,19 @@ uint8_t onewire_read_bit(onewire_bus_handle_t *bus);
/** /**
* @brief Read bit * @brief Read bit
* *
* @param bus Bus handle * @param bus Bus handle
* *
* @return Byte returned by device * @return Byte returned by device
*/ */
uint8_t onewire_read_byte(onewire_bus_handle_t *bus); uint8_t onewire_read_byte(onewire_bus_handle_t *bus);
/** /**
* @brief Send command to device * @brief Send command to device
* *
* @param bus Bus handle * @param bus Bus handle
* @param command Onewire rom command * @param command Onewire rom command
* *
*/ */
void onewire_send_command(onewire_bus_handle_t *bus, onewire_rom_commands_t command); void onewire_send_command(onewire_bus_handle_t *bus, onewire_rom_commands_t command);

View file

@ -3,6 +3,8 @@
#include "zh_network.h" #include "zh_network.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "lwip/sockets.h" #include "lwip/sockets.h"
#include "ds18b20.h"
#include "onewire.h"
#include <strings.h> #include <strings.h>
// #define EXIT_NODE // #define EXIT_NODE
@ -42,6 +44,41 @@ typedef struct
int sock; int sock;
int getUpTime()
{
// Get system uptime in milliseconds
int uptime = (xTaskGetTickCount() * (1000 / configTICK_RATE_HZ));
return uptime;
}
float getTemp()
{
float temp = 0.0;
ds18b20_handler_t sensor;
// Initialize DS18B20 sensor
if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT))
{
ESP_LOGE("DS18B20", "Failed to initialize DS18B20 sensor!");
return -1.0; // Indicate an error with a negative value
}
// Convert temperature
ds18b20_convert_temp(&sensor);
// Read the temperature
temp = ds18b20_read_temp(&sensor);
// Check if the temperature is within a reasonable range for DS18B20
if (temp < -55.0 || temp > 125.0)
{
ESP_LOGE("DS18B20", "Temperature reading out of range: %.2f", temp);
return -1.0; // Indicate invalid reading
}
return temp;
}
void app_main(void) void app_main(void)
{ {
esp_log_level_set("zh_vector", ESP_LOG_NONE); esp_log_level_set("zh_vector", ESP_LOG_NONE);
@ -52,7 +89,6 @@ void app_main(void)
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_config); esp_wifi_init(&wifi_init_config);
esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_mode(WIFI_MODE_STA);
esp_read_mac(_self_mac, WIFI_MODE_STA);
#ifdef EXIT_NODE #ifdef EXIT_NODE
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
@ -64,14 +100,14 @@ void app_main(void)
}; };
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_start();
esp_wifi_set_channel(ESP_CHANNEL, 1); esp_wifi_set_channel(ESP_CHANNEL, 1);
esp_wifi_connect(); esp_wifi_connect();
#endif #endif
esp_wifi_start();
// esp_wifi_set_max_tx_power(8); // Power reduction is for example and testing purposes only. Do not use in your own programs! // esp_wifi_set_max_tx_power(8); // Power reduction is for example and testing purposes only. Do not use in your own programs!
zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT(); zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
network_init_config.max_waiting_time = 1000; network_init_config.max_waiting_time = 1000;
network_init_config.wifi_channel = ESP_CHANNEL; network_init_config.wifi_channel = 7;
zh_network_init(&network_init_config); zh_network_init(&network_init_config);
#ifdef CONFIG_IDF_TARGET_ESP8266 #ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL); esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL);
@ -109,8 +145,9 @@ void app_main(void)
for (;;) for (;;)
{ {
tcp_message_t packet; tcp_message_t packet;
memcpy(packet.mac, _self_mac, 6); esp_read_mac(packet.mac, 0);
packet.battery_voltage = 3.3f packet.temperature = getTemp(); packet.battery_voltage = 3.3f;
packet.temperature = getTemp();
packet.up_time = getUpTime(); packet.up_time = getUpTime();
vTaskDelay(10000 / portTICK_PERIOD_MS); vTaskDelay(10000 / portTICK_PERIOD_MS);
zh_network_send(broadcast, (uint8_t *)&data, sizeof(data)); zh_network_send(broadcast, (uint8_t *)&data, sizeof(data));
@ -171,38 +208,3 @@ void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
} }
// BoskoopBase // BoskoopBase
int getUpTime()
{
// Get system uptime in milliseconds
int uptime = (xTaskGetTickCount() * (1000 / configTICK_RATE_HZ));
return uptime;
}
float getTemp()
{
float temp = 0.0;
ds18b20_handler_t sensor;
// Initialize DS18B20 sensor
if (!ds18b20_init(&sensor, GPIO_NUM_2, TEMP_RES_12_BIT))
{
ESP_LOGE("DS18B20", "Failed to initialize DS18B20 sensor!");
return -1.0; // Indicate an error with a negative value
}
// Convert temperature
ds18b20_convert_temp(&sensor);
// Read the temperature
temp = ds18b20_read_temp(&sensor);
// Check if the temperature is within a reasonable range for DS18B20
if (temp < -55.0 || temp > 125.0)
{
ESP_LOGE("DS18B20", "Temperature reading out of range: %.2f", temp);
return -1.0; // Indicate invalid reading
}
return temp;
}