Chat freely about anything...

User avatar
By nevyn
#18582 I'm trying to put together a simple sensor network as an experiment and as the data is not critical I'm thinking of just using UDP as the transport mechanism. Each sensor should wake up periodically and send it's data over the network for it to be collected by a server app.

I'm trying to work with native C on the ESP8266-01 module I have but I am getting what I think is an invalid parameter code coming back from an API call and to me everything looks OK.

I have a pointer to a connection object which I initialise as follows:
Code: Select allstruct espconn *_ptrUDPServer;
//
//  Allocate an "espconn" for the UDP connection.
//
_ptrUDPServer = (struct espconn *) os_zalloc(sizeof(struct espconn));
_ptrUDPServer->type = ESPCONN_UDP;
_ptrUDPServer->state = ESPCONN_NONE;
_ptrUDPServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
_ptrUDPServer->proto.udp->local_port = 1000;
sint8 result = espconn_create(_ptrUDPServer);


I have a timer running every second which is trying to send a message to the server:
Code: Select allvoid TimerCallback(void *arg)
{
    struct ip_info info;

    wifi_get_ip_info(STATION_IF, &info);
    if (wifi_station_get_connect_status() != STATION_GOT_IP || info.ip.addr == 0)
    {
        BitBang(0x01);
    }
    else
    {
        BitBang(0x02);
        sint8 result = espconn_sent(_ptrUDPServer, (uint8 *) USER_DATA, (uint16) strlen(USER_DATA));
        BitBang(result);
    }
}


Where USER_DATA is a simple string for the minute. The BitBang method just dumps data out on GPIO0 and GPIO2 for debugging.

I have tried setting the remote port, the destination IP address (direct and the broadcast address for the subnet) and various combinations of each.

Has anyone managed to get this sort of scenario working as I' appreciate some pointers.

Regards,
Mark
User avatar
By nevyn
#18703 Thanks to the NTP example by Richard Burton I managed to get this going. I needed to change the code setting up the pointer to the connection structure to the following:
Code: Select all_ptrUDPServer = (struct espconn *) os_zalloc(sizeof(struct espconn));
_ptrUDPServer->type = ESPCONN_UDP;
_ptrUDPServer->state = ESPCONN_NONE;
_ptrUDPServer->proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
_ptrUDPServer->proto.udp->local_port = espconn_port();
_ptrUDPServer->proto.udp->remote_port = 11000;
os_memcpy(_ptrUDPServer->proto.udp->remote_ip, udpServerIP, 4);

Where the uspServerIP is an array of bytes containing the IP address of the server I wanted to talk to.

Thanks to Richards work and I hope this helps others,
Mark