Running sketch from flash on power up

I have a standard Wisblock core 4631. If I program a sketch, then disconnect and reconnect the USB power, should the sketch run from flash? Same question if you disconnect and reconnect a battery.

Right now, I have to reflash the device from the arduino IDE after a USB power cycle. Am I doing something wrong or do I need an additional module to run from flash on power up?

BSP Library : 0.21.001 Bootloader : s140 6.1.1

The device just has temperature and humidity sensors and a LoRa antenna which connects to TTN and flashes an LED when connected. This works fine when programmed via the IDE.

Thanks
Richard.

Hello @richstimson , welcome back to the forum.

When you program a sketch, the sketch is saved in the flash memory of the RAK4631. Whenever the RAK4631 is powered up after programming a sketch, the sketch will automatically start. There is no other module required.

Can you share the setup() part of your sketch?

Hi @beegee, thanks for the reply. Here’s my setup() code:

void setup() 
{
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  // Initialize LoRa chip.
  lora_rak4630_init();

  // Initialize Serial for debug output
  Serial.begin(115200);
        while(!Serial){delay(10);}
  Serial.println("=====================================");
  Serial.println("Welcome to RAK4630 LoRaWan!!!");
  Serial.println("Type: OTAA");


#if defined(REGION_AS923)
    Serial.println("Region: AS923");
#elif defined(REGION_AU915)
    Serial.println("Region: AU915");
#elif defined(REGION_CN470)
    Serial.println("Region: CN470");
#elif defined(REGION_CN779)
    Serial.println("Region: CN779");
#elif defined(REGION_EU433)
    Serial.println("Region: EU433");
#elif defined(REGION_IN865)
    Serial.println("Region: IN865");
#elif defined(REGION_EU868)
    Serial.println("Region: EU868");
#elif defined(REGION_KR920)
    Serial.println("Region: KR920");
#elif defined(REGION_US915)
    Serial.println("Region: US915");
#elif defined(REGION_US915_HYBRID)
    Serial.println("Region: US915_HYBRID");
#else
    Serial.println("Please define a region in the compiler options.");
#endif
    Serial.println("=====================================");
    
  Wire.begin();
 /* shtc3 init */
  Serial.println("shtc3 init");
  Serial.print("Beginning sensor. Result = ");           // Most SHTC3 functions return a variable of the type "SHTC3_Status_TypeDef" to indicate the status of their execution 
  mySHTC3.begin();                              // To start the sensor you must call "begin()", the default settings use Wire (default Arduino I2C port)
  Wire.setClock(400000);                                      // The sensor is listed to work up to 1 MHz I2C speed, but the I2C clock speed is global for all sensors on that bus so using 400kHz or 100kHz is recommended
  Serial.println();

  if(mySHTC3.passIDcrc)                                       // Whenever data is received the associated checksum is calculated and verified so you can be sure the data is true
  {                                                           // The checksum pass indicators are: passIDcrc, passRHcrc, and passTcrc for the ID, RH, and T readings respectively
    Serial.print("ID Passed Checksum. ");
    Serial.print("Device ID: 0b"); 
    Serial.println(mySHTC3.ID, BIN);                       // The 16-bit device ID can be accessed as a member variable of the object
  }
  else
  {
    Serial.println("ID Checksum Failed. ");
  }

#if 0
   /* LPS22HB init */
    if (!BARO.begin()) {
      Serial.println("Failed to initialize pressure sensor!");
      while (1);
    }
  /* opt3001 init */
  opt3001.begin(OPT3001_ADDRESS);
  Serial.print("OPT3001 Manufacturer ID");
  Serial.println(opt3001.readManufacturerID());
  Serial.print("OPT3001 Device ID");
  Serial.println(opt3001.readDeviceID());

  configureSensor();
#endif
  // Initialize Scheduler and timer
    uint32_t err_code;

  err_code = timers_init();
  if (err_code != 0)
  {
    Serial.printf("timers_init failed - %d\n", err_code);
  }

  // Setup the EUIs and Keys
  lmh_setDevEui(nodeDeviceEUI);
  lmh_setAppEui(nodeAppEUI);
  lmh_setAppKey(nodeAppKey);

  // Initialize LoRaWan
  err_code = lmh_init(&lora_callbacks, lora_param_init,doOTAA);
  if (err_code != 0)
  {
    Serial.printf("lmh_init failed - %d\n", err_code);
  }

  // Start Join procedure
  lmh_join();
}

Hi @beegee, I upgraded the bootloader with these instructions Bootloader Fails to upgrade via BLE - #3 by beegee.

I thought that fixed at it first but I’ve now realised that it only runs correctly when the Arduino IDE serial monitor is connected - otherwise it hangs at this point:

23:18:36.742 → =====================================
23:18:36.742 → Welcome to RAK4630 LoRaWan!!!
23:18:36.742 → Type: OTAA
23:18:36.742 → Region: EU868
23:18:36.742 → =====================================
23:18:36.742 → shtc3 init
23:18:36.742 → Beginning sensor. Result =
23:18:36.742 → ID Passed Checksum. Device ID: 0b100001000111
23:18:36.742 → [OTAA ]
23:18:36.742 → DevEui=AC-1F-09-FF-FE-03-EF-DF
23:18:36.742 → DevAdd=00000000
23:18:36.742 → AppEui=70-B3-D5-7E-D0-04-08-BB
23:18:36.742 → AppKey=A9-03-A3-5F-DB-5C-FA-18-AB-92-7F-4F-0E-58-56-73
23:18:36.780 → [LMH ] Selected subband 1
23:18:36.828 →
23:18:36.828 → BSP Library : 0.21.001
23:18:36.828 → Bootloader : s140 6.1.1
23:18:36.828 → Serial No : 569705986BC2F74B
23:18:36.828 →
23:18:36.869 → [LM ] OnRadioTxDone
23:18:36.869 → [LM ] OnRadioTxDone => RX Windows #1 4989 #2 5995
23:18:36.869 → [LM ] OnRadioTxDone => TX was Join Request

As soon as the serial monitor is connected, I get the following debug (after 2-3 seconds) and it all works fine :

23:18:41.947 → [LM ] OnRadioRxDone
23:18:41.947 → [LM ] OnRadioRxDone => FRAME_TYPE_JOIN_ACCEPT
23:18:41.947 → OTAA Mode, Network Joined!
23:19:42.980 → Sending frame now…
23:19:42.980 → result: Tem:24.82C Hum:57.17%
23:19:42.980 → lmh_send ok count 1

It doesn’t matter if I start the serial monitor after 10 seconds or 5 minutes - it always springs in to life at that point.

I added extra debug in and it stopped at a different place:

23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → =====================================
23:35:37.501 → Welcome to RAK4630 LoRaWan!!!
23:35:37.501 → Type: OTAA
23:35:37.501 → Region: EU868
23:35:37.501 → =====================================
23:35:37.501 → shtc3 init

Again connecting the serial monitor showed me the output above and then continued after a few seconds and started working ok.

Any advice welcome…
Thanks
Richard.

This part of the code will loop forever when no USB cable is plugged in:

What we do in our example sketches is to use a timeout to stop waiting for Serial in case the USB cable is not plugged int:

   // Wait for USB Serial to be ready or terminal to be connected
    time_t timeout = millis(); // Timeout in case the system runs on its own
    // Waiting for Serial
    while (!Serial)
    {
        if ((millis() - timeout) < 5000)
        {
            delay(100);
        }
        else
        {
            // Timeout while waiting for USB Serial
            break;
        }
    }
1 Like

Thanks @beegee, that fixed it :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.