I am setting up a series of temperature sensors for my home, using the LOLIN (WEMOS) D1 mini pro (a ESP8266-based board). I have multiple sensors, with the code for each being identical except for the variables which tell the server which sensor it is. In this case there are four:
char sensorIDtemp[] = "livingroomT";
char sensorIDhumd[] = "livingroomH";
which are used to send the temperature and humidity to a MagicMirror as part of an HTTP POST.
unsigned long myChannelNumber = 123456;
const char * myWriteAPIKey = "A1B2C3D4E5F6G7H8";
which sends the same data to a ThingSpeak channel for logging purposes.
These work fine if they are hard-coded into the beginning of the sketch. But whenever I update the code, I have to remember to change each of each of the four variables as I am uploading the sketchto each board. To avoid doing that, I'm trying to work out a solution where each device would read some unique identifier and automatically adjust the variables.
I have three questions (in bold below):
-
What I have below is just my very un-knowledgable, hacked-up solution. If anyone has suggestions for a more elegant way of doing this, I'd be happy to consider it.
-
I decided to use the MAC address as the unique identifier. Because the MAC address -- as-is from WiFi.macAddress() -- can't be used with switch, I decided to derive a relatively unique integer for each MAC address. I turned each octet into an integer and then did a little math on the six numbers to produce a 4-digit integer. It's no where near as unique as the actual MAC address, but with 5,000+ possibilities, the chances of having duplicate values is quite small.
This works, but it feels like a lot of fiddling. Anyone know of a better way to do this?
byte mac[6];
// at the beginning of the code, and then in in void setup()
WiFi.macAddress(mac);
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
int mac0 = mac[0];
int mac1 = mac[1];
int mac2 = mac[2];
int mac3 = mac[3];
int mac4 = mac[4];
int mac5 = mac[5];
int macIfM = ( ( mac0 * 1 ) + ( mac1 * 2 ) + ( mac2 * 3 ) + ( mac3 * 4 ) + ( mac4 * 5 ) + ( mac5 * 6 ) );
Serial.print("Integer from MAC: ");
Serial.println(macIfM);
- The thing I can't get to work at all, however, is the switch itself. Here's the code I have currently. (I'll include just one case for brevity, but the actually code has many):
switch (macIfM) {
case 3579: {
char sensorReadName[] = "Living Room"; // for Serial.print
char sensorIDtemp[] = "livingroomT";
char sensorIDhumd[] = "livingroomH";
unsigned long myChannelNumber = 162534;
const char * myWriteAPIKey = "A1B2C3D4E5F6G7H8";;
}
break;
default: { // these are just dummy value to avoid "not declared" errors
char sensorReadName[] = "Unknown";
char sensorIDtemp[] = "unknownT";
char sensorIDhumd[] = "unknownH";
unsigned long myChannelNumber = 121212;
const char * myWriteAPIKey = "XY12XY12XY12XY12";
}
break;
}
I tried it without enclosing each case in curly brackets, but I got
error: jump to case label [-fpermissive]
I tried it with curly brackets and got
not declared in this scope
when I call the variable later in the sketch.
I tried declaring the variables at the beginning of the sketch (moving the default/dummy values to the beginning of the sketch), hoping the switch would overwrite them, but it didn't. The sketch just uses the dummy values throughout.
Can anyone figure out how to do this properly?
I'm still pretty green when it comes to coding, so you're welcome to include any extra detail you'd like with your answer.
Thanks!