Labs

Programming Languages Labs

Project: Base Code Hello

Go to your git "TheIoTLearningInitiative" repository, under "InternetOfThings101" directory

    root@board:~# cd TheIoTLearningInitiative/InternetOfThings101
    root@board:~/TheIoTLearningInitiative/InternetOfThings101#

Create a file called main.py to host our project code and print in console "Hello Internet of Things 101"

    root@board:~/TheIoTLearningInitiative/InternetOfThings101# vi main.py
#!/usr/bin/python

import time

if __name__ == '__main__':

    while True:
        print "Hello Internet of Things 101"
        time.sleep(5)

# End of File
    root@board:~/TheIoTLearningInitiative/InternetOfThings101# python main.py
    Hello Internet of Things 101
    ^C
    Traceback (most recent call last):
      File "main.py", line 11, in <module>
        time.sleep(5)
    KeyboardInterrupt
    root@board:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        main.py

nothing added to commit but untracked files present (use "git add" to track)
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git add main.py 
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   main.py                                                                          
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git commit -s
Internet of Things 101: 5.1.4.6 PL: Project: Base Code Hello

Create a file called main.py to host our project code and print in
console "Hello Internet of Things 101"

Signed-off-by: Name LastName <email@gmail.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#       new file:   main.py
#
[master 4d18e65] Internet of Things 101: 5.1.4.6 PL: Project: Base Code Hello
 1 file changed, 11 insertions(+)
 create mode 100644 InternetOfThings101/main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git push
Username for 'https://github.com': 
Password for 'https://xe1gyq@github.com': 
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 648 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
   7b6f34c..b197227  master -> master
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git show
commit b1972276af178f5542e4c6a1219dfd363bb16e1b
Author: Name LastName <email@gmail.com>
Date:   Sat Apr 30 22:21:44 2016 +0000

    Internet of Things 101: 5.1.4.6 PL: Project: Base Code Hello

    Create a file called main.py to host our project code and print in
    console "Hello Internet of Things 101"

    Signed-off-by: Name LastName <email@gmail.com>

diff --git a/InternetOfThings101/main.py b/InternetOfThings101/main.py
new file mode 100644
index 0000000..0311096
--- /dev/null
+++ b/InternetOfThings101/main.py
@@ -0,0 +1,11 @@
+#!/usr/bin/python
+
+import time
+
+if __name__ == '__main__':
+
+    while True:
+        print "Hello Internet of Things 101"
+        time.sleep(5)
+
+# End of File
(END)

Project: Signal Handler

Add function to handle signals e.g. CTRL-Z

    root@edison:~/TheIoTLearningInitiative/InternetOfThings101# vi main.py
#!/usr/bin/python

import signal
import sys
import time

def functionSignalHandler(signal, frame):
    sys.exit(0)

if __name__ == '__main__':

    signal.signal(signal.SIGINT, functionSignalHandler)

    while True:
        print "Hello Internet of Things 101"
        time.sleep(5)

# End of File
    root@edison:~/TheIoTLearningInitiative/InternetOfThings101# python main.py 
    Hello Internet of Things 101
    ^Z
    [1]+  Stopped(SIGTSTP)        python main.py
    root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   main.py

no changes added to commit (use "git add" and/or "git commit -a")
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git add main.py 
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git commit -s
Internet of Things 101: 5.1.4.6 PL: Project: Signal Handler

Add function to handle signals e.g. CTRL-Z

Signed-off-by: Name LastName <email@gmail.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#       modified:   main.py
[master ce05cfa] Internet of Things 101: 5.1.4.6 PL: Project: Signal Handler
 1 file changed, 7 insertions(+)
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git push
Username for 'https://github.com':                                    
Password for 'https://xe1gyq@github.com':
Counting objects: 4, done.                                                              
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 679 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)                                              
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
   b197227..ce05cfa  master -> master                                              
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
commit ce05cfa63558da802eed7e79d66b7bbae6c677b9
Author: Name LastName <email@gmail.com>
Date:   Sat Apr 30 22:38:39 2016 +0000
    Internet of Things 101: 5.1.4.6 PL: Project: Signal Handler

    Add function to handle signals e.g. CTRL-Z

    Signed-off-by: Name LastName <email@gmail.com>

diff --git a/InternetOfThings101/main.py b/InternetOfThings101/main.py
index 0311096..f2f1045 100644
--- a/InternetOfThings101/main.py
+++ b/InternetOfThings101/main.py
@@ -1,9 +1,16 @@
 #!/usr/bin/python

+import signal
+import sys
 import time

+def functionSignalHandler(signal, frame):
+    sys.exit(0)
+
 if __name__ == '__main__':

+    signal.signal(signal.SIGINT, functionSignalHandler)
+
     while True:
         print "Hello Internet of Things 101"
         time.sleep(5)
(END)

Your github repository directory structure shall be at this point as follows:

TheIoTLearningInitiative Github Repository
https://github.com/YourGithubUserName/TheIoTLearningInitiative
├── LICENSE
├── README.md
├── InternetOfThings101
   ├── README.md
   ├── main.py

Last updated