Clueless when attempting to outsource Some of my Code into Library

Hey there,
Im a Newbie, tackeling a Projekt (thats much to big for my Skill level ^^). which is planned to be split up into serveral Classes for better overview. Im using an NodeMCU ESP8266 with no external Circuit attached. My Projekt is to build a Clock, using a UDP to get Time information from the NTP Pool and Displaying everything on some MAX7219 driven LED Displays. Ive checked Everything indiviualy and now i want to merge some Subsketches. I was starting by Splitting the following (and working) Code into a Simplyfied Skeleton and an Library containing all necessary routines. With the plan to implement the other Sketches later on.

The Following Code contains a Sketch which can contact a Server from the Europe NTP Pool, is listening for the Reply and then dumping the package Content back through Serial to me.

// orientiert an: https://siytek.com/esp8266-udp-send-receive/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

// Set WiFi credentials
#define WIFI_SSID ""
#define WIFI_PASS ""
#define BotherNTPServer 0 // 1 for true

WiFiUDP UDP;
const byte NTP_Package[48] = {0xE3, 0, 6, 0xEC, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0x4E, 49, 52};

void setup() {
  // Setup serial port
  Serial.begin(9600);
  delay(1000);

  // Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  printf("Connecting to %s\n", WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED) { // Loop continuously while WiFi is not connected
    delay(100);
    printf(".");
  }

  // Connected to WiFi
  uint32_t localip = WiFi.localIP();
  printf("\nConnected! IP address: %u.%u.%u.%u\n", localip & 0xFF, (localip >> 8) & 0xFF, (localip >> 16) & 0xFF, (localip >> 24) & 0xFF);

  // Begin listening to UDP port
  UDP.begin(123);
  printf("Listening on UDP port %i\n", 123);

  //Sending NTP_Package
#if (BotherNTPServer == 1)
  printf("Sending NTP Package\n");
  UDP.beginPacket("europe.pool.ntp.org", 123);
  UDP.write(NTP_Package, 48);
  UDP.endPacket();
  printf("NTP Packet Send\n");
#else
  printf("Not Bothering NTP Server\n");
#endif

  printf("Setup Complete\n");
}

void loop() {
  int packetSize = UDP.parsePacket();
  if (packetSize) {
    uint32_t remoteip = UDP.remoteIP();
    unsigned char* buffer;
    printf("Received Packet!: Size: %i\tIP: %u.%u.%u.%u\tPORT: %i\n", packetSize, remoteip & 0xFF, (remoteip >> 8) & 0xFF, (remoteip >> 16) & 0xFF, (remoteip >> 24) & 0xFF, UDP.remotePort());
    int len = UDP.read(buffer, 127);
    if (len > 0) {
      buffer[len] = '\0';
    }
    printf("Packet received at:\t%s\n", String(millis()));
    for (int i = 0; i < packetSize; i++) {
      printf("%x ", buffer[i]);
    }
    printf("\n");
  }
}

There is a Word of Warning: Ive Uploaded the Sketch today and for some reason it wont show the content of the received Packages. Every thing else works just fine!. By moving a printf line around i narrowed the Error down to ln. 55 int len = UDP.read(buffer, 127)
this isnt my main problem but if you have any idea i would be glad to hear about it!

So i splitted everything up into the following Files:

Class_Test.ino

// orientiert an: https://siytek.com/esp8266-udp-send-receive/
#include <ESP8266WiFi.h>
#include "networkUDP.h"

// Set WiFi credentials
#define WIFI_SSID ""
#define WIFI_PASS ""
#define NTPaddress "europe.pool.ntp.org"

#define BotherNTPServer 0 // ==1 for yes

const byte NTP_initPackage[48] = {0xE3, 0, 6, 0xEC, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0x4E, 49, 52};


networkUDP netUDP();

void setup() {
  // Setup serial port
  Serial.begin(9600);

  // Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  printf("Connecting to %s\n", WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED) { // Loop continuously while WiFi is not connected
    delay(100);
    printf(".");
  }

  // Connected to WiFi
  uint32_t localip = WiFi.localIP();
  printf("\nConnected! IP address: %u.%u.%u.%u\n", localip & 0xFF, (localip >> 8) & 0xFF, (localip >> 16) & 0xFF, (localip >> 24) & 0xFF);

  //begin UDP listening and send init message
  netUDP.UDPinit();
#if (BotherNTPServer == 1)
  netUDP.sendNTPinitMessage(NTPaddress, NTP_initPackage*);
#endif

  printf("Setup Complete\n");
}

void loop() {
  netUDP.processIncomingPackets();
}

networkUDP.cpp

#include "Arduino.h"
#include "networkUDP.h"


networkUDP::networkUDP() {
  // constructor
  WiFiUDP UDP;
}


void networkUDP::UDPinit() {
  // Begin listening to UDP port
  UDP.begin(123);
  printf("Listening on UDP port %i\n", 123);
}

void networkUDP::sendNTPinitMessage(char _addr, char* _NTP_Package) {
  //Sending NTP_Package
  printf("Sending NTP Package\n");
  UDP.beginPacket(_addr, 123);
  UDP.write(_NTP_Package, 48);
  UDP.endPacket();
  printf("NTP Packet Send\n");
}

void networkUDP::processIncomingPackets() {
  int packetSize = UDP.parsePacket();
  if (packetSize) {
    uint32_t remoteip = UDP.remoteIP();
    unsigned char* buffer;
    printf("Received Packet!: Size: %i\tIP: %u.%u.%u.%u\tPORT: %i\n", packetSize, remoteip & 0xFF, (remoteip >> 8) & 0xFF, (remoteip >> 16) & 0xFF, (remoteip >> 24) & 0xFF, UDP.remotePort());
    int len = UDP.read(buffer, 127);
    if (len > 0) {
      buffer[len] = '\0';
    }

    printf("Packet received at:\t%s\n", String(millis()));
    for (int i = 0; i < packetSize; i++) {
      printf("%x ", buffer[i]);
    }
    printf("\n");
  }
}

networkUDP.h

#ifndef netUDP
#define netUDP
#include <WiFiUdp.h>

#if (ARDUINO >=100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class networkUDP {
  public:
    // constructor
    networkUDP();

    // Methods
    void UDPinit();
    void sendNTPinitMessage(char _addr, char* _NTP_Package);
    void processIncomingPackets();

    WiFiUDP UDP;
  private:

};
#endif

When i Compile, this is the Error Message:

/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp: In member function 'void networkUDP::processIncomingPackets()':
/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp:37:12: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'String' [-Wformat=]
   37 |     printf("Packet received at:\t%s\n", String(millis()));
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~
      |                                         |
      |                                         String
/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp:32:23: warning: 'buffer' may be used uninitialized in this function [-Wmaybe-uninitialized]
   32 |     int len = UDP.read(buffer, 127);
      |               ~~~~~~~~^~~~~~~~~~~~~
Class-Test_UDP:15:20: error: expected constructor, destructor, or type conversion before ';' token
   15 | networkUDP netUDP();
      |                    ^
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void setup()':
Class-Test_UDP:35:9: error: expected primary-expression before '.' token
   35 |   netUDP.UDPinit();
      |         ^
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void loop()':
Class-Test_UDP:43:10: error: expected primary-expression before '.' token
   43 |    netUDP.processIncomingPackets();
      |          ^
Bibliothek ESP8266WiFi in Version 1.0 im Ordner: /home/janosch/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi  wird verwendet
exit status 1
expected constructor, destructor, or type conversion before ';' token

I think something is wrong with contructor. But i Cant figure out what.. :frowning:
Also the Array containing the Package, send to the NTP Server is suppose to be in the Contructor too. This is also not working.. I solved it by making the array a global constant and parsing it as an argument. Which i thought was not ideal.. If you have any suggestion regarding more simplistic code, your very welcome to share, but again this is not the focus of my question ^^

Thank you so much in advance! for any help, or just reading through this block of medium grade English ^^

yours Janosch

 printf("Packet received at:\t%s\n", String(millis()));

Where do you expect printf() to output to ? Which Arduino board are you using ?

In any case, as the error message tells you, the %s format parameter works with char * variables and not Strings (uppercase S). Why are you casting millis() to a String in any case ?

1 Like

Hello

unsigned char* buffer;
int len = UDP.read(buffer, 127);

Don't you see what is wrong here ? If not, you must read about pointers.

1 Like

Loose the () if you want to define an object and not declare a function that returns an object.

That's it???? Why so complicated? I'll try and share mine:

// 2010-09-01 change brightness for 6 digit display
// 2010-09-03 works with first lib mod
// 2021-09-06 change library to TMxx, with 6 digit display and ESP32 Dev V1
// revert to 4 digit local with colon

#include <Arduino.h>
#include <TM1637.h>
#include <WiFi.h>
#include "time.h"

#if !defined(LED_BUILTIN)
#define LED_BUILTIN 2
#endif

struct tm timeinfo;
struct tm localtimeinfo;

const char* ssid     = "wireless2";
const char* password = "xxxxxx";

const int displayBright = 7;
const int displayDim = 3;

// Module connection pins (Digital Pins)
//#define CLK PD8
//#define DIO PD10
#define CLK 18
#define DIO 19

TM1637 module(DIO, CLK);  // DIO=5, CLK=4

const uint8_t colonMask = 0b00000100;
char text[17];

unsigned long lastDisplayUpdate;
const unsigned long displayUpdateInterval = 100;
int attributeInterval;
int deciSeconds;
int numberOfTimeRequests = 0;

void setup()
{
  Serial.begin(115200);
  while (not Serial);
  Serial.println("4 digit display and ESP32 Dev V1");
  module.setupDisplay(true, displayBright);
  module.setDisplayToString("Find");

  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  digitalWrite(LED_BUILTIN, HIGH);     // switch (active) low LED off

  Start_WiFi(ssid, password);

  module.setDisplayToString("Got ");

  configTime(0, 0, "pool.ntp.org");
  // Set timezone to Eastern Standard Time
  setenv("TZ", "EST5EDT,M3.2.0/2,M11.1.0", 1);

  while (!getLocalTime(&timeinfo)) {
    sprintf(text, "r=%2u", numberOfTimeRequests);
    module.setDisplayToString(text, 0);
    numberOfTimeRequests++;
  }

  lastDisplayUpdate = millis();
}

void loop()
{
  if (millis() - lastDisplayUpdate >= displayUpdateInterval) {
    lastDisplayUpdate += displayUpdateInterval;

    time_t tnow = time(nullptr);
    gmtime_r(&tnow, &timeinfo );
    localtime_r(&tnow, &localtimeinfo );

    deciSeconds++;
    if (deciSeconds > 19) {
      deciSeconds = 0;
    }
    attributeInterval++;

    if (attributeInterval > 99) {
      attributeInterval = 0;

      if (localtimeinfo.tm_hour >= 7 and localtimeinfo.tm_hour <= 22) {
        module.setupDisplay(true, displayBright);
      }
      else {
        module.setupDisplay(true, displayDim);
      }
    }

    sprintf(text, "%2u%2.2u", hourFormat12(localtimeinfo.tm_hour), localtimeinfo.tm_min);

    if ((tnow & 1) and timeinfo.tm_year != 0) {
      module.setDisplayToString(text, colonMask);
      //      digitalWrite(LED_BUILTIN, LOW);     // switch (active) low LED off
    }
    else
    {
      module.setDisplayToString(text, 0);
      //      digitalWrite(LED_BUILTIN, HIGH);     // switch (active) low LED off
    }
  }
  yield();
}

uint8_t hourFormat12(uint8_t t) { // the hour for the given time in 12 hour format
  if ( t == 0 )
    return 12; // 12 midnight
  else if ( t  > 12)
    return t - 12;
  else
    return t;
}
int Start_WiFi(const char* ssid, const char* password) {
  int connAttempts = 0;
  Serial.print("\r\nConnecting to: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED ) {
    delay(100);
    sprintf(text, "C=%2u", connAttempts);
    module.setDisplayToString(text, 0);

    //    Serial.print(".");
    if (connAttempts > 20) return -5;
    connAttempts++;
  }
  return 1;
}

Disclaimer: it's for ESP32 and TM1637, not ESP8266/MAX7219. Also a personal program not intended for publication really...

The esp8266/32 has built-in ntp functionality so a simple clock can have a core of a few lines of code. The function configTime() sets it all up. Here is an example:

For general guidance about converting a large, amorphous lump of code into separate .cpp/.h files, you could start here: https://forum.arduino.cc/t/organising-code-for-a-large-scale-modular-project/591328/5 by @gfvalvo

1 Like

Hello, Thank you all for your answers!

I use printf as a convenient way to Format my Serial output.
I use an ESP8266.

I think this is a remainder of trying to fix the Bug. Ive now change it to %lu and millis()

Thank you i will do it. is it required to have a pointer in this place, pointing at your buffer array? I think its kind of odd that this has worked and now kind of decided to not work at all.

Thanks for the answer. I tried this and now i get this Error:

/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp: In member function 'void networkUDP::processIncomingPackets()':
/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp:32:23: warning: 'buffer' may be used uninitialized in this function [-Wmaybe-uninitialized]
   32 |     int len = UDP.read(buffer, 127);
      |               ~~~~~~~~^~~~~~~~~~~~~
Class-Test_UDP:15:1: error: declaration does not declare anything [-fpermissive]
   15 | networkUDP netUDP;
      | ^~~~~~~~~~
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void setup()':
Class-Test_UDP:35:9: error: expected primary-expression before '.' token
   35 |   netUDP.UDPinit();
      |         ^
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void loop()':
Class-Test_UDP:44:9: error: expected primary-expression before '.' token
   44 |   netUDP.processIncomingPackets();
      |         ^
Bibliothek ESP8266WiFi in Version 1.0 im Ordner: /home/janosch/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi  wird verwendet
exit status 1
declaration does not declare anything [-fpermissive]

Im confused.. isnt this the exact same way i initialized the WiFiUDP Object?

@6v6gt, @anon57585045 Im very grateful for your answer! Ive looked before at some of the NTP librarys before and decided to not implement them because of the lack of implemation of some functionalities like accessing the sub-second part of the timestamp and having a conversation (i think this is the right word) with the server. To calculate the roundtrip time and average time error.

I would like to have a look at the build in ESP8266/32 time functionality to check if this is sufficient. Do you have any more resources regarding this topic?

Espressif web site. They actually have pretty good documentation. I'm not sure but I think I remember seeing something about sub-second support in there somewhere...

1 Like

The constructor should initialize the class components.

You create a WiFiUDP object in local scope, that gets destroyed directly,
it may even be removed by the compiler.

networkUDP::networkUDP() : UDP() {}

Is probably closer to your intention.

If it worked before, it was only by luck :slight_smile:

Your pointer points to nothing, it must point to a valid memory place, for example if you have a global variable unsigned char myBuffer[128];, you can then point to this variable with unsigned char * buffer = myBuffer;.

Or simply don't use a pointer

unsigned char buffer[128];
int len = UDP.read(buffer, 127);

The system time is automatically updated so you should then be able to use functions like gettimeofday() to get a timestamp to the nearest microsecond. That is at least the theory.
If you want to update an RTC, say in case the internet connection fails,, you need to schedule an update for the next second rollover. This is because most (all?) RTCs work to a granularity of 1 second for setting.

Hey there,
So i tried some things:

First of all i tried what @Whandall suggested:

But i get basically the same error.. I actually dont really understand what this line does. Do you or someone else have an explaination what this does? :sweat_smile:

Class-Test_UDP.ino

// orientiert an: https://siytek.com/esp8266-udp-send-receive/
#include <ESP8266WiFi.h>
#include "networkUDP.h"

// Set WiFi credentials
#define WIFI_SSID ""
#define WIFI_PASS ""
#define NTPaddress "europe.pool.ntp.org"

#define BotherNTPServer 0 // ==1 for yes

const byte NTP_initPackage[48] = {0xE3, 0, 6, 0xEC, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0x4E, 49, 52};

networkUDP netUDP;

void setup() {
  // Setup serial port
  Serial.begin(9600);

  // Begin WiFi
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  printf("Connecting to %s\n", WIFI_SSID);

  while (WiFi.status() != WL_CONNECTED) { // Loop continuously while WiFi is not connected
    delay(100);
    printf(".");
  }

  // Connected to WiFi
  uint32_t localip = WiFi.localIP();
  printf("\nConnected! IP address: %u.%u.%u.%u\n", localip & 0xFF, (localip >> 8) & 0xFF, (localip >> 16) & 0xFF, (localip >> 24) & 0xFF);

  //begin UDP listening and send init message
  netUDP.UDPinit();
#if (BotherNTPServer == 1)
  netUDP.sendNTPinitMessage(NTPaddress, NTP_initPackage*);
#endif

  printf("Setup Complete\n");
}

void loop() {
  netUDP.processIncomingPackets();
}

networkUDP.cpp

#include "Arduino.h"
#include "networkUDP.h"

networkUDP::networkUDP() : UDP() {}

void networkUDP::UDPinit() {
  // Begin listening to UDP port
  UDP.begin(123);
  printf("Listening on UDP port %i\n", 123);
}

void networkUDP::sendNTPinitMessage(char _addr, char* _NTP_Package) {
  //Sending NTP_Package
  printf("Sending NTP Package\n");
  UDP.beginPacket(_addr, 123);
  UDP.write(_NTP_Package, 48);
  UDP.endPacket();
  printf("NTP Packet Send\n");
}

void networkUDP::processIncomingPackets() {
  int packetSize = UDP.parsePacket();
  if (packetSize) {
    uint32_t remoteip = UDP.remoteIP();
    unsigned char buffer[128];
    printf("Received Packet!: Size: %i\tIP: %u.%u.%u.%u\tPORT: %i\n", packetSize, remoteip & 0xFF, (remoteip >> 8) & 0xFF, (remoteip >> 16) & 0xFF, (remoteip >> 24) & 0xFF, UDP.remotePort());
    int len = UDP.read(buffer, 127);
    if (len > 0) {
      buffer[len] = '\0';
    }

    printf("Packet received at:\t%li\n", millis());
    for (int i = 0; i < packetSize; i++) {
      printf("%x ", buffer[i]);
    }
    printf("\n");
  }
}

networkUDP.h

#ifndef netUDP
#define netUDP
#include <WiFiUdp.h>

#if (ARDUINO >=100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class networkUDP {
  public:
    // constructor
    networkUDP();

    // Methods
    void UDPinit();
    void sendNTPinitMessage(char _addr, char* _NTP_Package);
    void processIncomingPackets();

  private:
    WiFiUDP UDP;
};
#endif

This is what i got so far, but im not sure if i have to alter the .h file when using this??

I also looked at one NTP-Library again to see how they've done it. Here they initialize the WiFiUDP Object in the .ino file and parse it to the constructor, so they can use a pointer to use the Object in the Library. As i was told to look at pointers, i tried this aswell ^^

So i changed the constructor to:

networkUDP::networkUDP(UDP& udp) {
  _udp = udp;
 }

and changed UDP to _udp and made a private UDP& _udp; entry in the header file, just like in the library...

Well and this is the error i got :sweat_smile:

networkUDP.cpp:4:1: error: no declaration matches 'networkUDP::networkUDP(UDP&)'
    4 | networkUDP::networkUDP(UDP& udp) {
      | ^~~~~~~~~~
In file included from /home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp:2:
/home/janosch/Arduino/Class-Test_UDP/networkUDP.h:11:7: note: candidates are: 'constexpr networkUDP::networkUDP(networkUDP&&)'
   11 | class networkUDP {
      |       ^~~~~~~~~~
/home/janosch/Arduino/Class-Test_UDP/networkUDP.h:11:7: note:                 'constexpr networkUDP::networkUDP(const networkUDP&)'
/home/janosch/Arduino/Class-Test_UDP/networkUDP.h:14:5: note:                 'networkUDP::networkUDP()'
   14 |     networkUDP();
      |     ^~~~~~~~~~
/home/janosch/Arduino/Class-Test_UDP/networkUDP.h:11:7: note: 'class networkUDP' defined here
   11 | class networkUDP {
      |       ^~~~~~~~~~
/home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp: In member function 'void networkUDP::sendNTPinitMessage(char, char*)':
networkUDP.cpp:18:14: error: invalid conversion from 'char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive]
   18 |   _udp.write(_NTP_Package, 48);
      |              ^~~~~~~~~~~~
      |              |
      |              char*
In file included from /home/janosch/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi/src/WiFiUdp.h:25,
                 from /home/janosch/Arduino/Class-Test_UDP/networkUDP.h:3,
                 from /home/janosch/Arduino/Class-Test_UDP/networkUDP.cpp:2:
/home/janosch/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/cores/esp8266/Udp.h:62:45: note:   initializing argument 1 of 'virtual size_t UDP::write(const uint8_t*, size_t)'
   62 |         virtual size_t write(const uint8_t *buffer, size_t size) =0;
      |                              ~~~~~~~~~~~~~~~^~~~~~
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino:15:18: warning: unnecessary parentheses in declaration of 'UDP' [-Wparentheses]
   15 | networkUDP netUDP(UDP);
      |                  ^
Class-Test_UDP:15:19: error: conflicting declaration 'networkUDP UDP'
   15 | networkUDP netUDP(UDP);
      |                   ^~~
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino:14:9: note: previous declaration as 'WiFiUDP UDP'
   14 | WiFiUDP UDP;
      |         ^~~
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void setup()':
Class-Test_UDP:35:9: error: expected primary-expression before '.' token
   35 |   netUDP.UDPinit();
      |         ^
/home/janosch/Arduino/Class-Test_UDP/Class-Test_UDP.ino: In function 'void loop()':
Class-Test_UDP:44:9: error: expected primary-expression before '.' token
   44 |   netUDP.processIncomingPackets();
      |         ^
Bibliothek ESP8266WiFi in Version 1.0 im Ordner: /home/janosch/.arduino15/packages/esp8266/hardware/esp8266/3.0.2/libraries/ESP8266WiFi  wird verwendet
exit status 1
no declaration matches 'networkUDP::networkUDP(UDP&)'

Yeah if you have a look at the library ive used before, you will see that this doesnt even use the sub-second part of the timestamp at all. And i think as far as i understand the code. This library doesnt update the systemtime. But i think there are librarys, that do exactly this if i search longer. But im now kindof fixated to get this working my own way ^^

And at the end:
I altered my original Sketch like this

This doesnt Produce any error. But if you have a look at the code: This should print out every Byte of the Package:

Unfortunatly i only get this output:

17:00:13.118 -> Connecting to WIFI_SSID
17:00:13.251 -> ................................................................
17:00:20.347 -> Connected! IP address: 192.168.2.166
17:00:20.380 -> Listening on UDP port 123
17:00:20.413 -> Not Bothering NTP Server
17:00:20.447 -> Setup Complete

Now invoking echo "Hello" > /dev/udp/192.168.2.166/123

17:00:23.431 -> Received Packet!: Size: 6	IP: 192.168.2.167	PORT: 52281
17:00:23.497 -> Packet received at:	11447
17:00:23.531 -> 

Do you have any idea why?

It uses an initialization list to call the constructor of the embedded object.

1 Like

Hi there,
I've found the Errors or worked it out somehow..

First of all i tried everything to get a Serial output from my original Sketch again. Turns out that Printf is behaving pretty weird while formatting byte an Char arrays. It seems to only be possible to print out the Content of Char Array with printf("%c", char array[]). I havent found a way to get the ASCII value from it. Neither as %i,%u or %x. Same with byte arrays. So i just went back and changed it to Serial.print(). This does not have any Problems at all with handling those array and formatting them.

But i find it kind of weird to get no error message or warning doing so with printf. It just dont do it when its uploaded. Does anyone maybe have more info about this? Shouldnt the Compiler "know" about this Problem?

I then Started the outsourcing Process again, by defining the following three functions again. But this time in the .ino file

As this was working i made a Pointer to the Instanz of my WiFiUDP Object:

WiFiUDP * UDPpointer = new WiFiUDP;

After that i tried to pass this pointer as an argument into my functions. And inside my functions refering to the Object via pointer rather then to the Object itself. I found out tha its critical then use the -> intstead of the normal . for this to work. I then went through the whole Process again making my own Library as shown Here. Only to find the Same Error again...

I was Really lucky to find this Stackoverflow Topic to find out that i was accidentialy Naming my header guard the same as my Library instanz. Knowing this Problem i will encurage you to use #pragma once instead of making an own header guard via

#ifndef name
#define name

#endif

If you really want to use this methode in the Forum Topic somone said the name of the header guard could be choosen to be a checksum of some version of a file. To avoid those errors.

Thank you all for helping (closing)
Janosch

Please post a small but complete sketch illustrating the problem. Is in an array of chars, bytes or what ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.