How to run Google Assistant library (on AIY kit), upon startup of Raspberry Pi? - raspberry-pi

We set up a voice kit using Raspberry Pi (using "the MagPi essentials AIY Projects" manual). We are able to enable Google Assistant using the command "src/assistant_library_demo.py" in the dev terminal, after Raspberry Pi starts up. We would like to embed the voice kit in a stuffed animal with a portable power supply (i.e., used to charge cell phone on the go). But when the portable power supply is charged, the Raspberry Pi resets. That requires us to go back into the Raspberry Pi, open the dev terminal, and run the Google Assistant file.
My question: Is it possible to run a startup script that automatically runs Google Assistant upon Raspberry Pi starting up? How to do this?

I ended up creating a crontab job after a 10 second wait. Starting right at boot didn't give it enough time for the internet to connect fully.
In terminal type:
crontab -e
Choose an option if it asks how you want to open/edit the file. Then at the bottom put:
#reboot sleep 10 && /home/pi/pathtofile > /home/pi/cronlog 2>&1
Save the file and reboot or pull the cable out and plug it back in. The cronlog helped me troubleshoot this whole process and get feedback on why it didn't work.

Take a look at this page. It tells you how to set up a service which will run automatically.
If the link has gone bad, here is a short explanation of it:
Create a file called my_assistant.service in the src directory, and put in the following code
[Unit]
Description=My awesome assistant app
[Service]
Environment=XDG_RUNTIME_DIR=/run/user/1000
ExecStart=/bin/bash -c 'python3 -u src/my_assistant.py'
WorkingDirectory=/home/pi/AIY-projects-python
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Where the file says src/my_assistant.py, replace my_assistant with your program's filename. Now go to the folder that file the .sevice file is in, and run the command sudo mv my_assistant.service /lib/systemd/system/. This code moves the file to the services folder. Now you can run the following commands to change the service:
Enable the service- sudo systemctl enable my_assistant.service
Disable it- sudo systemctl disable my_assistant.service
Start it (just runs it once, enabling makes it run on startup)- sudo service my_assistant start
Stop it- sudo service my_assistant stop
See the logs, when the program was started and if an error occurred- sudo service my_assistant status

Related

are there ways to use VS code plugins in google cloud shell?

I have a few quick navigation plugins such as "block travel" I use all the time. Is there a way to use these in cloud shell?
I imagine there are some restrictions, but even some simple editor plugins can be huge timesavers.
While I'm at it - alt-D to duplicate a line, or transpose lines - some of those seem to be missing and hard to use key remapping to get working, at least within the shell. In general maybe keyboard shortcuts seem to get trapped by the browser or PWA wrapper. I'm using cloudshell as a webapp on a chromebook FWIW, for various secure projects.
I have come up with a solution that covers both aspects of your question
To get Unlimited Persistent Disk:
You can use Google Cloud Storage FUSE
Google Cloud Storage FUSE lets you mount a GCS bucket as a folder to your linux instance. By doing that you get an “unlimited '' persistent disk and it is super simple to set up since gcsfuse is already installed in cloud shell.
1. Create a GCS bucket (you only need to run this once) -- replace BUCKET_NAME with any name:
gsutil mb "gs://BUCKET_NAME/"
2. Create a local directory for mounting -- replace FOLDER_NAME with the chosen directory name:
mkdir /home/[USER]/[FOLDER_NAME]
chmod 777 /home/[USER]/[FOLDER_NAME]
3. Mount the bucket onto the local filesystem (note: you need to re-run this every time Cloud Shell starts)
gcsfuse -o nonempty -file-mode=777 -dir-mode=777 --uid=1000 --debug_gcs [BUCKET_NAME] /home/[USER]/[FOLDER_NAME]
To use third party plugins in cloud shell:
You can use an environment customization script (.customization_environment) as mentioned in the public documentation. It allows you to install additional packages into your Cloud Shell environment when it starts.
For reference, below are the steps to install VS Code plug-in.
Step 1:
To install the VSCode server, run the script named visual_studio_code.sh as below, in the root directory workspace of Cloud Shell Editor.
visual_studio_code.sh file:
export VERSION=`curl -s https://api.github.com/repos/cdr/code-server/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
wget https://github.com/cdr/code-server/releases/download/$VERSION/code-server-3.10.2-linux-amd64.tar.gz
tar -xvzf code-server-3.10.2-linux-amd64.tar.gz
cd code-server-3.10.2-linux-amd64
Run the script using the below command in shell,
./visual_studio_code.sh
if getting permission denied error then run this following command in shell,
chmod +x visual_studio_code.sh
./visual_studio_code.sh
Step 2:
Make a customization script in the root directory workspace in Cloud Shell Editor to start VS Code Server on boot with the below commands :
.customization_environment file :
#!/bin/sh
#.customize_environmnet run in background as root, wait for your user to initialize
sleep 20
sudo -u [USER] /home/[USER]/code-server-3.10.2-linux-amd64/code-server --auth none --port 9090
Step 3:
To view Visual Studio Code server on port 9090 :
Click on Web Preview > Change Port > 9090
If getting a 404 error, remove ‘?authuser=0’ from the url.
Visual Studio Code Server will now be running on the browser!!!
Block travel navigation plugin:
To have the block travel navigation plugin in cloud shell,follow the below commands and run them in shell in root directory:
wget https://github.com/efatsi/block-travel/archive/refs/tags/v1.0.0.tar.gz
tar xzvf v1.0.0.tar.gz
ls
#You will see block-travel-1.0.0
block-travel-1.0.0/keymaps/block-travel.cson --auth none --port 9090
#You might get Permission denied if yes, then follow the next two commands else go to webport view in 9090
chmod +x block-travel-1.0.0/keymaps/block-travel.cson
block-travel-1.0.0/keymaps/block-travel.cson --auth none --port 9090
Open the webport view in 9090, you will be able to navigate through the vs code files using :
Alt+up for block-travel.jumpUp
Alt+shift+up for block-travel.selectUp
Alt+down for block-travel.jumpDown
Alt+shift+down for block-travel.selectDown
WARNING: This should not be considered a long term solution, just a stop gap until this is supported in an easier fashion.
This might not be the greatest idea but it does seem to work for the vim extension I tried in my environment. Probably best to make a request through the in product feedback to get it officially added but until then you can follow these steps.
Upload the .vsix package to your $HOME directory.
Unzip the package into the /google/devshell/editor/theia/plugins directory. This action will not persist so you'll want to add the command to the .customize_environment script actions.
e.g.
sudo unzip vscodevim.vsix -d /google/devshell/editor/theia/plugins/vscode-vim
Now for the questionable part. You'll want to install the pslist package to make life easy so you have access to the rkill command. You probably also want to add this to the .customize_environment file as well since it also will not persist.
sudo apt install pslist
Now we need to get the process id for the editor. Currently this seems to be spawned by a supervisord command, which also spawns the tmux section so we're going to grab the process id that is from the runuser command it spawns (and filter for the theia one just in case).
ps ax | grep runuser | grep "theia start"
Then we can use rkill to kill the process and all of the its children, which will cause supervisord to restart it for us and the plugin should be available.
sudo rkill PID_OF_GREP_OUTPUT
I'm not sure the best way to script the rkill command yet, since I don't know the timing of when it's up vs the .customize_environment execution, so right now I run this each time I start up a new VM.
If anything goes horribly wrong, you should be able to request a restart of the VM from the menu options and get a fresh one.
Cloud Shell offers VS Code editor experience through Theia. Did you try cloud Code editor in the cloud shell? that is exposed through "Open Editor" button on the top right, this will open cloud code editor that gives you VSCode experience. You have all the navigation keys that are available in the editor.

How can I activate a service after the desktop on raspberry pi (not immediately with boot)?

I want to launch an executable script python automatically. How can I activate a service after the desktop on raspberry pi?
I want to launch an executable script python automatically. How can I activate a service after the desktop on raspberry pi?
When I launched the service at the boot, it's failes (need some library python).
I'll try autostart, sudo nano /etc/rc.local and service.
Nothing works.
So, the answer i think is to launch the script automatically after boot.
Thanks a lot for your help
You can insert the script run in crontab, for it to run after a few seconds upon reboot. For example:
#reboot sleep 20 && /usr/bin/python my_script.py
Hope it helps!

How to remote open playframework app (prod)?

I've done my development of play framework,and I have dist it to be a binary version uploaded to the server.
So,my problem is everytime i open it on ssh , it will shutdown when i logout , how to make it running until i shut it down manually?
You can use screen. It will start a new session that will stay open even if you disconnect from the server. You can then, on the next ssh, reconnect with screen -r -D. There are a ton of useful features such as creating multiple "screens" with ctrl+a and then c (see e.g. screen intro on digitalocean for more) so you can continue working on the server while the app is running
add nohup to your launch command to ensure it is not terminated when you exit your shell.
nohup exec /path/to/my/app/bin/myapp -Dhttp.port=8080

Pi 3 + Microstack GPS communication

I bought a Raspberry Pi 3 Model B, a microstack baseboard and a microstack GPS.
I followed the instructions written in the official documentation but I can't connect to the GPS.
Basically, the GPS led flashes so it means it has find a gps fix, but I can't see any data from the PI.
For example, if I try
cat /dev/ttyAMA0
I get always nothing. I already used raspi-config to disable kernel serial and to enable i2c and spi.
Also if I try
stty -F /dev/ttyAMA0 ispeed 4800 && cat </dev/ttyAMA0"
I get an error.
Anyone has any advice, please?
Or maybe these modules are not compatible with Pi 3?
Thanks a lot!
It's hard telling what has been done, or not done. The 'official' documentation from Microstack provides two methods of setting up the serial port. It then continues with
Automatically Starting gpsd
To automatically start the gps service when the Raspberry Pi® boots, re­configure the GPS
daemon by typing into a terminal
sudo dpkg-­reconfigure gpsd
● Choose <yes> when asked if you want to start gpsd automatically.
● Choose <no> when asked “should gpsd handle attached USB GPS
receivers automatically” .
● When asked which “Device the GPS receiver is attached to” , enter
/dev/ttyAMA0 .
● Accept the defaults for other options.
If you have configured your serial port as per instructions and configured gpsd to automagically start and pick up the device you have just setup, ...then, since the gpsd is handling the device (right?), the device will be busy and the command cat /dev/ttyAMA0 should return cat: /dev/ttyAMA0: Device or resource busy...but, it did not.
Either the serial is not configured as per instructions, or the gpsd is not running and picking up the device, or both.
The goal is not to cat your gps data, but the cat not returning an error indicates something is amiss.
Typically, it's just a typo. But it can be from skipping a step in the process, or mixing instructions from various sources so that the left hand doesn't know what the right hand has done.
Try:
sudo killall gpsd if it returns gpsd: no process found that answers one question.
If it returns with no error, follow it up with cat /dev/ttyAMA0. If it returns nothing, the direction you should look is your serial port configuration.
If sudo dpkg-reconfigure gpsd is not allowing you to reconfigure (I thought it was just me) you can modify the configuration of gpsd by using your favourite editor, e.g. sudo nano /etc/default/gpsd and entering something like:
# Default settings for the gpsd init script and the hotplug wrapper.
# Start the gpsd daemon automatically at boot time
START_DAEMON="true"
# Use USB hotplugging to add new USB devices automatically to the daemon
USBAUTO="false"
# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyAMA0"
# Other options you want to pass to gpsd
GPSD_OPTIONS="-n -G"
You may find the -n and -G useful. Save and restart.
I finally found a solution:
On the Pi 3, there are more steps to use serial, you have to disable the BT.
Edit config file:
sudo nano /boot/config.txt
Add this at the bottom:
dtoverlay=pi3-disable-bt-overlay
Then run this:
sudo systemctl disable hciuart
This article was very usefull for me
http://spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3/#comment-67160
and i recommand you to read it
Now on RPI3 the Serial port
ttyAMA0 is used by BlueTooth
ttySO refers to the GPIO
The Alias Serial0 refers to the GPIO both on RPI2 and RPI3.
So :
after a reinstallation of Jessie with a recent version to be sure all is clean
as usual
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo raspi-config
go to Advanced options : Would you like a login shell to be accessible over serial? response "yes" (not as in the microstack datasheet )
$ sudo apt-­get install python3-­microstacknode
To install the gpsd standard tools
$ sudo apt­-get install gpsd gpsd-­clients python­-gps
edit GPSD by
$ sudo nano /etc/default/gpsd
and put the options :
START_DAEMON="true"
USBAUTO="false"
DEVICES="/dev/serial0"
GPSD_OPTIONS="-n -G"
equally
$ sudo nano /boot/cmdline.txt
remove
console=serial0,115200
and save
$ sudo nano /boot/config.txt
you must have at the end
enable_uart=1
Disable the console by
$ sudo systemctl stop serial-getty#ttyS0.service
$ sudo systemctl disable serial-getty#ttyS0.service
i have equally done $ sudo chmod 775 ../../dev/ttySO ( i dont know if it is really necessary )
and obviously a reboot
and miracle the gps works with
cgps -s
NB: you must have a fix on the GPS ( the red led flashes )
jpherrenknecht

Autostart deluge daemon 1.3.10 on boot on Raspberry Pi

I have already been through different tutorials on how to set raspberry pi into a torrent box, but I think most of the how-to tutorials are out-dated.
I have also check my version of deluge daemon using his command:
deluge -v
And it returns this:
deluged: 1.3.10
libtorrent: 0.16.18.0
I have followed the How-To Geek tutorial so far.
Link: http://www.howtogeek.com/142044/how-to-turn-a-raspberry-pi-into-an-always-on-bittorrent-box/
After I stared to get errors I have fully uninstalled and deleted all the files of deluge.
The tutorial suggests this command:
sudo wget -O /etc/default/deluge-daemon http://cdn5.howtogeek.com/wp-content/uploads/gg/up/sshot5151a8c86fb85.txt
But there is no such file as /etc/default/deluge-daemon, instead there is a deluged named file (maybe short for deluge-daemon in new version)
Basically what the command does is that it copies the content of the file http://cdn5.howtogeek.com/wp-content/uploads/gg/up/sshot5151a8c86fb85.txt to the file located at /etc/default/deluge-daemon.
As I can't find deluged-daemon, I chose to do this with /etc/default/deluged
The original content of /etc/default/deluged:
# Defaults for deluged initscript
# sourced by /etc/init.d/deluged
# change to 1 to enable daemon
ENABLE_DELUGED=0
Content provided on the file http://cdn5.howtogeek.com/wp-content/uploads/gg/up/sshot5151a8c86fb85.txt:
# Configuration for /etc/init.d/deluge-daemon
# The init.d script will only run if this variable non-empty.
DELUGED_USER="pi" # !!!CHANGE THIS!!!!
# Should we run at startup?
RUN_AT_STARTUP="YES"
But both files looks different and the deluge daemon doesn't load up on startup.
I managed to resolve this issue using this guide: http://dev.deluge-torrent.org/wiki/UserGuide/Service/systemd.
Follow instructions from this guide (you may skip deluge-web instructions).
Note that deluge user was created with --home /var/lib/deluge.
Update auth (setup an account) and core.conf (set allow_remote flag) files in home dir of deluge user (as opposed to home dir of pi user that is usually mentioned in other tutorials).
Reboot