commit 9cbf53d42129086b636aad4466f4c45ddada39a9 Author: Ulysse Cura Date: Fri May 23 17:02:24 2025 +0200 Converted in C diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..87e5342 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,46 @@ +cmake_minimum_required(VERSION 3.13) + +include(pico_sdk_import.cmake) + +project(PicoWCommLib C CXX ASM) +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 17) + +# Définir explicitement la carte comme Pico W +set(PICO_BOARD pico_w) +if(NOT DEFINED PICO_BOARD) + add_definitions(-DPICO_BOARD=${PICO_BOARD}) +endif() + +pico_sdk_init() + +add_executable(PicoWCommLib + src/test_connection.c + src/wifi_operator/wifi_operator.c + src/udp/udp_client.c + src/udp/udp_server.c +) + +target_include_directories(PicoWCommLib PRIVATE + ${CMAKE_CURRENT_LIST_DIR}/src + ${CMAKE_CURRENT_LIST_DIR}/src/SerialOutput + ${CMAKE_CURRENT_LIST_DIR}/src/WifiOperator + ${CMAKE_CURRENT_LIST_DIR}/src/udp) + +target_link_libraries(PicoWCommLib + pico_stdlib + pico_multicore + hardware_uart + pico_cyw43_arch_lwip_poll +) + +# Configuration du stdio +pico_enable_stdio_usb(PicoWCommLib 1) +pico_enable_stdio_uart(PicoWCommLib 1) + +pico_add_extra_outputs(PicoWCommLib) + +add_custom_target(Flash + DEPENDS PicoWCommLib + COMMAND picotool load -f ${PROJECT_BINARY_DIR}/PicoWCommLib.uf2 +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..f17acd1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# PicoWCommLib + +Library for rapsberry pi pico to communicate over WIFI \ No newline at end of file diff --git a/compilAndFlash.sh b/compilAndFlash.sh new file mode 100755 index 0000000..a13ee1d --- /dev/null +++ b/compilAndFlash.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Build the project +if [ "$1" = "-D" ] || [ "$1" = "--delete" ]; then + rm -rf build/* + echo "Build directory cleaned" +fi + +# Create build directory if it doesn't exist +if [ ! -d "build" ]; then + mkdir build +fi + +# Enter build directory +cd build || exit 1 + +# Only run CMake if CMakeCache.txt doesn't exist or if it's older than CMakeLists.txt +if [ ! -f "CMakeCache.txt" ] || [ "../CMakeLists.txt" -nt "CMakeCache.txt" ]; then + cmake -DPICO_BOARD=pico_w .. + if [ $? -ne 0 ]; then + echo "CMake configuration failed" + exit 1 + fi +fi + +# Build the project +if ! make -j; then + echo "Build failed" + exit 1 +fi + +# Flash the device +sudo make Flash diff --git a/pico_sdk_import.cmake b/pico_sdk_import.cmake new file mode 100644 index 0000000..28efe9e --- /dev/null +++ b/pico_sdk_import.cmake @@ -0,0 +1,62 @@ +# This is a copy of /external/pico_sdk_import.cmake + +# This can be dropped into an external project to help locate this SDK +# It should be include()ed prior to project() + +if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) + set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) + message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) + set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) + message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") +endif () + +if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) + set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) + message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") +endif () + +set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") +set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") +set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") + +if (NOT PICO_SDK_PATH) + if (PICO_SDK_FETCH_FROM_GIT) + include(FetchContent) + set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) + if (PICO_SDK_FETCH_FROM_GIT_PATH) + get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") + endif () + FetchContent_Declare( + pico_sdk + GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk + GIT_TAG master + ) + if (NOT pico_sdk) + message("Downloading Raspberry Pi Pico SDK") + FetchContent_Populate(pico_sdk) + set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) + endif () + set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) + else () + message(FATAL_ERROR + "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." + ) + endif () +endif () + +get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") +if (NOT EXISTS ${PICO_SDK_PATH}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") +endif () + +set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) +if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) + message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") +endif () + +set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) + +include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/python_test_codes/test_udp_receiver.py b/python_test_codes/test_udp_receiver.py new file mode 100755 index 0000000..e167f99 --- /dev/null +++ b/python_test_codes/test_udp_receiver.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import socket +import struct + +def start_udp_server(): + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind(('0.0.0.0', 10101)) + print("Serveur UDP démarré sur le port 10101") + + while True: + data, addr = sock.recvfrom(1024) + if len(data) == 2: + packet_num, instruction = struct.unpack('BB', data) + print(f"Reçu: packet_number={packet_num}, instruction={instruction}") + +if __name__ == '__main__': + start_udp_server() \ No newline at end of file diff --git a/python_test_codes/test_udp_send.py b/python_test_codes/test_udp_send.py new file mode 100755 index 0000000..ab9e3fc --- /dev/null +++ b/python_test_codes/test_udp_send.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import socket +import time +import struct + +def send_test_packets(): + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + pico_address = ('192.168.128.1', 4243) # Adresse IP du Pico W + + packet_number = 0 + instruction = 0 + + while True: + # Créer le message binaire + message = struct.pack('BB', packet_number, instruction) + sock.sendto(message, pico_address) + + packet_number = (packet_number + 1) % 256 + instruction = (instruction + 1) % 256 + + time.sleep(3.5) + +if __name__ == '__main__': + send_test_packets() \ No newline at end of file diff --git a/src/lwipopts.h b/src/lwipopts.h new file mode 100644 index 0000000..b5d3861 --- /dev/null +++ b/src/lwipopts.h @@ -0,0 +1,97 @@ +#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 +#ifndef MEM_SIZE +#define MEM_SIZE 32768 // Augmenté pour plus de mémoire disponible +#endif +#define MEMP_NUM_TCP_SEG 32 +#define MEMP_NUM_ARP_QUEUE 10 +#define PBUF_POOL_SIZE 32 // Augmenté pour réduire les allocations +#define LWIP_ARP 1 +#define LWIP_ETHERNET 1 +#define LWIP_ICMP 1 +#define LWIP_RAW 1 +#define TCP_WND (16 * TCP_MSS) // Augmenté pour de meilleures performances +#define TCP_MSS 1460 +#define TCP_SND_BUF (8 * TCP_MSS) // Augmenté pour de meilleures performances +#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_DHCP_SERVER 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 + +#define SYS_LIGHTWEIGHT_PROT 1 // Protection pour le multicore +#define MEMP_NUM_PBUF 32 // Augmenté pour les buffers +#define ICMP_TTL 255 // Augmenté pour la fiabilité + +#endif /* __LWIPOPTS_H__ */ \ No newline at end of file diff --git a/src/test_connection.c b/src/test_connection.c new file mode 100644 index 0000000..276dbe9 --- /dev/null +++ b/src/test_connection.c @@ -0,0 +1,120 @@ +// Necessary includes from the Pico SDK +#include +#include +#include "pico/cyw43_arch.h" + +// Custom libraries includes +#include "wifi_operator/wifi_operator.h" +#include "udp/udp_client.h" +#include "udp/udp_server.h" + +// Multicore includes to limit latency on packet reception on core 0 +#include "pico/multicore.h" +#include "hardware/irq.h" +#include "hardware/sync.h" +#include "pico/mutex.h" + +#define UART_ID uart0 +#define BAUD_RATE 115200 +#define UART_TX_PIN 0 // GP0 +#define UART_RX_PIN 1 // GP1 +#define UDP_SERVER_PORT 4242 +#define UDP_CLIENT_PORT 4243 + +// Ajouter ces defines en haut du fichier +#define TARGET_IP "192.168.128.2" // IP de votre PC +#define TARGET_PORT 10101 // Port sur lequel votre PC écoute + +// Access Point configuration +#define AP_SSID "PicoW_AP" +#define AP_PASSWORD "password123" +#define AP_CHANNEL 13 + +// Déclarer le mutex comme variable globale +auto_init_mutex(wifi_mutex); + +// En haut du fichier, après les includes existants +#define BUFFER_SIZE 1024 + +void custom_message_callback(uint8_t packet_number, uint8_t instruction, const ip_addr_t *addr, u16_t port) +{ + printf("Core0: Received: packet=%d, instruction=%d, from %s:%d\n", packet_number, instruction, ipaddr_ntoa(addr), port); + + //making the led blink twice in 0.1s + for (int i = 0; i < 2; i++) + { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); + sleep_ms(25); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false); + sleep_ms(25); + } +} + +void core1_entry() +{ + int diodeState = 0; + udp_server_init(UDP_SERVER_PORT); // Uniquement le serveur sur core 1 + + uint8_t buffer[2]; + uint8_t *packet_number = &buffer[0]; + uint8_t *instruction = &buffer[1]; + + printf("Core1: Starting UDP server on port %d\n", UDP_SERVER_PORT); + + while(true) + { + mutex_enter_blocking(&wifi_mutex); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, diodeState); // Update LED + udp_server_send(buffer, sizeof(buffer), TARGET_IP, TARGET_PORT); // Send message + mutex_exit(&wifi_mutex); + + // Increment counters + *packet_number = (*packet_number + 1) % 256; + *instruction = (*instruction + 1) % 256; + + diodeState = !diodeState; + + sleep_ms(2000); + } +} + +int main() +{ + stdio_init_all(); + + sleep_ms(2000); + + puts("Pi Starting..."); + + if(cyw43_arch_init()) + { + puts("WiFi init failed !"); + return -1; + } + + multicore_launch_core1(core1_entry); + + puts("WiFi is configuring..."); + + mutex_enter_blocking(&wifi_mutex); + wifi_operator_init_ap_mode(AP_SSID, AP_PASSWORD, AP_CHANNEL); + mutex_exit(&wifi_mutex); + + puts("WiFi configured !"); + + // Starts UDP client on core 0 + udp_client_init(UDP_CLIENT_PORT, custom_message_callback); + // UDP client receive data on port 4243 automatically and execute the message callback on each receptions + puts("UDP client started on core 0 !"); + + while(true) + { + mutex_enter_blocking(&wifi_mutex); + cyw43_arch_poll(); + mutex_exit(&wifi_mutex); + + tight_loop_contents(); + } + + udp_client_exit(); // Hehehe, I know you could forgot this one +} diff --git a/src/udp/udp_client.c b/src/udp/udp_client.c new file mode 100644 index 0000000..46c814a --- /dev/null +++ b/src/udp/udp_client.c @@ -0,0 +1,62 @@ +#include "udp_client.h" + +#include + +udp_client_t udp_client; + +static inline void handle_receive(struct pbuf *p, const ip_addr_t *addr, u16_t port) +{ + if(p->len >= 2) + { + uint8_t packet_number = ((uint8_t*)p->payload)[0]; + uint8_t instruction = ((uint8_t*)p->payload)[1]; + + udp_client.message_callback(packet_number, instruction, addr, port); + } + + pbuf_free(p); +} + +static inline void udp_receive_callback(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) +{ + udp_client = *(udp_client_t *)&arg; + handle_receive(p, addr, port); +} + +// Default callback func +static void default_message_callback(uint8_t packet_number, uint8_t instruction, const ip_addr_t *addr, u16_t port) +{ + printf("Reçu: packet=%d, instruction=%d depuis %s:%d\n", packet_number, instruction, ipaddr_ntoa(addr), port); +} + +void udp_client_init(int port, message_callback_t callback) +{ + udp_client.message_callback = callback ? callback : default_message_callback; + + udp_client.pcb = udp_new(); + if(udp_client.pcb == NULL) + { + puts("Error creating UDP client"); + return; + } + + udp_recv(udp_client.pcb, udp_receive_callback, &udp_client); + + err_t err = udp_bind(udp_client.pcb, IP_ADDR_ANY, port); + if(err != ERR_OK) + { + printf("Erreur bind UDP client: %d\n", err); + return; + } + + printf("UDP client started on port %d\n", port); +} + +void udp_client_exit(void) +{ + if(udp_client.pcb) + { + udp_remove(udp_client.pcb); + udp_client.pcb = NULL; + } +} diff --git a/src/udp/udp_client.h b/src/udp/udp_client.h new file mode 100644 index 0000000..bb36890 --- /dev/null +++ b/src/udp/udp_client.h @@ -0,0 +1,26 @@ +#ifndef UDP_CLIENT_H +#define UDP_CLIENT_H + +#include +#include "lwip/udp.h" + +#define BUFFER_SIZE 1024 + +// Message callback deffinition +typedef void (*message_callback_t)(uint8_t packet_number, uint8_t instruction, const ip_addr_t *addr, uint16_t port); + +// Data in here is used by the hardware +typedef struct udp_client_t { + struct udp_pcb *pcb; // Like this + ip_addr_t local_addr; // Or this + uint16_t local_port; // So don't remove them, even if they are not used explicitely in the program + uint8_t recv_buffer[BUFFER_SIZE]; // Please (Not even change their position) + message_callback_t message_callback; +} udp_client_t; + +// Init udp client, set callback to NULL for the default callback +void udp_client_init(int port, message_callback_t callback); +// Exit udp client +void udp_client_exit(void); + +#endif // UDP_CLIENT_H \ No newline at end of file diff --git a/src/udp/udp_server.c b/src/udp/udp_server.c new file mode 100644 index 0000000..0ada432 --- /dev/null +++ b/src/udp/udp_server.c @@ -0,0 +1,87 @@ +#include "udp_server.h" + +#include +#include "pico/cyw43_arch.h" +#include "lwip/def.h" + +udp_server_t udp_server; + +void udp_server_init(int port) +{ + udp_server.pcb = udp_new(); + if(!udp_server.pcb) + { + puts("Error creating UDP server"); + return; + } + + err_t err = udp_bind(udp_server.pcb, IP_ADDR_ANY, port); + if(err != ERR_OK) + { + printf("Error binding UDP server: %d\n", err); + return; + } +} + +void udp_server_exit(void) +{ + if(udp_server.pcb) + { + udp_remove(udp_server.pcb); + udp_server.pcb = NULL; + } +} + +void udp_server_send(const uint8_t *data, size_t length, const char *ip, uint16_t port) +{ + ip4addr_aton(ip, &udp_server.remote_addr); + udp_server.remote_port = port; + + // Nombre maximum de tentatives + const int MAX_RETRIES = 3; + uint tries = 0; + + while(tries < MAX_RETRIES) + { + struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_RAM); + if(p == NULL) + { + printf("Try %d - Error allocating pbuf\n", ++tries); + sleep_ms(100); // Wait before retry + continue; + } + + memcpy(p->payload, (char *)data, length); + err_t err = udp_sendto(udp_server.pcb, p, &udp_server.remote_addr, udp_server.remote_port); + pbuf_free(p); + + if (err == ERR_OK) + { + return; // Succès + } + else + { + const char* error_msg; + + switch(err) + { + case ERR_MEM: + error_msg = "Insuficient memory"; + break; + + case ERR_BUF: + error_msg = "Buffer full"; + break; + + default: + error_msg = "Unknown error"; + } + + printf("Try %d - Error send UDP: %d (%s)\n", ++tries, err, error_msg); + + sleep_ms(100); // Wait before retry + } + } + + printf("Error sending data after %d tries...\n", retry); +} diff --git a/src/udp/udp_server.h b/src/udp/udp_server.h new file mode 100644 index 0000000..6e8ff2b --- /dev/null +++ b/src/udp/udp_server.h @@ -0,0 +1,17 @@ +#ifndef UDP_SERVER_H +#define UDP_SERVER_H + +#include +#include "lwip/udp.h" + +typedef struct udp_server_t { + struct udp_pcb *pcb; + ip_addr_t remote_addr; + uint16_t remote_port; +} udp_server_t; + +void udp_server_init(int port); +void udp_server_exit(void); +void udp_server_send(const uint8_t *data, size_t length, const char *ip, uint16_t port); + +#endif // UDP_SERVER_H \ No newline at end of file diff --git a/src/wifi_operator/wifi_operator.c b/src/wifi_operator/wifi_operator.c new file mode 100644 index 0000000..53a6290 --- /dev/null +++ b/src/wifi_operator/wifi_operator.c @@ -0,0 +1,69 @@ +#include "wifi_operator.h" + +#include +#include "pico/cyw43_arch.h" +#include "lwip/netif.h" +#include "lwip/ip4_addr.h" + +void wifi_operator_init_ap_mode(const char *ssid, const char *password, int channel) +{ + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); + + // Configuration IP pour le mode AP + ip4_addr_t ip, netmask, gateway; + IP4_ADDR(&ip, 192, 168, 128, 1); + IP4_ADDR(&netmask, 255, 255, 255, 0); + IP4_ADDR(&gateway, 192, 168, 128, 1); + + puts("WiFi is configuring..."); + + // Désactiver le mode d'économie d'énergie + cyw43_wifi_pm(&cyw43_state, CYW43_NO_POWERSAVE_MODE); + + // Activer le mode AP + cyw43_arch_enable_ap_mode(ssid, password, CYW43_AUTH_WPA2_AES_PSK); + + // Attendre que l'interface soit prête + int retry = 0; + while (netif_default == NULL && retry < 10) + { + sleep_ms(500); + retry++; + } + + if (netif_default == NULL) + { + puts("Error: wireless interface not initialised"); + return; + } + + // Configuration du canal WiFi + cyw43_wifi_ap_set_channel(&cyw43_state, channel); + + // Configuration de l'interface réseau + netif_set_up(netif_default); + netif_set_link_up(netif_default); + netif_set_addr(netif_default, &ip, &netmask, &gateway); + + puts("IP configuration ended !"); + puts("Access point activated !"); + printf("SSID : %s\n", ssid); + printf("Password : %s\n", password); + printf("Channel : %d\n", channel); +} + +void wifi_operator_init(const char *ssid, const char *password) +{ + // Mode client + cyw43_arch_enable_sta_mode(); + cyw43_arch_wifi_connect_timeout_ms(ssid, password, CYW43_AUTH_WPA2_AES_PSK, 30000); + puts("Connexion au réseau Wi-Fi..."); + int status = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA); + while (status != CYW43_LINK_UP) + { + sleep_ms(500); + puts("Connexion en cours..."); + status = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA); + } + puts("Connexion établie!"); +} diff --git a/src/wifi_operator/wifi_operator.h b/src/wifi_operator/wifi_operator.h new file mode 100644 index 0000000..b68cf4d --- /dev/null +++ b/src/wifi_operator/wifi_operator.h @@ -0,0 +1,8 @@ +#ifndef WIFI_OPERATOR_H +#define WIFI_OPERATOR_H + +void wifi_operator_init_ap_mode(const char *ssid, const char *password, int channel); + +void wifi_operator_init(const char *ssid, const char *password); + +#endif // WIFI_OPERATOR_H \ No newline at end of file