The use case is a set of remote microcontrollers sending data to a central 'broker' microcontroller using the excellent ESP-Now module written by @Glenn20.
Each remote microcontroller and the broker contain a config file containing lots of Constants that define the sensor and actuator types by IPSO code. This is a very small subset:
Code: Select all
TEMP_SENSOR = 3303
HUMIDITY_SENSOR = 3304
ACCELEROMETER_SENSOR = 3313
BAROMETER_SENSOR = 3315
VOLTAGE_SENSOR = 3316
CURRENT_SENSOR = 3317
ALTITUDE_SENSOR = 3321
BUZZER_ACTUATOR = 3338
LIGHT_ACTUATOR = 3311
ILLUMINANCE_SENSOR = 3301
PRESENCE_SENSOR = 3302
Code: Select all
from esp import espnow
# A WLAN interface must be active to send()/recv()
w0 = network.WLAN(network.STA_IF)
w0.active(True)
e = espnow.ESPNow()
e.init()
peer = config.broker # MAC address of broker's wifi interface
e.add_peer(peer)
#ESP-now publisher
message = (ustruct.pack('IIIIII',config.TEMP_SENSOR, temp_data, config.HUMIDITY_SENSOR, humidity_data, config.VOLTAGE_SENSOR, battery_data))
e.send(peer, message, True)
Typically, I would do this sort of thing with enumerate, which is not available in Micropython.
Is there a way to quickly search for which constant has the value without putting all the constants in a list and iterating across it?
I assume you will have realised by now I am not a programmer so I apologise in advance if this question is nonsense.