Initial commit
This commit is contained in:
81
basic-udp/main.c
Normal file
81
basic-udp/main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/cyw43_arch.h"
|
||||
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/dhcp.h"
|
||||
|
||||
#include "wifi-settings.h"
|
||||
|
||||
static struct udp_pcb *udp;
|
||||
static ip_addr_t dest_ip;
|
||||
|
||||
extern struct netif *netif_default;
|
||||
|
||||
static void send_hello(void) {
|
||||
const char msg[] = "hello world\n";
|
||||
|
||||
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, sizeof(msg) - 1, PBUF_RAM);
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(p->payload, msg, sizeof(msg) - 1);
|
||||
udp_sendto(udp, p, &dest_ip, 5505);
|
||||
pbuf_free(p);
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
|
||||
if (cyw43_arch_init()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
cyw43_arch_enable_sta_mode();
|
||||
|
||||
if (cyw43_arch_wifi_connect_timeout_ms(
|
||||
WIFI_SSID,
|
||||
WIFI_PASS,
|
||||
CYW43_AUTH_WPA2_AES_PSK,
|
||||
30000)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Raw lwIP setup */
|
||||
udp = udp_new_ip_type(IPADDR_TYPE_V4);
|
||||
if (!udp) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ipaddr_aton("192.168.2.99", &dest_ip);
|
||||
|
||||
absolute_time_t next = get_absolute_time();
|
||||
|
||||
bool dhcp_done = false;
|
||||
|
||||
while (true) {
|
||||
cyw43_arch_poll();
|
||||
|
||||
if (!dhcp_done) {
|
||||
if (netif_default &&
|
||||
netif_is_up(netif_default) &&
|
||||
!ip4_addr_isany_val(*netif_ip4_addr(netif_default))) {
|
||||
|
||||
dhcp_done = true;
|
||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (absolute_time_diff_us(get_absolute_time(), next) <= 0) {
|
||||
send_hello();
|
||||
next = delayed_by_ms(next, 1000);
|
||||
}
|
||||
|
||||
sleep_ms(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user