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..
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