0

I have a bunch of thermometers (between 1 and 128). The thermometers have an internal OneWire ID such as "28edce1e500e5" and it is not readable by humans.

Normally I would make an associative array like this:

map["28edce1e500e5"] = "Temp1";
map["28ffd33b9116420"] = "Temp2";
Serial.print(map[printAddress(allDevices[i])]);
Serial.print(" : ");
Serial.print(sensors.getTempC(allDevices[i]));

But it looks like the only hashmap library for Arduino is no longer supported and appears to be quite expensive.

The solution does not have to involve associative array.

The values are populated from a JSON file, loaded from an SD card.

I'm looking for a solutiuon to the problem, not a way to implement an associative array. (Unless that's the best way to solve the problem)

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Oct 3, 2017 at 22:07
3
  • What is your question? Where to find a library? How to implement an associative array? Commented Oct 3, 2017 at 22:14
  • There you go, i formulated it as a question now. Commented Oct 3, 2017 at 22:35
  • Actually the 1-Wire ROM identity is not a string. It is 8-bytes which actually includes a family code (1 byte) and a CRC code (1-byte). You can simply use uint64_t or uint8[8] instead. Put them sorted in a vector and use binary search for the mapping. The standard clib has support for that. nongnu.org/avr-libc/user-manual/… Commented Oct 3, 2017 at 23:07

2 Answers 2

3

Make a struct containing the name and id.

struct device{char[MAX_ID_LENGTH] id; char[MAX_NAME_LENGTH] name; }

Then store the those pairs in an array which you can have sorted by one field or the other. That way you can binary search on that field.

answered Oct 3, 2017 at 22:17
2
  • I cannot make it work with chars, but String it works, do you know what is wrong ? Commented Oct 5, 2017 at 11:12
  • Ok this is working and on the NodeMCU it got enough memory not to be a problem. Though it could be improved im going to accept this answer! Commented Oct 6, 2017 at 8:07
0

Taking the device structure from rachet freak's post you could implement a map of your own using something like this:

template <class K, class V> class MyMap
{
public:
 MyMap()
 {
 }
 void Add(K key, V value)
 {
 }
 void Remove(K key)
 {
 }
 V operator[](const& K)
 {
 }
private:
}

But I think templates are expensive under Arduino so you might want to manually instantiate it to drop the template.

answered Oct 4, 2017 at 7:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.