UDP Client Code

steve2077
Posts: 4
Joined: Mon Feb 02, 2015 3:20 pm

UDP Client Code

Postby steve2077 » Sun Mar 08, 2015 12:56 pm

Can anybody pinpoint what's wrong with my code?
I would like to send a message by ESP_station(STATIONAP Mode) to my PC udp server(listening at port:1025).
It seems user_udp_sent_cb doesn't work out :( .
Here is my full code for your ref.

Code: Select all

   /******************************************************************************
     * Copyright 2013-2014 Espressif Systems
     *
    *******************************************************************************/
    #include "ets_sys.h"
    #include "os_type.h"
    #include "osapi.h"
    #include "mem.h"
    #include "user_interface.h"

    #include "espconn.h"
    #include "user_json.h"
    #include "user_devicefind.h"
   #define ESPCONN_OK 0

    const char *device_find_request = "Are You Smart Device?";
    const char *device_find_response_ok = "I'm Steve Device.";

 /*---------------------------------------------------------------------------*/
    LOCAL struct espconn ptrespconn;

/******************************************************************************
 * FunctionName : user_udp_sent_cb
 * Description  : Data has been sent successfully and acknowledged by the remote host.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_udp_sent_cb(void *arg)
{
    struct espconn *pespconn = arg;
    os_printf("user_udp_sent_cb\n");
}

/******************************************************************************
 * FunctionName : user_udp_sent
 * Description  : Processing the application data and sending it to the host
 * Parameters   : pespconn -- the espconn used to connetion with the host
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_udp_sent(struct espconn *pespconn)
{
   unsigned short length;
   unsigned short packet_size=1024;
    char *pbuf = (char *)os_zalloc(packet_size);
   length=os_strlen(device_find_request);
   os_memcpy(pbuf,device_find_request,length);
    espconn_sent(pespconn, pbuf, os_strlen(pbuf));
    os_free(pbuf);
}   

    /******************************************************************************
     * FunctionName : user_devicefind_init
     * Description  : create a udp listening
     * Parameters   : none
     * Returns      : none
    *******************************************************************************/
    void ICACHE_FLASH_ATTR
    user_udp_init(void)
    {
      const char dst_ip[4]={192,168,1,108}; // Server IP
        ptrespconn.type = ESPCONN_UDP;
        ptrespconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp));
      ptrespconn.proto.udp->remote_port = 1025;// Remote Server Port
      os_memcpy(ptrespconn.proto.udp->remote_ip, dst_ip, 4); // Remote Server IP
      ptrespconn.proto.udp->local_port=espconn_port();// ESP8266 udp port
        //ptrespconn.proto.udp->local_port = 1025;  // ESP8266 udp port
        //espconn_regist_recvcb(&ptrespconn, user_udp_recv); // register a udp packet receiving callback
      espconn_regist_sentcb(&ptrespconn, user_udp_sent_cb); // register a udp packet sending callback
      user_udp_sent(&ptrespconn); // sent data
        espconn_create(&ptrespconn);   // create udp
    }

    /******************************************************************************
     * FunctionName : user_init
     * Description  : entry of user application, init user function here
     * Parameters   : none
     * Returns      : none
    *******************************************************************************/
    void user_init(void)
    {
       os_printf("SDK version:%s\n", system_get_sdk_version());
      char ssid[32] = "steve";
      char password[64] = "steveahn";
      struct station_config stationConf;
       
       //Set softAP + station mode
       wifi_set_opmode(STATIONAP_MODE);
      
       //Set ap settings
      os_memcpy(&stationConf.ssid, ssid, 32);
      os_memcpy(&stationConf.password, password, 64);
      wifi_station_set_config(&stationConf);
      
      os_delay_us(15000000);
      os_printf("start from here\r\n");

       // Create udp listening.
       user_udp_init();
    }

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: UDP Client Code

Postby Her Mary » Mon Mar 09, 2015 4:23 pm

:roll:

It seems that you need to call espconn_create to create a udp first, then call espconn_sent to send data..

steve2077
Posts: 4
Joined: Mon Feb 02, 2015 3:20 pm

Re: UDP Client Code

Postby steve2077 » Tue Mar 17, 2015 12:58 am

Thank you, Her Majesty.

Yes, you are right. create udp socket/frame first(espconn_create), then send the data(user_udp_sent). it's working well now. :D
But to make it work out, I add some of additional code to get local ip prior to creating udp/sending data.
happy coding and happy IoT...

Who is online

Users browsing this forum: No registered users and 175 guests