126.96.36.199 Packet Tracer – SBC Actuate With Python (Instructor Version)
Become proficient using Python to program Packet Tracer’s SBC device.
Background / Scenario
Python is wide-spread, robust and easy to programming language that runs on several different computer platforms. Packet Tracer 7.1 takes advantage of this and implements Python-supported IoT devices.
In this Packet Tracer activity you will program the Packet Tracer 7.1’s SBC device using Python. The goal is to allow a coffee maker to start brewing coffee when someone enters the kitchen (movement is detected). A countertop lamp will also turn on.
- Packet Tracer 7.1 or newer.
Part 1: Adding and Connecting the Necessary Devices
You will start with an empty work space. Add the following devices to Packet Tracer’s work space:
a. A SBC device can be found under Components >> Boards >> SBC-PT.
b. A Coffee Maker can be found under End Devices >> Home >>Appliance.
c. A Counter Top Lamp can be found under End Devices >> Home>> Light.
d. A Motion Sensor can be found under Components >> Sensors >> Motion Sensor
e. Change the names of the devices to match the diagram.
f. Using IoT Custom Cable, connect the devices to the PT-SBC board. The IoT Custom Cable can be found under Connections.
Use the following table to find the correct ports:
Part 2: Programming the SBC
The SBC mimics real-world single board computers such as the Raspberry Pi.
One of the advantages of SBC-PT is that it can be programmed with Python.
a. Click on the SBC and select the Programming tab.
Is there any code pre-loaded in the SBC?
The contents of the Programming tab are divided into two main parts. The left pane displays all the files containing the programs created by you. The right pane displays the contents of the file currently selected on the left portion. Click the New button above the left pane to create a new file. Name it main.py and select Empty – Python for the template type. Click Create. The left pane should now list your new file, main.py.
b. On the left pane, select main.py and click Open; the Open button is located immediately above the left pane. The right pane is now ready to receive the code for your main.py program.
c. Using the right pane, write a program to instruct the SBC to turn on the Coffee Maker and the Light when motion is detected by the Motion Detector.
1. from gpio import * 2. from time import * 3. def main(): 4. while True: 5. motion_sensor = digitalRead(9) 6. if motion_sensor == HIGH: 7. print("Someone's awake."); 8. print("Making Coffee..."); 9. customWrite(1,1) 10. customWrite(2,1) 11. delay(6000) 12. print("Done. Coffee is ready."); 13. customWrite(1,0) 14. customWrite(2,0) 15. delay(500) 16. if __name__ == "__main__": 17. main()
Lines 1 and 2 include the gpio and time modules in the program. These modules are required for the digitalRead(), customWrite(), and delay() functions.
Line 3 defines the main() function.
Line 4 defines the WHILE loop. Note that since the condition is always true (the Boolean value True), the loop will run forever.
Line 5 reads the digital signal from pin 9 (PT-Motion detector signal) and stores it in the motion_sensor variable. The PT-Motion detector sends HIGH or LOW reports to report motion or lack thereof, respectively.
Line 6 tests the content of the motion_sensor variable. If it is equal to STOP (there is movement), lines 7 to 14 are executed.
Lines 7 and 8 print messages to the Python console.
Lines 9 and 10 send the digital value of 1 to digital pins 1 and 2. These pins connect to the PT-Coffee Maker and PT-Light, respectively. This will power on both devices.
Line 11 forces the interpreter to wait 6000 milliseconds (6 seconds).
Line 12 prints another message to the Python console.
Lines 13 and 14 send a digital 0 signal to digital pins 1 and 2. These pins connect to the PT-Coffee Maker and PT-Light, respectively. This will disconnect the two devices.
Line 15 forces the interpreter to wait 500 milliseconds (0.5 seconds).
Lines 16 and 17 are unique to the Packet Tracer Python interpreter and are outside the scope of this lab.
Part 3: Testing
To test your code, first run the main.py by clicking Run. Then press the ALT key while moving the mouse over the Motion Sensor. This should signify movement to Packet Tracer which will be captured by the PTMotion Sensor. Once movement is detected, a HIGH digital signal is sent to the SBC which in turn, turns on the coffee maker and the light. Messages also appear in the console of the SBC.
Did the Coffee Maker turn on?
Did the light turn on?
What changes would have to be made in the SBC if the motion detector was moved to port D5?
The signal would have to read pin 5 instead of 9. In the case of the answering program given above, this is done on line 5.
motion_sensor = digitalRead(5)
What PT-SBC port, if any, should be used to connect an analog sensor?
None. SBC_PT has no analog ports. To use analog sensors, an MCU-PT is required.