Skip to content

Commit

Permalink
[SYS] Add serial self test feature (#1964)
Browse files Browse the repository at this point in the history
Add self testing for Theengs Bridge v1.1 (TBRIDGE02)
This feature enables factory testing
  • Loading branch information
1technophile authored Jun 12, 2024
1 parent e321448 commit a74a6eb
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 7 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
- "thingpulse-espgateway"
- "theengs-bridge"
- "esp32dev-ble-idf"
- "theengs-bridge-v11"
runs-on: ubuntu-latest
name: Build with PlatformIO
steps:
Expand Down
41 changes: 41 additions & 0 deletions environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,47 @@ build_flags =
custom_description = BLE gateway with external antenna and Ethernet/WiFi connectivity, [user guide](https://tbridge01.theengs.io/)
custom_hardware = Theengs Bridge gateway ethernet

[env:theengs-bridge-v11]
platform = ${com.esp32_platform}
board = esp32dev
extra_scripts = ${com-esp32.extra_scripts}
board_build.partitions = min_spiffs.csv
lib_deps =
${com-esp32.lib_deps}
${libraries.ble}
${libraries.adafruit_neopixel}
${libraries.decoder}
build_flags =
${com-esp32.build_flags}
'-DZgatewayBT="BT"'
'-DLED_SEND_RECEIVE=2'
'-DLED_SEND_RECEIVE_ON=0'
'-DESP32_ETHERNET=true'
'-DETH_PHY_TYPE=ETH_PHY_LAN8720'
'-DETH_PHY_ADDR=0'
'-DETH_PHY_POWER=5'
'-DETH_PHY_MDC=23'
'-DETH_PHY_MDIO=18'
'-DETH_CLK_MODE=ETH_CLOCK_GPIO16_OUT'
'-DANEOPIX_IND_DATA_GPIO=32'
'-DANEOPIX_IND_NUM_LEDS=4'
'-DANEOPIX_INFO_LED=0'
'-DANEOPIX_SEND_RECEIVE_LED=1'
'-DANEOPIX_ERROR_LED=1'
'-DANEOPIX_BRIGHTNESS=100'
'-DRGB_INDICATORS=true'
'-DAttemptBLEConnect=false'
'-DTRIGGER_GPIO=4'
'-DUSE_MAC_AS_GATEWAY_NAME'
'-DGATEWAY_MANUFACTURER="Theengs"'
'-DGATEWAY_MODEL="TBRIDGE02"'
;'-DBOARD_HAS_PSRAM'
'-UZwebUI="WebUI"' ; Disable WebUI
'-DSELF_TEST=true'
;'-mfix-esp32-psram-cache-issue'
custom_description = BLE gateway with external antenna and Ethernet/WiFi connectivity, [user guide](https://tbridge02.theengs.io/)
custom_hardware = Theengs Bridge gateway ethernet

[env:esp32dev-all-test]
platform = ${com.esp32_platform}
board = esp32dev
Expand Down
82 changes: 82 additions & 0 deletions main/ZboardTheengs.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#if SELF_TEST
# include <esp_system.h>

String getChipModel() {
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);

switch (chip_info.model) {
case CHIP_ESP32:
return "ESP32";
case CHIP_ESP32S2:
return "ESP32-S2";
case CHIP_ESP32S3:
return "ESP32-S3";
case CHIP_ESP32C3:
return "ESP32-C3";
case CHIP_ESP32H2:
return "ESP32-H2";
default:
return "Unknown";
}
}

// From https://github.com/ThingPulse/espgateway-ethernet-testbed

void addTestMessage(JsonArray& data, String name, String value, String result) {
JsonObject object = data.createNestedObject();
object["name"] = name;
object["value"] = value;
object["result"] = result;
}

void testDevice() {
StaticJsonDocument<1200> doc;
JsonArray data = doc.to<JsonArray>();

addTestMessage(data, "Mac Address", String(WiFi.macAddress()), "OK");
addTestMessage(data, "Chip Model", String(ESP.getChipModel()), "OK");
addTestMessage(data, "Chip Revision", String(ESP.getChipRevision()), "OK");
addTestMessage(data, "Available Cores", String(ESP.getChipCores()), ESP.getChipCores() == 2 ? "OK" : "NOK");
addTestMessage(data, "Heap Size", String(ESP.getHeapSize() / 1024) + "kb", ESP.getHeapSize() > 100000 ? "OK" : "NOK");
addTestMessage(data, "Free Heap", String(ESP.getFreeHeap() / 1024) + "kb", ESP.getFreeHeap() > 100000 ? "OK" : "NOK");
addTestMessage(data, "ETH MAC Address", String(ETH.macAddress()), ETH.macAddress() ? "OK" : "NOK");
addTestMessage(data, "ETH Local IP", ETH.localIP().toString(), ETH.localIP() ? "OK" : "NOK");
addTestMessage(data, "ETH Full Duplex", ETH.fullDuplex() ? "true" : "false", ETH.fullDuplex() ? "OK" : "NOK");
addTestMessage(data, "ETH Link Speed", String(ETH.linkSpeed()) + "Mbs", ETH.linkSpeed() ? "OK" : "NOK");
addTestMessage(data, "Build Date", String(__DATE__), "OK");
addTestMessage(data, "Build Time", String(__TIME__), "OK");
serializeJson(doc, Serial);
Serial.println();
}

void testLeds() {
Log.notice(F("LED Test" CR));
InfoIndicatorON();
SendReceiveIndicatorON();
Log.notice(F("LED Test Finished" CR));
}

void checkSerial() {
Log.notice(F("READY_FOR_SELFTEST" CR));
unsigned long start = millis();
while (millis() - start < 1000) {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim(); // Remove any extra whitespace
if (input == "SELFTEST") {
Log.notice(F("SELFTEST Launched" CR));
testLeds();
// Print 20 times the test data
for (int i = 0; i < 20; i++) {
testDevice();
delay(1000);
}
Log.notice(F("SELFTEST Finished" CR));
// Restart
ESPRestart(9);
}
}
}
}
#endif
28 changes: 21 additions & 7 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,12 @@ void setup() {
# ifdef ESP32_ETHERNET
setup_ethernet_esp32();
# endif

# if SELF_TEST
// Check serial input to trigger a Self Test sequence if required
checkSerial();
# endif

Log.notice(F("No config in flash, launching wifi manager" CR));
// In failSafeMode we don't want to setup wifi manager as it has already been done before
if (!failSafeMode) setupwifi(false);
Expand Down Expand Up @@ -1507,6 +1513,7 @@ void setupTLS(int index) {
6 - OTA Update
7 - Parameters changed
8 - not enough memory to pursue
9 - SELFTEST end
*/
void ESPRestart(byte reason) {
delay(1000);
Expand Down Expand Up @@ -1862,7 +1869,7 @@ bool loadConfigFromFlash() {
configFile.close();
}
} else {
Log.notice(F("no config file found defining default values" CR));
Log.notice(F("No config file found defining default values" CR));
# ifdef USE_MAC_AS_GATEWAY_NAME
String s = WiFi.macAddress();
sprintf(gateway_name, "%.2s%.2s%.2s%.2s%.2s%.2s",
Expand Down Expand Up @@ -2075,14 +2082,21 @@ void setup_ethernet_esp32() {
ETH.config(ip, gateway, subnet, Dns);
ethBeginSuccess = ETH.begin();
# else
Log.trace(F("Spl eth cfg" CR));
Log.notice(F("Spl eth cfg" CR));
ethBeginSuccess = ETH.begin();
# endif
Log.trace(F("Connecting to Ethernet" CR));
while (!ethConnected && failure_number_ntwk <= maxConnectionRetryNetwork) {
delay(500);
Log.trace(F("." CR));
failure_number_ntwk++;
if (ethBeginSuccess) {
Log.notice(F("Ethernet started" CR));
Log.notice(F("OpenMQTTGateway MAC: %s" CR), ETH.macAddress().c_str());
Log.notice(F("OpenMQTTGateway IP: %s" CR), ETH.localIP().toString().c_str());
Log.notice(F("OpenMQTTGateway link speed: %d Mbps" CR), ETH.linkSpeed());
while (!ethConnected && failure_number_ntwk <= maxConnectionRetryNetwork) {
delay(500);
Log.notice(F("." CR));
failure_number_ntwk++;
}
} else {
Log.error(F("Ethernet not started" CR));
}
}

Expand Down

0 comments on commit a74a6eb

Please sign in to comment.