From 0670a1e0136c3065c7692e8e00bac4b423cb53f4 Mon Sep 17 00:00:00 2001 From: Samuel Date: Thu, 15 Aug 2024 02:20:18 +0200 Subject: [PATCH] =?UTF-8?q?Teleplot=20fonctionnel=20en=20Wifi=20et=20en=20?= =?UTF-8?q?USB/S=C3=A9rie=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .vscode/tasks.json | 4 +- CMakeLists.txt | 20 +++--- Teleplot.c | 138 +++++++++++++++++++++++++++++++++++++ Teleplot.h | 9 +++ lwipopts.h | 10 +++ lwipopts_examples_common.h | 90 ++++++++++++++++++++++++ main.c | 25 +++++-- wifi_settings.h.default | 12 ++++ 9 files changed, 295 insertions(+), 16 deletions(-) create mode 100644 Teleplot.c create mode 100644 Teleplot.h create mode 100644 lwipopts.h create mode 100644 lwipopts_examples_common.h create mode 100644 wifi_settings.h.default diff --git a/.gitignore b/.gitignore index d163863..e4d478c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build/ \ No newline at end of file +build/ +wifi_settings.h \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c98bf9b..82bbd98 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,7 +2,7 @@ "tasks": [ { "type": "shell", - "command": "cd build; cmake ../; make", + "command": "cd build; cmake ../ -DPICO_BOARD=pico_w -DCMAKE_BUILD_TYPE=Release; 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 -DCMAKE_BUILD_TYPE=Release; make Flash", "label": "CMake & Make & Flash", "problemMatcher": [], "group": { diff --git a/CMakeLists.txt b/CMakeLists.txt index 7171dd2..be53319 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.13) include(pico_sdk_import.cmake) -project(Mon_Projet C CXX ASM) +project(PAMI_Cours C CXX ASM) set(CMAKE_C_STNDARD 11) set(CMAKE_CXX_STANDARD 17) @@ -10,25 +10,27 @@ set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR}) pico_sdk_init() -add_executable(Mon_Projet +add_executable(PAMI_Cours main.c + Teleplot.c ) -target_include_directories(Mon_Projet PRIVATE Mon_Projet_ULD_API/inc/) +target_include_directories(PAMI_Cours PRIVATE ${CMAKE_CURRENT_LIST_DIR}) -target_link_libraries(Mon_Projet +target_link_libraries(PAMI_Cours hardware_i2c hardware_uart pico_stdlib pico_multicore + pico_cyw43_arch_lwip_poll ) -pico_enable_stdio_usb(Mon_Projet 1) -pico_enable_stdio_uart(Mon_Projet 1) +pico_enable_stdio_usb(PAMI_Cours 1) +pico_enable_stdio_uart(PAMI_Cours 1) -pico_add_extra_outputs(Mon_Projet) +pico_add_extra_outputs(PAMI_Cours) add_custom_target(Flash - DEPENDS Mon_Projet - COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/Mon_Projet.uf2 + DEPENDS PAMI_Cours + COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/PAMI_Cours.uf2 ) diff --git a/Teleplot.c b/Teleplot.c new file mode 100644 index 0000000..5af9d53 --- /dev/null +++ b/Teleplot.c @@ -0,0 +1,138 @@ +#include "pico/cyw43_arch.h" +#include "Teleplot.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 + +char teleplot_tampon[BEACON_MSG_LEN_MAX]=""; + +struct udp_pcb* pcb; +ip_addr_t addr; + +long teleplote_temps_ms; +bool teleplot_temps_fige; + +int Teleplot_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); + teleplot_temps_fige = false; + return 0; +} + +void teleplot_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 +} + +void Teleplot_send_string(char * teleplot_tampon){ +#ifdef WIFI_ENABLE + teleplot_udp_send_string(teleplot_tampon); +#else + printf(teleplot_tampon); +#endif +} + +/// @brief Renvoi le temps en milliseconde, le temps réel ou le temps figé +/// @param +/// @return temps en millisecondes +long Teleplot_get_temps(void){ + if(teleplot_temps_fige){ + return teleplote_temps_ms; + } + return (long) (time_us_64()/1000); +} + +/// @brief Permet de "bloquer le temps" pour recevoir toutes les données datées à la même milliseconde +/// Simplifie beaucoup le traitement des données en CSV lors d'un import dans un tableur. +void Teleplot_fige_temps(void){ + teleplot_temps_fige = false; + teleplote_temps_ms = Teleplot_get_temps(); + teleplot_temps_fige = true; +} +void Teleplot_relache_temps(void){ + teleplot_temps_fige = false; +} + + +void Teleplot_envoie_tampon(void){ + Teleplot_send_string(teleplot_tampon); + teleplot_tampon[0]='\0'; // On "vide" le tampon +} + + +void Teleplot_ajout_ou_envoie_tampon(char * message){ + // Si le tampon ne peut pas accueillir le prochain message + // On envoie et on vide le tampon + if(strlen(message) + strlen(teleplot_tampon) > BEACON_MSG_LEN_MAX){ + Teleplot_envoie_tampon(); + } + // On ajoute le message au tampon + strcat(teleplot_tampon, message); + +} + +void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur){ + char tampon[100]; +#ifdef WIFI_ENABLE + sprintf(tampon, "%s:%lu:%.2f\n", nom_variable, Teleplot_get_temps(), valeur); +#else + sprintf(tampon, ">%s:%lu:%.2f\n", nom_variable, Teleplot_get_temps(), valeur); +#endif + Teleplot_ajout_ou_envoie_tampon(tampon); +} + +void Teleplot_add_variable_int(char * nom_variable, int valeur){ + char tampon[100]; +#ifdef WIFI_ENABLE + sprintf(tampon, "%s:%lu:%d\n", nom_variable, Teleplot_get_temps(), valeur); +#else + sprintf(tampon, ">%s:%lu:%d\n", nom_variable, Teleplot_get_temps(), valeur); +#endif + Teleplot_ajout_ou_envoie_tampon(tampon); +} \ No newline at end of file diff --git a/Teleplot.h b/Teleplot.h new file mode 100644 index 0000000..ddbb96b --- /dev/null +++ b/Teleplot.h @@ -0,0 +1,9 @@ +int Teleplot_init(void); +void Teleplot_send_string(char * message); +void Teleplot_envoie_tampon(void); +void Teleplot_add_variable_float_2decimal(char * nom_variable, float valeur); +void Teleplot_add_variable_int(char * nom_variable, int valeur); + +void Teleplot_fige_temps(void); +void Teleplot_relache_temps(void); + 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 f79df64..fd3cc21 100644 --- a/main.c +++ b/main.c @@ -1,16 +1,33 @@ /***** - * Copyright (c) 2023 - Poivron Robotique + * Copyright (c) 2024 - Poivron Robotique * * SPDX-License-Identifier: BSD-3-Clause */ #include "pico/stdlib.h" +#include "Teleplot.h" #include +void setup(void); +void loop(void); + void main(void) { - stdio_init_all(); + setup(); while(1){ - printf("Exemple\n"); - sleep_ms(1000); + loop(); } } + +void setup(void){ + stdio_init_all(); + Teleplot_init(); +} + +void loop(void){ + printf("Exemple\n"); + + Teleplot_add_variable_int("t", 2); + Teleplot_envoie_tampon(); + + sleep_ms(1000); +} \ No newline at end of file diff --git a/wifi_settings.h.default b/wifi_settings.h.default new file mode 100644 index 0000000..fcc6f8e --- /dev/null +++ b/wifi_settings.h.default @@ -0,0 +1,12 @@ +// 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 + +// Si vous n'utilisez pas le wifi, commentez cette ligne: +#define WIFI_ENABLE + +#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