IOT RECIPE: 04 Headless Raspberry Pi programming

How many monitor you need to have to program a Raspberry Pi. 
No one, as you now: with ssh is possible to interact with Raspberry Pi from a remote machine. The majority of distributions enable SSH by default, so you can access it once connected to your local network with command ssh username@rpiname.local and replace rpiname with raspberry host name.
With text-only Nano editor is also always possible to develop directly on ssh interface. 

Is possible otherwise to develop a complex project like IOT Energy Monitor Webserver using a combination of SFTP, your favourite Text Editor and of course Terminal to run WebServer and debug.

SFTP Mac OSx Drive Mount 

"Cyberduck is a libre FTP, SFTP, WebDAV, S3, Backblaze B2, Azure & OpenStack Swift browser for Mac and Windows."


"Map or Mount Amazon Cloud Drive, Google Drive, Dropbox, Box, OneDrive, SFTP, WebDAV, S3 and more as a Network Drive. Seamless access to files without sync."
ExpanDrive is not free.

Extreme text Editor

Sublime Text

"Sublime Text is a sophisticated text editor for code, markup and prose. You'll love the slick user interface, extraordinary features and amazing performance."

Atom Text Editor

"Atom is a text editor that's modern, approachable, yet hackable to the core—a tool you can customize to do anything but also use productively without ever touching a config file."


Bash (Bourne Again Shell)

Terminal is the OSx shell. From here you can connect via ssh to the Raspberry Pi shell.
OSx terminal can be customized in colors and transparency.
Copy & Paste work as expected in terminal, and more than one session may be opened, if you need to run a server and rename/move/copy/list/... at the same time.

Ssh first connection

Once connected raspi to the local network, you need to find IP to access and configure the network name (and other configurations). Personally i use router DHCP list to find the assigned IP, but more options are available, like ip scanner.
The first connection to ssh host, terminal ask you if you want to add ssh key to known hosts, tap "yes" to gain access to the machine.

Ssh offending message (RSA key changed)

Sometime, when IP changes on Raspi, you are not more allowed to connect to remote raspi: an update is needed.
Use the command "ssh-keygen -R rpiname.local" to clean RSA key, then reconnect via ssh and generate another key, as a first connection.

Lost connection while running flask server

What happens if you lose connection while a flask server is running?
The session is closed and the server is terminated. To change interval here there is a guide to change timeout interval on Raspi.  
If you want to run the flask server on startup, follow the next step.

Running python script on startup via systemd

The simplest way to run a python script on startup on Debian distro (like Raspbian) is via UNIT files.
There are a lot of guides on the web, but the majority is related to init.d script. This approach is more simpler and effective (related also to running order). I had a lot of headache running python script via init.d cause "exited" python script status.

To run a script via systemctl follow these steps:
1) Be sure your python script works as expected
2) Paste the python executable version on top of the pythons script and save: #!/usr/bin/python
3) Make the script executable with chmod +x
4) Create a configuration file with sudo nano /lib/systemd/system/flask.service
5) Paste the following content to the nano editor session and save

Description=Flask server



6) Set unit file permission with: sudo chmod 644 /lib/systemd/system/flask.service
7) Load unit file with: sudo systemctl daemon-reload
8) Enable unit file with: sudo systemctl enable flask.service
9) Reboot

After reboot you can control status of the service, stop and restart using these commands:
  • sudo systemctl status flask.service
  • sudo systemctl stop flask.service
  • sudo systemctl start flask.service
  • sudo systemctl restart flask.service

1 comment:

  1. Very informative blog. Energy Monitor is about close and accurate energy consumption monitoring and reporting of data.


Popular posts