Hi everyone,
I’m developing a sketch using the MKR ETH shield. I want to initialize the ethernet into a seperate file (not in main.cpp)
For that reason I created the LAN.h and LAN.c files but something is going totally wrong with the way I am including the libraries. Initially I include the ethernet.h and SPI.h libraries in LAN.h but the program were not compiled. So after digging around I found that I must also include the ethernet and spi libraries in the sketch file.
The problem is that when I try to include the LAN.h into the sketch file the program does not compile… any suggestion?
This is the structure of my files:
This is the LAN.h file
#ifndef LAN_H
#define LAN_H
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
void ethernet_init(byte CS_PIN);
void DHCP_config(byte *MAC);
void hardware_present(void);
void connection_link(void);
#endif
This is the LAN.c file
#include "LAN.h"
// Configure the CS pin
void ethernet_init(byte CS_PIN)
{
Ethernet.init(CS_PIN);
}
void DHCP_config(byte *MAC)
{
bool dhcp = Ethernet.begin(mac); // Returns 0 if the DHCP configuration failed, and 1 if it succeeded
if (!dhcp)
{
Serial.println("DHCP configuration failed");
}
else if (dhcp)
{
Serial.println("DHCP configuration succeed");
}
}
Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Connect ethernet shield");
while (true)
{
delay(1); // do nothing, no point running without Ethernet hardware
}
}
else
{
Serial.print("Ethernet shield found: ");
switch (Ethernet.hardwareStatus())
{
case 1:
Serial.println("EthernetW5100");
break;
case 2:
Serial.println("EthernetW5200");
break;
case 3:
Serial.println("EthernetW5500");
break;
default:
break;
}
}
//Check if the ethernet cable is conencted
while (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
delay(500);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
This is the main.cpp file
#include <SPI.h>
#include <Ethernet.h>
#include <Arduino.h>
#include “config.h”
#include “…/lib/Ethernet_lib/LAN.h”
byte mac[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; // MAC address for ethernet shield. This is a random address and you can change it
byte CS_PIN = 5;
void setup()
{
Serial.begin(115200);
delay(1000); // give some time for the serial monitor to open
ethernet_init(CS_PIN);
DHCP_config(mac);
}
void loop()
{
Serial.print(“Hi”);
delay(1000);
}
What is this not-comment doing inside code at the top level?
this entire code is outside of an function and illegal.
Just #include <LAN.h>
, do not traverse the directory structure. PIO will find this library, and if not, you have to specify lib_deps = Ethernet_lib
in the platformio.ini
.
Yeah I can give you a suggestion if you show the exact error message, which you didn’t show 
Hi @maxgerhardt and thank you for your support!!
It seems that my problem solved as soon as I renamed the LAN.c to LAN.cpp.
I know that arduino IDE accepts both c and c++ libraries but for the current project I use VScode and Platformio, so in general I have to use only c++ libraries (unfortunately I do not have the error logging before renaming)?
After renaming the file from .c to .cpp, two separate error popped up
Building in release mode
Compiling .pio\build\mkrwifi1010\src\main.cpp.o
Linking .pio\build\mkrwifi1010\firmware.elf
.pio\build\mkrwifi1010\src\main.cpp.o: In function `setup':
main.cpp:(.text.setup+0x18): undefined reference to `ethernet_init(unsigned char)'
main.cpp:(.text.setup+0x1e): undefined reference to `DHCP_config(unsigned char*)'
main.cpp:(.text.setup+0x22): undefined reference to `hardware_present()'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\mkrwifi1010\firmware.elf] Error 1
and this error:
Building in release mode
Compiling .pio\build\mkrwifi1010\libe6c\Ethernet_lib\LAN.cpp.o
Archiving .pio\build\mkrwifi1010\libe6c\libEthernet_lib.a
arm-none-eabi-ar: unable to rename '.pio\build\mkrwifi1010\libe6c\libEthernet_lib.a'; reason: File exists
*** [.pio\build\mkrwifi1010\libe6c\libEthernet_lib.a] Error 1
I solved these error by removing the project folder from the workspace and adding it back again… This is something like a bug or what?
These are the files after error fixing
Main.cpp (main .ino)
#include <SPI.h>
#include <Ethernet.h>
#include <Arduino.h>
#include "config.h"
#include <LAN.h>
uint8_t mac[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}; // MAC address for ethernet shield. This is a random address and you can change it
byte CS_PIN = 5;
void setup()
{
Serial.begin(115200);
delay(1000); // give some time for the serial monitor to open
ethernet_init(CS_PIN); // Configure the CS pin
DHCP_config(mac);
hardware_present();
connection_link();
}
void loop()
{
Serial.print("Hi");
delay(1000);
}
Lan.cpp
#include "LAN.h"
//Configure the CS pin
void ethernet_init(byte CS_PIN)
{
Ethernet.init(CS_PIN); // Configure the CS pin
}
void DHCP_config(uint8_t *mac)
{
bool DHCP = Ethernet.begin(mac); // Returns 0 if the DHCP configuration failed, and 1 if it succeeded
if (!DHCP)
{
Serial.println("DHCP configuration failed");
}
else if (DHCP)
{
Serial.println("DHCP configuration succeed");
}
}
//Check for Ethernet hardware present
void hardware_present()
{
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Connect ethernet shield");
while (true)
{
delay(1); // do nothing, no point running without Ethernet hardware
}
}
else
{
Serial.print("Ethernet shield found: ");
switch (Ethernet.hardwareStatus())
{
case 1:
Serial.println("EthernetW5100");
break;
case 2:
Serial.println("EthernetW5200");
break;
case 3:
Serial.println("EthernetW5500");
break;
default:
break;
}
}
}
//Check if the ethernet cable is conencted
void connection_link()
{
while (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
delay(500);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
}
LAN.h
#ifndef LAN_H
#define LAN_H
#include <Arduino.h>
#include <SPI.h>
#include <Ethernet.h>
void ethernet_init(byte CS_PIN);
void DHCP_config(uint8_t *mac);
void hardware_present(void);
void connection_link(void);
#endif
Oh yes I oversaw that, that is absolutely right. You are using C++ classes and APIs there (Ethernet
, Serial.println()...
), so you absolutely need to be writing that code in a C++ file.
Seems to me that the project just needs a Clean and Build.
@maxgerhardt
Clean and build it solved the issue. Thanks a lot!!