Setting up a Headless Intel Edison System
Repeatedly connecting to the Intel Edison over the serial terminal (over the USB) may sometimes be a painful exercise. Further, to allow multiple users to access the Intel Edison system is not possible under the current setup. A neat workaround for the same would be using the inherent Wifi capabilities of the Edison to allow for Wifi based connectivity to your setup.
You have already seen how to connect the Edison to the Wifi networks available in the vicinity. Once connected over the serial terminal to your Edison, run the following command:
For further details please refer to the links listed in the Intel Edison setup
To configure remote network connectivity to the Intel Edison, you would need to setup a account password. If you haven't already done so, please enter the following command on the Edison.
You should see a screen with the following printed on it as you enter your password.
---Configure Edison: Device Password
---Enter a new password (leave empty to abort)
---This will be used to connect to the access point and login to the device.
---Please enter the password again: *
---The device password has been changed.
Creating the Mailer Code
The code for this section has been modified from the original source at this tutorial by Cody Giles.
Note that the code above was developed for the Raspberry Pi and may be familiar to students who have taken a previous IoT course at Columbia. With minimal modifications it can be used for the Intel Edison as well.
The code code below has a few of those modifications for your reference.
Instructions on using the code are right after this code snippet.
__author__ = 'Cody Giles' __license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License" __version__ = "1.0" __maintainer__ = "Cody Giles" __status__ = "Production" import subprocess import smtplib from email.mime.text import MIMEText import datetime def connect_type(word_list): """ This function takes a list of words, then, depeding which key word, returns the corresponding internet connection type as a string. ie) 'ethernet'. """ if 'wlan0' in word_list or 'wlan1' in word_list: con_type = 'wifi' elif 'eth0' in word_list: con_type = 'ethernet' else: con_type = 'current' return con_type # CHANGE TO YOUR ACCOUNT INFORMATION # Account Information to = 'email@example.com' # Email to send to. gmail_user = 'firstname.lastname@example.org' # Email to send from. (MUST BE GMAIL) gmail_password = 'password' # Gmail password. smtpserver = smtplib.SMTP('smtp.gmail.com', 587) # Server to use. smtpserver.ehlo() # Says 'hello' to the server smtpserver.starttls() # Start TLS encryption smtpserver.ehlo() smtpserver.login(gmail_user, gmail_password) # Log in to server today = datetime.date.today() # Get current time/date arg='ip route list' # Linux command to retrieve ip addresses. # Runs 'arg' in a 'hidden terminal'. p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE) data = p.communicate() # Get data from 'p terminal'. # Split IP text block into three, and divide the two containing IPs into words. ip_lines = data.splitlines() split_line_a = ip_lines.split() split_line_b = ip_lines.split() # con_type variables for the message text. ex) 'ethernet', 'wifi', etc. ip_type_a = connect_type(split_line_a) ip_type_b = connect_type(split_line_b) """Because the text 'src' is always followed by an ip address, we can use the 'index' function to find 'src' and add one to get the index position of our ip. """ ipaddr_a = split_line_a[split_line_a.index('src')+1] ipaddr_b = split_line_b[split_line_b.index('src')+1] # Creates a sentence for each ip address. my_ip_a = 'Your %s ip is %s' % (ip_type_a, ipaddr_a) my_ip_b = 'Your %s ip is %s' % (ip_type_b, ipaddr_b) # Creates the text, subject, 'from', and 'to' of the message. msg = MIMEText(my_ip_a + "\n\n" + my_ip_b) msg['Subject'] = 'IPs For Intel Edison on %s' % today.strftime('%b %d %Y') msg['From'] = gmail_user msg['To'] = to # Sends the message smtpserver.sendmail(gmail_user, [to], msg.as_string()) # Closes the smtp server. smtpserver.quit()
Copy paste this code above using an editor of your choice. By default, the vi editor is available on the Intel Edison yocto image. You may choose to install one of the other editors, such as nano, as described in the link on additional installations.
Use (on the terminal), depending on your choice of editor:
Modify the email address & password fields. Please note to use a GMAIL ID for the sender's address. Save this file.
Test the code above by running
You should expect to see some errors with regards to notifications.
Go to https://myaccount.google.com/securityusing the sending Gmail ID as login and as part of the "Connected Apps & SItes" section, set "Allow less secure apps" to ON.
Set this to ON.(Google Settings as on 8th January, 2016)
Try running the command again. You should have eliminated the issue here and be able to see an email in your addressee email account.
Make the python file an executable.
chmod +x startup_mailer.py
Lastly, make a note of the file path using the "pwd" command.
Setting Code to Startup
This next section diverges from the tutorial. This has limited formal troubleshooting documentation and is mostly based on this community post.
You may also want to review Stephanie Moyerman's post as the source material for the following section.
Begin by navigating to the /etc folder and creating a init.d directory there as follows.
cd /etc mkdir init.d cd init.d
Following this, we shall get started on creating the shell script that shall actually run at startup time. (As an alternative, you can proceed by directly creating a copy of the python file in this folder. Please refer the Additional Notes & Troubleshooting Section for an overview of this method)
Inside the script add the following lines:
The 3rd line is fairly straightforward. This provides the path to the python file. This path was obtained earlier using the pwd command.
Note the significance of the 1st and 4th lines. The 1st line indicates to the executable (that we shall create) that the file is a shell script. The last line serves to indicate the end of the service that we are about to create.
The 2nd line MAY not be required for your script. On the test system, this delay time was needed to execute the file at bootup. You can modify the values or wholly ignore it as per your system's requirements.
Save and exit your shell script.
Back on the command line, create an executable out of your shell script.
chmod u+x startup.sh
In case of no errors, proceed with the final section of this tutorial by typing in the following on your command line.
update-rc.d startup.sh defaults
This step finalizes the startup service creation for your python file.
Verify this step by typing in the following command:
ls -l /etc/rc?.d/*startup*
In case you see the screen as below, your setup process should be complete. Reboot your system and verify receiving the email.
Finally, from a remote system, you can ssh into your Edison, without the need for it to be explicitly connected over USB.
On the command lines of Mac & Linux, this should be fairly straightforward. You can use putty as an SSH utility for Windows.
ssh <username>@<wifi ipaddress>
At the time of testing, this translated to a "ssh email@example.com" command
Note of Interest: You could also display your Wifi IP on your LCD screen. Use the code as described on
ADDITIONAL NOTES & TROUBLESHOOTING:
1. In case you do not receive an email notification, first review the validity of your executables & code itself. If your codes & executables work correctly, try playing around with the sleep time. On the test system, a time of 30 (in the "sleep 30" line) was finalized after a number of attempts.
2. If you wish to instead use your python code directly, that is indeed possible. You must however note to add the equivalent of the "#!/bin/sh" line in your python code & appropriately call the sleep & exit conditions from within python. Lastly, do place the file inside the etc/init.d directory. This approach is left as an exercise for the reader.
3. In case of generic python & shell script errors, please note that both python & bash give importance to indentations & spacing respectively. Particularly if you are a first time shell programmer, do take note of the space-sensitivity of the bash editor in particular.