-->
Page 1 of 2

How to create unique client IDs using GPIO pins

PostPosted: Tue Mar 29, 2016 3:59 pm
by rwkiii
I have a sketch that gets written to 30 different ESPs. Each of these devices can be controlled independent of each other, although normal control is as a group. The only difference in the sketch that gets uploaded after changes is the client ID. This gets old...

So I am considering installing DIP switches to allow setting a unique ID for each. This can be read in code and appended/prepended to the client ID I use for MQTT messages. I think this is the best approach, but I don't have any DIP switches currently so I am hoping for a short term solution.

I got to thinking that I could use some of the GPIO pins on my NodeMCU v1.0 dev boards. If I am correct, I think I can get 32 unique settings out of using 5 pins. Thinking in terms of binary on/off, high/low.

Is it possible to connect a GPIO pin to GND or 3.3v so it can be pulled high or low and then read a set of 5 of these pins to get a value of 0 to 31?

Of course this is just temporary - a DIP would be better, but I only want to do this to 3 of my prototypes so I can avoid keeping track of the client ID setting in the sketch.

I'm not sure if I would use analog or digital pins for this. Can anyone tell me if this is possible and if so, just how I would go about doing it (hardware and software example).

Re: How to create unique client IDs using GPIO pins

PostPosted: Tue Mar 29, 2016 4:37 pm
by martinayotte
Of course it is possible, as long as you have free GPIOs, otherwise, you can add an PCF8574 I2C GPIO expander to get more.

For code, it simply a concatenation of the 5 GPIOs, in Arduino framework, it would looks like :

Code: Select allint boardid = (digitalRead(4) << 4) | (digitalRead(5) << 3) | (digitalRead(6) << 3) | (digitalRead(7) << 2) | (digitalRead(8) << 1) | digitalRead(9)

Re: How to create unique client IDs using GPIO pins

PostPosted: Wed Mar 30, 2016 8:44 am
by gerardwr
rwkiii wrote:So I am considering installing DIP switches to allow setting a unique ID for each. This can be read in code and appended/prepended to the client ID I use for MQTT messages. I think this is the best approach, but I don't have any DIP switches currently so I am hoping for a short term solution.


Did you consider using the ESP's chipid, I use this as an unique identifier.

Re: How to create unique client IDs using GPIO pins

PostPosted: Thu Mar 31, 2016 4:19 pm
by rwkiii
Martinayotte,

Thank you for the code example. I am using GPIO16, GPIO5, GPIO4, and GPIO14 (D0, D1, D2, and D5 respectively).

Code: Select all  int boardid = (digitalRead(16) << 3) | (digitalRead(5) << 2) | (digitalRead(4) << 1) | digitalRead(14);
  Serial.print("Pinned ID: "); Serial.println(boardid);


Connecting all 4 to GND returns a value of 0.

Connecting all 4 to 3.3V returns a value of 15.

Perfect! Thank you.