chore: onewire

This commit is contained in:
2024-11-11 01:31:19 +06:00
parent 39f7755477
commit 8fe50deeed
16 changed files with 1180 additions and 66 deletions

View File

@@ -30,12 +30,6 @@ typedef struct {
const char* name; // Device name (used in MQTT topics)
} rs485_device_t;
// OneWire naming scheme configuration
typedef struct {
const char* location; // Location prefix for the sensor
const char* name_prefix; // Prefix for auto-generated names
} onewire_naming_t;
// Network Configuration
#define MQTT_SERVER_IP {192, 168, 102, 100}
#define MQTT_PORT 1883
@@ -51,26 +45,10 @@ static const node_config_t NODE_CONFIG = {
.id = "ch32-node1", .name = "CH32 Node 1", .location = "somewhere"};
// RS485 Devices Configuration
#define RS485_DEVICE_COUNT 2
#define RS485_DEVICE_COUNT 1
static const rs485_device_t RS485_DEVICES[RS485_DEVICE_COUNT] = {
{.slave_id = 0x01, .type = DEVICE_RELAY, .name = "relay-1"},
{.slave_id = 0x02, .type = DEVICE_SOIL_SENSOR, .name = "soil-monitor-1"}};
// OneWire Naming Configuration
#define MAX_ONEWIRE_DEVICES 8
static const onewire_naming_t ONEWIRE_NAMING[] = {
{.location = "tank", .name_prefix = "water-temp"},
{.location = "ambient", .name_prefix = "air-temp"},
{.location = "soil", .name_prefix = "soil-temp"}};
// Structure to store discovered OneWire devices
typedef struct {
uint8_t address[8]; // OneWire address
uint8_t location_index; // Index into ONEWIRE_NAMING array
char name[32]; // Generated name (e.g., "tank-water-temp-1")
uint8_t sequence; // Sequence number within its location
} onewire_device_t;
{.slave_id = 0x01, .type = DEVICE_RELAY, .name = "relay-1"}};
// {.slave_id = 0x02, .type = DEVICE_SOIL_SENSOR, .name = "soil-monitor-1"}};
#endif // CONFIG_H

View File

@@ -11,7 +11,7 @@ typedef void (*modbus_value_cb)(uint8_t device_idx, const char* property,
void modbus_handler_init(modbus_context_t* ctx, modbus_value_cb value_callback);
void modbus_handler_process(void);
uint8_t modbus_handler_send_request(uint8_t device_idx, const char* property,
bool modbus_handler_send_request(uint8_t device_idx, const char* property,
uint8_t is_write, uint16_t value);
#endif

View File

@@ -2,6 +2,7 @@
#define __MODBUS_MASTER_H
#include <stdint.h>
#include <stdbool.h>
// Function codes
#define MODBUS_FC_READ_HOLDING_REGISTERS 0x03
@@ -51,6 +52,6 @@ void modbus_init(modbus_context_t* ctx,
void (*error_callback)(uint8_t));
void modbus_set_timeout(modbus_context_t* ctx, uint32_t timeout_ms);
void modbus_process(modbus_context_t* ctx);
uint8_t modbus_send_request(modbus_context_t* ctx, uint8_t slave_addr,
bool modbus_send_request(modbus_context_t* ctx, uint8_t slave_addr,
uint8_t function, uint16_t address, uint16_t value);
#endif // __MODBUS_MASTER_H

View File

@@ -2,10 +2,13 @@
#define MQTT_HANDLER_H
#include <DHCP/dhcp.h>
#include <stdbool.h>
#include "ch32v003fun.h"
#include "w5500.h"
#define MAX_PAYLOAD_LENGTH 256
// Options structure for client identification
typedef struct {
char* clientid;
@@ -20,14 +23,20 @@ typedef struct {
ch32_mqtt_options_t opts;
uint32_t last_reconnect;
uint32_t last_yield;
uint8_t is_connected;
bool is_connected;
char base_topic[64];
} mqtt_state_t;
extern char nodes_list[MAX_PAYLOAD_LENGTH];
void mqtt_init(mqtt_state_t* state);
void mqtt_process(mqtt_state_t* state);
void message_arrived(MessageData* md);
void publish_value(MQTTClient* client, const char* device_name,
const char* property, uint16_t value);
void publish_retained(MQTTClient* client, const char* topic,
const char* payload);
void publish_message(MQTTClient* client, const char* payload,
const char* topic);
#endif

46
include/onewire_temp.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef ONEWIRE_TEMP_H
#define ONEWIRE_TEMP_H
#include <stdbool.h>
#include <stdint.h>
#include "mqtt_handler.h"
#define ONEWIRE_MAX_SENSORS 16
#define ONEWIRE_TEMP_INVALID -999.0f
typedef enum {
ONEWIRE_STATE_READY,
ONEWIRE_STATE_CONVERTING,
ONEWIRE_STATE_READ
} onewire_state_t;
// Initialize OneWire temperature system
void onewire_temp_init(void);
// Process all sensors (call regularly)
void onewire_temp_process(void);
// Start parallel conversion on all sensors
void onewire_temp_start_parallel(void);
// Set parallel conversion mode
void onewire_temp_set_parallel(bool enable);
// Get temperature for sensor index
float onewire_temp_get(uint8_t index);
// Get total number of discovered sensors
uint8_t onewire_temp_count(void);
// Get sensor address
const uint8_t* onewire_temp_address(uint8_t index);
// Check if sensor is valid
bool onewire_temp_valid(uint8_t index);
// MQTT
void onewire_temp_publish_discovery(MQTTClient* client, const char* node_id);
void onewire_temp_publish_values(MQTTClient* client, const char* node_id);
#endif // ONEWIRE_TEMP_H

View File

@@ -1,9 +1,11 @@
#ifndef SYSTEM_INIT_H
#define SYSTEM_INIT_H
#include <stdbool.h>
#define W5500_INIT_DELAY_MS 55
void init_system(void);
int wait_for_dhcp(void);
bool wait_for_dhcp(void);
#endif