Go Down

Topic: Send values from MAX to Arduino via Ethernet Shield (Read 286 times) previous topic - next topic

carinola

Hello, I try to control a simple buzzer from Max to arduino (Mega) via an ethernet shield. It does not work, something is wrong in this sketch, several things certainly. 

I'm using in Max the object <udpsend 10.0.0.177 8888> to send values between 0 and 255.

Please, have you an advice ? Thanks !
Code: [Select]

#include <Ethernet.h>
#include <SPI.h>
#include <EthernetUdp.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

unsigned int localPort = 8888;      // local port to listen on
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

byte freq1 = 0;

void setup() {
  // start the Ethernet
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
}

void loop() {
  int packetSize = Udp.available();
  if (packetSize) {
    freq1 = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    tone(7, map (freq1, 1, 255, 1000, 20000));
  }
}

carinola

I corrected and simplified the previous code. It only works if the shield is connected to my internet box. But I would like to control the card directly from the computer. Do you know if this is possible? The shield model is the W5100, I tried with a crossover cable. I am working with Mac OSX Sierra.
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 177);

unsigned int localPort = 8888;      // local port to listen on
byte packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

//byte freq1 = 0;

void setup() {
  // start the Ethernet
  Serial.begin(115200);
  Serial.println("setup");
  Ethernet.begin(mac, ip);
  // print your local IP address:
  Serial.println(Ethernet.localIP());
  Udp.begin(localPort);
}

void loop() {
  int packetSize = Udp.parsePacket();

  if (packetSize > 0) {
    Serial.println("=====================");
    Serial.print(packetSize);
    Serial.println("===========");
    int res = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    for (int i = 0; i < packetSize; i++) {
      Serial.println(packetBuffer[i]);
    }
    //tone(7, map ( packetBuffer[0], 1, 255, 1000, 20000));
  }
}

Go Up