Arduino EtherCard UDP: constant repeats

951 Views Asked by At

TL;DR Arduino sends UDP twice, but it appears as it is sending it non-stop on the receiving end.

I have a wireless doorbell connected to Arduino. This is the code I'm using to send a UDP packet using ENC28J60 and EtherCard from Arduino:

#include "Network.h"
#include <HardwareSerial.h>
#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte udp_broadcast[] = {0xFF, 0xFF, 0xFF, 0xFF};
static const char KEEP_ALIVE[] = "keep-alive";

byte Ethernet::buffer[700];

Network::Network(unsigned short udp_port)
    : ethernet_initialised_(false) {
  this->udp_port_ = udp_port;
  Serial.println("Starting Network class on port " + udp_port);

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {
     Serial.println(F("Failed to access Ethernet controller"));
     return;
  }

  if (!ether.dhcpSetup()) {
    Serial.println(F("DHCP failed"));
    return;
  }

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  Serial.println("Initialization completed");

  ethernet_initialised_ = true;
}

Network::~Network() {

}

void Network::broadcast(const char* message) {
  if (ethernet_initialised_) {
    ether.sendUdp(
        message,
        strlen(message),
        udp_port_,
        udp_broadcast,
        udp_port_ + 1);
  }
}

The calling code actually sends two UDP packets (TL;DR the way measuring code works). Basically, like this:

void loop(){
  float val = analogRead(inputPin);
  if (val > 5.0f) {
    Serial.println("");
    Serial.println(val);
    network->broadcast("HELLO");
    delay(5000);
  }
}

This is how I read it on the client:

  while True:
    sock = socket.socket(socket.AF_INET, # Internet
                         socket.SOCK_DGRAM) # UDP
    sock.bind((UDP_IP, port))
    print "Waiting for data..."
    data, addr = sock.recvfrom(1024) 

    callback(addr, data)

So what I completely fail to understand is: why after triggering the doorbell my calling code constantly prints out

Doorbell rang:  HELLO ('192.168.0.2', 31715)
Waiting for data...
Doorbell rang:  HELLO ('192.168.0.2', 31715)
Waiting for data...
Doorbell rang:  HELLO ('192.168.0.2', 31715)
Waiting for data...
Doorbell rang:  HELLO ('192.168.0.2', 31715)
Waiting for data...
...
...

as if Arduino is sending UDP packets (and I know for a fact it isn't). Looks like I'm missing something silly and can't quite see what's going on.

1

There are 1 best solutions below

0
On

So the correct answer is ....

well, it has nothing to do with the programming. On the other hand, it has a lot to do with electronics - the voltage of the source I was measuring was different when Arduino (and the circuit) were powered by USB and when they were powered by a normal power supply. Solution was: adding a pull-up resistor to compensate for this and using different branches of code for USB and "production" mode. In addition, doing development when connected by USB and using power supply is also a thing and works reasonably well.