Hey there,
I started a project where I wanted to send sensor data from one arduino to another and let this 2nd Arduino send the data to a tablet to visualize sensordata like speed, temperature etc.
I wanted to send this in intervalls so UDP would be my way to go. I used the sketches which were provided from the Arduino software which worked well for a while. But after relocating to a new place it doesn't seem to work and I do not know why - I readjusted IP's, Ports and resetted my router.
This is my code for the UDPWrite:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "dht.h"
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
// 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, 178);
IPAddress IP_Remote(192, 168, 1, 179);
IPAddress Port_Remote(8887);
unsigned int localPort = 8887; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Serial.begin(9600);
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.println(Ethernet.localIP());
}
void loop() {
DHT.read11(dht_apin);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write("Current humidity");
Udp.endPacket();
Serial.println("Test");
delay(2000);//Wait 2 seconds before accessing sensor again.
}
And this for the UDP Receiver:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// 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, 179);
unsigned int localPort = 8887; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup() {
// start the Ethernet and UDP:
Serial.begin(9600);
Ethernet.begin(mac,ip);
delay(500);
Udp.begin(localPort);
}
void loop() {
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,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}
The package do seem to get sent but neither in Wireshark nor in my serial monitor is there any trace of a package received.
What do I do wrong?
Thank you very much