0

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 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, 177);
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("hello");
    Udp.write("Current humidity");
    Udp.endPacket();

         Serial.println("Test");

    delay(2000);//Wait 2 seconds before accessing sensor again.

}

This is my UDPRead

#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, 177);

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);
  Udp.begin(localPort);
  Serial.print(Ethernet.localIP());
}

void loop() {

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
      Serial.println("Test");
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("Test");
      }
    }
    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);
}
}

This is what Wirehsark shows: enter image description here

eceived.

The Arduinos are connected to my Switch Port5 and Port6 which are mirrored to Port8 to which I am connected via Notebook.

After a few tests, I do not seem to get into my

int packetSize = Udp.parsePacket();

if(packetSize) {

statement for a reason I cannot yet grasp. Udp.parsePacket should receive the UDP package from 192.168.1.178

I can Ping the .178 but not the .177. I changed cables, ports, IPs, Switch and even the Arduinos. So I would think that the problem is code based.

Thank you very much for your help.

  • not the cause, but: 1) you can print the IPAddress with Serial.print(ip). 2) why do you have Udp.begin() on reader. it starts listening – Juraj May 18 at 9:05
  • if you can't ping the board, the shield is not connected to the network. run some of the example sketches of the Ethernet 2.00 library. for example the WebClient example – Juraj May 18 at 9:09
  • Change local port to 8080, other ports are not generally available for local network communication. – Vaibhav May 18 at 9:24
  • @Vaibhav - Well I'll be damned, it works now. I assure you, it has worked before with the ports I have assigned and I even switched ports. But 8080 works. Thank you very much – SKR11Twelve May 18 at 9:33
  • what could block the port? the router? between two local devices? I don't believe it – Juraj May 18 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.