Again my equipment is:
- Pollin AVR NETIO Board
- Arduino 1.0.3
- avr-netino (Project)
Patch tcpip.cpp
To enable UDP receiving a patch of the tcpip.cpp file is needed. The patched file can be found here. You need to overwrite the original file libraries/EtherCard/tcpip.cpp with the patched one.Arduino program
Arduino program to receive UDP packets:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Environment: | |
// * Pollin AVR NETIO board, | |
// * Arduino 1.0.3, | |
// * avr-netino (https://code.google.com/p/avr-netino/) | |
// | |
// This is a demo of receiving udp packets | |
// | |
// 2012-12-22 <Thomas.Mohaupt@gmail.com> http://creativecommons.org/licenses/by-sa/3.0/ | |
// Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) | |
// | |
// Example to send udp packets to board: https://gist.github.com/4359006 | |
// | |
// Note: you need to specify the gateway | |
#include <EtherCard.h> | |
static unsigned int NETIO_CSPIN_ENC28J60 = 28; | |
byte macMy[] = {0x00, 0x22, 0xF9, 0x01, 0x30, 0xDA }; | |
byte ipMy[] = {10, 0, 0, 52}; | |
byte ipGateway[] = {10, 0, 0, 254}; | |
word portMy = 7777; | |
byte Ethernet::buffer[220]; | |
void setup() { | |
ether.begin(sizeof Ethernet::buffer, macMy, NETIO_CSPIN_ENC28J60); | |
ether.hisport = portMy; | |
ether.staticSetup(ipMy, ipGateway); | |
while (ether.clientWaitingGw()) | |
ether.packetLoop(ether.packetReceive()); | |
Serial.begin(9600); | |
Serial.println("finish setup"); | |
} | |
void loop() { | |
word len = ether.packetReceive(); | |
word pos = ether.packetLoop(len); | |
if (pos) { | |
Serial.println((const char *) ðer.buffer[pos]); | |
} | |
} |
Example script to send UDP
Here is a small example how to send UDP packets to avr-netio board:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import socket | |
# Set the socket parameters | |
# avr-netio ip address | |
host = "10.0.0.52" | |
# avr-netio port | |
port = 7777 | |
addr = (host,port) | |
# Create socket | |
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) | |
def_msg = "===Enter message to send ==="; | |
print "\n",def_msg | |
# Send messages | |
while (1): | |
data = raw_input('>> ') | |
if not data: | |
break | |
else: | |
if(UDPSock.sendto(data,addr)): | |
print "Sending message '",data,"'....." | |
# Close socket | |
UDPSock.close() |