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,14 +3,16 @@
#include "onewire.h"
typedef enum {
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,
@ -19,7 +21,8 @@ typedef enum {
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

@ -22,14 +22,16 @@
static const char *TAG_ONEWIRE = "ONEWIRE";
typedef enum {
typedef enum
{
_ROM_READ = 0x33,
_ROM_SEARCH = 0xF0,
_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;
}