Dynamic structs and referencing by MAC address


I have a project I am working on where I will receive a mac address from either a known or unknown host and I need to store the state and various properties of that mac address and reference it later on. I am trying to figure out the best way to manage this. I need to be able to dynamically add a new device to the devices dict if it turns up on the wire, and change state and sequence on existing devices when those values change

In python it is quite straightforward to have a dict and then reference that dict:

devices = {
  "1122334455667788": {'online':True,'sequence':'33'},
  "1122334455667799": {'online':False,'sequence':'01'}
print("All Devices ", devices)
print("Device 1234 online ", devices["1122334455667788"]["online"] )
devices["1122334455667788"]["online"] = False
devices["1122334455667788"]["sequence"] = '42'
print("Device 1234 online ", devices["1122334455667788"] )
devices.update( { "1122334455667700" : {'online':True,'sequence':'44'} } )
print("All Devices ", devices)

In C I am trying:

 Logic simulator AND
 Input  SW1 SW2
 Output LED1

 LED1 = SW1 + SW2

 created 2018
 NextStep LLC <https:next-step.asia/>
 by Kaz Ueno
 This example code is in the public domain.
typedef struct {
//  uint8_t mac;
  String mac;
  boolean online;
  uint8_t sequence;
} device;

device devices[10];

void setup() {

  // put your setup code here, to run once:
//  devices[0].mac[8] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };
  device *d0 = &devices[0];
  d0->mac = "1122334455667788";
  d0->online = false;
  d0->sequence= 0x33;
//  devices[1].mac[8] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x99 };
  device *d1 = &devices[0];
  d1->mac = "1122334455667799";
  d1->online = false;
  d1->sequence= 0x01;
  Serial.println("Testing Struct");
  Serial.print("Device 0 Mac");
  Serial.print("Device 0 Online State ");

  if (d1->online == true) {
	Serial.println("Start Device 0 is online");
  } else {
	Serial.println("Start Device 0 is offline");

void loop() {
  device *d0 = &devices[0];
  if (d0->online  == true ) {
	Serial.println("After Device 0 is online");
	d0->online = false;
  } else {
	Serial.println("After Device 0 is offline");
    d0->online = true;

Is there an easy way to find the mac address rather than needing to loop through all devices and be able to do:

  device *d = &devices['1122334455667788'];

Or something similar?

It can be done in C++, e.g. std::map, but that's not something I'd do on an Arduino with its tiny RAM. What's the issue with iterating through the array?

Arduino with an SD card would probably work. Store everything on the card and only pull up the one you want to access.

-jim lee

Looping through all the devices is probably easiest and fastest and smallest, at least for moderate numbers of devices.

Thanks, it just feels wrong iterating through the array each time I need to look it up which I need to do many few times so am currently setting a global variable and accessing it for each loop and using a function to I need it. It's on a ESP32 so not as typically memory constrained as other Arduino devices.
Or is there a better way to store a dynamically sized array / structure rather? The maximum possible devices would be 8 or so.. so I can just leave it as a defined variable and then iterate through.
It just means I need to have a function to find the next free entry when adding a new one, and was thinking there might be a better way to achieve this other than looping but that doesn't seem to be the case.

At a max of 8 devices, any tweaking is just a waste of time. Anything that works for you should be fine. Move on to more pressing issues.

-jim lee

At a max of 8 devices, any tweaking is just a waste of time. Anything that works for you should be fine. Move on to more pressing issues.

This. There are plenty of techniques to help in a case where you have a large number of MAC addresses to manage, but none of them are likely to be as efficient as a simple loop when you only have eight.

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