Code won't run within init.m

I have a Raspberry Pi connected via a USB serial connection to an Arduino Uno, which is connected to an AM2302 humidity/temperature sensor. I am able to read the data from the sensor into Mathematica and then send the data by email and to a web server. No problems if I run the code within a Notebook.

My next step is to put the code into my init.m file (which is in /usr/share/WolframEngine/Kernel) so that it runs when I start Mathematica. This is where I run into a problem. The code starts to evaluate fine, with an email message letting me know, but then I get these messages:

    Import::format: Cannot import data as HeldExpressions.
    
    DeviceOpen::noclass: 
       A driver for Serial was not found on your local computer or currently
         available paclet sites. If you can locate the driver, add the driver
         directory to $Path or load the driver directly with Get. If you cannot
         locate the driver, contact the device manufacturer or create a driver
         using the Wolfram Device Framework. See http://devices.wolfram.com for
         more information.

The code that worked in the Notebook is the only thing that is in init.m, so it isn’t a matter of a conflict with other code.
The serial connection I successfully make within the Notebook version is

    port = "/dev/ttyACM0"
    dev = DeviceOpen["Serial", {port, "BaudRate" -> 9600}];

Any idea what’s going on

Ken Levasseur
UMass Lowell

Is this the same directory as

FileNameJoin[{$UserBaseDirectory, "Kernel"}]

(I actually have no idea about Raspberry Pi s, etc… just offering the location where I put revised init.m things on my laptop Mathematica.)

David:

Yes, the value of $AddOnsDirectory in the Raspberry Pi version of Mathematica is

/usr/share/WolframEngine/Kernel

The init file evaluates, but for some reason, the Serial connection doesn’t get established when I try to do it as part of init.m.

Not all functionality is completely initialized and available to use during the early stages of kernel startup.

Try using the following in init.m instead:

task := (...);  (* code to be executed *)
RunScheduledTask[task, {1}]

Thanks Ilian, it hadn’t occurred to me that the serial connection wasn’t initialized. I simply put my code into a package and I delayed running it a minute within a ScheduledTask. It worked!