171 lines
5.5 KiB
C
171 lines
5.5 KiB
C
#include "nvs_flash.h"
|
|
#include "esp_netif.h"
|
|
#include "zh_network.h"
|
|
#include "driver/gpio.h"
|
|
#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 TAG "TCP"
|
|
#define PORT 7999
|
|
#define HOST_IP_ADDR
|
|
|
|
#define EXAMPLE_ESP_WIFI_SSID
|
|
#define EXAMPLE_ESP_WIFI_PASS
|
|
|
|
#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};
|
|
|
|
typedef struct
|
|
{
|
|
char char_value[30];
|
|
int int_value;
|
|
float float_value;
|
|
bool bool_value;
|
|
} example_message_t;
|
|
|
|
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();
|
|
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);
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta = {
|
|
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
|
.password = EXAMPLE_ESP_WIFI_PASS,
|
|
.channel = 7},
|
|
|
|
};
|
|
|
|
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_connect();
|
|
// 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
|
|
|
|
char rx_buffer[128];
|
|
char addr_str[128];
|
|
int addr_family;
|
|
int ip_protocol;
|
|
|
|
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);
|
|
|
|
int 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);
|
|
continue;
|
|
}
|
|
ESP_LOGI(TAG, "Successfully connected");
|
|
for (;;)
|
|
{
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
int err = send(sock, payload, strlen(payload), 0);
|
|
if (err < 0)
|
|
{
|
|
ESP_LOGE(TAG, "Error ocured during sending: errno %d", errno);
|
|
break;
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
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;
|
|
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;
|
|
}
|
|
} |