From 38b847bf248a27cac5216adb711db906a2f09b06 Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 18 May 2026 18:49:49 +0200 Subject: [PATCH] Reception d'un paquet UDP: OK --- .vscode/tasks.json | 4 +- CMakeLists.txt | 3 ++ Wifi.c | 96 ++++++++++++++++++++++++++++++++++++++ Wifi.h | 2 + lwipopts.h | 10 ++++ lwipopts_examples_common.h | 90 +++++++++++++++++++++++++++++++++++ main.c | 19 ++++---- wifi_settings.h | 8 ++++ wifi_settings.h.default | 9 ++++ 9 files changed, 231 insertions(+), 10 deletions(-) create mode 100644 Wifi.c create mode 100644 Wifi.h create mode 100644 lwipopts.h create mode 100644 lwipopts_examples_common.h create mode 100644 wifi_settings.h create mode 100644 wifi_settings.h.default diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c98bf9b..3d804d0 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,7 +2,7 @@ "tasks": [ { "type": "shell", - "command": "cd build; cmake ../; make", + "command": "mkdir -p build; cd build; cmake ../ -DPICO_BOARD=pico_w; make", "label": "CMake in build/", "problemMatcher": [], "group": { @@ -12,7 +12,7 @@ }, { "type": "shell", - "command": "cd build; cmake ../; make Flash", + "command": "cd build; cmake ../ -DPICO_BOARD=pico_w; make Flash", "label": "CMake & Make & Flash", "problemMatcher": [], "group": { diff --git a/CMakeLists.txt b/CMakeLists.txt index 208c5b0..341f85e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,12 +39,14 @@ add_executable(Mon_Projet VL53L8CX_ULD_API/src/vl53l8cx_plugin_motion_indicator.c VL53L8CX_ULD_API/src/vl53l8cx_plugin_xtalk.c VL53L8_2024.c + Wifi.c Platform/platform.c ) pico_generate_pio_header(Mon_Projet ${CMAKE_CURRENT_LIST_DIR}/quadrature_encoder.pio) target_include_directories(Mon_Projet PRIVATE VL53L8CX_ULD_API/inc/) +target_include_directories(Mon_Projet PRIVATE ${CMAKE_CURRENT_LIST_DIR}) target_link_libraries(Mon_Projet hardware_adc @@ -53,6 +55,7 @@ target_link_libraries(Mon_Projet hardware_pio pico_stdlib pico_multicore + pico_cyw43_arch_lwip_threadsafe_background ) pico_enable_stdio_usb(Mon_Projet 1) diff --git a/Wifi.c b/Wifi.c new file mode 100644 index 0000000..afbcdb3 --- /dev/null +++ b/Wifi.c @@ -0,0 +1,96 @@ +#include "pico/cyw43_arch.h" +// Si le fichier n'existe pas, créez-le à partir du modèle "wifi_settings.h.default" +#include "wifi_settings.h" + +#define BEACON_MSG_LEN_MAX 500 +#define UDP_CLIENT_PORT 1234 + +struct udp_pcb* pcb; +struct udp_pcb * pcb_r; +ip_addr_t addr; + +void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ + printf("UDP callback !\n"); + printf("IP: %d.%d.%d.%d, port %d\n", (addr->addr >>24) & 0xFF, (addr->addr >>16) & 0xFF, (addr->addr >>8) & 0xFF, addr->addr & 0xFF, port); + printf("Taille:%d\n", p->len); + printf("Contenu: "); + for(int i=0; i < p->len; i++){ + printf("%c", ((char*)p->payload)[i]); + } + printf("\n"); +} + +int UDP_client_init(void){ + pcb_r = udp_new(); + if(pcb_r == NULL) + { + puts("Error creating UDP client"); + return -1; + } + udp_recv(pcb_r, udp_receive_callback, NULL); + err_t err = udp_bind(pcb_r, IP_ADDR_ANY, UDP_CLIENT_PORT); + if(err) + { + printf("Error binding UDP client: %d\n", err); + return -1; + } + + printf("UDP client started on port %d\n", UDP_CLIENT_PORT); + +} + +int Wifi_init(void){ + if (cyw43_arch_init()) { + printf("failed to initialise\n"); + return 1; + } + + cyw43_arch_enable_sta_mode(); + + printf("Connecting to Wi-Fi...\n"); + if (cyw43_arch_wifi_connect_timeout_ms(MY_WIFI_SSID, MY_WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) { + printf("failed to connect.\n"); + return 1; + } else { + printf("Connected.\n"); + } + + pcb = udp_new(); + ipaddr_aton(BEACON_TARGET, &addr); + UDP_client_init(); + return 0; +} + +void Wifi_udp_send_string(char * message){ + static int counter = 0; + + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, BEACON_MSG_LEN_MAX+1, PBUF_RAM); + char *req = (char *)p->payload; + memset(req, 0, BEACON_MSG_LEN_MAX+1); + + snprintf(req, BEACON_MSG_LEN_MAX, "%s", message); + + err_t er = udp_sendto(pcb, p, &addr, UDP_PORT); + + pbuf_free(p); + if (er != ERR_OK) { + printf("Failed to send UDP packet! error=%d", er); + } else { + //printf("Sent packet %d\n", counter); + counter++; + } + + // Note in practice for this simple UDP transmitter, + // the end result for both background and poll is the same + +#if PICO_CYW43_ARCH_POLL + // if you are using pico_cyw43_arch_poll, then you must poll periodically from your + // main loop (not from a timer) to check for Wi-Fi driver or lwIP work that needs to be done. + cyw43_arch_poll(); +#else + // if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work + // is done via interrupt in the background. This sleep is just an example of some (blocking) + // work you might be doing. + // sleep_ms(BEACON_INTERVAL_MS); +#endif +} \ No newline at end of file diff --git a/Wifi.h b/Wifi.h new file mode 100644 index 0000000..bd6328d --- /dev/null +++ b/Wifi.h @@ -0,0 +1,2 @@ +int Wifi_init(void); +void Wifi_udp_send_string(char * message); \ No newline at end of file diff --git a/lwipopts.h b/lwipopts.h new file mode 100644 index 0000000..8571ed5 --- /dev/null +++ b/lwipopts.h @@ -0,0 +1,10 @@ +#ifndef _LWIPOPTS_H +#define _LWIPOPTS_H + +// Generally you would define your own explicit list of lwIP options +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html) +// +// This example uses a common include to avoid repetition +#include "lwipopts_examples_common.h" + +#endif diff --git a/lwipopts_examples_common.h b/lwipopts_examples_common.h new file mode 100644 index 0000000..217cb13 --- /dev/null +++ b/lwipopts_examples_common.h @@ -0,0 +1,90 @@ +#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H +#define _LWIPOPTS_EXAMPLE_COMMONH_H + + +// Common settings used in most of the pico_w examples +// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) + +// allow override in some examples +#ifndef NO_SYS +#define NO_SYS 1 +#endif +// allow override in some examples +#ifndef LWIP_SOCKET +#define LWIP_SOCKET 0 +#endif +#if PICO_CYW43_ARCH_POLL +#define MEM_LIBC_MALLOC 1 +#else +// MEM_LIBC_MALLOC is incompatible with non polling versions +#define MEM_LIBC_MALLOC 0 +#endif +#define MEM_ALIGNMENT 4 +#define MEM_SIZE 4000 +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 24 +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (8 * TCP_MSS) +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) +#define LWIP_NETIF_STATUS_CALLBACK 1 +#define LWIP_NETIF_LINK_CALLBACK 1 +#define LWIP_NETIF_HOSTNAME 1 +#define LWIP_NETCONN 0 +#define MEM_STATS 0 +#define SYS_STATS 0 +#define MEMP_STATS 0 +#define LINK_STATS 0 +// #define ETH_PAD_SIZE 2 +#define LWIP_CHKSUM_ALGORITHM 3 +#define LWIP_DHCP 1 +#define LWIP_IPV4 1 +#define LWIP_TCP 1 +#define LWIP_UDP 1 +#define LWIP_DNS 1 +#define LWIP_TCP_KEEPALIVE 1 +#define LWIP_NETIF_TX_SINGLE_PBUF 1 +#define DHCP_DOES_ARP_CHECK 0 +#define LWIP_DHCP_DOES_ACD_CHECK 0 + +#ifndef NDEBUG +#define LWIP_DEBUG 1 +#define LWIP_STATS 1 +#define LWIP_STATS_DISPLAY 1 +#endif + +#define ETHARP_DEBUG LWIP_DBG_OFF +#define NETIF_DEBUG LWIP_DBG_OFF +#define PBUF_DEBUG LWIP_DBG_OFF +#define API_LIB_DEBUG LWIP_DBG_OFF +#define API_MSG_DEBUG LWIP_DBG_OFF +#define SOCKETS_DEBUG LWIP_DBG_OFF +#define ICMP_DEBUG LWIP_DBG_OFF +#define INET_DEBUG LWIP_DBG_OFF +#define IP_DEBUG LWIP_DBG_OFF +#define IP_REASS_DEBUG LWIP_DBG_OFF +#define RAW_DEBUG LWIP_DBG_OFF +#define MEM_DEBUG LWIP_DBG_OFF +#define MEMP_DEBUG LWIP_DBG_OFF +#define SYS_DEBUG LWIP_DBG_OFF +#define TCP_DEBUG LWIP_DBG_OFF +#define TCP_INPUT_DEBUG LWIP_DBG_OFF +#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF +#define TCP_RTO_DEBUG LWIP_DBG_OFF +#define TCP_CWND_DEBUG LWIP_DBG_OFF +#define TCP_WND_DEBUG LWIP_DBG_OFF +#define TCP_FR_DEBUG LWIP_DBG_OFF +#define TCP_QLEN_DEBUG LWIP_DBG_OFF +#define TCP_RST_DEBUG LWIP_DBG_OFF +#define UDP_DEBUG LWIP_DBG_OFF +#define TCPIP_DEBUG LWIP_DBG_OFF +#define PPP_DEBUG LWIP_DBG_OFF +#define SLIP_DEBUG LWIP_DBG_OFF +#define DHCP_DEBUG LWIP_DBG_OFF + +#endif /* __LWIPOPTS_H__ */ diff --git a/main.c b/main.c index 2d9b47e..9b31d40 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ #include "Trajet.h" #include "VL53L8_2024.h" #include "vl53l8cx_api.h" +#include "Wifi.h" #include #include #include "QEI.h" @@ -75,6 +76,13 @@ void main(void) #endif Localisation_init(get_identifiant()); Trajet_init(get_identifiant()); + + sleep_ms(3000); + Wifi_init(); + sleep_ms(50); + Wifi_udp_send_string("Demarrage:1\n"); + Wifi_udp_send_string("test:1\n"); + //i2c_maitre_init(); //Servomoteur_Init(); @@ -95,9 +103,10 @@ void main(void) gpio_set_dir(LED1PIN, GPIO_OUT ); gpio_put(LED1PIN, 1); + /* gpio_init(PICO_DEFAULT_LED_PIN); gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT ); - gpio_put(PICO_DEFAULT_LED_PIN, 1); + gpio_put(PICO_DEFAULT_LED_PIN, 1);*/ @@ -235,13 +244,7 @@ void main(void) id_carte = message.donnees[1]; registre = message.donnees[2]; if(id_carte == 'D'){ - if(message.donnees[4] > 0 && message.donnees[4] < 30){ - //printf("LED ON\n"); - gpio_put(PICO_DEFAULT_LED_PIN, 1); - }else{ - //printf("LED OFF\n"); - gpio_put(PICO_DEFAULT_LED_PIN, 0); - } + } break; case 'd': diff --git a/wifi_settings.h b/wifi_settings.h new file mode 100644 index 0000000..42d68d3 --- /dev/null +++ b/wifi_settings.h @@ -0,0 +1,8 @@ +#define MY_WIFI_SSID "Poivron" +#define MY_WIFI_PASSWORD "vfS7Kru219vYU3P61D" + +#define UDP_PORT 47269 +#define BEACON_TARGET "192.168.1.58" + +/*#define MY_WIFI_SSID "Livebox-A040" +#define MY_WIFI_PASSWORD "QkqoXCPt6UgvaUAWmq"*/ \ No newline at end of file diff --git a/wifi_settings.h.default b/wifi_settings.h.default new file mode 100644 index 0000000..14ea373 --- /dev/null +++ b/wifi_settings.h.default @@ -0,0 +1,9 @@ +// Copier ce fichier en "wifi_settings.h" et renseignez vos identifiants WiFi. +// Le port est le port par défaut de Teleplot +// BEACON_TARGET est l'IP ou l'adresse du serveur Teleplot + +#define MY_WIFI_SSID "My_SSID" +#define MY_WIFI_PASSWORD "My_WiFi_Password" + +#define UDP_PORT 47269 +#define BEACON_TARGET "192.168.1.2" \ No newline at end of file