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.


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:
  digitalWrite(LED_BUILTIN, LOW);

  // Initialize LoRa chip.

  // Initialize Serial for debug output
  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");
    Serial.println("Please define a region in the compiler options.");
 /* 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

  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
    Serial.println("ID Checksum Failed. ");

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

  // 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

  // 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

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…

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)
            // Timeout while waiting for USB Serial
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.