Hi all
I need some help with my programming. I'm not totally new to Arduino, but I'm new to Arduino and ethernet communication.
I am trying to communicate with a Mettler Toledo balance using ethernet, UDP. The balance is model XPR 205, and the settings on the balance is as follows:
IP: 123.123.123.123
Subnet: 255.255.255.000
Port: 8001
The arduino is an UNO R3 equipped with a Ethernet Shield 2.
My code is based on the UDPSendRecieveString exampel and is as follows:
#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[] = {
0xA8, 0x61, 0x0A, 0xAE, 0xDE, 0xF9
};
IPAddress ip(123, 123, 123, 124); // Ip address for arduino
IPAddress remoteIP(123, 123, 123, 123); // Receiver IP address
//byte gateway[] = { XXX, XXX, X, X }; //set Gateway
//byte subnet[] = { 255, 255, 255, 0 }; //set Subnetmask
unsigned int localPort = 8001; // local port to listen on
unsigned int remotePort = 8001; // Remote port to transmit on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
//char ReplyBuffer[] = "acknowledged"; // a string to send back
// buffer for sending data
char SendBuffer[] = "S\n"; // a string to send
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 10000; // interval at which to blink (milliseconds)
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
// start the Ethernet
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start UDP
Udp.begin(localPort);
}
void loop() {
// if there's data available, read a packet
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 packetBuffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
}
delay(10);
//Serial.println(remoteIP);
//Serial.println(remotePort);
// send data to the remote IP address and port
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you did something
previousMillis = currentMillis;
Udp.beginPacket(remoteIP, remotePort);
Udp.write(SendBuffer);
Udp.endPacket();
Serial.println(SendBuffer);
}
}
The code sends the S\n to the weight and i can see the Serial.println(SendBuffer); in the serial monitor, but I don't seem to get any data from the balance. I have also tried sending s, S or s\n. The ethernet port blinks every 10 seconds to indicate data is being send. I can connect to the balance by USB and send commands with a terminal program, and receive the corresponding data, with no problem.
What is my problem? Do I need to use the subnet address somehow? Is the programming correct in order to receive a string?