Here is the code for the RPi "server". Compile and run with
Code: Select all
cc udpbasic.c -o udpbasic
./udpbasic
Code: Select all
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
int main(int argc, char *argv[]) {
if(argc < 2) {
// if(daemon(0,0) == -1) err(1,NULL);
}
int sock, n, nr;
socklen_t fromlen;
struct sockaddr_in server;
struct sockaddr_in from;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
printf("Can not create socket in server\n");
memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET;
server.sin_port = htons(5005);
server.sin_addr.s_addr = INADDR_ANY;
if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
printf("Can not bind in server!\n");
memset(&from, 0, sizeof(struct sockaddr_in));
fromlen = sizeof(struct sockaddr_in);
while(1) {
int n, l1;
unsigned char tBuf[300];
unsigned char outBuf[300];
// fflush(stdout);
n = recvfrom(sock, tBuf, 300, 0, (struct sockaddr*) &from, &fromlen);
if (n < 0) {
printf("Can not receive in server!\n");
}
printf("%s from IP:%s, Port:%hu\r\n",&tBuf[0],inet_ntoa(from.sin_addr), ntohs(from.sin_port));
strcpy(outBuf,"127 127 0 0\n\0");
socklen_t length = sizeof(struct sockaddr_in);
n = sendto(sock, tBuf, 64, 0, (const struct sockaddr *)&from, fromlen);
if(n < 0) {
printf("Can not send from client");
}
}
}
Here is the code for the Arduino "clients". Change the mac address and IP for each client. No duplicates of either. Change the remoteIP to the IP address of the RPi "server".
Code: Select all
#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, 1, 4);
IPAddress remoteIP(192,168,1,3);
unsigned int localPort = 8888; // local port to listen on
unsigned int remotePort = 5005;
unsigned int loopCount = 0;
unsigned long currentTime;
unsigned long lastTime;
// buffers for receiving and sending data
byte packetBuffer[48]; //buffer to hold incoming packet,
byte ReplyBuffer[48];
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
Serial.begin(115200);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
currentTime = millis();
lastTime = currentTime;
}
void loop() {
currentTime = millis();
if(currentTime - lastTime > 5000) {
lastTime = currentTime;
// send a reply, to the IP address and port that sent us the packet we received
ReplyBuffer[0] = 0x12;
ReplyBuffer[1] = 0x34;
ReplyBuffer[2] = 0x56;
ReplyBuffer[3] = 0x78;
sprintf((char*)&ReplyBuffer[4],"Arduino count: %u",loopCount);
Udp.beginPacket(remoteIP, remotePort);
Udp.write(ReplyBuffer,48);
// Udp.print(loopCount,DEC);
if(Udp.endPacket()) Serial.println("OK") ;
loopCount++;
}
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,48);
Serial.println("Contents:");
Serial.print(packetBuffer[0],DEC);
Serial.print(" ");
Serial.print(packetBuffer[1],DEC);
Serial.print(" ");
Serial.print(packetBuffer[2],DEC);
Serial.print(" ");
Serial.println(packetBuffer[3],DEC);
unsigned long myEpoch = 0UL;
myEpoch = packetBuffer[0];
myEpoch = myEpoch << 8;
myEpoch = (myEpoch | packetBuffer[1]) << 8;
myEpoch = (myEpoch | packetBuffer[2]) << 8;
myEpoch = myEpoch | packetBuffer[3];
Serial.println(myEpoch);
Serial.println((char*)&packetBuffer[4]);
}
}