Skip to content

Instantly share code, notes, and snippets.

@davidgraeff
Created March 27, 2015 15:19
Show Gist options
  • Save davidgraeff/a9e5900611c7a10c6729 to your computer and use it in GitHub Desktop.
Save davidgraeff/a9e5900611c7a10c6729 to your computer and use it in GitHub Desktop.
This user_main.c should be able to receive UDP broadcast packets with SDK 1.0 but doesn't.
#include "c_types.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "gpio.h"
#include "at_custom.h"
#include "osapi.h"
/**
* Minimal example: Setup wifi for station mode, setup connection, wait for an IP.
* If an IP has been assigned (we poll for that every 3s), start a udp socket. No remote
* IP set, only local port set.
* Expected: Broadcast UDP works.
* Current behaviour: Only unicast traffic is accepted, no broadcast packets.
*/
static struct espconn* pUdpServer;
static os_timer_t timer_check_connection;
static void ICACHE_FLASH_ATTR network_received(void *arg, char *data, unsigned short len) ;
static void ICACHE_FLASH_ATTR network_udp_start(void) ;
static void ICACHE_FLASH_ATTR timeout_func(void *arg);
static void ICACHE_FLASH_ATTR init_check_timer();
extern void uart_init(int uart0_br, int uart1_br);
void user_init(void)
{
uart_init(115200, 115200);
at_init();
//Set station mode
wifi_set_opmode( 0x1 );
//Set ap settings
char ssid[32] = "AP_NAME";
char password[64] = "AP_PWD";
struct station_config stationConf;
os_memcpy(&stationConf.ssid, ssid, sizeof(ssid));
os_memcpy(&stationConf.password, password, sizeof(password));
wifi_station_set_config(&stationConf);
init_check_timer();
}
static void ICACHE_FLASH_ATTR init_check_timer() {
//Disarm timer
os_timer_disarm(&timer_check_connection);
//Setup and arm timer
os_timer_setfn(&timer_check_connection, (os_timer_func_t *)timeout_func, 0);
os_timer_arm(&timer_check_connection, 3000, 1);
timeout_func(0);
}
static void ICACHE_FLASH_ATTR timeout_func(void *arg)
{
// We execute this timer function as long as we do not have an IP assigned
struct ip_info info;
wifi_get_ip_info(STATION_IF, &info);
at_port_print("...\n\r");
if (wifi_station_get_connect_status() != STATION_GOT_IP || info.ip.addr == 0)
return;
// IP assigned, disarm timer
os_timer_disarm(&timer_check_connection);
network_udp_start();
}
static void ICACHE_FLASH_ATTR network_udp_start(void)
{
pUdpServer = (struct espconn *)os_zalloc(sizeof(struct espconn));
pUdpServer->type=ESPCONN_UDP;
pUdpServer->state=ESPCONN_NONE;
pUdpServer->proto.udp= (esp_udp *)os_zalloc(sizeof(esp_udp));
pUdpServer->proto.udp->local_port=3338; // Set local port to 2222
//pUdpServer->proto.udp->remote_port=3338; // Set remote port
if(espconn_create(pUdpServer) == 0)
{
espconn_regist_recvcb(pUdpServer, network_received);
at_port_print("UDP OK\n\r");
}
}
static void ICACHE_FLASH_ATTR network_received(void *arg, char *data, unsigned short len)
{
// print ACK on UART
at_port_print("UDP received\n\r");
// send ACK to sender via udp
struct espconn *udpconn=(struct espconn *)arg;
static const char msg[] = "REC\n";
espconn_sent(udpconn, (uint8 *)msg, sizeof(msg));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment