#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; 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); 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_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_udp_send_string(teleplot_tampon); teleplot_tampon[0]='\0'; // On "vide" le 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]; sprintf(tampon, "%s:%lu:%.2f\n", nom_variable, (long)time_us_64()/1000, valeur); Teleplot_ajout_ou_envoie_tampon(tampon); } void Teleplot_add_variable_int(char * nom_variable, int valeur){ char tampon[100]; sprintf(tampon, "%s:%lu:%d\n", nom_variable, (long)time_us_64()/1000, valeur); Teleplot_ajout_ou_envoie_tampon(tampon); }