Project

Implement a function to simulate a Sensor, data will be based on system-wide network I/O statistics using psutil python library, the Simulated Sensor is implemented in case no physical sensor is available

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

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

import psutil
import signal
import sys
import time

def functionDataSensor():
    netdata = psutil.net_io_counters()
    data = netdata.packets_sent + netdata.packets_recv
    return data

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

if __name__ == '__main__':

    signal.signal(signal.SIGINT, functionSignalHandler)

    while True:
        print "Hello Internet of Things 101"
        print "Data Sensor: %s " % functionDataSensor()
        time.sleep(5)

# End of File
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# python main.py 
Hello Internet of Things 101
Data: 1877 
^Z
[2]+  Stopped(SIGTSTP)        python main.py
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git add main.py 
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git commit -s
Internet of Things 101: 5.2.1 Sensors: Project: Data Collection

Implement a function to collect data, data will be based on system-wide
network I/O statistics using psutil python library, this will simulate
our sensor in case no physical sensor is available

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 15b9441] Internet of Things 101: 5.2.1 Sensors: Project: Data Collection
 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), 737 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To https://github.com/xe1gyq/TheIoTLearningInitiative.git
   ce05cfa..15b9441  master -> master
root@edison:~/TheIoTLearningInitiative/InternetOfThings101#
root@edison:~/TheIoTLearningInitiative/InternetOfThings101# git show
commit 92e8b9f26a239952b9be9f9a06502c4334a66698
Author: Name LastName <email@gmail.com>
Date:   Sat Apr 30 23:00:38 2016 +0000

    Internet of Things 101: 5.2.1 Sensors: Project: Data Collection

    Implement a function to collect data, data will be based on system-wide
    network I/O statistics using psutil python library, this will simulate
    our sensor in case no physical sensor is available

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

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

+import psutil
 import signal
 import sys
 import time

+def functionDataSensor():
+    netdata = psutil.net_io_counters()
+    data = netdata.packets_sent + netdata.packets_recv
+    return data
+
 def functionSignalHandler(signal, frame):
     sys.exit(0)

@@ -13,6 +19,7 @@ if __name__ == '__main__':

     while True:
         print "Hello Internet of Things 101"
+        print "Data Sensor: %s " % functionDataSensor()
         time.sleep(5)

 # End of File
(END)

Last updated