Pages: [1]   Go Down
Author Topic: Problem about UDP send with ethercard ENC28J60(Solve Now)  (Read 2730 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I'am from China ,I‘am sorry for my poor english.
I have a problem about UDP send with ethercard.

My problem is about:
I use Arduino uno3+ ENC28J60 + DHT11 + two 7segment  LED to  display temperature and humidity and send data to host PC by UDP , all the hardware and sub-module test normal, the ENC28J60 CS connect to D10, the measured power supply voltage for ENC28J60 is 3.3V, The IDE with the 1.03 version, EtherCard withis the latest version. all functions in normal when I running wireshake to capture data on host PC,I got correctly display of temperature and humidity and the received UDP data is OK, but when I stop capture data with wireshake,the host computer can not receive UDP data, what possible with the problem?

Thanks!

the code as below:
Code:


//CS→D10;SI→D11;SO→D12;SCK→D13

#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {192,168,2,23};
static byte gwip[] = {192,168,23,254};
byte Ethernet::buffer[100];
static byte destip[]= {192,168,2,10};
static int myport=1002,destport=1001;

// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// dp,G,F,E,D,C,B,A
const int segmentPins[] = { 9,8,7,6,5,4,3,2};
const int nbrDigits= 2; // the number of digits in the LED display
//dig 1 2
const int digitPins[nbrDigits] = { A4,A5};
int dppin=A3; //set the decimal point link to PIN A3

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN A0 //DHT11 PIN 3 →UNO A0

char string1[2];//humidity
char string2[2];//temperature
 
void setup()
{
  Serial.begin(19200);
 
  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
  
//    ether.setGwIp(gwip);
  
for(int i=0; i < 8; i++)
pinMode(segmentPins[i], OUTPUT); // set segment and DP pins to output
for(int i=0; i < nbrDigits; i++)
pinMode(digitPins[i], OUTPUT);

}
 
void loop()
{
 
  int chk = DHT11.read(DHT11PIN);
 
  Serial.print("Read sensor: ");
  switch (chk)
  {
   case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
  }

int humd=int(DHT11.humidity);
int temp=int(DHT11.temperature-2);

itoa(humd,string1,10);
itoa(temp,string2,10);

//ether.udpPrepare(myport,destip,destport);
//ether.udpTransmit(4);

ether.sendUdp ((char*) string1, 64, myport, destip, destport);
Serial.println("DATA send by UDP!");
Serial.println(humd);
Serial.println(temp);
//  delay(2000);
 

//Display on 7segment
  int counter1=200;
while(counter1)
 {
    counter1--;
//    showNumber(DHT11.humidity);
showNumber(humd);
   }
    counter1=200;
while(counter1)
 {
    counter1--;
    analogWrite(dppin,0);
//    showNumber(DHT11.temperature-2);
showNumber(temp);
    analogWrite(dppin,200);
   }
}


void showNumber( int number)
{
if(number == 0)
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
else
{
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--)
{
if(number > 0)
{
showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int segnumber, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment <8; segment++)
{
boolean isBitSet = bitRead(numeral[segnumber], segment);
// isBitSet will be true if given bit is 1

//isBitSet = ! isBitSet; // remove this line if common cathode display

digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}

« Last Edit: March 02, 2013, 03:46:58 am by superid888 » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WinShake doesn't capture network data. Did you mean WireShark?
If you can capture the data sent by the Arduino on your PC, the Arduino side is doing fine. I don't know what you mean by "host computer can not receive UDP data". What program do you use to receive the data?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WinShake doesn't capture network data. Did you mean WireShark?
If you can capture the data sent by the Arduino on your PC, the Arduino side is doing fine. I don't know what you mean by "host computer can not receive UDP data". What program do you use to receive the data?

Sorry, the program I used to capture network data is WinShake. I've try several different program on host PC and got the same result.
"host computer can not receive UDP data" mean when I do the capturing with winshake, the program I use to receive UDP data can got correct result, when I stop capturing with winshake, the program I use to receive UDP data can't received any data.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I do a test by replace ENC28J60 with W5100 ,I got a correct correct result without runing winshask.

The code as below:
Code:
/*
This sketch receives humidity and temperature from sensor DHT11,
display on two digits seven-segment LED and send UDP message to remote server, prints them to the serial port.
 Created 10 Feb 2013, by Chen JW, superid888@gmail.com
*/

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>       // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 2, 23);
static byte ServerIp[] = {192,168,2,10};
unsigned int localPort = 1000,ServerPort=1001;      // set local port and server port

// buffers for receiving and sending data
char packetBuffer[100]; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;


// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// dp,G,F,E,D,C,B,A
const int segmentPins[] = { 9,8,7,6,5,4,3,2};
const int nbrDigits= 2; // the number of digits in the LED display
//dig 1 2
const int digitPins[nbrDigits] = { A4,A5};
int dppin=A3; //set the decimal point link to PIN A3

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN A0 //DHT11 PIN 3 连接UNO A0

char string1[2];//set char to humidity
char string2[2];//set char to temperature
 
void setup()
{
  Serial.begin(19200);
  

  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
    
for(int i=0; i < 8; i++)
pinMode(segmentPins[i], OUTPUT); // set segment pins to output
for(int i=0; i < nbrDigits; i++)
pinMode(digitPins[i], OUTPUT);

}
 
void loop()
{
  
  int chk = DHT11.read(DHT11PIN);
 
  Serial.print("Read sensor: ");
  switch (chk)
  {
   case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
  }

int humd=int(DHT11.humidity);
int temp=int(DHT11.temperature-2);

itoa(humd,string1,10);
itoa(temp,string2,10);

    // send UDP packet
    Udp.beginPacket(ServerIp, ServerPort);
    Udp.write((char*) string1);
    Udp.endPacket();
    
Serial.println("UDP DATA send !");
Serial.println(humd);
Serial.println(temp);

 

//Display on 7segment
  int counter1=200;
while(counter1)
 {
    counter1--;
//    showNumber(DHT11.humidity);
showNumber(humd);
   }
    counter1=200;
while(counter1)
 {
    counter1--;
    analogWrite(dppin,0);
//    showNumber(DHT11.temperature-2);
showNumber(temp);
    analogWrite(dppin,200);
   }
}


void showNumber( int number)
{
if(number == 0)
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
else
{
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--)
{
if(number > 0)
{
showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int segnumber, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment <8; segment++)
{
boolean isBitSet = bitRead(numeral[segnumber], segment);
// isBitSet will be true if given bit is 1

//isBitSet = ! isBitSet; // remove this line if common cathode display

digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}
« Last Edit: March 01, 2013, 05:12:31 am by superid888 » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you notice the w5100 code starts UDP listening on a port. I did not see that in the ENC28J60 code. There must be a similar function.
Code:
  Udp.begin(localPort);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes,I think the code for UDP in ethercard maybe short of  something for bind sorport to  disport,but I don't know how to modify the library.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 I don't know how to use ether.udpTransmit() for Transmit udp data,can someone give me a example?
Thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I try another Library for ENC28J60 today,everything is OK and the .HEX file is smaller.

/*
This sketch receives humidity and temperature from sensor DHT11,
display on two digits seven-segment LED and send UDP message to remote server, prints them to the serial port.
The code can only compily by IDE 002X
Created 10 Feb 2013, by Chen JW. superid888@gmail.com
*/

#include "EtherShield.h"
static uint8_t mymac[6] = { 0x54,0x55,0x58,0x10,0x00,0x25};
static uint8_t myip[4] = { 192,168,2,23};
static uint8_t broadcastip[4] = { 192,168,2,255};
// DestPort 1001, SrcPort 1000
#define DEST_PORT_L  0xE9
#define DEST_PORT_H  0x03
#define SRC_PORT_L  0xE8
#define SRC_PORT_H  0x03
const char iphdr[] PROGMEM ={ 0x45,0,0,0x82,0,0,0x40,0,0x20}; // 0x82 is the total

struct UDPPayload {
uint8_t data[2];
};
UDPPayload udpPayload;

// Packet buffer, must be big enough to packet and payload
#define BUFFER_SIZE 150
static uint8_t buf[BUFFER_SIZE+1];

EtherShield es=EtherShield();

// bits representing segments A through G (and decimal point) for numerals 0-9
const int numeral[10] = {
//ABCDEFG /dp
B11111100, // 0
B01100000, // 1
B11011010, // 2
B11110010, // 3
B01100110, // 4
B10110110, // 5
B10111110, // 6
B11100000, // 7
B11111110, // 8
B11110110, // 9
};
// pins for decimal point and each segment
// dp,G,F,E,D,C,B,A
const int segmentPins[] = { 9,8,7,6,5,4,3,2};
const int nbrDigits= 2; // the number of digits in the LED display
//dig 1 2
const int digitPins[nbrDigits] = { A4,A5};
int dppin=A3; //set the decimal point link to PIN A3

#include <dht11.h>
dht11 DHT11;
#define DHT11PIN A0 //DHT11 PIN 3 == UNO A0

char string1[2];//Humd
char string2[2];//Temp

void setup(){

  Serial.begin(19200);

  es.ES_enc28j60Init(mymac);
  //init the ethernet/ip layer:
  es.ES_init_ip_arp_udp_tcp(mymac,myip,80);

for(int i=0; i < 8; i++)
pinMode(segmentPins, OUTPUT); // set segment and DP pins to output
for(int i=0; i < nbrDigits; i++)
pinMode(digitPins, OUTPUT);

//  delay(1000);
}

void loop(){

 int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
   case DHTLIB_OK:
                Serial.println("OK");
                break;
    case DHTLIB_ERROR_CHECKSUM:
                Serial.println("Checksum error");
                break;
    case DHTLIB_ERROR_TIMEOUT:
                Serial.println("Time out error");
                break;
    default:
                Serial.println("Unknown error");
                break;
  }

int humd=int(DHT11.humidity);
int temp=int(DHT11.temperature-2);

//itoa(humd,string1,10);
//itoa(temp,string2,10);
//byte babb = 0x45;
//string ass = ((char)babb).ToString();

Serial.println(humd);
Serial.println(temp);

udpPayload.data[0] =humd;
udpPayload.data[1] =temp;

//udpPayload.data[0] =string1;
//udpPayload.data[1] =string2;

 broadcastData() ;

//Display on 7segment
  int counter1=200;
while(counter1)
 {
    counter1--;
//    showNumber(DHT11.humidity);
showNumber(humd);
   }
    counter1=200;
while(counter1)
 {
    counter1--;
    analogWrite(dppin,0);
//    showNumber(DHT11.temperature-2);
showNumber(temp);
    analogWrite(dppin,200);
   }
}

// Broadcast the data in the udpPayload structure
void broadcastData( void ) {
  uint8_t i=0;
  uint16_t ck;
  // Setup the MAC addresses for ethernet header
  while(i<6){
    buf[ETH_DST_MAC +i]= 0xff; // Broadcsat address
    buf[ETH_SRC_MAC +i]=mymac;
    i++;
  }
  buf[ETH_TYPE_H_P] = ETHTYPE_IP_H_V;
  buf[ETH_TYPE_L_P] = ETHTYPE_IP_L_V;
  es.ES_fill_buf_p(&buf[IP_P],9,iphdr);

  // IP Header
  buf[IP_TOTLEN_L_P]=28+sizeof(UDPPayload);
  buf[IP_PROTO_P]=IP_PROTO_UDP_V;
  i=0;
  while(i<4){
    buf[IP_DST_P+i]=broadcastip;
    buf[IP_SRC_P+i]=myip;
    i++;
  }
  es.ES_fill_ip_hdr_checksum(buf);
  buf[UDP_DST_PORT_H_P]=DEST_PORT_H;
  buf[UDP_DST_PORT_L_P]=DEST_PORT_L;
  buf[UDP_SRC_PORT_H_P]=SRC_PORT_H;
  buf[UDP_SRC_PORT_L_P]=SRC_PORT_L; // lower 8 bit of src port
  buf[UDP_LEN_H_P]=0;
  buf[UDP_LEN_L_P]=8+sizeof(UDPPayload); // fixed len
  // zero the checksum
  buf[UDP_CHECKSUM_H_P]=0;
  buf[UDP_CHECKSUM_L_P]=0;
  // copy the data:
  i=0;
  // most fields are zero, here we zero everything and fill later
  uint8_t* b = (uint8_t*)&udpPayload;
  while(i< sizeof( UDPPayload ) ){
    buf[UDP_DATA_P+i]=*b++;
    i++;
  }
  // Create correct checksum
  ck=es.ES_checksum(&buf[IP_SRC_P], 16 + sizeof( UDPPayload ),1);
  buf[UDP_CHECKSUM_H_P]=ck>>8;
  buf[UDP_CHECKSUM_L_P]=ck& 0xff;
  es.ES_enc28j60PacketSend(42 + sizeof( UDPPayload ), buf);
}

void showNumber( int number)
{
if(number == 0)
showDigit( 0, nbrDigits-1) ; // display 0 in the rightmost digit
else
{
// display the value corresponding to each digit
// leftmost digit is 0, rightmost is one less than the number of places
for( int digit = nbrDigits-1; digit >= 0; digit--)
{
if(number > 0)
{
showDigit( number % 10, digit) ;
number = number / 10;
}
}
}
}
// Displays given number on a 7-segment display at the given digit position
void showDigit( int segnumber, int digit)
{
digitalWrite( digitPins[digit], HIGH );
for(int segment = 1; segment <8; segment++)
{
boolean isBitSet = bitRead(numeral[segnumber], segment);
// isBitSet will be true if given bit is 1

//isBitSet = ! isBitSet; // remove this line if common cathode display

digitalWrite( segmentPins[segment], isBitSet);
}
delay(5);
digitalWrite( digitPins[digit], LOW );
}

// End
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,

the below function works fine , i am using UDP client in java to test all send and receive works fine,
check your ports may be you are making a mistake there.
ether.sendUdp(msg, sizeof msg, 1337, ipDestination, 1111);
Logged

Pages: [1]   Go Up
Jump to: