I have the following code which I like to include in all of my applications so I know what application each device is running and what version(date) of the application and OS by simply requesting the associated Particle variable:
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
char gDeviceInfo [200] = "";
void updateDeviceInfo() {
snprintf(gDeviceInfo, sizeof(gDeviceInfo)
,"Application: %s, Date: %s, Time: %s, System firmware: %s, SSID: %s"
,__FILENAME__
,__DATE__
,__TIME__
,(const char*)System.version() // cast required for String
,(const char*)WiFi.SSID() // cast not required but always safe when in doubt if String or char*
);
}
I also have this in setup()
updateDeviceInfo();
Particle.variable("deviceInfo",gDeviceInfo);
I would like to include the actual name of the device in this Particle variable and so I made the following changes:
gDeviceName char [40] = "";
void nameHandler(const char *topic,const char *data) {
strncpy(gDeviceName,data,sizeof(gDeviceName-1));
updateDeviceInfo();
}
I also added the following to setup();
Particle.subscribe("particle/device/name",nameHandler);
Particle.publish("particle/device/name",PRIVATE);
and modified my updateDeviceInfo to this:
void updateDeviceInfo() {
snprintf(gDeviceInfo, sizeof(gDeviceInfo)
,"Name: %s, Application: %s, Date: %s, Time: %s, System firmware: %s, SSID: %s"
,(const char*)gDeviceName
,__FILENAME__
,__DATE__
,__TIME__
,(const char*)System.version() // cast required for String
,(const char*)WiFi.SSID() // cast not required but always safe when in doubt if String or char*
);
}
I’m finally getting SOMETHING out for Name, but only 4 characters. The actual device name is MailboxChimeMonitor, but here’s the output when I look at deviceInfo:
Name: Mail, Application: MailboxChimeMonitor.ino, Date: Jan 5 2021, Time: 15:44:29, System firmware: 2.0.1, SSID: HVAC Star 2.4
I’ve been looking at this too long and can’t see the forest for the trees, as they say. Suggestions would be greatly appreciated!