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.
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.
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();
}
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:
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;
}
}