Hi,
I've been trying to send a simple packet from an Arduino to a Raspberry pi. I got it working with a struct containing 2 chars, but when it's a char and an int/float I only get the first part (See the output at the bottom).
The client sends the string 'temp' to the server which creates a packet with a char value of 'A', and a float value of '3'. Then I memcpy the packet in to a buffer, and send bytes equal to the size of the packet. On the client side I memcpy the buffer back to a packet and print it to the screen.
Server side - Arduino
#include <Ethernet.h> //Load Ethernet Library
#include <EthernetUdp.h> //Load UDP Library
#include <SPI.h> //Load the SPI Library
byte mac[] = { 0x00, 0xAC, 0xBF, 0xCC, 0x02, 0x08 }; //Assign a mac address
IPAddress ip(192, 168, 1, 177); //Assign my IP adress
unsigned int localPort = 5000; //Assign a Port to talk over
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String datReq; //String for our data
int packetSize; //Size of Packet
EthernetUDP Udp; //Define UDP Object
//TMP36 Pin Variables
int sensorPin = 0;
struct Packet {
char val;
float temp;
};
void setup() {
Serial.begin(9600); //Turn on Serial Port
Ethernet.begin(mac, ip); //Initialize Ethernet
Udp.begin(localPort); //Initialize Udp
delay(1500); //delay
}
void loop() {
packetSize = Udp.parsePacket(); //Read the packetSize
if (packetSize>0) { //Check to see if a request is present
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); //Reading the data request on the Udp
String datReq(packetBuffer); //Convert packetBuffer array to string datReq
//Serial.println(datReq);
if (datReq == "temp") { //See if Red was requested
Packet pkt;
pkt.val = 'A';
pkt.temp = 3;
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); //Initialize Packet send
char txBuffer[128] = { 0 };
memcpy(txBuffer, &pkt, sizeof(Packet));
Udp.write(txBuffer, sizeof(Packet));
Udp.endPacket(); //Packet has been sent
}
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
}
}
Client side - Raspberry pi
//See Description.txt
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <iostream>
using std::cout;
using std::endl;
struct Packet{
char val;
float temp;
};
int main(void)
{
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
cout << "socket failed" << endl;
return 1;
}
struct sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(5000);
serveraddr.sin_addr.s_addr = inet_addr("192.168.0.25");
for(int i = 0; i < 5; i++){
Packet pkt;
pkt.temp = 0;
sendto(fd, "temp", 4, 0, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
char rxBuffer[128] = {0};
socklen_t addr_len = sizeof(serveraddr);
recvfrom(fd, rxBuffer, sizeof(struct Packet), 0, (sockaddr *)&serveraddr, &addr_len);
memcpy(&pkt, rxBuffer, sizeof(struct Packet));
cout << pkt.val << " : " << pkt.temp << endl;
}
close(fd);
}
Output:
A : 9.24857e-44
A : 9.24857e-44
A : 9.24857e-44
A : 9.24857e-44
A : 9.24857e-44
Can someone point out the problem here please?