1
votes

I'm trying to send sensor data via UDP. At the moment I'm struggling with the "packing" of the UDP packets. It says "incomingData" is not declared when I try to send it. I would appreciate any kind of advice. Code below. Thank you :)

//Version 1.012

//necessary libraries
#include <SPI.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>

//Pin settings
#define CTD 19

//Network Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xEC, 0xAB };  //set MAC Address Ethernet Shield (Backside)
byte ip[]  = { XXX, XXX, X, X };                      //set IP-Address
byte gateway[] = { XXX, XXX, X, X };                  //set Gateway
byte subnet[]  = { 255, 255, 255, 1 };                //set Subnetmask


//local UDP port to listen on
unsigned int localPort = 4000;

//Recipient IP
IPAddress RecipientIP(127, 0, 0, 1);

//Recipient UDP port
unsigned int RecipientPort = 4444;

//Buffer for sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

//EthernetUDP instance
EthernetUDP Udp;



void setup()
{
   //Start Ethernet
  Ethernet.begin(mac, ip);

  //Start UDP
  Udp.begin(localPort);

  //for debug only
  Serial.begin(9600);

  //Serial baud rate for CTD
  Serial1.begin(1200);

  //Version 1.012
Serial.print("Version 1.012");

  //CTD
  pinMode(CTD, INPUT);
}

void loop()
{

//If CTD is sending
if (Serial1.available())
{
  //read incoming data
  int incomingData = Serial1.read();

  //for debug only
  Serial.print("Data: ");
  Serial.println(incomingData, BIN);
}

//Send UDP packets
int packetSize = Udp.parsePacket();
  if (packetSize) {

    // read the packet into packetBufffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

    // send to the IP address and port
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(incomingData);
    Udp.endPacket();
  }
}
1
I think the if (packetSize) is exexuted first before if (Serial1.available()) when you try to send data and thus incomingData is not getting declared but you are trying to use it inside if (packetSize).Thus you get the error.Mathews Sunny

1 Answers

1
votes

In your code you have declared incomingData as int inside void loop () function inside if (Serial1.available()) .

But if the above if loop fails the incomingData will not be declared and say packetsize is greater than zero ( packet available) then if (packetSize) segment will be executed.Thus incomingData is not declared but it is used .That's why you get the error you stated.