ApfelNetzwerk/embedded/main/apple.c

211 lines
6 KiB
C
Raw Normal View History

2024-10-11 11:23:11 +02:00
#include "nvs_flash.h"
#include "esp_netif.h"
#include "zh_network.h"
#include "driver/gpio.h"
#include "lwip/sockets.h"
2024-10-14 01:19:50 +02:00
#include "ds18b20.h"
#include "onewire.h"
2024-10-11 11:23:11 +02:00
#include <strings.h>
2024-10-14 00:31:33 +02:00
// #define EXIT_NODE
2024-10-11 11:23:11 +02:00
2024-10-13 23:35:00 +02:00
#ifdef EXIT_NODE
2024-10-11 11:23:11 +02:00
#define TAG "TCP"
#define PORT 7999
#define HOST_IP_ADDR
2024-10-13 23:35:00 +02:00
#define ESP_WIFI_SSID
#define ESP_WIFI_PASS
#endif
2024-10-11 11:23:11 +02:00
2024-10-13 23:35:00 +02:00
#define ESP_CHANNEL 7
2024-10-11 11:23:11 +02:00
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
2024-10-13 23:35:00 +02:00
uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t test[6] = {0x09, 0xF2, 0x69, 0x42, 0x11, 0xA9};
2024-10-14 00:31:33 +02:00
uint8_t _self_mac[6] = {0};
2024-10-11 11:23:11 +02:00
typedef struct
{
2024-10-13 21:05:19 +02:00
float temperature;
float battery_voltage;
unsigned long up_time;
} sensor_message_t;
2024-10-11 11:23:11 +02:00
2024-10-13 23:35:00 +02:00
typedef struct
2024-10-11 11:23:11 +02:00
{
2024-10-13 23:35:00 +02:00
uint8_t mac[6];
float temperature;
float battery_voltage;
unsigned long up_time;
} tcp_message_t;
int sock;
2024-10-11 11:23:11 +02:00
2024-10-14 01:19:50 +02:00
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;
}
2024-10-13 23:35:00 +02:00
void app_main(void)
{
2024-10-11 11:23:11 +02:00
esp_log_level_set("zh_vector", ESP_LOG_NONE);
esp_log_level_set("zh_network", ESP_LOG_NONE);
nvs_flash_init();
esp_netif_init();
esp_event_loop_create_default();
wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_init_config);
2024-10-13 23:35:00 +02:00
esp_wifi_set_mode(WIFI_MODE_STA);
2024-10-11 11:23:11 +02:00
2024-10-13 23:35:00 +02:00
#ifdef EXIT_NODE
2024-10-11 11:23:11 +02:00
wifi_config_t wifi_config = {
.sta = {
2024-10-13 23:35:00 +02:00
.ssid = ESP_WIFI_SSID,
.password = ESP_WIFI_PASS,
.channel = ESP_CHANNEL},
2024-10-11 11:23:11 +02:00
};
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
2024-10-13 23:35:00 +02:00
esp_wifi_set_channel(ESP_CHANNEL, 1);
2024-10-11 11:23:11 +02:00
esp_wifi_connect();
2024-10-13 23:35:00 +02:00
#endif
2024-10-14 01:19:50 +02:00
esp_wifi_start();
2024-10-11 11:23:11 +02:00
// 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;
2024-10-14 01:19:50 +02:00
network_init_config.wifi_channel = 7;
2024-10-11 11:23:11 +02:00
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);
#else
esp_event_handler_instance_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL, NULL);
#endif
2024-10-13 23:35:00 +02:00
#ifdef EXIT_NODE
uint8_t data = 0xFF;
2024-10-11 11:23:11 +02:00
for (;;)
{
struct sockaddr_in destAddr;
bzero(&destAddr, sizeof(destAddr));
destAddr.sin_family = AF_INET;
destAddr.sin_port = htons(7999);
inet_pton(AF_INET, HOST_IP_ADDR, &destAddr.sin_addr);
2024-10-13 23:35:00 +02:00
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2024-10-11 11:23:11 +02:00
if (sock < 0)
{
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
return;
}
ESP_LOGI(TAG, "Socket created");
int err = connect(sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
if (err != 0)
{
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
close(sock);
2024-10-13 23:35:00 +02:00
vTaskDelay(500 / portTICK_PERIOD_MS);
2024-10-11 11:23:11 +02:00
continue;
}
ESP_LOGI(TAG, "Successfully connected");
for (;;)
{
2024-10-13 23:35:00 +02:00
tcp_message_t packet;
2024-10-14 01:19:50 +02:00
esp_read_mac(packet.mac, 0);
packet.battery_voltage = 3.3f;
packet.temperature = getTemp();
2024-10-14 00:31:33 +02:00
packet.up_time = getUpTime();
2024-10-13 23:35:00 +02:00
vTaskDelay(10000 / portTICK_PERIOD_MS);
zh_network_send(broadcast, (uint8_t *)&data, sizeof(data));
int err = send(sock, (uint8_t *)&packet, sizeof(tcp_message_t), 0);
2024-10-11 11:23:11 +02:00
if (err < 0)
{
ESP_LOGE(TAG, "Error ocured during sending: errno %d", errno);
break;
}
}
close(sock);
}
2024-10-13 23:35:00 +02:00
#endif
2024-10-11 11:23:11 +02:00
}
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
switch (event_id)
{
case ZH_NETWORK_ON_RECV_EVENT:;
zh_network_event_on_recv_t *recv_data = event_data;
2024-10-13 23:35:00 +02:00
#ifdef EXIT_NODE
sensor_message_t *recv_message = (sensor_message_t *)recv_data->data;
tcp_message_t packet;
memcpy(packet.mac, recv_data->mac_addr, 6);
packet.temperature = recv_message->temperature;
packet.battery_voltage = recv_message->battery_voltage;
packet.up_time = recv_message->up_time;
heap_caps_free(recv_data->data);
int err = send(sock, (uint8_t *)&packet, sizeof(packet), 0);
if (err < 0)
{
ESP_LOGE(TAG, "Error sending TCP data");
break;
}
#else
heap_caps_free(recv_data->data);
2024-10-14 00:31:33 +02:00
sensor_message_t message = {0};
message.temperature = getTemp();
message.battery_voltage = 3.3f;
message.up_time = getUpTime();
zh_network_send(recv_data->mac_addr, (uint8_t *)&message, sizeof(message));
2024-10-13 23:35:00 +02:00
#endif
2024-10-11 11:23:11 +02:00
case ZH_NETWORK_ON_SEND_EVENT:;
zh_network_event_on_send_t *send_data = event_data;
if (send_data->status == ZH_NETWORK_SEND_SUCCESS)
{
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent success.\n", MAC2STR(send_data->mac_addr));
}
else
{
printf("Message to MAC %02X:%02X:%02X:%02X:%02X:%02X sent fail.\n", MAC2STR(send_data->mac_addr));
}
break;
default:
break;
}
2024-10-14 00:31:33 +02:00
}
// BoskoopBase