#include "nvs_flash.h" #include "esp_netif.h" #include "zh_network.h" #include "driver/gpio.h" #include "lwip/sockets.h" #include "ds18b20.h" #include "onewire.h" #include // #define EXIT_NODE #ifdef EXIT_NODE #define TAG "TCP" #define PORT 7999 #define HOST_IP_ADDR #define ESP_WIFI_SSID #define ESP_WIFI_PASS #endif #define ESP_CHANNEL 7 #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); uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; uint8_t test[6] = {0x09, 0xF2, 0x69, 0x42, 0x11, 0xA9}; uint8_t _self_mac[6] = {0}; typedef struct { float temperature; float battery_voltage; unsigned long up_time; } sensor_message_t; typedef struct { uint8_t mac[6]; float temperature; float battery_voltage; unsigned long up_time; } tcp_message_t; 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); 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); esp_wifi_set_mode(WIFI_MODE_STA); #ifdef EXIT_NODE wifi_config_t wifi_config = { .sta = { .ssid = ESP_WIFI_SSID, .password = ESP_WIFI_PASS, .channel = ESP_CHANNEL}, }; esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); 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 = 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); #else esp_event_handler_instance_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL, NULL); #endif #ifdef EXIT_NODE uint8_t data = 0xFF; 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); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 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); vTaskDelay(500 / portTICK_PERIOD_MS); continue; } ESP_LOGI(TAG, "Successfully connected"); for (;;) { tcp_message_t packet; 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)); int err = send(sock, (uint8_t *)&packet, sizeof(tcp_message_t), 0); if (err < 0) { ESP_LOGE(TAG, "Error ocured during sending: errno %d", errno); break; } } close(sock); } #endif } 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; #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); 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)); #endif 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; } } // BoskoopBase