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)) {
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 {
id: ActiveValue::Set(mac),
@ -72,7 +75,9 @@ async fn main() -> std::io::Result<()> {
let result = sensor_data.insert(&db).await;
match result {
Err(_) => println!("Failed to insert data"),
Err(_) => println!(
"Failed to insert data (You probably didnt add the node)"
),
_ => (),
}
} else {

View file

@ -3,7 +3,7 @@
#include "freertos/task.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_resolution_val[] = {0x1F, 0x3F, 0x5F, 0x7F};

View file

@ -3,23 +3,26 @@
#include "onewire.h"
typedef enum {
TEMP_RES_9_BIT = 0,
typedef enum
{
TEMP_RES_9_BIT = 0,
TEMP_RES_10_BIT = 1,
TEMP_RES_11_BIT = 2,
TEMP_RES_12_BIT = 3
} ds18b20_temp_res_t;
typedef enum {
typedef enum
{
_SCRATCH_WRITE = 0x4E,
_SCRATCH_READ = 0xBE,
_SCRATCH_COPY = 0x48,
_CONVERT_T = 0x44
_SCRATCH_READ = 0xBE,
_SCRATCH_COPY = 0x48,
_CONVERT_T = 0x44
} ds18b20_commands_t;
typedef uint8_t ds18b20_scratchpad_t[9];
typedef struct {
typedef struct
{
onewire_bus_handle_t bus;
ds18b20_temp_res_t res;
ds18b20_scratchpad_t scratchpad;

View file

@ -19,7 +19,7 @@ uint8_t onewire_configure_gpio(gpio_num_t pin, gpio_config_t *custom_config)
{
config.intr_type = GPIO_INTR_DISABLE;
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_up_en = 0;
}
@ -49,7 +49,7 @@ uint8_t onewire_init(onewire_bus_handle_t *bus, gpio_num_t bus_pin, gpio_config_
bus->mutex = xSemaphoreCreateMutex();
// configure GPIO
if(!onewire_configure_gpio(bus_pin, custom_config))
if (!onewire_configure_gpio(bus_pin, custom_config))
{
return 0;
}

View file

@ -7,29 +7,31 @@
#include "esp_types.h"
#include "esp_err.h"
#define _ONEWIRE_WRITE1_LOW 6
#define _ONEWIRE_WRITE1_WAIT 64
#define _ONEWIRE_WRITE0_LOW 60
#define _ONEWIRE_WRITE0_WAIT 10
#define _ONEWIRE_READ_WAIT 9
#define _ONEWIRE_READ_RECOVERY 55
#define _ONEWIRE_RESET_WAIT 480
#define _ONEWIRE_PRESENCE_WAIT 70
#define _ONEWIRE_RESET_RECOVERY 410
#define _ONEWIRE_WRITE1_LOW 6
#define _ONEWIRE_WRITE1_WAIT 64
#define _ONEWIRE_WRITE0_LOW 60
#define _ONEWIRE_WRITE0_WAIT 10
#define _ONEWIRE_READ_WAIT 9
#define _ONEWIRE_READ_RECOVERY 55
#define _ONEWIRE_RESET_WAIT 480
#define _ONEWIRE_PRESENCE_WAIT 70
#define _ONEWIRE_RESET_RECOVERY 410
#define _BLOCK_TIME pdMS_TO_TICKS(1000)
#define _SEMFAIL_MSG "Failed to obtain semaphore. (%s)"
#define _BLOCK_TIME pdMS_TO_TICKS(1000)
#define _SEMFAIL_MSG "Failed to obtain semaphore. (%s)"
static const char *TAG_ONEWIRE = "ONEWIRE";
typedef enum {
_ROM_READ = 0x33,
typedef enum
{
_ROM_READ = 0x33,
_ROM_SEARCH = 0xF0,
_ROM_MATCH = 0x55,
_ROM_SKIP = 0xCC
_ROM_MATCH = 0x55,
_ROM_SKIP = 0xCC
} onewire_rom_commands_t;
typedef struct {
typedef struct
{
gpio_num_t pin;
SemaphoreHandle_t mutex;
} onewire_bus_handle_t;

View file

@ -3,6 +3,8 @@
#include "zh_network.h"
#include "driver/gpio.h"
#include "lwip/sockets.h"
#include "ds18b20.h"
#include "onewire.h"
#include <strings.h>
// #define EXIT_NODE
@ -42,6 +44,41 @@ typedef struct
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)
{
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();
esp_wifi_init(&wifi_init_config);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_read_mac(_self_mac, WIFI_MODE_STA);
#ifdef EXIT_NODE
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_start();
esp_wifi_set_channel(ESP_CHANNEL, 1);
esp_wifi_connect();
#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!
zh_network_init_config_t network_init_config = ZH_NETWORK_INIT_CONFIG_DEFAULT();
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);
#ifdef CONFIG_IDF_TARGET_ESP8266
esp_event_handler_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL);
@ -109,8 +145,9 @@ void app_main(void)
for (;;)
{
tcp_message_t packet;
memcpy(packet.mac, _self_mac, 6);
packet.battery_voltage = 3.3f packet.temperature = getTemp();
esp_read_mac(packet.mac, 0);
packet.battery_voltage = 3.3f;
packet.temperature = getTemp();
packet.up_time = getUpTime();
vTaskDelay(10000 / portTICK_PERIOD_MS);
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
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;
}