tcp working

This commit is contained in:
Conner 2024-10-13 23:35:00 +02:00
parent de4d5448de
commit aab15384d3
2 changed files with 68 additions and 71 deletions

View file

@ -16,11 +16,13 @@ struct AppState {
}
#[derive(DekuRead, DekuWrite, Debug)]
#[deku(endian = "little")]
struct Data {
mac: [u8; 6],
#[deku(bytes = 8)]
mac: [u8; 8],
temp: f32,
battery_voltage: f32,
up_time: u32,
up_time: u64,
}
#[actix_web::main]
@ -52,18 +54,17 @@ async fn main() -> std::io::Result<()> {
loop {
if let Ok((mut stream, _)) = listener.accept().await {
let mut buffer = vec![0; 1024];
println!("ESP CONNECTED");
let mut buffer = vec![0; 24];
loop {
if let Ok(size) = stream.read(&mut buffer).await {
buffer.truncate(size);
if let Ok((data, _)) = Data::from_bytes((&buffer, 0)) {
println!("Received: {:?}", data);
println!("{:#x?}", &buffer);
if let Ok((data, value)) = Data::from_bytes((&buffer, 0)) {
println!("Received: {:#?}", value);
// Process the data or save it to the database
} else {
println!("Failed to parse data");
}
} else {
break;
}
}
}

View file

@ -5,24 +5,31 @@
#include "lwip/sockets.h"
#include <strings.h>
static const char *payload = "Nüttchen";
#define GPIO_OUTPUT_IO_0 2
#define GPIO_OUTPUT_IO_1 16
#define GPIO_OUTPUT_PIN_SEL ((1ULL << GPIO_OUTPUT_IO_0) | (1ULL << GPIO_OUTPUT_IO_1))
#define EXIT_NODE a
#ifdef EXIT_NODE
#define TAG "TCP"
#define PORT 7999
#define HOST_IP_ADDR
#define ESP_WIFI_SSID
#define ESP_WIFI_PASS
#endif
#define EXAMPLE_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS
#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);
// bc:dd:c2:82:82:9e
uint8_t target[6] = {0xBC, 0xDD, 0xC2, 0x82, 0x82, 0x9E};
uint8_t broadcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
uint8_t test[6] = {0x09, 0xF2, 0x69, 0x42, 0x11, 0xA9};
typedef struct
{
float temperature;
float battery_voltage;
unsigned long up_time;
} sensor_message_t;
typedef struct
{
@ -30,24 +37,12 @@ typedef struct
float temperature;
float battery_voltage;
unsigned long up_time;
} sensor_message_t;
} tcp_message_t;
int sock;
void app_main(void)
{
gpio_config_t io_conf;
// disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
// set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
// bit mask of the pins that you want to set,e.g.GPIO15/16
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
// disable pull-down mode
io_conf.pull_down_en = 0;
// disable pull-up mode
io_conf.pull_up_en = 0;
// configure GPIO with the given settings
gpio_config(&io_conf);
esp_log_level_set("zh_vector", ESP_LOG_NONE);
esp_log_level_set("zh_network", ESP_LOG_NONE);
nvs_flash_init();
@ -55,25 +50,26 @@ void app_main(void)
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 = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
.channel = 7},
.ssid = ESP_WIFI_SSID,
.password = ESP_WIFI_PASS,
.channel = ESP_CHANNEL},
};
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
esp_wifi_set_channel(7, 1);
esp_wifi_set_channel(ESP_CHANNEL, 1);
esp_wifi_connect();
#endif
// 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;
network_init_config.wifi_channel = ESP_CHANNEL;
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);
@ -81,10 +77,8 @@ void app_main(void)
esp_event_handler_instance_register(ZH_NETWORK, ESP_EVENT_ANY_ID, &zh_network_event_handler, NULL, NULL);
#endif
char rx_buffer[128];
char addr_str[128];
int addr_family;
int ip_protocol;
#ifdef EXIT_NODE
uint8_t data = 0xFF;
for (;;)
{
@ -94,7 +88,7 @@ void app_main(void)
destAddr.sin_port = htons(7999);
inet_pton(AF_INET, HOST_IP_ADDR, &destAddr.sin_addr);
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0)
{
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
@ -106,13 +100,20 @@ void app_main(void)
{
ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);
close(sock);
vTaskDelay(500 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGI(TAG, "Successfully connected");
for (;;)
{
vTaskDelay(5000 / portTICK_PERIOD_MS);
int err = send(sock, payload, strlen(payload), 0);
tcp_message_t packet;
memcpy(packet.mac, test, 6);
packet.battery_voltage = 3.3f;
packet.temperature = 21.2f;
packet.up_time = 213273948;
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);
@ -121,20 +122,7 @@ void app_main(void)
}
close(sock);
}
example_message_t send_message = {0};
strcpy(send_message.char_value, "Test Message");
send_message.float_value = 1.234;
send_message.bool_value = false;
for (;;)
{
printf("Sending Message");
zh_network_send(target, (uint8_t *)&send_message, sizeof(send_message));
vTaskDelay(5000 / portTICK_PERIOD_MS);
gpio_set_level(GPIO_OUTPUT_IO_0, 0);
vTaskDelay(100 / portTICK_PERIOD_MS);
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
}
#endif
}
void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
@ -143,17 +131,25 @@ void zh_network_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
{
case ZH_NETWORK_ON_RECV_EVENT:;
zh_network_event_on_recv_t *recv_data = event_data;
printf("Message from MAC %02X:%02X:%02X:%02X:%02X:%02X is received. Data lenght %d bytes.\n", MAC2STR(recv_data->mac_addr), recv_data->data_len);
example_message_t *recv_message = (example_message_t *)recv_data->data;
printf("Char %s\n", recv_message->char_value);
printf("Int %d\n", recv_message->int_value);
printf("Float %f\n", recv_message->float_value);
printf("Bool %d\n", recv_message->bool_value);
heap_caps_free(recv_data->data); // Do not delete to avoid memory leaks!
gpio_set_level(GPIO_OUTPUT_IO_0, 0);
vTaskDelay(100 / portTICK_PERIOD_MS);
gpio_set_level(GPIO_OUTPUT_IO_0, 1);
break;
#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);
// READ DATA
zh_network_send(recv_data->mac_addr, )
#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)