[SOLVED] Start Scratch controller and script together on boot time

I made a simple Scratch script for the BrickPi3 and it runs fine, but I noticed the “Scratch Controller” Lxterminal has to be open for it to work. I’m using Raspian for Robots.

When I open my file (let’s say MyFile.sb), the scratch controller doesn’t automatically open, so my BrickPi does nothing. What I’ve ended up needing to do is start Scratch from Dexter’s executable which first runs a selection screen where I have to select my product and click “start programming,” then opens Scratch Controller and a new Scratch file. Then I have to close the new Scratch file and open MyFile.sb. It seems more cumbersome than should be possible.

  1. Is there a script I could write or a way to open MyFile.sb so that Scratch Controller starts automatically with it, so I can run my file without having to go through this circuitous route?

  2. One step further, but is there a script that would start either Scratch Cotroller or Scratch Controller and MyFile.sb on startup of Raspian?

I tried looking for a Scratch Controller executable to run individually but couldn’t find one.

Hello @ravdawg3
Which version of Raspbian for Robots are you running?
There should already be something in place that lets you double click your own Scratch program so I need to know which version you’re using so I can debug the issue.

As for running the Scratch program at startup, it can only be done partially. There’s no way to force a click on the green flag for example.

Cleo

Hi @cleoqc

I’m using “Raspian GNU/Linux 9 (stretch).” The version of Scratch is 1.4 (it seems like Scratch 2 may also be on the BrickPi3 as I came across it in my digging for the scratch controller, but the Scratch desktop shortcut that actually works opens 1.4).

Also, regarding running Scratch at startup, I’m fine with having to click the green flag or my sprite to run the Scratch script itself. I’m mostly looking for something that will actually open the Scratch Controller terminal and my Scratch .sb file at startup so I can run it without having to go through the circuitous route as mentioned above. Even if it just opens Scratch controller at startup so I just have to open my file manually, but avoid everything else, I’d be fine with it.

Any luck looking into this?

@ravdawg3 I’m looking into this today, and I can totally double click on my scratch file. It will open the controller and Scratch all at the same time.

What’s the “history” of your SD card? Is that a card you’ve had for a while and you ran DI Update on it and if so, how old is your card to start with?

Also, can you check what is the default program for opening a Scratch extension? See this GIF on how to assign it to Scratch for Robots

ScratchDefault

@cleoqc

The SD card is a brand new one that I got with the BrickPi starter kit. I had initially installed the image for CINCH but overwrote it with Raspian for Robots after 1 use. I’ll try to set the default as you showed and get back to you.

Edit:
I checked the default for my .sb file; it was blank. I set it to Scratch for Robots as you showed, but now ONLY the Scratch Controller opens. The .sb file itself does not. I tried changing the default to “Scratch” instead of “Scratch for Robots,” and the .sb file opened in Scratch 1.4 again, but without the Scratch controller. I’m still not sure how to get both to open at the same time.

When did you update your card to Raspbian for Robots? Is it possible it’s not the newest one?

I wrote the image on 7/13/18, and the ZIP folder containing it is labeled “2018.06.27_Raspbian_For_Robots_by_Dexter_Industries-stretch” - is there a newer version than this one?

I can try re-writing the disc image if you think it’s worth a shot. Could you provide a link to the latest image, and is there anything special I should do to first wipe the current image from the SD card?

If you want to write a script file, or have it run at boot (there are various approaches ) you would need the following line:

/usr/bin/python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /path/to/your/scratch/program.sb

You could create a systemd setup, or use rc.local

Cleo

Hi @cleoqc

Unfortunately, I’m still not having any success. Here’s what I’ve tried since my last comment:
-Cleared SD card completely with Windows DiskPart and re-wrote image for Raspbian For Robots (the 6/27/18 zip is still the latest up on SourceForge).
-Ran get-update and get-upgrade

For some reason, the BrickPi3 SPI had no response, so I followed this: [SOLVED] No SPI response

-Rewrote my Scratch script and tested it again. It runs when I do what I originally did (open Scratch for Robots, select BrickPi and Start Programming, open my .sb file, run)

Unfortunately, these seems to have made things even worse. Now there seems to be no just plain “Scratch” program on the Raspberry Pi - only Scratch 2 and Scratch for Robots. So I can’t even open my .sb file by double-clicking it. When Scratch for Robots is the default program, double clicking my .sb file only opens the Scratch Controller, like I mentioned previously. But there’s no “Scratch” default option, so now the only way I can open the .sb file itself is to go back to clicking Scratch For Robots, selecting BrickPi, choosing Start Programming, then when Scratch boots up, opening my .sb file through the Scratch GUI.

So I’m still doing this workaround and clicking the .sb file is essentially useless.

Also, maybe the same core issue is at play here, but I tried both systemd and rc.local methods by adding the code you gave (and of course replacing my .sb file path), and nothing booted up at startup. Not even the Scratch GUI or Scratch controller.

Is there any way to troubleshoot this or to show you what’s going on? I’m at a loss for what else to do.

1 Like

@ravdawg3
I’ve tested it again on mine, with a brand new SD card, and it works.
Now I’m running sudo apt-get update and sudo apt-get upgrade` to see it it breaks it.

Note: there shouldn’t be a plain Scratch` only Scratch for Robots and Scratch 2.
When I double click my .sb file it launches both the controller and Scratch.

I will come back as soon as the upgrade is done, to see if that replicated your issue.

Cleo

It finally updated/upgraded…

The double click didn’t work at first, I had to relink the .sb extension to Scratch for Robots.
But apart from that, I can still double click my file(after I’ve reassociated it)
and it works.

if you run
/usr/bin/python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /path/to/your/scratch/program.sb
from a terminal, what happens?

Hi @cleoqc,

I got it! It seems Scratch For Robots doesn’t play nicely with spaces in the file name. Here’s what I did:

I tried double-clicking again like you, but got the same result as before (only Scratch Controller opened, even after I double checked that the .sb extension was linked to Scratch For Robots).

I ran the terminal code as you wrote as well, and my result was:
-Scratch controller and Scratch For Robots (new file) opened
-Scratch For Robots gave me an error about not being able to find my file

My file was named “Asus Remote Start.sb” and was located at home/pi/Desktop/*. I tried the terminal code first with underscores instead of spaces, then again with actual spaces when it gave me the error, then with no spaces when I still got the “file not found” error. So then I just changed my filename and deleted the spaces, to “AsusRemoteStart.sb”. I ran the terminal code a final time, and it worked. I double-clicked my .sb file on the Desktop, and that worked, too!

I’ll try the code to run it at boot later, but I imagine that’ll work just fine now as well.

As an aside, that’s odd that a plain “Scratch” shouldn’t be available. Not that I want it, but it did appear the first time I set up my BrickPi3, before I wiped the SD card and tried again.

Thanks for troubleshooting with me.

1 Like

Actually, the run on boot still isn’t working. I’ve tried systemd and rc.local methods as described here: https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/

For systemd, in the Unit file, I tried the code you wrote above:
ExecStart=/usr/bin/python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /home/pi/Desktop/AsusRemoteStart.sb

Nothing ran on boot or after several minutes of waiting after booting up. Same result with rc.local method when I put the following in the rc.local file:

sudo python /usr/bin/python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /home/pi/Desktop/AsusRemoteStart.sb &

I also tried just “sudo python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py” in rc.local to see if the Scratch GUI would boot up, but that also did nothing.

What am I doing wrong?

Hello @ravdawg3

Great news about figuring it out! I should have thought about the spaces, using spaces in a filename is always tricky at one point or another.

For systemd to work at boot, you have to enable it.
Did you run

sudo systemctl enable <your systemd file>

And in rc.local, it should read:
sudo python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /home/pi/Desktop/AsusRemoteStart.sb &

Hi @cleoqc

For systemd, I did enable it. The code I used was actually from that link, sudo systemctl enable AsusRemoteStart.service
I just tried “sudo systemd enable” and it gave me an error: “Excess arguments.”

For rc.local, I tried that code without any success. Still nothing happened on boot. This time, I tried adding the log file code > /home/pi/Desktop/log.txt 2>&1 after the ampersand, and the log file created after reboot was blank. Any thoughts?

Hi @ravdawg3,

I researched this a little bit and it looks like systemd is not the default choice for starting GUI apps. On Raspbian it’s better if you go with LXDE’s autostarting mechanism.

You can autostart a GUI application with LXDE for all users by placing commands in /etc/xdg/lxsession/LXDE-pi/autostart file or by modifying the LXDE autostart file for a specific user (the pi) by adding commands in $HOME/.config/lxsession/LXDE-pi/autostart.

In our case, we modify the autostart configuration for the pi user. So, type in nano $HOME/.config/lxsession/LXDE-pi/autostart (without a sudo) and add in your command that needs to run on GUI-boot-up. Make sure to place an @ symbol before each command you have. Add something like this:

@/usr/bin/env python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /home/pi/Desktop/yourprogram.sb

Just to give you an example, my autostart file looks this way:

lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@/usr/bin/env python /home/pi/Dexter/lib/Dexter/Scratch_GUI/Scratch_Start.py /home/pi/Desktop/yourprogram.sb

Now, save the file and reboot your Pi. The next time the Pi boots up, your Scratch program will pop up as a GUI.

@cleoqc, check this out.

Thank you!

@RobertLucian,

Thanks! This works exactly as desired.

@cleoqc, thanks again for helping troubleshoot.

This topic was automatically closed after 4 days. New replies are no longer allowed.