Can't send UDP packets

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

Can't send UDP packets

Postby bountyla » Sun Oct 17, 2010 10:29 am

Hello, hope someone can help me..

I want to use my blackwidow as a client sending UDP packets to a computer (server) on my network.
On the server I run a small java program listening for udp packets on port 5003, and writing the packet content to the console when something comes in.

I did a small java program (client), sending UDP packets to port 5003, and got it to run on a different computer on my network.

With this setup the server recieves the packets as expected, and everything is nice.. This tells me that my network isn't blocking anything..

Now I want to make my BlackWidow a client sending UDP packets to the server.
I have made a short version of the udpapp.c, to make it as simple as possible. I simply just want it to send something which my server can recieve.

My problem is that the server isn't recieving anything. - My BlackWidow indicates that it's connected to the network.

I have java experience but im quite new to c/c++, so perhaps im overlooking something, or missunderstanding something..
Any ideas very appreciated...!

Here comes my short version of the udpapp:

#include "uip.h"
#include <string.h>
#include "udpapp.h"
#include "config.h"

#define STATE_INIT 0
#define STATE_LISTENING 1
#define STATE_HELLO_RECEIVED 2
#define STATE_NAME_RECEIVED 3

static struct udpapp_state s;

void dummy_app_appcall(void)
{
}

void udpapp_init(void){

uip_ipaddr_t addr;
struct uip_udp_conn *c;

uip_ipaddr(&addr, 192,168,1,108);
c = uip_udp_new(&addr, HTONS(5003));
if(c != NULL) {
uip_udp_bind(c, HTONS(0));
}

s.state = STATE_HELLO_RECEIVED;

PT_INIT(&s.pt);
}

static unsigned char parse_msg(void){
return 0;
}

static void send_request(void){
char str[] = "Hello. What is your name?\n";
memcpy(uip_appdata, str, strlen(str));
uip_send(uip_appdata, strlen(str));
}

static void send_response(void){
}

static PT_THREAD(handle_connection(void)){
PT_BEGIN(&s.pt);
send_request();

PT_END(&s.pt);
}

void udpapp_appcall(void){
handle_connection();
}
bountyla
 
Posts: 4
Joined: Sun Oct 17, 2010 4:35 am

Re: Can't send UDP packets

Postby GregEigsti » Sun Oct 17, 2010 11:05 am

You should tab/space your code to make it more readable; its currently pretty difficult to read - and that makes a difference when you are asking for help (and it will make a difference for you as you read through it and debug it, etc.) - you want your code as easy to read as possible or folks may take one look, think "this is too hard to read" and move on to something else. In fact I missed some bits for the example below because, well, it is pretty cryptic without good formatting.

For instance your code looks like:
Code: Select all
void udpapp_init(void){

uip_ipaddr_t addr;
struct uip_udp_conn *c;

uip_ipaddr(&addr, 192,168,1,108);
c = uip_udp_new(&addr, HTONS(5003));
if(c != NULL) {
uip_udp_bind(c, HTONS(0));
}

s.state = STATE_HELLO_RECEIVED;

PT_INIT(&s.pt);
}


It could look like:
Code: Select all
void udpapp_init(void)
{
   uip_ipaddr_t addr;
   struct uip_udp_conn *c;

   uip_ipaddr(&addr, 192,168,1,108);
   c = uip_udp_new(&addr, HTONS(5003));
   if(c != NULL) {
      uip_udp_bind(c, HTONS(0));
   }

   s.state = STATE_HELLO_RECEIVED;

   PT_INIT(&s.pt);
}


And thats where we are. I could take your code and reformat it in order to help you out. But its Sunday and I have other things I'd like to get to... So if I could have read through it quickly/easily you may have had an answer right now; but I looked at it for a few minutes and said "screw this, I have better things to do". Case in point ;)
Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: Can't send UDP packets

Postby bountyla » Mon Oct 18, 2010 11:56 am

Hi Greg

Sorry for that. - I'm blind and do my programming via screen reader software and my ears. - I'm probably not enough aware about the importance of visual code formating. In fact that kind of formating is quite disturbing to the way I work.

Anyway, I would still be very happy if someone out there can help me to get my BlackWidow to send some UDP-packets.

I include my code again, and hope it's more visual appealing this time.

Thanks Bountyla

Code: Select all
#include "uip.h"
#include <string.h>
#include "udpapp.h"
#include "config.h"

#define STATE_INIT            0
#define STATE_LISTENING         1
#define STATE_HELLO_RECEIVED   2
#define STATE_NAME_RECEIVED      3

static struct udpapp_state s;

void dummy_app_appcall(void)
{
}

void udpapp_init(void)
{
    uip_ipaddr_t addr;
    struct uip_udp_conn *c;
    uip_ipaddr(&addr, 192,168,1,108);
    c = uip_udp_new(&addr, HTONS(5003));

    if(c != NULL) {
        uip_udp_bind(c, HTONS(0));
    }

    s.state = STATE_HELLO_RECEIVED;
    PT_INIT(&s.pt);
}

static unsigned char parse_msg(void)
{
    return 0;
}

static void send_request(void)
{
    char str[] = "Hello. What is your name?\n";
    memcpy(uip_appdata, str, strlen(str));
    uip_send(uip_appdata, strlen(str));
}

static void send_response(void)
{
}

static PT_THREAD(handle_connection(void))
{
    PT_BEGIN(&s.pt);
    send_request();
    PT_END(&s.pt);
}

void udpapp_appcall(void)
{
    handle_connection();
}

bountyla
 
Posts: 4
Joined: Sun Oct 17, 2010 4:35 am

Re: Can't send UDP packets

Postby GregEigsti » Mon Oct 18, 2010 3:45 pm

Please include the rest of your sketch (the .pde file); the .c portion looks about correct but its hard to say as the rest of the sketch is not there to prove it. The only thing that I can think of is that udpapp_init is not being called - but its hard to say without the rest of your code. Having udpapp_appcall call handle_connection in the manner which yours does could have some unintended side effects - namely you would be sending a UDP packet every time udpapp_appcall is called (and it is called for a multitude of different reasons). It is best to wrap calls such as handle_connection in a conditional that tests whether or not it is appropriate to send at that time.

Feel free to post untabbed code if doing so makes your work easier to accomplish.

Thanks
Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: Can't send UDP packets

Postby bountyla » Wed Oct 20, 2010 11:52 am

Hi Greg, thank you for your answer..

I include my sketch-file, which I haven't changed from the original UDP example.
So far, I just want the BlackWidow to turn on, and send the message "Hello. What is your name" on port 5003.

Code: Select all
#include <WiShield.h>

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,107};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"luftguitar"};      // max 32 bytes

unsigned char security_type = 3;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"*******************"};   // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 1
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 2
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00   // Key 3
                        };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

void setup()
{
   WiFi.init();
}

void loop()
{
   WiFi.run();
}



My apps-conf.h looks like this:
Code: Select all

/******************************************************************************

  Filename:      apps-conf.h
  Description:   Web application configuration file

******************************************************************************

  TCP/IP stack and driver for the WiShield 1.0 wireless devices

  Copyright(c) 2009 Async Labs Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Contact Information:
  <[email protected]>

   Author               Date        Comment
  ---------------------------------------------------------------
   AsyncLabs         05/29/2009   Initial port

*****************************************************************************/

#ifndef __APPS_CONF_H__
#define __APPS_CONF_H__

//Here we include the header file for the application(s) we use in our project.
//#define APP_WEBSERVER
//#define APP_WEBCLIENT
//#define APP_SOCKAPP
#define APP_UDPAPP
//#define APP_WISERVER

#ifdef APP_WEBSERVER
#include "webserver.h"
#endif

#ifdef APP_WEBCLIENT
#include "webclient.h"
#endif

#ifdef APP_SOCKAPP
#include "socketapp.h"
#endif

#ifdef APP_UDPAPP
#include "udpapp.h"
#endif

#ifdef APP_WISERVER
#include "server.h"
#endif

#endif /*__APPS_CONF_H__*/




Am I right, that my sketch should now end up sending the "hello bla bla bla" message?

Thank you..
Everything best..
bountyla
 
Posts: 4
Joined: Sun Oct 17, 2010 4:35 am

Re: Can't send UDP packets

Postby GregEigsti » Wed Oct 20, 2010 11:23 pm

Sorry, have not had much of a chance to look at this; will try to do so tomorrow. Probably something very simple - it all looks pretty close to good.

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: Can't send UDP packets

Postby GregEigsti » Thu Oct 21, 2010 5:47 pm

I am starting to look at this and remembered for UDP you not only have to enable the udp app type in apps-conf.h - which you posted - but you also have to enable UDP in uip-conf.h; which is another file in the WiShield driver directory. The user contrib branch has worked around this so you only have to set it in apps-conf.h - I really only use the user contrib branch for my projects and I had forgotten about this until I started playing with your code using the release branch.

I'll let you know how it goes...

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: Can't send UDP packets

Postby bountyla » Fri Oct 22, 2010 1:35 pm

Hi Greg

Thank you for trying to help me out..

I'm not sure what you mean by "user contrib branch" versus "release branch."

Anyway, I did also configure the UDP part from the uip-conf.h file, it comes here.
Everything best..
Code: Select all

/******************************************************************************

  Filename:      uip-conf.h
  Description:   uIP configuration file

******************************************************************************

  TCP/IP stack and driver for the WiShield 1.0 wireless devices

  Copyright(c) 2009 Async Labs Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Contact Information:
  <[email protected]>

   Author               Date        Comment
  ---------------------------------------------------------------
   AsyncLabs         05/29/2009   Initial port

*****************************************************************************/

#ifndef __UIP_CONF_H__
#define __UIP_CONF_H__

#include <inttypes.h>
#include <avr/io.h>
#include <stdio.h>
//#include <stdbool.h>

/**
* 8 bit datatype
*
* This typedef defines the 8-bit type used throughout uIP.
*
* \hideinitializer
*/
typedef uint8_t u8_t;

/**
* 16 bit datatype
*
* This typedef defines the 16-bit type used throughout uIP.
*
* \hideinitializer
*/
typedef uint16_t u16_t;

/**
* Statistics datatype
*
* This typedef defines the dataype used for keeping statistics in
* uIP.
*
* \hideinitializer
*/
typedef unsigned short uip_stats_t;

/**
* Maximum number of TCP connections.
*
* \hideinitializer
*/
#define UIP_CONF_MAX_CONNECTIONS 1

/**
* Maximum number of listening TCP ports.
*
* \hideinitializer
*/
#define UIP_CONF_MAX_LISTENPORTS 1

/**
* uIP buffer size.
*
* \hideinitializer
*/
#define UIP_CONF_BUFFER_SIZE     400
/**
* CPU byte order.
*
* \hideinitializer
*/
#define UIP_CONF_BYTE_ORDER      LITTLE_ENDIAN

/**
* Logging on or off
*
* \hideinitializer
*/
#define UIP_CONF_LOGGING         0

/**
* UDP support on or off
*
* \hideinitializer
*/
#define UIP_CONF_UDP             1

/**
* UDP checksums on or off
*
* \hideinitializer
*/
#define UIP_CONF_UDP_CHECKSUMS  1

/**
* uIP statistics on or off
*
* \hideinitializer
*/
#define UIP_CONF_STATISTICS      0

/**
* Broadcast support.
*
* \hideinitializer
*/
#define UIP_CONF_BROADCAST      1

/**
* The maximum amount of concurrent UDP connections.
*
* \hideinitializer
*/
#define UIP_CONF_UDP_CONNS      1

//Include app configuration
#include "apps-conf.h"

#endif /* __UIP_CONF_H__ */

/** @} */
/** @} */
bountyla
 
Posts: 4
Joined: Sun Oct 17, 2010 4:35 am

Re: Can't send UDP packets

Postby GregEigsti » Sun Oct 24, 2010 10:18 am

On GitHub there are several branches of the WiShield code - one of them the normal release version that is pointed at by the WiShield docs. The other (that I care about) is the user contrib branch which contains bug fixes and enhancements (well mostly enhancements, but a few bug fixes).

I tried your sketch and did not receive any UDP packets on my computer (python script as server) - I also tried some of my known working UDP sketches and did not receive any packets either so I am thinking the problem is with the python script (it is dead simple though). I wrote a .Net/C# UDP server yesterday but have yet to try it with the WiShield. Next step would be to try with some of my known working C code.

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website


Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 1 guest