ESP8266 Developer Zone The Official ESP8266 Forum 2015-03-17T00:58:15+08:00 https://bbs.espressif.com:443/feed.php?f=7&t=253 2015-03-17T00:58:15+08:00 2015-03-17T00:58:15+08:00 https://bbs.espressif.com:443/viewtopic.php?t=253&p=1045#p1045 <![CDATA[Re: UDP Client Code]]>
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...

Statistics: Posted by steve2077 — Tue Mar 17, 2015 12:58 am


]]>
2015-03-09T16:23:43+08:00 2015-03-09T16:23:43+08:00 https://bbs.espressif.com:443/viewtopic.php?t=253&p=973#p973 <![CDATA[Re: UDP Client Code]]>

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

Statistics: Posted by Her Mary — Mon Mar 09, 2015 4:23 pm


]]>
2015-03-08T12:56:12+08:00 2015-03-08T12:56:12+08:00 https://bbs.espressif.com:443/viewtopic.php?t=253&p=961#p961 <![CDATA[UDP Client 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:

   /******************************************************************************
     * 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();
    }

Statistics: Posted by steve2077 — Sun Mar 08, 2015 12:56 pm


]]>