mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-19 17:35:54 +08:00
添加智能灯固件代码
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
# (Not part of the boilerplate)
|
||||
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(udp_server)
|
||||
@@ -0,0 +1,11 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := udp_server
|
||||
|
||||
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
|
||||
# UDP Server example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
The application creates UDP socket with the specified port number and waits for the data to be received. Received data are printed as ASCII text and retransmitted back to the client.
|
||||
|
||||
## How to use example
|
||||
|
||||
In order to create UDP client that communicates with UDP server example, choose one of the following options.
|
||||
|
||||
There are many host-side tools which can be used to interact with the UDP/TCP server/client.
|
||||
One command line tool is [netcat](http://netcat.sourceforge.net) which can send and receive many kinds of packets.
|
||||
Note: please replace `192.168.0.167 3333` with desired IPV4/IPV6 address (displayed in monitor console) and port number in the following commands.
|
||||
|
||||
In addition to those tools, simple Python scripts can be found under sockets/scripts directory. Every script is designed to interact with one of the examples.
|
||||
|
||||
### Send UDP packet via netcat
|
||||
```
|
||||
echo "Hello from PC" | nc -w1 -u 192.168.0.167 3333
|
||||
```
|
||||
|
||||
### Receive UDP packet via netcat
|
||||
```
|
||||
echo "Hello from PC" | nc -w1 -u 192.168.0.167 3333
|
||||
```
|
||||
|
||||
### UDP client using netcat
|
||||
```
|
||||
nc -u 192.168.0.167 3333
|
||||
```
|
||||
|
||||
### Python scripts
|
||||
Script example_test.py could be used as a counter part to the udp-server application,
|
||||
IP address and the message to be send to the server shall be stated as arguments. Example:
|
||||
|
||||
```
|
||||
python example_test.py 192.168.0.167 Message
|
||||
```
|
||||
Note that this script is used in automated tests, as well, so the IDF test framework packages need to be imported;
|
||||
please add `$IDF_PATH/tools/ci/python_packages` to `PYTHONPATH`.
|
||||
|
||||
## Hardware Required
|
||||
|
||||
This example can be run on any commonly available ESP32 development board.
|
||||
|
||||
## Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
Set following parameters under Example Configuration Options:
|
||||
|
||||
* Set `IP version` of the example to be IPV4 or IPV6.
|
||||
|
||||
* Set `Port` number that represents remote port the example will create.
|
||||
|
||||
Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details.
|
||||
|
||||
## Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
Start server first, to receive data sent from the client (application).
|
||||
@@ -0,0 +1,87 @@
|
||||
# This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
# Unless required by applicable law or agreed to in writing, this
|
||||
# software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
# CONDITIONS OF ANY KIND, either express or implied.
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import socket
|
||||
import ttfw_idf
|
||||
|
||||
|
||||
# ----------- Config ----------
|
||||
PORT = 3333
|
||||
INTERFACE = 'eth0'
|
||||
# -------------------------------
|
||||
|
||||
|
||||
def udp_client(address, payload):
|
||||
for res in socket.getaddrinfo(address, PORT, socket.AF_UNSPEC,
|
||||
socket.SOCK_DGRAM, 0, socket.AI_PASSIVE):
|
||||
family_addr, socktype, proto, canonname, addr = res
|
||||
try:
|
||||
sock = socket.socket(family_addr, socket.SOCK_DGRAM)
|
||||
except socket.error as msg:
|
||||
print('Could not create socket: ' + str(msg[0]) + ': ' + msg[1])
|
||||
raise
|
||||
try:
|
||||
sock.sendto(payload, addr)
|
||||
reply, addr = sock.recvfrom(128)
|
||||
if not reply:
|
||||
return
|
||||
print('Reply[' + addr[0] + ':' + str(addr[1]) + '] - ' + str(reply))
|
||||
except socket.error as msg:
|
||||
print('Error Code : ' + str(msg[0]) + ' Message: ' + msg[1])
|
||||
sock.close()
|
||||
raise
|
||||
return reply
|
||||
|
||||
|
||||
@ttfw_idf.idf_example_test(env_tag="Example_WIFI")
|
||||
def test_examples_protocol_socket(env, extra_data):
|
||||
MESSAGE = "Data to ESP"
|
||||
"""
|
||||
steps:
|
||||
1. join AP
|
||||
2. have the board connect to the server
|
||||
3. send and receive data
|
||||
"""
|
||||
dut1 = env.get_dut("udp_server", "examples/protocols/sockets/udp_server", dut_class=ttfw_idf.ESP32DUT)
|
||||
# check and log bin size
|
||||
binary_file = os.path.join(dut1.app.binary_path, "udp_server.bin")
|
||||
bin_size = os.path.getsize(binary_file)
|
||||
ttfw_idf.log_performance("udp_server_bin_size", "{}KB".format(bin_size // 1024))
|
||||
ttfw_idf.check_performance("udp_server_bin_size", bin_size // 1024, dut1.TARGET)
|
||||
|
||||
# start test
|
||||
dut1.start_app()
|
||||
|
||||
ipv4 = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30)[0]
|
||||
ipv6_r = r':'.join((r'[0-9a-fA-F]{4}',) * 8) # expect all 8 octets from IPv6 (assumes it's printed in the long form)
|
||||
ipv6 = dut1.expect(re.compile(r' IPv6 address: ({})'.format(ipv6_r)), timeout=30)[0]
|
||||
print("Connected with IPv4={} and IPv6={}".format(ipv4, ipv6))
|
||||
|
||||
# test IPv4
|
||||
received = udp_client(ipv4, MESSAGE)
|
||||
if not received == MESSAGE:
|
||||
raise
|
||||
dut1.expect(MESSAGE)
|
||||
# test IPv6
|
||||
received = udp_client("{}%{}".format(ipv6, INTERFACE), MESSAGE)
|
||||
if not received == MESSAGE:
|
||||
raise
|
||||
dut1.expect(MESSAGE)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if sys.argv[2:]: # if two arguments provided:
|
||||
# Usage: example_test.py <server_address> <message_to_send_to_server>
|
||||
udp_client(sys.argv[1], sys.argv[2])
|
||||
else: # otherwise run standard example test as in the CI
|
||||
test_examples_protocol_socket()
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "udp_server.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,19 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
config EXAMPLE_IPV4
|
||||
bool "IPV4"
|
||||
default y
|
||||
|
||||
config EXAMPLE_IPV6
|
||||
bool "IPV6"
|
||||
default n
|
||||
select EXAMPLE_CONNECT_IPV6
|
||||
|
||||
config EXAMPLE_PORT
|
||||
int "Port"
|
||||
range 0 65535
|
||||
default 3333
|
||||
help
|
||||
Local port the example server will listen on.
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,137 @@
|
||||
/* BSD Socket API Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "protocol_examples_common.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#define PORT CONFIG_EXAMPLE_PORT
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
static void udp_server_task(void *pvParameters)
|
||||
{
|
||||
char rx_buffer[128];
|
||||
char addr_str[128];
|
||||
int addr_family = (int)pvParameters;
|
||||
int ip_protocol = 0;
|
||||
struct sockaddr_in6 dest_addr;
|
||||
|
||||
while (1) {
|
||||
|
||||
if (addr_family == AF_INET) {
|
||||
struct sockaddr_in *dest_addr_ip4 = (struct sockaddr_in *)&dest_addr;
|
||||
dest_addr_ip4->sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
dest_addr_ip4->sin_family = AF_INET;
|
||||
dest_addr_ip4->sin_port = htons(PORT);
|
||||
ip_protocol = IPPROTO_IP;
|
||||
} else if (addr_family == AF_INET6) {
|
||||
bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));
|
||||
dest_addr.sin6_family = AF_INET6;
|
||||
dest_addr.sin6_port = htons(PORT);
|
||||
ip_protocol = IPPROTO_IPV6;
|
||||
}
|
||||
|
||||
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
|
||||
if (sock < 0) {
|
||||
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
ESP_LOGI(TAG, "Socket created");
|
||||
|
||||
#if defined(CONFIG_EXAMPLE_IPV4) && defined(CONFIG_EXAMPLE_IPV6)
|
||||
if (addr_family == AF_INET6) {
|
||||
// Note that by default IPV6 binds to both protocols, it is must be disabled
|
||||
// if both protocols used at the same time (used in CI)
|
||||
int opt = 1;
|
||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
|
||||
}
|
||||
#endif
|
||||
|
||||
int err = bind(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
||||
if (err < 0) {
|
||||
ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);
|
||||
}
|
||||
ESP_LOGI(TAG, "Socket bound, port %d", PORT);
|
||||
|
||||
while (1) {
|
||||
|
||||
ESP_LOGI(TAG, "Waiting for data");
|
||||
struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6
|
||||
socklen_t socklen = sizeof(source_addr);
|
||||
int len = recvfrom(sock, rx_buffer, sizeof(rx_buffer) - 1, 0, (struct sockaddr *)&source_addr, &socklen);
|
||||
|
||||
// Error occurred during receiving
|
||||
if (len < 0) {
|
||||
ESP_LOGE(TAG, "recvfrom failed: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
// Data received
|
||||
else {
|
||||
// Get the sender's ip address as string
|
||||
if (source_addr.sin6_family == PF_INET) {
|
||||
inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
|
||||
} else if (source_addr.sin6_family == PF_INET6) {
|
||||
inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);
|
||||
}
|
||||
|
||||
rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string...
|
||||
ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);
|
||||
ESP_LOGI(TAG, "%s", rx_buffer);
|
||||
|
||||
int err = sendto(sock, rx_buffer, len, 0, (struct sockaddr *)&source_addr, sizeof(source_addr));
|
||||
if (err < 0) {
|
||||
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sock != -1) {
|
||||
ESP_LOGE(TAG, "Shutting down socket and restarting...");
|
||||
shutdown(sock, 0);
|
||||
close(sock);
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
||||
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
||||
* examples/protocols/README.md for more information about this function.
|
||||
*/
|
||||
ESP_ERROR_CHECK(example_connect());
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_IPV4
|
||||
xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
|
||||
#endif
|
||||
#ifdef CONFIG_EXAMPLE_IPV6
|
||||
xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET6, 5, NULL);
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
CONFIG_EXAMPLE_IPV4=y
|
||||
CONFIG_EXAMPLE_IPV6=y
|
||||
Reference in New Issue
Block a user