Skip to content

Commit

Permalink
Merge pull request #247 from brentru/refactor-mac-addr
Browse files Browse the repository at this point in the history
Write hardware info to debug output and boot file
  • Loading branch information
brentru authored Apr 12, 2022
2 parents c9279f5 + 833a68a commit 065d98d
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 61 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit WipperSnapper Beta
version=1.0.0-beta.29
version=1.0.0-beta.30
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino client for Adafruit.io WipperSnapper
Expand Down
47 changes: 35 additions & 12 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Wippersnapper::~Wippersnapper() {
*/
/**************************************************************************/
void Wippersnapper::provision() {
// Get MAC address from network interface
getMacAddr();

// init. LED for status signaling
statusLEDInit();
#ifdef USE_TINYUSB
Expand Down Expand Up @@ -122,7 +125,7 @@ void Wippersnapper::_disconnect() {
MAC address.
*/
/****************************************************************************/
void Wippersnapper::setUID() {
void Wippersnapper::getMacAddr() {
WS_DEBUG_PRINTLN("ERROR: Please define a network interface!");
}

Expand Down Expand Up @@ -1064,26 +1067,22 @@ bool Wippersnapper::buildWSTopics() {
WS._network_pass == NULL)
return false;

// Get UID from the network iface
setUID();
// Create device unique identifier
// Move the top 3 bytes from the UID
for (int i = 5; i > 2; i--) {
WS._uid[6 - 1 - i] = WS._uid[i];
WS._macAddr[6 - 1 - i] = WS._macAddr[i];
}
snprintf(WS.sUID, sizeof(WS.sUID), "%02d%02d%02d", WS._uid[0], WS._uid[1],
WS._uid[2]);

// Get board ID from _Boards.h
snprintf(WS.sUID, sizeof(WS.sUID), "%02d%02d%02d", WS._macAddr[0],
WS._macAddr[1], WS._macAddr[2]);
// Set device UID
WS._boardId = BOARD_ID;

// Set client UID
_device_uid = (char *)malloc(sizeof(char) + strlen("io-wipper-") +
strlen(WS._boardId) + strlen(WS.sUID));
strcpy(_device_uid, "io-wipper-");
strcat(_device_uid, WS._boardId);
strcat(_device_uid, WS.sUID);

// Create MQTT client object
// Initialize MQTT client
setupMQTTClient(_device_uid);

// Global registration topic
Expand Down Expand Up @@ -1262,7 +1261,7 @@ void Wippersnapper::errorWriteHang(String error) {
// Print error
WS_DEBUG_PRINTLN(error);
#ifdef USE_TINYUSB
_fileSystem->writeErrorToBootOut(error.c_str());
_fileSystem->writeToBootOut(error.c_str());
#endif
// Signal and hang forever
while (1) {
Expand Down Expand Up @@ -1519,6 +1518,30 @@ bool validateAppCreds() {
*/
/**************************************************************************/
void Wippersnapper::connect() {
WS_DEBUG_PRINTLN("Adafruit.io WipperSnapper");

// Print all identifiers to the debug log
WS_DEBUG_PRINTLN("-------Device Information-------");

WS_DEBUG_PRINT("Firmware Version: ");
WS_DEBUG_PRINTLN(WS_VERSION);

WS_DEBUG_PRINT("Board ID: ");
WS_DEBUG_PRINTLN(BOARD_ID);

WS_DEBUG_PRINT("Adafruit.io User: ");
WS_DEBUG_PRINTLN(WS._username);

WS_DEBUG_PRINT("WiFi Network: ");
WS_DEBUG_PRINTLN(WS._network_ssid);

char sMAC[18] = {0};
sprintf(sMAC, "%02X:%02X:%02X:%02X:%02X:%02X", WS._macAddr[0], WS._macAddr[1],
WS._macAddr[2], WS._macAddr[3], WS._macAddr[4], WS._macAddr[5]);
WS_DEBUG_PRINT("MAC Address: ");
WS_DEBUG_PRINTLN(sMAC);
WS_DEBUG_PRINTLN("-------------------------------");

// enable WDT
WS.enableWDT(WS_WDT_TIMEOUT);

Expand Down
6 changes: 3 additions & 3 deletions src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#endif

#define WS_VERSION \
"1.0.0-beta.29" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.30" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
Expand Down Expand Up @@ -197,7 +197,7 @@ class Wippersnapper {
void connect();
void disconnect();

virtual void setUID();
virtual void getMacAddr();
virtual void setupMQTTClient(const char *clientID);

virtual ws_status_t networkStatus();
Expand Down Expand Up @@ -274,7 +274,7 @@ class Wippersnapper {
WipperSnapper_LittleFS
*_littleFS; ///< Instance of LittleFS Filesystem (non-native USB)

uint8_t _uid[6]; /*!< Unique network iface identifier */
uint8_t _macAddr[6]; /*!< Unique network iface identifier */
char sUID[13]; /*!< Unique network iface identifier */
const char *_boardId; /*!< Adafruit IO+ board string */
Adafruit_MQTT *_mqtt; /*!< Reference to Adafruit_MQTT, _mqtt. */
Expand Down
25 changes: 12 additions & 13 deletions src/network_interfaces/Wippersnapper_AIRLIFT.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
@note For the ESP32, the UID is the MAC address.
*/
/********************************************************/
void setUID() {
void getMacAddr() {
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
WiFi.macAddress(mac);
memcpy(WS._uid, mac, sizeof(mac));
memcpy(WS._macAddr, mac, sizeof(mac));
}

/********************************************************/
Expand Down Expand Up @@ -197,17 +198,15 @@ class Wippersnapper_AIRLIFT : public Wippersnapper {
const char *connectionType() { return "AIRLIFT"; }

protected:
const char *_ssid; /*!< Network SSID. */
const char *_pass; /*!< Network password. */
const char *_mqttBrokerURL; /*!< MQTT broker URL. */
String _fv; /*!< nina-fw firmware version. */
uint8_t mac[6] = {0x00, 0x00, 0x00,
0x00, 0x00, 0x00}; /*!< ESP32 interface's MAC address. */
int _ssPin = -1; /*!< SPI S.S. pin. */
int _ackPin = -1; /*!< SPI ACK pin. */
int _rstPin = -1; /*!< SPI RST pin. */
int _gpio0Pin = -1; /*!< SPI GPIO0 pin, unused. */
WiFiSSLClient *_mqtt_client; /*!< Instance of a secure WiFi client. */
const char *_ssid; /*!< Network SSID. */
const char *_pass; /*!< Network password. */
const char *_mqttBrokerURL; /*!< MQTT broker URL. */
String _fv; /*!< nina-fw firmware version. */
int _ssPin = -1; /*!< SPI S.S. pin. */
int _ackPin = -1; /*!< SPI ACK pin. */
int _rstPin = -1; /*!< SPI RST pin. */
int _gpio0Pin = -1; /*!< SPI GPIO0 pin, unused. */
WiFiSSLClient *_mqtt_client; /*!< Instance of a secure WiFi client. */
SPIClass *_wifi; /*!< Instance of the SPI bus used by the AirLift. */

/**************************************************************************/
Expand Down
6 changes: 3 additions & 3 deletions src/network_interfaces/Wippersnapper_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class Wippersnapper_ESP32 : public Wippersnapper {
@note On ESP32, the UID is the MAC address.
*/
/********************************************************/
void setUID() {
void getMacAddr() {
uint8_t mac[6] = {0};
WiFi.macAddress(mac);
memcpy(WS._uid, mac, sizeof(mac));
memcpy(WS._macAddr, mac, sizeof(mac));
}

/********************************************************/
Expand Down Expand Up @@ -141,7 +142,6 @@ class Wippersnapper_ESP32 : public Wippersnapper {
const char *_ssid;
const char *_pass;
const char *_mqttBrokerURL;
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
WiFiClientSecure *_mqtt_client;

// io.adafruit.us
Expand Down
6 changes: 3 additions & 3 deletions src/network_interfaces/Wippersnapper_ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
@note For the ESP8266, the UID is the MAC address.
*/
/********************************************************/
void setUID() {
void getMacAddr() {
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
WiFi.macAddress(mac);
memcpy(WS._uid, mac, sizeof(mac));
memcpy(WS._macAddr, mac, sizeof(mac));
}

/*******************************************************************/
Expand Down Expand Up @@ -155,7 +156,6 @@ class Wippersnapper_ESP8266 : public Wippersnapper {
const char *_ssid = NULL;
const char *_pass = NULL;
const char *_mqttBrokerURL = NULL;
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
WiFiClientSecure *_wifi_client;

/**************************************************************************/
Expand Down
4 changes: 2 additions & 2 deletions src/network_interfaces/Wippersnapper_WIFININA.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ class Wippersnapper_WIFININA : public Wippersnapper {
@note For the ESP32, the UID is the MAC address.
*/
/********************************************************/
void setUID() {
void getMacAddr() {
uint8_t mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
WiFi.macAddress(mac);
memcpy(WS._uid, mac, sizeof(mac));
memcpy(WS._macAddr, mac, sizeof(mac));
}

/********************************************************/
Expand Down
69 changes: 46 additions & 23 deletions src/provisioning/tinyusb/Wippersnapper_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ Wippersnapper_FS::Wippersnapper_FS() {
*/
/************************************************************/
Wippersnapper_FS::~Wippersnapper_FS() {
//io_username = NULL;
//io_key = NULL;
// io_username = NULL;
// io_key = NULL;
}

/**************************************************************************/
Expand Down Expand Up @@ -249,9 +249,24 @@ void Wippersnapper_FS::eraseBootFile() {
/**************************************************************************/
bool Wippersnapper_FS::createBootFile() {
bool is_success = false;
char sMAC[18] = {0};

File bootFile = wipperFatFs.open("/wipper_boot_out.txt", FILE_WRITE);
if (bootFile) {
bootFile.println("Adafruit.io WipperSnapper");

bootFile.print("Firmware Version: ");
bootFile.println(WS_VERSION);

bootFile.print("Board ID: ");
bootFile.println(BOARD_ID);

sprintf(sMAC, "%02X:%02X:%02X:%02X:%02X:%02X", WS._macAddr[0],
WS._macAddr[1], WS._macAddr[2], WS._macAddr[3], WS._macAddr[4],
WS._macAddr[5]);
bootFile.print("MAC Address: ");
bootFile.println(sMAC);

bootFile.flush();
bootFile.close();
is_success = true;
Expand Down Expand Up @@ -299,8 +314,8 @@ void Wippersnapper_FS::createConfigFileSkel() {
"HERE\",\n\t\t\"network_password\":\"YOUR_WIFI_PASS_HERE\"\n\t}\n}");
secretsFile.flush();
secretsFile.close();
writeErrorToBootOut(
"* Please edit the secrets.json file. Then, reset your board.");
writeToBootOut(
"* Please edit the secrets.json file. Then, reset your board.\n");
}

/**************************************************************************/
Expand All @@ -323,17 +338,17 @@ void Wippersnapper_FS::parseSecrets() {
WS_DEBUG_PRINT("ERROR: deserializeJson() failed with code ");
WS_DEBUG_PRINTLN(err.c_str());

writeErrorToBootOut("ERROR: deserializeJson() failed with code");
writeErrorToBootOut(err.c_str());
writeToBootOut("ERROR: deserializeJson() failed with code\n");
writeToBootOut(err.c_str());
fsHalt();
}

// Get io username
const char * io_username = doc["io_username"];
const char *io_username = doc["io_username"];
// error check against default values [ArduinoJSON, 3.3.3]
if (io_username == nullptr) {
WS_DEBUG_PRINTLN("ERROR: invalid io_username value in secrets.json!");
writeErrorToBootOut("ERROR: invalid io_username value in secrets.json!");
writeToBootOut("ERROR: invalid io_username value in secrets.json!\n");
while (1) {
WS.statusLEDBlink(WS_LED_STATUS_FS_WRITE);
yield();
Expand All @@ -342,20 +357,24 @@ void Wippersnapper_FS::parseSecrets() {

// check if username is from templated json
if (doc["io_username"] == "YOUR_IO_USERNAME_HERE") {
writeErrorToBootOut(
writeToBootOut(
"* ERROR: Default username found in secrets.json, please edit "
"the secrets.json file and reset the board for the changes to take "
"effect");
"effect\n");
fsHalt();
}
WS._username = io_username;

writeToBootOut("Adafruit.io Username: ");
writeToBootOut(WS._username);
writeToBootOut("\n");

// Get io key
const char *io_key = doc["io_key"];
// error check against default values [ArduinoJSON, 3.3.3]
if (io_key == nullptr) {
WS_DEBUG_PRINTLN("ERROR: invalid io_key value in secrets.json!");
writeErrorToBootOut("ERROR: invalid io_key value in secrets.json!");
writeToBootOut("ERROR: invalid io_key value in secrets.json!\n");
fsHalt();
}
WS._key = io_key;
Expand All @@ -379,16 +398,16 @@ void Wippersnapper_FS::parseSecrets() {
WS_DEBUG_PRINTLN(
"ERROR: invalid network_type_wifi_airlift_network_password value in "
"secrets.json!");
writeErrorToBootOut(
writeToBootOut(
"ERROR: invalid network_type_wifi_airlift_network_password value in "
"secrets.json!");
"secrets.json!\n");
fsHalt();
}
// check if SSID is from template (not entered)
if (doc["network_type_wifi_airlift"]["network_password"] ==
"YOUR_WIFI_SSID_HERE") {
writeErrorToBootOut("Default SSID found in secrets.json, please edit "
"the secrets.json file and reset the board");
writeToBootOut("Default SSID found in secrets.json, please edit "
"the secrets.json file and reset the board\n");
fsHalt();
}

Expand All @@ -415,16 +434,16 @@ void Wippersnapper_FS::parseSecrets() {
WS_DEBUG_PRINTLN(
"ERROR: invalid network_type_wifi_native_network_password value in "
"secrets.json!");
writeErrorToBootOut(
writeToBootOut(
"ERROR: invalid network_type_wifi_native_network_password value in "
"secrets.json!");
"secrets.json!\n");
fsHalt();
}
// check if SSID is from template (not entered)
if (doc["network_type_wifi_native"]["network_password"] ==
"YOUR_WIFI_SSID_HERE") {
writeErrorToBootOut("Default SSID found in secrets.json, please edit "
"the secrets.json file and reset the board");
writeToBootOut("Default SSID found in secrets.json, please edit "
"the secrets.json file and reset the board\n");
fsHalt();
}

Expand All @@ -438,11 +457,15 @@ void Wippersnapper_FS::parseSecrets() {
if (!setNetwork) {
WS_DEBUG_PRINTLN(
"ERROR: Network interface not detected in secrets.json file.");
writeErrorToBootOut(
"ERROR: Network interface not detected in secrets.json file.");
writeToBootOut(
"ERROR: Network interface not detected in secrets.json file.\n");
fsHalt();
}

writeToBootOut("WiFi Network: ");
writeToBootOut(WS._network_ssid);
writeToBootOut("\n");

// Optional, Set the IO URL
WS._mqttBrokerURL = doc["io_url"];

Expand All @@ -460,11 +483,11 @@ void Wippersnapper_FS::parseSecrets() {
PROGMEM string.
*/
/**************************************************************************/
void Wippersnapper_FS::writeErrorToBootOut(PGM_P str) {
void Wippersnapper_FS::writeToBootOut(PGM_P str) {
// Append error output to FS
File bootFile = wipperFatFs.open("/wipper_boot_out.txt", FILE_WRITE);
if (bootFile) {
bootFile.println(str);
bootFile.print(str);
bootFile.flush();
bootFile.close();
} else {
Expand Down
Loading

0 comments on commit 065d98d

Please sign in to comment.