Teleplot fonctionnel en Wifi et en USB/Série !

This commit is contained in:
Samuel 2024-08-15 02:20:18 +02:00
parent 4f8ba36580
commit 0670a1e013
9 changed files with 295 additions and 16 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
build/ build/
wifi_settings.h

4
.vscode/tasks.json vendored
View File

@ -2,7 +2,7 @@
"tasks": [ "tasks": [
{ {
"type": "shell", "type": "shell",
"command": "cd build; cmake ../; make", "command": "cd build; cmake ../ -DPICO_BOARD=pico_w -DCMAKE_BUILD_TYPE=Release; make",
"label": "CMake in build/", "label": "CMake in build/",
"problemMatcher": [], "problemMatcher": [],
"group": { "group": {
@ -12,7 +12,7 @@
}, },
{ {
"type": "shell", "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", "label": "CMake & Make & Flash",
"problemMatcher": [], "problemMatcher": [],
"group": { "group": {

View File

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.13)
include(pico_sdk_import.cmake) include(pico_sdk_import.cmake)
project(Mon_Projet C CXX ASM) project(PAMI_Cours C CXX ASM)
set(CMAKE_C_STNDARD 11) set(CMAKE_C_STNDARD 11)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
@ -10,25 +10,27 @@ set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
pico_sdk_init() pico_sdk_init()
add_executable(Mon_Projet add_executable(PAMI_Cours
main.c 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_i2c
hardware_uart hardware_uart
pico_stdlib pico_stdlib
pico_multicore pico_multicore
pico_cyw43_arch_lwip_poll
) )
pico_enable_stdio_usb(Mon_Projet 1) pico_enable_stdio_usb(PAMI_Cours 1)
pico_enable_stdio_uart(Mon_Projet 1) pico_enable_stdio_uart(PAMI_Cours 1)
pico_add_extra_outputs(Mon_Projet) pico_add_extra_outputs(PAMI_Cours)
add_custom_target(Flash add_custom_target(Flash
DEPENDS Mon_Projet DEPENDS PAMI_Cours
COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/Mon_Projet.uf2 COMMAND sudo picotool load -f ${PROJECT_BINARY_DIR}/PAMI_Cours.uf2
) )

138
Teleplot.c Normal file
View File

@ -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);
}

9
Teleplot.h Normal file
View File

@ -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);

10
lwipopts.h Normal file
View File

@ -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

View File

@ -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__ */

25
main.c
View File

@ -1,16 +1,33 @@
/***** /*****
* Copyright (c) 2023 - Poivron Robotique * Copyright (c) 2024 - Poivron Robotique
* *
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*/ */
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "Teleplot.h"
#include <stdio.h> #include <stdio.h>
void setup(void);
void loop(void);
void main(void) void main(void)
{ {
stdio_init_all(); setup();
while(1){ while(1){
printf("Exemple\n"); loop();
sleep_ms(1000);
} }
} }
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);
}

12
wifi_settings.h.default Normal file
View File

@ -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"