Statistics: Posted by Her Mary — Tue Feb 26, 2019 5:37 pm
Code:
static void ICACHE_FLASH_ATTR udp_recv(void *arg, char *pusrdata, unsigned short length) {
struct espconn *pespconn = arg;
os_printf("client %d.%d.%d.%d:%d -> ", pespconn->proto.udp->remote_ip[0],
pespconn->proto.udp->remote_ip[1], pespconn->proto.udp->remote_ip[2],
pespconn->proto.udp->remote_ip[3], pespconn->proto.udp->remote_port);
os_printf("received %d bytes of data\n", length);
os_printf("server %d.%d.%d.%d:%d\n", pespconn->proto.udp->local_ip[0],
pespconn->proto.udp->local_ip[1], pespconn->proto.udp->local_ip[2],
pespconn->proto.udp->local_ip[3], pespconn->proto.udp->local_port);
uint16_t i;
for(i = 0; i < length; ++i) {
os_printf("%02x ", pusrdata[i]); // the IP and port are not in the user data. It should be in the connection
}
os_printf("\n");
sint8_t errcode = espconn_send(pespconn, response, sizeof(response)); //It's being sent to the wrong place because I don'r know the right IP/port pair.
if(errcode != 0) {
os_printf("%s\n", toURL(question));
os_printf("UDP send: ");
switch(errcode) {
//case 0: os_printf("ok\n"); break;
case ESPCONN_ISCONN: os_printf("already connected\n"); break;
case ESPCONN_MEM: os_printf("lack of memory\n"); break;
case ESPCONN_IF: os_printf("fail\n"); break;
default: os_printf("unknown error (%d)\n", errcode);
}
}
}
Code:
void ICACHE_FLASH_ATTR server_start(void) {
struct espconn *conn = (struct espconn *) os_zalloc(sizeof(struct espconn));
if (conn == NULL) return;
conn->type = ESPCONN_UDP;
conn->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
conn->proto.udp->local_port = XX; // port my devices listens to
conn->proto.udp->remote_port = YYYY; // (????) port I should be sending te messages back
char tcpserverip[16];
os_sprintf(tcpserverip, "%s", "255.255.255.255\0"); // I broadcasting, but I should use the right IP address
uint32_t ip = ipaddr_addr(tcpserverip);
ip = ipaddr_addr(tcpserverip);
os_memcpy(conn->proto.udp->remote_ip, &ip, 4);
espconn_regist_recvcb(conn, udp_recv);
sint8_t errcode = espconn_create(conn);
conn->proto.udp->remote_ip[2], conn->proto.udp->remote_ip[3]);
os_printf("UDP connection: ");
switch(errcode) {
case 0: os_printf("created\n"); break;
case ESPCONN_ISCONN: os_printf("already connected\n"); break;
case ESPCONN_MEM: os_printf("lack of memory\n"); break;
case ESPCONN_ARG: os_printf("invalid arguments\n"); break;
default: os_printf("unknown error (%d)\n", errcode);
}
}
Statistics: Posted by Guest — Fri Feb 01, 2019 9:15 pm