How to use DHCP and specify a mac address?


#1

Hello all.

Question here…
I can either start Cayenne using DHCP ou specify all info for my network using one of these two:

Cayenne.begin(token); // This one uses DHCP so my router provides all network info

or

Cayenne.begin(token, ipAddress, ipDNS, ipGateway, subnet_mask, mac); // Where I specify all

But… how can I start the network using DHCP AND have the MAC address uniquely defined by me?
If I use the ethernet.h library, I could do a

Ethernet.begin(mac)

giving just the mac address and everything else comes from my router.
The problem is that ethernet shields require us to provide unique mac addresses cause they all have the same default so if you have 2 or more arduinos on the network with cayenne and using dhcp, we end up with mac address conflicts.

Is there a way I could do a…

Cayenne.begin(token, mac);

and use DHCP and just provide unique MAC per hardware?
Thanks in advance…

Carlos Benjamin.


#2

I’m pretty sure our sketches boil down to Ethernet.h at some point when you peel away all the layers :slight_smile:

I gave it a shot with the ManualConnection sketch we provide in the Cayenne Library, and it appears that at least some of the parameters are optional – I was able to connect my Arduino to Cayenne with just a token and MAC address, and still got an IP via DHCP. Here is what I did to the sketch – basically just commented out the parts I didn’t care about.

Just note that if your Ethernet shield is not a W5100 you’ll have to change the #include statements to match your shield, as per the comments at the top of the sketch.

/*
Cayenne Ethernet Manual IP Example

This sketch connects to the Cayenne server using an Arduino Ethernet Shield W5100 and a 
manually specified MAC address, IP and gateway.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/

//#define CAYENNE_DEBUG             // Uncomment to show debug messages
#define CAYENNE_PRINT Serial        // Comment this out to disable prints and save space
#include <CayenneEthernet.h>        // Comment this out if you uncomment a different Ethernet device below.
//#include <CayenneEthernetW5500.h> // Uncomment this and comment out CayenneEthernet.h to use an Ethernet 2 shield or other Ethernet W5500 shield.
                                    // You will need the Ethernet2 library installed. See the ArduinoEthernetW5500 example sketch for more info.
//#include <CayenneEthernetW5200.h> // Uncomment this and comment out CayenneEthernet.h to use an Ethernet W5200 shield.
                                    // You will need the EthernetW5200 library installed. See the ArduinoEthernetW5200 example sketch for more info.

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "AuthToken";

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
//IPAddress arduino_ip(10, 0, 0, 20);
//IPAddress dns_ip(8, 8, 8, 8);
//IPAddress gateway_ip(10, 0, 0, 1);
//IPAddress subnet_mask(255, 255, 255, 0);

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(token, arduino_mac);
}

void loop()
{
	Cayenne.run();
}

#3

Cool, thank you rsiegel.
Will try and update my code with it.
I found an example that creates a random MAC at first execution and stores it in EEPROM. Then on following executions, it checks the existence of the mac and if there, uses it from that loop on, so every prototype will have a statistically unique mac within a network.
Cheers!
Carlos


#4

Hi Carlos!

I was wondering that if you allow the router to assign all parameters, as I do mostly, then I look for the MAC assigned in router dhcp client list I then reserve that ip and mac in the section (haven’t done it in some time) I forget the description of the router reservation page. I did this for my Rpi3 and arduino. I wonder if this is a shortcut or perhaps doing what I do and adding it to your code.

Just looking for an opinion, haven’t got stuck yet keyword yet.

William


#5

Hi wmontg5988,

The problem I faced was that on the network I was installing the arduino did not use DHCP so they assigned IP based on the MAC address and as all my boards had the same MAC as default so I was facing conflicts and things were not working… so I wanted to specify the MAC individually to each ethernet shield.

Regards,
Carlos


#6

ok, I see! seems like I had a sketch that had you input any mac address whick seemed strange at the time. I have read blogs that mentioned getting the mac address off the sticker on the ethernet shield but well all know there ain’t no sticker. So might there actually be an internal mac address and can it be found using AT commands like getting the ip address.
Hmmmmmm


#7

I likely bought a cheap model that requires a MAC to be specified… they all have the same address if I don’t specify :slight_smile: made in who-knows-where… but they work.

Carlos Benjamin